1 /* test 2 */
2
3 #include <sys/types.h>
4 #include <sys/times.h>
5 #include <sys/wait.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <stdio.h>
12
13 #define ITERATIONS 5
14 #define MAX_ERROR 4
15
16 int is, array[4], parsigs, parcum, sigct, cumsig, errct, subtest;
17 int iteration, kk = 0, errct = 0;
18 char buf[2048];
19
20 _PROTOTYPE(int main, (int argc, char *argv []));
21 _PROTOTYPE(void test2a, (void));
22 _PROTOTYPE(void test2b, (void));
23 _PROTOTYPE(void test2c, (void));
24 _PROTOTYPE(void test2d, (void));
25 _PROTOTYPE(void test2e, (void));
26 _PROTOTYPE(void test2f, (void));
27 _PROTOTYPE(void test2g, (void));
28 _PROTOTYPE(void test2h, (void));
29 _PROTOTYPE(void sigpip, (int s));
30 _PROTOTYPE(void quit, (void));
31 _PROTOTYPE(void e, (int n));
32
33 int main(argc, argv)
34 int argc;
35 char *argv[];
36 {
37 int i, m = 0xFFFF;
38
39 sync();
40
41 if (argc == 2) m = atoi(argv[1]);
42
43 printf("Test 2 ");
44 fflush(stdout); /* have to flush for child's benefit */
45
46 system("rm -rf DIR_02; mkdir DIR_02");
47 chdir("DIR_02");
48
49 for (i = 0; i < ITERATIONS; i++) {
50 iteration = i;
51 if (m & 0001) test2a();
52 if (m & 0002) test2b();
53 if (m & 0004) test2c();
54 if (m & 0010) test2d();
55 if (m & 0020) test2e();
56 if (m & 0040) test2f();
57 if (m & 0100) test2g();
58 if (m & 0200) test2h();
59 }
60 subtest = 100;
61 if (cumsig != ITERATIONS) e(101);
62 quit();
63 return(-1); /* impossible */
64 }
65
66
67 void test2a()
68 {
69 /* Test pipes */
70
71 int fd[2];
72 int n, i, j, q = 0;
73
74 subtest = 1;
75 if (pipe(fd) < 0) {
76 printf("pipe error. errno= %d\n", errno);
77 errct++;
78 quit();
79 }
80 i = fork();
81 if (i < 0) {
82 printf("fork failed\n");
83 errct++;
84 quit();
85 }
86 if (i != 0) {
87 /* Parent code */
88 close(fd[0]);
89 for (i = 0; i < 2048; i++) buf[i] = i & 0377;
90 for (q = 0; q < 8; q++) {
91 if (write(fd[1], buf, 2048) < 0) {
92 printf("write pipe err. errno=%d\n", errno);
93 errct++;
94 quit();
95 }
96 }
97 close(fd[1]);
98 wait(&q);
99 if (q != 256 * 58) {
100 printf("wrong exit code %d\n", q);
101 errct++;
102 quit();
103 }
104 } else {
105 /* Child code */
106 close(fd[1]);
107 for (q = 0; q < 32; q++) {
108 n = read(fd[0], buf, 512);
109 if (n != 512) {
110 printf("read yielded %d bytes, not 512\n", n);
111 errct++;
112 quit();
113 }
114 for (j = 0; j < n; j++)
115 if ((buf[j] & 0377) != (kk & 0377)) {
116 printf("wrong data: %d %d %d \n ",
117 j, buf[j] & 0377, kk & 0377);
118 } else {
119 kk++;
120 }
121 }
122 exit(58);
123 }
124 }
125
126
127 void test2b()
128 {
129 int fd[2], n;
130 char buf[4];
131
132 subtest = 2;
133 sigct = 0;
134 signal(SIGPIPE, sigpip);
135 pipe(fd);
136 if (fork()) {
137 /* Parent */
138 close(fd[0]);
139 while (sigct == 0) {
140 write(fd[1], buf, 1);
141 }
142 wait(&n);
143 } else {
144 /* Child */
145 close(fd[0]);
146 close(fd[1]);
147 exit(0);
148 }
149 }
150
151
152
153 void test2c()
154 {
155 int n;
156
157 subtest = 3;
158 signal(SIGINT, SIG_DFL);
159 is = 0;
160 if ((array[is++] = fork()) > 0) {
161 if ((array[is++] = fork()) > 0) {
162 if ((array[is++] = fork()) > 0) {
163 if ((array[is++] = fork()) > 0) {
164 signal(SIGINT, SIG_IGN);
165 kill(array[0], SIGINT);
166 kill(array[1], SIGINT);
167 kill(array[2], SIGINT);
168 kill(array[3], SIGINT);
169 wait(&n);
170 wait(&n);
171 wait(&n);
172 wait(&n);
173 } else {
174 pause();
175 }
176 } else {
177 pause();
178 }
179 } else {
180 pause();
181 }
182 } else {
183 pause();
184 }
185 }
186
187 void test2d()
188 {
189
190 int pid, stat_loc, s;
191
192 /* Test waitpid. */
193 subtest = 4;
194
195 /* Test waitpid(pid, arg2, 0) */
196 pid = fork();
197 if (pid < 0) e(1);
198 if (pid > 0) {
199 /* Parent. */
200 s = waitpid(pid, &stat_loc, 0);
201 if (s != pid) e(2);
202 if (WIFEXITED(stat_loc) == 0) e(3);
203 if (WIFSIGNALED(stat_loc) != 0) e(4);
204 if (WEXITSTATUS(stat_loc) != 22) e(5);
205 } else {
206 /* Child */
207 exit(22);
208 }
209
210 /* Test waitpid(-1, arg2, 0) */
211 pid = fork();
212 if (pid < 0) e(6);
213 if (pid > 0) {
214 /* Parent. */
215 s = waitpid(-1, &stat_loc, 0);
216 if (s != pid) e(7);
217 if (WIFEXITED(stat_loc) == 0) e(8);
218 if (WIFSIGNALED(stat_loc) != 0) e(9);
219 if (WEXITSTATUS(stat_loc) != 33) e(10);
220 } else {
221 /* Child */
222 exit(33);
223 }
224
225 /* Test waitpid(0, arg2, 0) */
226 pid = fork();
227 if (pid < 0) e(11);
228 if (pid > 0) {
229 /* Parent. */
230 s = waitpid(0, &stat_loc, 0);
231 if (s != pid) e(12);
232 if (WIFEXITED(stat_loc) == 0) e(13);
233 if (WIFSIGNALED(stat_loc) != 0) e(14);
234 if (WEXITSTATUS(stat_loc) != 44) e(15);
235 } else {
236 /* Child */
237 exit(44);
238 }
239
240 /* Test waitpid(0, arg2, WNOHANG) */
241 signal(SIGTERM, SIG_DFL);
242 pid = fork();
243 if (pid < 0) e(16);
244 if (pid > 0) {
245 /* Parent. */
246 s = waitpid(0, &stat_loc, WNOHANG);
247 if (s != 0) e(17);
248 if (kill(pid, SIGTERM) != 0) e(18);
249 if (waitpid(pid, &stat_loc, 0) != pid) e(19);
250 if (WIFEXITED(stat_loc) != 0) e(20);
251 if (WIFSIGNALED(stat_loc) == 0) e(21);
252 if (WTERMSIG(stat_loc) != SIGTERM) e(22);
253 } else {
254 /* Child */
255 pause();
256 }
257
258 /* Test some error conditions. */
259 errno = 9999;
260 if (waitpid(0, &stat_loc, 0) != -1) e(23);
261 if (errno != ECHILD) e(24);
262 errno = 9999;
263 if (waitpid(0, &stat_loc, WNOHANG) != -1) e(25);
264 if (errno != ECHILD) e(26);
265 }
266
267
268 void test2e()
269 {
270
271 int pid1, pid2, stat_loc, s;
272
273 /* Test waitpid with two children. */
274 subtest = 5;
275 if (iteration > 1) return; /* slow test, don't do it too much */
276 if ( (pid1 = fork())) {
277 /* Parent. */
278 if ( (pid2 = fork()) ) {
279 /* Parent. Collect second child first. */
280 s = waitpid(pid2, &stat_loc, 0);
281 if (s != pid2) e(1);
282 if (WIFEXITED(stat_loc) == 0) e(2);
283 if (WIFSIGNALED(stat_loc) != 0) e(3);
284 if (WEXITSTATUS(stat_loc) != 222) e(4);
285
286 /* Now collect first child. */
287 s = waitpid(pid1, &stat_loc, 0);
288 if (s != pid1) e(5);
289 if (WIFEXITED(stat_loc) == 0) e(6);
290 if (WIFSIGNALED(stat_loc) != 0) e(7);
291 if (WEXITSTATUS(stat_loc) != 111) e(8);
292 } else {
293 /* Child 2. */
294 sleep(2); /* child 2 delays before exiting. */
295 exit(222);
296 }
297 } else {
298 /* Child 1. */
299 exit(111); /* child 1 exits immediately */
300 }
301
302 }
303
304
305 void test2f()
306 {
307 /* test getpid, getppid, getuid, etc. */
308
309 pid_t pid, pid1, ppid, cpid, stat_loc, err;
310
311 subtest = 6;
312 errno = -2000;
313 err = 0;
314 pid = getpid();
315 if ( (pid1 = fork())) {
316 /* Parent. Do nothing. */
317 if (wait(&stat_loc) != pid1) e(1);
318 if (WEXITSTATUS(stat_loc) != (pid1 & 0377)) e(2);
319 } else {
320 /* Child. Get ppid. */
321 cpid = getpid();
322 ppid = getppid();
323 if (ppid != pid) err = 3;
324 if (cpid == ppid) err = 4;
325 exit(cpid & 0377);
326 }
327 if (err != 0) e(err);
328 }
329
330 void test2g()
331 {
332 /* test time(), times() */
333
334 time_t t1, t2;
335 clock_t t3, t4;
336 struct tms tmsbuf;
337
338 subtest = 7;
339 errno = -7000;
340
341 /* First time(). */
342 t1 = -1;
343 t2 = -2;
344 t1 = time(&t2);
345 if (t1 < 650000000L) e(1); /* 650000000 is Sept. 1990 */
346 if (t1 != t2) e(2);
347 t1 = -1;
348 t1 = time( (time_t *) NULL);
349 if (t1 < 650000000L) e(3);
350 t3 = times(&tmsbuf);
351 sleep(1);
352 t2 = time( (time_t *) NULL);
353 if (t2 < 0L) e(4);
354 if (t2 - t1 < 1) e(5);
355
356 /* Now times(). */
357 t4 = times(&tmsbuf);
358 if ( t4 == (clock_t) -1) e(6);
359 if (t4 - t3 < CLOCKS_PER_SEC) e(7);
360 if (tmsbuf.tms_utime < 0) e(8);
361 if (tmsbuf.tms_stime < 0) e(9);
362 if (tmsbuf.tms_cutime < 0) e(10);
363 if (tmsbuf.tms_cstime < 0) e(11);
364 }
365
366 void test2h()
367 {
368 /* Test getgroups(). */
369
370 gid_t g[10];
371
372 subtest = 8;
373 errno = -8000;
374 if (getgroups(10, g) != 0) e(1);
375 if (getgroups(1, g) != 0) e(2);
376 if (getgroups(0, g) != 0) e(3);
377 }
378
379
380 void sigpip(s)
381 int s; /* for ANSI */
382 {
383 sigct++;
384 cumsig++;
385 }
386
387 void quit()
388 {
389
390 chdir("..");
391 system("rm -rf DIR*");
392
393 if (errct == 0) {
394 printf("ok\n");
395 exit(0);
396 } else {
397 printf("%d errors\n", errct);
398 exit(4);
399 }
400 }
401
402 void e(n)
403 int n;
404 {
405 int err_num = errno; /* save errno in case printf clobbers it */
406
407 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
408 errno = err_num; /* restore errno, just in case */
409 perror("");
410 if (errct++ > MAX_ERROR) {
411 printf("Too many errors; test aborted\n");
412 chdir("..");
413 system("rm -rf DIR*");
414 exit(1);
415 }
416 }
417
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.