1*11be35a1SLionel Sambuc /* $NetBSD: t_pipe.c,v 1.3 2011/10/31 15:41:31 christos Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <sys/cdefs.h>
30*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
31*11be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_pipe.c,v 1.3 2011/10/31 15:41:31 christos Exp $");
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <sys/types.h>
35*11be35a1SLionel Sambuc #include <sys/wait.h>
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include <errno.h>
38*11be35a1SLionel Sambuc #include <fcntl.h>
39*11be35a1SLionel Sambuc #include <poll.h>
40*11be35a1SLionel Sambuc #include <sched.h>
41*11be35a1SLionel Sambuc #include <signal.h>
42*11be35a1SLionel Sambuc #include <stdio.h>
43*11be35a1SLionel Sambuc #include <stdlib.h>
44*11be35a1SLionel Sambuc #include <unistd.h>
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc #include <atf-c.h>
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc #include "../../../h_macros.h"
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc static pid_t pid;
51*11be35a1SLionel Sambuc static int nsiginfo = 0;
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc /*
54*11be35a1SLionel Sambuc * This is used for both parent and child. Handle parent's SIGALRM,
55*11be35a1SLionel Sambuc * the childs SIGINFO doesn't need anything.
56*11be35a1SLionel Sambuc */
57*11be35a1SLionel Sambuc static void
sighand(int sig)58*11be35a1SLionel Sambuc sighand(int sig)
59*11be35a1SLionel Sambuc {
60*11be35a1SLionel Sambuc if (sig == SIGALRM) {
61*11be35a1SLionel Sambuc kill(pid, SIGINFO);
62*11be35a1SLionel Sambuc }
63*11be35a1SLionel Sambuc if (sig == SIGINFO) {
64*11be35a1SLionel Sambuc nsiginfo++;
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc }
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc ATF_TC(pipe_restart);
ATF_TC_HEAD(pipe_restart,tc)69*11be35a1SLionel Sambuc ATF_TC_HEAD(pipe_restart, tc)
70*11be35a1SLionel Sambuc {
71*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe "
72*11be35a1SLionel Sambuc "works correctly after being interrupted and restarted "
73*11be35a1SLionel Sambuc "(kern/14087)");
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc
ATF_TC_BODY(pipe_restart,tc)76*11be35a1SLionel Sambuc ATF_TC_BODY(pipe_restart, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc int pp[2], st;
79*11be35a1SLionel Sambuc ssize_t sz, todo, done;
80*11be35a1SLionel Sambuc char *f;
81*11be35a1SLionel Sambuc sigset_t asigset, osigset, emptysigset;
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc /* Initialise signal masks */
84*11be35a1SLionel Sambuc RL(sigemptyset(&emptysigset));
85*11be35a1SLionel Sambuc RL(sigemptyset(&asigset));
86*11be35a1SLionel Sambuc RL(sigaddset(&asigset, SIGINFO));
87*11be35a1SLionel Sambuc
88*11be35a1SLionel Sambuc /* Register signal handlers for both read and writer */
89*11be35a1SLionel Sambuc REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR);
90*11be35a1SLionel Sambuc REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR);
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc todo = 2 * 1024 * 1024;
93*11be35a1SLionel Sambuc REQUIRE_LIBC(f = malloc(todo), NULL);
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc RL(pipe(pp));
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc RL(pid = fork());
98*11be35a1SLionel Sambuc if (pid == 0) {
99*11be35a1SLionel Sambuc /* child */
100*11be35a1SLionel Sambuc RL(close(pp[1]));
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc /* Do inital write. This should succeed, make
103*11be35a1SLionel Sambuc * the other side do partial write and wait for us to pick
104*11be35a1SLionel Sambuc * rest up.
105*11be35a1SLionel Sambuc */
106*11be35a1SLionel Sambuc RL(done = read(pp[0], f, 128 * 1024));
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc /* Wait until parent is alarmed and awakens us */
109*11be35a1SLionel Sambuc RL(sigprocmask(SIG_BLOCK, &asigset, &osigset));
110*11be35a1SLionel Sambuc while (nsiginfo == 0) {
111*11be35a1SLionel Sambuc if (sigsuspend(&emptysigset) != -1 || errno != EINTR)
112*11be35a1SLionel Sambuc atf_tc_fail("sigsuspend(&emptysigset): %s",
113*11be35a1SLionel Sambuc strerror(errno));
114*11be35a1SLionel Sambuc }
115*11be35a1SLionel Sambuc RL(sigprocmask(SIG_SETMASK, &osigset, NULL));
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc /* Read all what parent wants to give us */
118*11be35a1SLionel Sambuc while((sz = read(pp[0], f, 1024 * 1024)) > 0)
119*11be35a1SLionel Sambuc done += sz;
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc /*
122*11be35a1SLionel Sambuc * Exit with 1 if number of bytes read doesn't match
123*11be35a1SLionel Sambuc * number of expected bytes
124*11be35a1SLionel Sambuc */
125*11be35a1SLionel Sambuc printf("Read: %#zx\n", (size_t)done);
126*11be35a1SLionel Sambuc printf("Expected: %#zx\n", (size_t)todo);
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc exit(done != todo);
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc /* NOTREACHED */
131*11be35a1SLionel Sambuc } else {
132*11be35a1SLionel Sambuc RL(close(pp[0]));
133*11be35a1SLionel Sambuc
134*11be35a1SLionel Sambuc /*
135*11be35a1SLionel Sambuc * Arrange for alarm after two seconds. Since we have
136*11be35a1SLionel Sambuc * handler setup for SIGARLM, the write(2) call should
137*11be35a1SLionel Sambuc * be restarted internally by kernel.
138*11be35a1SLionel Sambuc */
139*11be35a1SLionel Sambuc (void)alarm(2);
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc /* We write exactly 'todo' bytes. The very first write(2)
142*11be35a1SLionel Sambuc * should partially succeed, block and eventually
143*11be35a1SLionel Sambuc * be restarted by kernel
144*11be35a1SLionel Sambuc */
145*11be35a1SLionel Sambuc while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
146*11be35a1SLionel Sambuc todo -= sz;
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc /* Close the pipe, so that child would stop reading */
149*11be35a1SLionel Sambuc RL(close(pp[1]));
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc /* And pickup child's exit status */
152*11be35a1SLionel Sambuc RL(waitpid(pid, &st, 0));
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(WEXITSTATUS(st), 0);
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc }
157*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)158*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
159*11be35a1SLionel Sambuc {
160*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, pipe_restart);
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc return atf_no_error();
163*11be35a1SLionel Sambuc }
164