1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * pipe04.c
23 *
24 * DESCRIPTION
25 * Check that processes are killable, even when they are still writing
26 * data to a pipe.
27 *
28 * ALGORITHM
29 * 1. Open a pipe
30 * 2. fork a two children that will write to the pipe
31 * 3. read a bit from both children
32 * 3. kill both children and wait to make sure they die
33 *
34 * USAGE: <for command-line>
35 * pipe04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -f : Turn off functionality Testing.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 11/2002 Ported by Paul Larson
45 */
46 #include <unistd.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include "test.h"
52 #include "safe_macros.h"
53
54 char *TCID = "pipe04";
55 int TST_TOTAL = 1;
56
57 int fildes[2]; /* fds for pipe read and write */
58
59 void setup(void);
60 void cleanup(void);
61 void c1func(void);
62 void c2func(void);
63 void alarmfunc(int);
64
do_read(int fd,void * buf,size_t count)65 ssize_t do_read(int fd, void *buf, size_t count)
66 {
67 ssize_t n;
68
69 do {
70 n = read(fd, buf, count);
71 } while (n < 0 && errno == EINTR);
72
73 return n;
74 }
75
main(int ac,char ** av)76 int main(int ac, char **av)
77 {
78 int lc;
79 pid_t c1pid, c2pid;
80 int wtstatus;
81 int bytesread;
82 int acnt = 0, bcnt = 0;
83
84 char rbuf[BUFSIZ];
85
86 tst_parse_opts(ac, av, NULL, NULL);
87 setup();
88
89 for (lc = 0; TEST_LOOPING(lc); lc++) {
90
91 /* reset tst_count in case we are looping */
92 tst_count = 0;
93
94 SAFE_PIPE(cleanup, fildes);
95
96 if ((c1pid = tst_fork()) == -1)
97 tst_brkm(TBROK, cleanup, "fork() failed - "
98 "errno %d", errno);
99 if (c1pid == 0)
100 c1func();
101 if ((c2pid = tst_fork()) == -1)
102 tst_brkm(TBROK, cleanup, "fork() failed - "
103 "errno %d", errno);
104 if (c2pid == 0)
105 c2func();
106
107 /* PARENT */
108 if (close(fildes[1]) == -1)
109 tst_resm(TWARN, "Could not close fildes[1] - errno %d",
110 errno);
111 /*
112 * Read a bit from the children first
113 */
114 while ((acnt < 100) && (bcnt < 100)) {
115 bytesread = do_read(fildes[0], rbuf, sizeof(rbuf));
116 if (bytesread < 0) {
117 tst_resm(TFAIL, "Unable to read from pipe, "
118 "errno=%d", errno);
119 break;
120 }
121 switch (rbuf[1]) {
122 case 'A':
123 acnt++;
124 break;
125 case 'b':
126 bcnt++;
127 break;
128 default:
129 tst_resm(TFAIL, "Got bogus '%c' "
130 "character", rbuf[1]);
131 break;
132 }
133 }
134
135 /*
136 * Try to kill the children
137 */
138 if (kill(c1pid, SIGKILL) == -1)
139 tst_resm(TFAIL, "failed to kill child 1, errno=%d",
140 errno);
141 if (kill(c2pid, SIGKILL) == -1)
142 tst_resm(TFAIL, "failed to kill child 1, errno=%d",
143 errno);
144
145 /*
146 * Set action for the alarm
147 */
148 if (signal(SIGALRM, alarmfunc) == SIG_ERR)
149 tst_resm(TWARN | TERRNO, "call to signal failed");
150 /*
151 * Set an alarm for 60 seconds just in case the child
152 * processes don't die
153 */
154 alarm(60);
155 if (waitpid(c1pid, &wtstatus, 0) != -1) {
156 if (wtstatus != SIGKILL)
157 tst_resm(TFAIL | TERRNO,
158 "unexpected wait status " "%d",
159 wtstatus);
160 else
161 tst_resm(TPASS, "Child 1 killed while "
162 "writing to a pipe");
163 }
164 if (waitpid(c2pid, &wtstatus, 0) != -1) {
165 if (!WIFSIGNALED(wtstatus) ||
166 WTERMSIG(wtstatus) != SIGKILL)
167 tst_resm(TFAIL | TERRNO,
168 "unexpected wait status " "%d",
169 wtstatus);
170 else
171 tst_resm(TPASS, "Child 2 killed while "
172 "writing to a pipe");
173 }
174 if (alarm(0) <= 0)
175 tst_resm(TWARN, "call to alarm(0) failed");
176 }
177 cleanup();
178
179 tst_exit();
180 }
181
182 /*
183 * setup() - performs all ONE TIME setup for this test.
184 */
setup(void)185 void setup(void)
186 {
187
188 tst_sig(FORK, DEF_HANDLER, cleanup);
189
190 TEST_PAUSE;
191 }
192
193 /*
194 * cleanup() - performs all ONE TIME cleanup for this test at
195 * completion or premature exit.
196 */
cleanup(void)197 void cleanup(void)
198 {
199 }
200
c1func(void)201 void c1func(void)
202 {
203 if (close(fildes[0]) == -1)
204 tst_resm(TWARN, "Could not close fildes[0] - errno %d", errno);
205 while (1)
206 if (write(fildes[1], "bbbbbbbbbbbbbbbbbbbbbbbbb", 25) == -1)
207 tst_resm(TBROK | TERRNO, "[child 1] pipe write failed");
208 }
209
c2func(void)210 void c2func(void)
211 {
212 if (close(fildes[0]) == -1)
213 tst_resm(TWARN, "Could not close fildes[0] - errno %d", errno);
214 while (1)
215 if (write(fildes[1], "AAAAAAAAAAAAAAAAAAAAAAAAA", 25) == -1)
216 tst_resm(TBROK | TERRNO, "[child 2] pipe write failed");
217 }
218
alarmfunc(int sig LTP_ATTRIBUTE_UNUSED)219 void alarmfunc(int sig LTP_ATTRIBUTE_UNUSED)
220 {
221 /* for some reason tst_brkm doesn't seem to work in a signal handler */
222 tst_brkm(TFAIL, cleanup, "one or more children did't die in 60 second "
223 "time limit");
224 }
225