xref: /minix/minix/tests/test42.c (revision e3b78ef1)
1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
2 #include <setjmp.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/wait.h>
10 #include <sys/select.h>
11 #include <sys/ptrace.h>
12 #include <sys/syslimits.h>
13 
14 #define ITERATIONS 3
15 int max_error = 4;
16 #include "common.h"
17 
18 #define my_e(n) { \
19 	if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
20 
21 
22 #define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
23 #define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
24 #define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
25 
26 #define timed_test(func) (timed_test_func(#func, func));
27 
28 int main(int argc, char **argv);
29 void test(int m, int a);
30 void timed_test_func(const char *s, void (* func)(void));
31 void timed_test_timeout(int signum);
32 pid_t traced_fork(void (*c) (void));
33 pid_t traced_pfork(void (*c) (void));
34 void WRITE(int value);
35 int READ(void);
36 void traced_wait(void);
37 void detach_running(pid_t pid);
38 void dummy_handler(int sig);
39 void exit_handler(int sig);
40 void count_handler(int sig);
41 void catch_handler(int sig);
42 void test_wait_child(void);
43 void test_wait(void);
44 void test_exec_child(void);
45 void test_exec(void);
46 void test_step_child(void);
47 void test_step(void);
48 void test_sig_child(void);
49 void test_sig(void);
50 void test_exit_child(void);
51 void test_exit(void);
52 void test_term_child(void);
53 void test_term(void);
54 void test_catch_child(void);
55 void test_catch(void);
56 void test_kill_child(void);
57 void test_kill(void);
58 void test_attach_child(void);
59 void test_attach(void);
60 void test_detach_child(void);
61 void test_detach(void);
62 void test_death_child(void);
63 void test_death(void);
64 void test_zdeath_child(void);
65 void test_zdeath(void);
66 void test_syscall_child(void);
67 void test_syscall(void);
68 void test_tracefork_child(void);
69 void test_tracefork(void);
70 void sigexec(int setflag, int opt, int *traps, int *stop);
71 void test_trapexec(void);
72 void test_altexec(void);
73 void test_noexec(void);
74 void test_defexec(void);
75 void test_reattach_child(void);
76 void test_reattach(void);
77 
78 static char *executable;
79 static int child = 0, attach;
80 static pid_t ppid;
81 static int pfd[4];
82 static int sigs, caught;
83 
84 int main(argc, argv)
85 int argc;
86 char **argv;
87 {
88   int i, m = 0xFFFFFF, n = 0xF;
89   char cp_cmd[NAME_MAX + 10];
90 
91   if (strcmp(argv[0], "DO CHECK") == 0) {
92 	exit(42);
93   }
94 
95   start(42);
96 
97   executable = argv[0];
98 
99   snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);
100   system(cp_cmd);
101 
102   if (argc >= 2) m = atoi(argv[1]);
103   if (argc >= 3) n = atoi(argv[2]);
104 
105   for (i = 0; i < ITERATIONS; i++) {
106 	if (n & 001) test(m, 0);
107 	if (n & 002) test(m, 1);
108 	if (n & 004) test(m, 2);
109 	if (n & 010) test(m, 3);
110   }
111 
112   quit();
113   return(-1);			/* impossible */
114 }
115 
116 void test(m, a)
117 int m;
118 int a;
119 {
120   attach = a;
121 
122   if (m & 00000001) timed_test(test_wait);
123   if (m & 00000002) timed_test(test_exec);
124 #if !defined(__arm__)
125   /* BJG: single-stepping isn't implemented on ARM */
126   if (m & 00000004) timed_test(test_step);
127 #endif
128   if (m & 00000010) timed_test(test_sig);
129   if (m & 00000020) timed_test(test_exit);
130   if (m & 00000040) timed_test(test_term);
131   if (m & 00000100) timed_test(test_catch);
132   if (m & 00000200) timed_test(test_kill);
133   if (m & 00000400) timed_test(test_attach);
134   if (m & 00001000) timed_test(test_detach);
135   if (m & 00002000) timed_test(test_death);
136   if (m & 00004000) timed_test(test_zdeath);
137   if (m & 00010000) timed_test(test_syscall);
138   if (m & 00020000) timed_test(test_tracefork);
139   if (m & 00040000) timed_test(test_trapexec);
140   if (m & 00100000) timed_test(test_altexec);
141   if (m & 00200000) timed_test(test_noexec);
142   if (m & 00400000) timed_test(test_defexec);
143   if (m & 01000000) test_reattach(); /* not timed, catches SIGALRM */
144 }
145 
146 static jmp_buf timed_test_context;
147 
148 void timed_test_timeout(int signum)
149 {
150   longjmp(timed_test_context, -1);
151   my_e(700);
152   quit();
153   exit(-1);
154 }
155 
156 void timed_test_func(const char *s, void (* func)(void))
157 {
158   if (setjmp(timed_test_context) == 0)
159   {
160     /* the function gets 60 seconds to complete */
161     if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { my_e(701); return; }
162     alarm(60);
163     func();
164     alarm(0);
165   }
166   else
167   {
168     /* report timeout as error */
169     printf("timeout in %s\n", s);
170     my_e(702);
171   }
172 }
173 
174 pid_t traced_fork(c)
175 void(*c) (void);
176 {
177   pid_t pid;
178   int r, status;
179 
180   if (pipe(pfd) != 0) my_e(200);
181   if (pipe(&pfd[2]) != 0) my_e(201);
182 
183   switch (attach) {
184   case 0:			/* let child volunteer to be traced */
185   	pid = fork();
186 
187   	if (pid < 0) my_e(202);
188 
189   	if (pid == 0) {
190 		child = 1;
191 
192 		if (ptrace(T_OK, 0, 0, 0) != 0) my_e(203);
193 
194 		WRITE(0);
195 
196 		c();
197 
198 		my_e(204);
199   	}
200 
201   	if (READ() != 0) my_e(205);
202 
203   	break;
204 
205   case 1:			/* attach to child process */
206 	pid = fork();
207 
208 	if (pid < 0) my_e(206);
209 
210 	if (pid == 0) {
211 		child = 1;
212 
213 		if (READ() != 0) my_e(207);
214 
215 		c();
216 
217 		my_e(208);
218 	}
219 
220 	if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(209);
221 
222 	if (waitpid(pid, &status, 0) != pid) my_e(210);
223 	if (!_WIFSTOPPED(status)) my_e(211);
224 	if (WSTOPSIG(status) != SIGSTOP) my_e(212);
225 
226 	if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(213);
227 
228 	WRITE(0);
229 
230 	break;
231 
232   case 2:			/* attach to non-child process */
233 	ppid = fork();
234 
235 	if (ppid < 0) my_e(214);
236 
237 	if (ppid == 0) {
238 		pid = fork();
239 
240 		if (pid < 0) exit(215);
241 
242 		if (pid == 0) {
243 			child = 1;
244 
245 			if (READ() != 0) my_e(216);
246 
247 			c();
248 
249 			my_e(217);
250 		}
251 
252 		child = 1;
253 
254 		WRITE(pid);
255 
256 		if (waitpid(pid, &status, 0) != pid) my_e(218);
257 		if (_WIFSTOPPED(status)) my_e(219);
258 		if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
259 
260 		exit(0);
261 	}
262 
263 	pid = READ();
264 
265 	if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(220);
266 
267 	if (waitpid(pid, &status, 0) != pid) my_e(221);
268 	if (!_WIFSTOPPED(status)) my_e(222);
269 	if (WSTOPSIG(status) != SIGSTOP) my_e(223);
270 
271 	if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(224);
272 
273 	WRITE(0);
274 
275   	break;
276 
277   case 3:			/* attach by forking from child */
278 	ppid = fork();
279 
280 	if (ppid < 0) my_e(225);
281 
282 	if (ppid == 0) {
283 		child = 1;
284 
285 		if (ptrace(T_OK, 0, 0, 0) != 0) my_e(226);
286 
287 		WRITE(0);
288 
289 		if (READ() != 0) my_e(227);
290 
291 		pid = fork();
292 
293 		if (pid < 0) my_e(228);
294 
295 		if (pid == 0) {
296 			c();
297 
298 			my_e(229);
299 		}
300 
301 		WRITE(pid);
302 
303 		if (waitpid(pid, &status, 0) != pid) my_e(230);
304 		if (_WIFSTOPPED(status)) my_e(231);
305 		if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
306 
307 		exit(0);
308 	}
309 
310 	if (READ() != 0) my_e(232);
311 
312 	if (kill(ppid, SIGSTOP) != 0) my_e(233);
313 
314 	if (waitpid(ppid, &status, 0) != ppid) my_e(234);
315 	if (!_WIFSTOPPED(status)) my_e(235);
316 	if (WSTOPSIG(status) != SIGSTOP) my_e(236);
317 
318 	if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(237);
319 
320 	if (ptrace(T_RESUME, ppid, 0, 0) != 0) my_e(238);
321 
322 	WRITE(0);
323 
324 	pid = READ();
325 
326 	if (waitpid(pid, &status, 0) != pid) my_e(239);
327 	if (!_WIFSTOPPED(status)) my_e(240);
328 	if (WSTOPSIG(status) != SIGSTOP) my_e(241);
329 
330 	if (ptrace(T_SETOPT, pid, 0, 0) != 0) my_e(242);
331 	if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(243);
332 
333 	detach_running(ppid);
334 
335 	break;
336   }
337 
338   return pid;
339 }
340 
341 pid_t traced_pfork(c)
342 void(*c) (void);
343 {
344   pid_t pid;
345 
346   if (pipe(pfd) != 0) my_e(300);
347   if (pipe(&pfd[2]) != 0) my_e(301);
348 
349   pid = fork();
350 
351   if (pid < 0) my_e(302);
352 
353   if (pid == 0) {
354 	child = 1;
355 
356 	c();
357 
358 	my_e(303);
359   }
360 
361   return pid;
362 }
363 
364 void WRITE(value)
365 int value;
366 {
367   if (write(pfd[child*2+1], &value, sizeof(value)) != sizeof(value)) my_e(400);
368 }
369 
370 int READ()
371 {
372   int value;
373 
374   if (read(pfd[2-child*2], &value, sizeof(value)) != sizeof(value)) my_e(401);
375 
376   return value;
377 }
378 
379 void traced_wait()
380 {
381   int r, status;
382 
383   if (attach == 2) {
384 	if (waitpid(ppid, &status, 0) != ppid) my_e(500);
385 	if (!_WIFEXITED(status)) my_e(501);
386 	if ((r = WEXITSTATUS(status)) != 0) my_e(r);
387   }
388   else {
389 	/* Quick hack to clean up detached children */
390   	waitpid(-1, NULL, WNOHANG);
391   }
392 
393   close(pfd[0]);
394   close(pfd[1]);
395   close(pfd[2]);
396   close(pfd[3]);
397 }
398 
399 void detach_running(pid)
400 pid_t pid;
401 {
402 /* Detach from a process that is not already stopped. This is the way to do it.
403  * We have to stop the child in order to detach from it, but as the child may
404  * have other signals pending for the tracer, we cannot assume we get our own
405  * signal back immediately. However, because we know that the kill is instant
406  * and resuming with pending signals will only stop the process immediately
407  * again, we can use T_RESUME for all the signals until we get our own signal,
408  * and then detach. A complicating factor is that anywhere during this
409  * procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
410  * will not happen.
411  */
412   int status;
413 
414   if (kill(pid, SIGSTOP) != 0) my_e(600);
415 
416   if (waitpid(pid, &status, 0) != pid) my_e(601);
417 
418   while (_WIFSTOPPED(status)) {
419 	if (WSTOPSIG(status) == SIGSTOP) {
420 		if (ptrace(T_DETACH, pid, 0, 0) != 0) my_e(602);
421 
422 		return;
423 	}
424 
425 	if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(603);
426 
427 	if (waitpid(pid, &status, 0) != pid) my_e(604);
428   }
429 
430   /* Apparently the process exited. */
431   if (!_WIFEXITED(status) && !_WIFSIGNALED(status)) my_e(605);
432 
433   /* In our tests, that should not happen. */
434   my_e(606);
435 }
436 
437 void dummy_handler(sig)
438 int sig;
439 {
440 }
441 
442 void exit_handler(sig)
443 int sig;
444 {
445   exit(42);
446 }
447 
448 void count_handler(sig)
449 int sig;
450 {
451   sigs++;
452 }
453 
454 void catch_handler(sig)
455 int sig;
456 {
457   sigset_t set;
458   int bit;
459 
460   switch (sig) {
461   case SIGUSR1: bit = 1; break;
462   case SIGUSR2: bit = 2; break;
463   case SIGTERM: bit = 4; break;
464   default: bit = 0; my_e(100);
465   }
466 
467   sigfillset(&set);
468   sigprocmask(SIG_SETMASK, &set, NULL);
469 
470   if (caught & bit) my_e(101);
471   caught |= bit;
472 }
473 
474 void test_wait_child()
475 {
476   exit(42);
477 }
478 
479 void test_wait()
480 {
481   pid_t pid;
482   int status;
483 
484   subtest = 1;
485 
486   pid = traced_fork(test_wait_child);
487 
488   if (waitpid(pid, &status, 0) != pid) my_e(1);
489   if (!_WIFEXITED(status)) my_e(2);
490   if (WEXITSTATUS(status) != 42) my_e(3);
491 
492   traced_wait();
493 }
494 
495 void test_exec_child()
496 {
497   if (READ() != 0) my_e(100);
498 
499   execl(executable, "DO CHECK", NULL);
500 
501   my_e(101);
502 }
503 
504 void test_exec()
505 {
506   pid_t pid;
507   int r, status;
508 
509   /* This test covers the T_OK case. */
510   if (attach != 0) return;
511 
512   subtest = 2;
513 
514   pid = traced_fork(test_exec_child);
515 
516   WRITE(0);
517 
518   /* An exec() should result in a trap signal. */
519   if (waitpid(pid, &status, 0) != pid) my_e(1);
520   if (!_WIFSTOPPED(status)) my_e(2);
521   if (WSTOPSIG(status) != SIGTRAP) my_e(3);
522 
523   if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(4);
524 
525   if (waitpid(pid, &status, 0) != pid) my_e(5);
526   if (!_WIFEXITED(status)) my_e(6);
527   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
528 
529   traced_wait();
530 }
531 
532 void test_step_child()
533 {
534   sigset_t set;
535 
536   signal(SIGUSR1, SIG_IGN);
537 
538   WRITE(0);
539 
540   if (READ() != 0) my_e(100);
541 
542   /* It must not be possible for the child to stop the single-step signal. */
543   signal(SIGTRAP, SIG_IGN);
544   sigfillset(&set);
545   sigprocmask(SIG_SETMASK, &set, NULL);
546 
547   exit(42);
548 }
549 
550 void test_step()
551 {
552   pid_t pid;
553   int r, status, count;
554 
555   subtest = 3;
556 
557   pid = traced_fork(test_step_child);
558 
559   if (READ() != 0) my_e(1);
560 
561   /* While the child is running, neither waitpid() nor ptrace() should work. */
562   if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
563   if (ptrace(T_RESUME, pid, 0, 0) != -1) my_e(3);
564   if (errno != EBUSY) my_e(4);
565 
566   if (kill(pid, SIGUSR1) != 0) my_e(5);
567 
568   WRITE(0);
569 
570   /* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
571   if (waitpid(pid, &status, 0) != pid) my_e(6);
572   if (!_WIFSTOPPED(status)) my_e(7);
573   if (WSTOPSIG(status) != SIGUSR1) my_e(8);
574 
575   /* ptrace(T_STEP) should result in instruction-wise progress. */
576   for (count = 0; ; count++) {
577 	if (ptrace(T_STEP, pid, 0, 0) != 0) my_e(9);
578 
579 	if (waitpid(pid, &status, 0) != pid) my_e(10);
580 	if (_WIFEXITED(status)) break;
581 	if (!_WIFSTOPPED(status)) my_e(11);
582 	if (WSTOPSIG(status) != SIGTRAP) my_e(12);
583   }
584 
585   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
586 
587   if (count < 10) my_e(13); /* in practice: hundreds */
588 
589   traced_wait();
590 }
591 
592 void test_sig_child()
593 {
594   signal(SIGUSR1, exit_handler);
595 
596   if (READ() != 0) my_e(100);
597 
598   pause();
599 
600   my_e(101);
601 }
602 
603 void test_sig()
604 {
605   pid_t pid;
606   int r, sig, status;
607 
608   subtest = 4;
609 
610   pid = traced_fork(test_sig_child);
611 
612   WRITE(0);
613 
614   /* allow the child to enter the pause */
615   sleep(1);
616 
617   if (kill(pid, SIGUSR1) != 0) my_e(1);
618   if (kill(pid, SIGUSR2) != 0) my_e(2);
619 
620   /* All signals should arrive at the tracer, although in "random" order. */
621   if (waitpid(pid, &status, 0) != pid) my_e(3);
622   if (!_WIFSTOPPED(status)) my_e(4);
623   if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(5);
624 
625   /* The tracer should see kills arriving while the tracee is stopped. */
626   if (kill(pid, WSTOPSIG(status)) != 0) my_e(6);
627 
628   if (waitpid(pid, &status, WNOHANG) != pid) my_e(7);
629   if (!_WIFSTOPPED(status)) my_e(8);
630   if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(9);
631   sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
632 
633   if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(10);
634 
635   if (waitpid(pid, &status, 0) != pid) my_e(11);
636   if (!_WIFSTOPPED(status)) my_e(12);
637   if (WSTOPSIG(status) != sig) my_e(13);
638 
639   if (waitpid(pid, &status, WNOHANG) != 0) my_e(14);
640 
641   if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(15);
642 
643   /* Ignored signals passed via ptrace() should be ignored. */
644   if (kill(pid, SIGUSR1) != 0) my_e(16);
645 
646   if (waitpid(pid, &status, 0) != pid) my_e(17);
647   if (!_WIFSTOPPED(status)) my_e(18);
648   if (WSTOPSIG(status) != SIGUSR1) my_e(19);
649 
650   if (ptrace(T_RESUME, pid, 0, SIGCHLD) != 0) my_e(20);
651 
652   /* if the pause has been aborted (shouldn't happen!), let the child exit */
653   sleep(1);
654 
655   if (waitpid(pid, &status, WNOHANG) != 0) my_e(21);
656 
657   /* Caught signals passed via ptrace() should invoke their signal handlers. */
658   if (kill(pid, SIGUSR1) != 0) my_e(22);
659 
660   if (waitpid(pid, &status, 0) != pid) my_e(23);
661   if (!_WIFSTOPPED(status)) my_e(24);
662   if (WSTOPSIG(status) != SIGUSR1) my_e(25);
663 
664   if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(26);
665 
666   if (waitpid(pid, &status, 0) != pid) my_e(27);
667   if (!_WIFEXITED(status)) my_e(28);
668   if ((r = WEXITSTATUS(status)) != 42) my_e(29);
669 
670   traced_wait();
671 }
672 
673 void test_exit_child()
674 {
675   WRITE(0);
676 
677   for(;;);
678 }
679 
680 void test_exit()
681 {
682   pid_t pid;
683   int r, status;
684 
685   subtest = 5;
686 
687   pid = traced_fork(test_exit_child);
688 
689   if (READ() != 0) my_e(1);
690 
691   sleep(1);
692 
693   if (kill(pid, SIGSTOP) != 0) my_e(2);
694 
695   if (waitpid(pid, &status, 0) != pid) my_e(3);
696   if (!_WIFSTOPPED(status)) my_e(4);
697   if (WSTOPSIG(status) != SIGSTOP) my_e(5);
698 
699   /* There should be no more signals pending for the tracer now. */
700   if (waitpid(pid, &status, WNOHANG) != 0) my_e(6);
701 
702   /* ptrace(T_EXIT) should terminate the process with the given exit value. */
703   if (ptrace(T_EXIT, pid, 0, 42) != 0) my_e(7);
704 
705   if (waitpid(pid, &status, 0) != pid) my_e(8);
706   if (!_WIFEXITED(status)) my_e(9);
707   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
708 
709   traced_wait();
710 }
711 
712 void test_term_child()
713 {
714   signal(SIGUSR1, SIG_DFL);
715   signal(SIGUSR2, dummy_handler);
716 
717   WRITE(0);
718 
719   pause();
720 
721   my_e(100);
722 }
723 
724 void test_term()
725 {
726   pid_t pid;
727   int status;
728 
729   subtest = 6;
730 
731   pid = traced_fork(test_term_child);
732 
733   if (READ() != 0) my_e(1);
734 
735   /* If the first of two signals terminates the traced child, the second signal
736    * may or may not be delivered to the tracer - this is merely a policy issue.
737    * However, nothing unexpected should happen.
738    */
739   if (kill(pid, SIGUSR1) != 0) my_e(2);
740   if (kill(pid, SIGUSR2) != 0) my_e(3);
741 
742   if (waitpid(pid, &status, 0) != pid) my_e(4);
743   if (!_WIFSTOPPED(status)) my_e(5);
744 
745   if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(6);
746 
747   if (waitpid(pid, &status, 0) != pid) my_e(7);
748 
749   if (_WIFSTOPPED(status)) {
750 	if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(8);
751 
752 	if (waitpid(pid, &status, 0) != pid) my_e(9);
753   }
754 
755   if (!_WIFSIGNALED(status)) my_e(10);
756   if (WTERMSIG(status) != SIGUSR1) my_e(11);
757 
758   traced_wait();
759 }
760 
761 void test_catch_child()
762 {
763   struct sigaction sa;
764   sigset_t set, oset;
765 
766   sa.sa_handler = catch_handler;
767   sigemptyset(&sa.sa_mask);
768   sa.sa_flags = SA_NODEFER;
769 
770   sigaction(SIGUSR1, &sa, NULL);
771   sigaction(SIGUSR2, &sa, NULL);
772   sigaction(SIGTERM, &sa, NULL);
773 
774   sigfillset(&set);
775   sigprocmask(SIG_SETMASK, &set, &oset);
776 
777   caught = 0;
778 
779   WRITE(0);
780 
781   while (caught != 7) sigsuspend(&oset);
782 
783   exit(42);
784 }
785 
786 void test_catch()
787 {
788   pid_t pid;
789   int r, sig, status;
790 
791   subtest = 7;
792 
793   pid = traced_fork(test_catch_child);
794 
795   if (READ() != 0) my_e(1);
796 
797   if (kill(pid, SIGUSR1) != 0) my_e(2);
798   if (kill(pid, SIGUSR2) != 0) my_e(3);
799 
800   if (waitpid(pid, &status, 0) != pid) my_e(4);
801   if (!_WIFSTOPPED(status)) my_e(5);
802   if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(6);
803   sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
804 
805   if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(7);
806 
807   if (kill(pid, SIGTERM) != 0) my_e(8);
808 
809   if (waitpid(pid, &status, 0) != pid) my_e(9);
810   if (!_WIFSTOPPED(status)) my_e(10);
811   if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGTERM) my_e(11);
812   if (WSTOPSIG(status) == sig) sig = SIGTERM;
813 
814   if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(12);
815 
816   if (kill(pid, SIGBUS) != 0) my_e(13);
817 
818   if (waitpid(pid, &status, 0) != pid) my_e(14);
819   if (!_WIFSTOPPED(status)) my_e(15);
820   if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGBUS) my_e(16);
821 
822   if (ptrace(T_RESUME, pid, 0, sig) != 0) my_e(17);
823 
824   if (WSTOPSIG(status) == sig) sig = SIGBUS;
825 
826   if (waitpid(pid, &status, 0) != pid) my_e(18);
827   if (!_WIFSTOPPED(status)) my_e(19);
828   if (WSTOPSIG(status) != sig) my_e(20);
829 
830   if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(21);
831 
832   if (waitpid(pid, &status, 0) != pid) my_e(22);
833   if (!_WIFEXITED(status)) my_e(23);
834   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
835 
836   traced_wait();
837 }
838 
839 void test_kill_child()
840 {
841   sigset_t set;
842 
843   signal(SIGKILL, SIG_IGN);
844   sigfillset(&set);
845   sigprocmask(SIG_SETMASK, &set, NULL);
846 
847   WRITE(0);
848 
849   pause();
850 
851   my_e(100);
852 }
853 
854 void test_kill()
855 {
856   pid_t pid;
857   int status;
858 
859   subtest = 8;
860 
861   pid = traced_fork(test_kill_child);
862 
863   if (READ() != 0) my_e(1);
864 
865   /* SIGKILL must be unstoppable in every way. */
866   if (kill(pid, SIGKILL) != 0) my_e(2);
867 
868   if (waitpid(pid, &status, 0) != pid) my_e(3);
869   if (!_WIFSIGNALED(status)) my_e(4);
870   if (WTERMSIG(status) != SIGKILL) my_e(5);
871 
872   /* After termination, the child must no longer be visible to the tracer. */
873   if (waitpid(pid, &status, WNOHANG) != -1) my_e(6);
874   if (errno != ECHILD) my_e(7);
875 
876   traced_wait();
877 }
878 
879 void test_attach_child()
880 {
881   if (ptrace(T_OK, 0, 0, 0) != -1) my_e(100);
882   if (errno != EBUSY) my_e(101);
883 
884   WRITE(0);
885 
886   if (READ() != 0) my_e(102);
887 
888   exit(42);
889 }
890 
891 void test_attach()
892 {
893   pid_t pid;
894 
895   subtest = 9;
896 
897   /* Attaching to kernel processes is not allowed. */
898   if (ptrace(T_ATTACH, -1, 0, 0) != -1) my_e(1);
899   if (errno != ESRCH) my_e(2);
900 
901   /* Attaching to self is not allowed. */
902   if (ptrace(T_ATTACH, getpid(), 0, 0) != -1) my_e(3);
903   if (errno != EPERM) my_e(4);
904 
905   /* Attaching to PM is not allowed. */
906 #if 0
907   /* FIXME: disabled until we can reliably determine PM's pid */
908   if (ptrace(T_ATTACH, 0, 0, 0) != -1) my_e(5);
909   if (errno != EPERM) my_e(6);
910 #endif
911 
912   pid = traced_fork(test_attach_child);
913 
914   /* Attaching more than once is not allowed. */
915   if (ptrace(T_ATTACH, pid, 0, 0) != -1) my_e(7);
916   if (errno != EBUSY) my_e(8);
917 
918   if (READ() != 0) my_e(9);
919 
920   /* Detaching a running child should not succeed. */
921   if (ptrace(T_DETACH, pid, 0, 0) == 0) my_e(10);
922   if (errno != EBUSY) my_e(11);
923 
924   detach_running(pid);
925 
926   WRITE(0);
927 
928   traced_wait();
929 }
930 
931 void test_detach_child()
932 {
933   struct sigaction sa;
934   sigset_t set, sset, oset;
935 
936   sa.sa_handler = catch_handler;
937   sigemptyset(&sa.sa_mask);
938   sa.sa_flags = SA_NODEFER;
939 
940   sigaction(SIGUSR1, &sa, NULL);
941   sigaction(SIGUSR2, &sa, NULL);
942   sigaction(SIGTERM, &sa, NULL);
943 
944   sigfillset(&set);
945   sigprocmask(SIG_SETMASK, &set, &oset);
946 
947   sigfillset(&sset);
948   sigdelset(&sset, SIGUSR1);
949 
950   caught = 0;
951 
952   WRITE(0);
953 
954   if (sigsuspend(&sset) != -1) my_e(102);
955   if (errno != EINTR) my_e(103);
956 
957   if (caught != 1) my_e(104);
958 
959   if (READ() != 0) my_e(105);
960 
961   while (caught != 7) sigsuspend(&oset);
962 
963   exit(42);
964 }
965 
966 void test_detach()
967 {
968   pid_t pid;
969   int r, status;
970 
971   /* Can't use traced_fork(), so simplify a bit */
972   if (attach != 0) return;
973 
974   subtest = 10;
975 
976   pid = traced_pfork(test_detach_child);
977 
978   if (READ() != 0) my_e(1);
979 
980   /* The tracer should not see signals sent to the process before attaching. */
981   if (kill(pid, SIGUSR2) != 0) my_e(2);
982 
983   if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(3);
984 
985   if (waitpid(pid, &status, 0) != pid) my_e(4);
986   if (!_WIFSTOPPED(status)) my_e(5);
987   if (WSTOPSIG(status) != SIGSTOP) my_e(6);
988 
989   if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(7);
990 
991   if (kill(pid, SIGUSR1) != 0) my_e(8);
992 
993   if (waitpid(pid, &status, 0) != pid) my_e(9);
994   if (!_WIFSTOPPED(status)) my_e(10);
995   if (WSTOPSIG(status) != SIGUSR1) my_e(11);
996 
997   /* Signals pending at the tracer should be passed on after detaching. */
998   if (kill(pid, SIGTERM) != 0) my_e(12);
999 
1000   /* A signal may be passed with the detach request. */
1001   if (ptrace(T_DETACH, pid, 0, SIGUSR1) != 0) my_e(13);
1002 
1003   WRITE(0);
1004 
1005   if (waitpid(pid, &status, 0) != pid) my_e(14);
1006   if (!_WIFEXITED(status)) my_e(15);
1007   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1008 
1009   traced_wait();
1010 }
1011 
1012 void test_death_child()
1013 {
1014   pid_t pid;
1015 
1016   pid = fork();
1017 
1018   if (pid < 0) my_e(100);
1019 
1020   if (pid == 0) {
1021 	ptrace(T_OK, 0, 0, 0);
1022 
1023 	WRITE(getpid());
1024 
1025   	for (;;) pause();
1026   }
1027 
1028   if (READ() != 0) my_e(101);
1029 
1030   kill(getpid(), SIGKILL);
1031 
1032   my_e(102);
1033 }
1034 
1035 void test_death()
1036 {
1037   pid_t pid, cpid;
1038   int status;
1039 
1040   subtest = 11;
1041 
1042   pid = traced_fork(test_death_child);
1043 
1044   cpid = READ();
1045 
1046   if (kill(cpid, 0) != 0) my_e(1);
1047 
1048   WRITE(0);
1049 
1050   if (waitpid(pid, &status, 0) != pid) my_e(2);
1051   if (!_WIFSIGNALED(status)) my_e(3);
1052   if (WTERMSIG(status) != SIGKILL) my_e(4);
1053 
1054   /* The children of killed tracers should be terminated. */
1055   while (kill(cpid, 0) == 0) sleep(1);
1056   if (errno != ESRCH) my_e(5);
1057 
1058   traced_wait();
1059 }
1060 
1061 void test_zdeath_child()
1062 {
1063   if (READ() != 0) my_e(100);
1064 
1065   exit(42);
1066 }
1067 
1068 void test_zdeath()
1069 {
1070   pid_t pid, tpid;
1071   int r, status;
1072 
1073   /* Can't use traced_fork(), so simplify a bit */
1074   if (attach != 0) return;
1075 
1076   subtest = 12;
1077 
1078   pid = traced_pfork(test_zdeath_child);
1079 
1080   tpid = fork();
1081 
1082   if (tpid < 0) my_e(1);
1083 
1084   if (tpid == 0) {
1085 	if (ptrace(T_ATTACH, pid, 0, 0) != 0) exit(101);
1086 
1087 	if (waitpid(pid, &status, 0) != pid) exit(102);
1088 	if (!_WIFSTOPPED(status)) exit(103);
1089 	if (WSTOPSIG(status) != SIGSTOP) exit(104);
1090 
1091 	if (ptrace(T_RESUME, pid, 0, 0) != 0) exit(105);
1092 
1093 	WRITE(0);
1094 
1095 	/* Unwaited-for traced zombies should be passed to their parent. */
1096 	sleep(2);
1097 
1098 	exit(84);
1099   }
1100 
1101   sleep(1);
1102 
1103   /* However, that should only happen once the tracer has actually died. */
1104   if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
1105 
1106   if (waitpid(tpid, &status, 0) != tpid) my_e(3);
1107   if (!_WIFEXITED(status)) my_e(4);
1108   if ((r = WEXITSTATUS(status)) != 84) my_e(r);
1109 
1110   if (waitpid(pid, &status, 0) != pid) my_e(5);
1111   if (!_WIFEXITED(status)) my_e(6);
1112   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1113 
1114   traced_wait();
1115 }
1116 
1117 void test_syscall_child()
1118 {
1119   signal(SIGUSR1, count_handler);
1120   signal(SIGUSR2, count_handler);
1121 
1122   sigs = 0;
1123 
1124   WRITE(0);
1125 
1126   if (READ() != 0) my_e(100);
1127 
1128   /* Three calls (may fail) */
1129   setuid(0);
1130   close(123);
1131   getpid();
1132 
1133   if (sigs != 2) my_e(101);
1134 
1135   exit(42);
1136 }
1137 
1138 void test_syscall()
1139 {
1140   pid_t pid;
1141   int i, r, sig, status;
1142 
1143   subtest = 13;
1144 
1145   pid = traced_fork(test_syscall_child);
1146 
1147   if (READ() != 0) my_e(1);
1148 
1149   if (kill(pid, SIGSTOP) != 0) my_e(2);
1150 
1151   if (waitpid(pid, &status, 0) != pid) my_e(3);
1152   if (!_WIFSTOPPED(status)) my_e(4);
1153   if (WSTOPSIG(status) != SIGSTOP) my_e(5);
1154 
1155   WRITE(0);
1156 
1157   /* Upon resuming a first system call, no syscall leave event must be sent. */
1158   if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(6);
1159 
1160   if (waitpid(pid, &status, 0) != pid) my_e(7);
1161 
1162   for (i = 0; _WIFSTOPPED(status); i++) {
1163 	if (WSTOPSIG(status) != SIGTRAP) my_e(8);
1164 
1165 	/* Signals passed via T_SYSCALL should arrive, on enter and exit. */
1166 	if (i == 3) sig = SIGUSR1;
1167 	else if (i == 6) sig = SIGUSR2;
1168 	else sig = 0;
1169 
1170 	if (ptrace(T_SYSCALL, pid, 0, sig) != 0) my_e(9);
1171 
1172 	if (waitpid(pid, &status, 0) != pid) my_e(10);
1173   }
1174 
1175   if (!_WIFEXITED(status)) my_e(11);
1176   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1177 
1178   /* The number of events seen is deterministic but libc-dependent. */
1179   if (i < 10 || i > 100) my_e(12);
1180 
1181   /* The last system call event must be for entering exit(). */
1182   if (!(i % 2)) my_e(13);
1183 
1184   traced_wait();
1185 }
1186 
1187 void test_tracefork_child()
1188 {
1189   pid_t pid;
1190 
1191   signal(SIGHUP, SIG_IGN);
1192 
1193   pid = setsid();
1194 
1195   WRITE(pid);
1196 
1197   if (READ() != 0) my_e(100);
1198 
1199   if ((pid = fork()) < 0) my_e(101);
1200 
1201   exit(pid > 0 ? 42 : 84);
1202 }
1203 
1204 void test_tracefork()
1205 {
1206   pid_t pgrp, ppid, cpid, wpid;
1207   int r, status, gotstop, ptraps, ctraps;
1208 
1209   subtest = 14;
1210 
1211   ppid = traced_fork(test_tracefork_child);
1212 
1213   if ((pgrp = READ()) <= 0) my_e(1);
1214 
1215   if (kill(ppid, SIGSTOP) != 0) my_e(2);
1216 
1217   if (waitpid(ppid, &status, 0) != ppid) my_e(3);
1218   if (!_WIFSTOPPED(status)) my_e(4);
1219   if (WSTOPSIG(status) != SIGSTOP) my_e(5);
1220 
1221   if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(6);
1222 
1223   WRITE(0);
1224 
1225   if (ptrace(T_SYSCALL, ppid, 0, 0) != 0) my_e(7);
1226 
1227   cpid = -1;
1228   gotstop = -1;
1229 
1230   /* Count how many traps we get for parent and child, until they both exit. */
1231   for (ptraps = ctraps = 0; ppid || cpid; ) {
1232 	wpid = waitpid(-pgrp, &status, 0);
1233 
1234 	if (wpid <= 0) my_e(8);
1235 	if (cpid < 0 && wpid != ppid) {
1236 		cpid = wpid;
1237 		gotstop = 0;
1238 	}
1239 	if (wpid != ppid && wpid != cpid) my_e(9);
1240 
1241 	if (_WIFEXITED(status)) {
1242 		if (wpid == ppid) {
1243 			if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1244 			ppid = 0;
1245 		}
1246 		else {
1247 			if ((r = WEXITSTATUS(status)) != 84) my_e(r);
1248 			cpid = 0;
1249 		}
1250 	}
1251 	else {
1252 		if (!_WIFSTOPPED(status)) my_e(10);
1253 
1254 		switch (WSTOPSIG(status)) {
1255 		case SIGCHLD:
1256 		case SIGHUP:
1257 			break;
1258 		case SIGSTOP:
1259 			if (wpid != cpid) my_e(11);
1260 			if (gotstop) my_e(12);
1261 			gotstop = 1;
1262 			break;
1263 		case SIGTRAP:
1264 			if (wpid == ppid) ptraps++;
1265 			else ctraps++;
1266 			break;
1267 		default:
1268 			my_e(13);
1269 		}
1270 
1271 		if (ptrace(T_SYSCALL, wpid, 0, 0) != 0) my_e(14);
1272 	}
1273   }
1274 
1275   /* The parent should get an odd number of traps: the first one is a syscall
1276    * enter trap (typically for the fork()), the last one is the syscall enter
1277    * trap for its exit().
1278    */
1279   if (ptraps < 3) my_e(15);
1280   if (!(ptraps % 2)) my_e(16);
1281 
1282   /* The child should get an even number of traps: the first one is a syscall
1283    * leave trap from the fork(), the last one is the syscall enter trap for
1284    * its exit().
1285    */
1286   if (ctraps < 2) my_e(17);
1287   if (ctraps % 2) my_e(18);
1288 
1289   traced_wait();
1290 }
1291 
1292 void sigexec(setflag, opt, traps, stop)
1293 int setflag;
1294 int opt;
1295 int *traps;
1296 int *stop;
1297 {
1298   pid_t pid;
1299   int r, status;
1300 
1301   pid = traced_fork(test_exec_child);
1302 
1303   if (kill(pid, SIGSTOP) != 0) my_e(1);
1304 
1305   if (waitpid(pid, &status, 0) != pid) my_e(2);
1306   if (!_WIFSTOPPED(status)) my_e(3);
1307   if (WSTOPSIG(status) != SIGSTOP) my_e(4);
1308 
1309   if (setflag && ptrace(T_SETOPT, pid, 0, opt) != 0) my_e(5);
1310 
1311   WRITE(0);
1312 
1313   if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(6);
1314 
1315   *traps = 0;
1316   *stop = -1;
1317 
1318   for (;;) {
1319   	if (waitpid(pid, &status, 0) != pid) my_e(7);
1320 
1321   	if (_WIFEXITED(status)) break;
1322 
1323   	if (!_WIFSTOPPED(status)) my_e(8);
1324 
1325   	switch (WSTOPSIG(status)) {
1326   	case SIGTRAP:
1327 		(*traps)++;
1328 		break;
1329   	case SIGSTOP:
1330   		if (*stop >= 0) my_e(9);
1331   		*stop = *traps;
1332   		break;
1333   	default:
1334 		my_e(10);
1335   	}
1336 
1337   	if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(11);
1338   }
1339 
1340   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1341 
1342   traced_wait();
1343 }
1344 
1345 void test_trapexec()
1346 {
1347   int traps, stop;
1348 
1349   subtest = 15;
1350 
1351   sigexec(1, 0, &traps, &stop);
1352 
1353   /* The exec does not cause a SIGSTOP. This gives us an even number of traps;
1354    * as above, but plus the exec()'s extra SIGTRAP. This trap is
1355    * indistinguishable from a syscall trap, especially when considering failed
1356    * exec() calls and immediately following signal handler invocations.
1357    */
1358   if (traps < 4) my_e(12);
1359   if (traps % 2) my_e(13);
1360   if (stop >= 0) my_e(14);
1361 }
1362 
1363 void test_altexec()
1364 {
1365   int traps, stop;
1366 
1367   subtest = 16;
1368 
1369   sigexec(1, TO_ALTEXEC, &traps, &stop);
1370 
1371   /* The exec causes a SIGSTOP. This gives us an odd number of traps: a pair
1372    * for each system call, plus one for the final exit(). The stop must have
1373    * taken place after a syscall enter event, i.e. must be odd as well.
1374    */
1375   if (traps < 3) my_e(12);
1376   if (!(traps % 2)) my_e(13);
1377   if (stop < 0) my_e(14);
1378   if (!(stop % 2)) my_e(15);
1379 }
1380 
1381 void test_noexec()
1382 {
1383   int traps, stop;
1384 
1385   subtest = 17;
1386 
1387   sigexec(1, TO_NOEXEC, &traps, &stop);
1388 
1389   /* The exec causes no signal at all. As above, but without the SIGSTOPs. */
1390   if (traps < 3) my_e(12);
1391   if (!(traps % 2)) my_e(13);
1392   if (stop >= 0) my_e(14);
1393 }
1394 
1395 void test_defexec()
1396 {
1397   int traps, stop;
1398 
1399   /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1400   if (attach != 0 && attach != 1) return;
1401 
1402   subtest = 18;
1403 
1404   /* Do not set any options this time. */
1405   sigexec(0, 0, &traps, &stop);
1406 
1407   /* See above. */
1408   if (attach == 0) {
1409 	if (traps < 4) my_e(12);
1410 	if (traps % 2) my_e(13);
1411 	if (stop >= 0) my_e(14);
1412   }
1413   else {
1414 	if (traps < 3) my_e(15);
1415 	if (!(traps % 2)) my_e(16);
1416 	if (stop >= 0) my_e(17);
1417   }
1418 }
1419 
1420 void test_reattach_child()
1421 {
1422   struct timeval tv;
1423 
1424   if (READ() != 0) my_e(100);
1425 
1426   tv.tv_sec = 2;
1427   tv.tv_usec = 0;
1428   if (select(0, NULL, NULL, NULL, &tv) != 0) my_e(101);
1429 
1430   exit(42);
1431 }
1432 
1433 void test_reattach()
1434 {
1435   pid_t pid;
1436   int r, status, count;
1437 
1438   subtest = 19;
1439 
1440   pid = traced_fork(test_reattach_child);
1441 
1442   if (kill(pid, SIGSTOP) != 0) my_e(1);
1443 
1444   if (waitpid(pid, &status, 0) != pid) my_e(2);
1445   if (!_WIFSTOPPED(status)) my_e(3);
1446   if (WSTOPSIG(status) != SIGSTOP) my_e(4);
1447 
1448   WRITE(0);
1449 
1450   signal(SIGALRM, dummy_handler);
1451   alarm(1);
1452 
1453   /* Start tracing system calls. We don't know how many there will be until
1454    * we reach the child's select(), so we have to interrupt ourselves.
1455    * The hard assumption here is that the child is able to enter the select()
1456    * within a second, despite being traced. If this is not the case, the test
1457    * may hang or fail, and the child may die from a SIGTRAP.
1458    */
1459   if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(5);
1460 
1461   for (count = 0; (r = waitpid(pid, &status, 0)) == pid; count++) {
1462 	if (!_WIFSTOPPED(status)) my_e(6);
1463 	if (WSTOPSIG(status) != SIGTRAP) my_e(7);
1464 
1465 	if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(8);
1466   }
1467 
1468   if (r != -1 || errno != EINTR) my_e(9);
1469 
1470   /* We always start with syscall enter event; the last event we should have
1471    * seen before the alarm was entering the select() call.
1472    */
1473   if (!(count % 2)) my_e(10);
1474 
1475   /* Detach, and immediately attach again. */
1476   detach_running(pid);
1477 
1478   if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(11);
1479 
1480   if (waitpid(pid, &status, 0) != pid) my_e(12);
1481   if (!_WIFSTOPPED(status)) my_e(13);
1482   if (WSTOPSIG(status) != SIGSTOP) my_e(14);
1483 
1484   if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(15);
1485 
1486   if (waitpid(pid, &status, 0) != pid) my_e(16);
1487 
1488   for (count = 0; _WIFSTOPPED(status); count++) {
1489 	if (WSTOPSIG(status) != SIGTRAP) my_e(17);
1490 
1491 	if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(18);
1492 
1493 	if (waitpid(pid, &status, 0) != pid) my_e(19);
1494   }
1495 
1496   if (!_WIFEXITED(status)) my_e(20);
1497   if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1498 
1499   /* We must not have seen the select()'s syscall leave event, and the last
1500    * event will be the syscall enter for the exit().
1501    */
1502   if (!(count % 2)) my_e(21);
1503 
1504   traced_wait();
1505 }
1506 
1507