<sys/syslimits.h>
[minix3.git] / test / test42.c
blob1b6f9c0e8bbcc90d70ccadea0a1d23a744777eaf
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>
14 #define ITERATIONS 3
15 int max_error = 4;
16 #include "common.h"
18 #define my_e(n) { \
19 if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
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))
26 #define timed_test(func) (timed_test_func(#func, func));
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);
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;
84 int main(argc, argv)
85 int argc;
86 char **argv;
88 int i, m = 0xFFFFFF, n = 0xF;
89 char cp_cmd[NAME_MAX + 10];
91 if (strcmp(argv[0], "DO CHECK") == 0) {
92 exit(42);
95 start(42);
97 executable = argv[0];
99 snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);
100 system(cp_cmd);
102 if (argc >= 2) m = atoi(argv[1]);
103 if (argc >= 3) n = atoi(argv[2]);
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);
112 quit();
113 return(-1); /* impossible */
116 void test(m, a)
117 int m;
118 int a;
120 attach = a;
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 */
146 static jmp_buf timed_test_context;
148 void timed_test_timeout(int signum)
150 longjmp(timed_test_context, -1);
151 my_e(700);
152 quit();
153 exit(-1);
156 void timed_test_func(const char *s, void (* func)(void))
158 if (setjmp(timed_test_context) == 0)
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);
166 else
168 /* report timeout as error */
169 printf("timeout in %s\n", s);
170 my_e(702);
174 pid_t traced_fork(c)
175 void(*c) (void);
177 pid_t pid;
178 int r, status;
180 if (pipe(pfd) != 0) my_e(200);
181 if (pipe(&pfd[2]) != 0) my_e(201);
183 switch (attach) {
184 case 0: /* let child volunteer to be traced */
185 pid = fork();
187 if (pid < 0) my_e(202);
189 if (pid == 0) {
190 child = 1;
192 if (ptrace(T_OK, 0, 0, 0) != 0) my_e(203);
194 WRITE(0);
196 c();
198 my_e(204);
201 if (READ() != 0) my_e(205);
203 break;
205 case 1: /* attach to child process */
206 pid = fork();
208 if (pid < 0) my_e(206);
210 if (pid == 0) {
211 child = 1;
213 if (READ() != 0) my_e(207);
215 c();
217 my_e(208);
220 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(209);
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);
226 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(213);
228 WRITE(0);
230 break;
232 case 2: /* attach to non-child process */
233 ppid = fork();
235 if (ppid < 0) my_e(214);
237 if (ppid == 0) {
238 pid = fork();
240 if (pid < 0) exit(215);
242 if (pid == 0) {
243 child = 1;
245 if (READ() != 0) my_e(216);
247 c();
249 my_e(217);
252 child = 1;
254 WRITE(pid);
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);
260 exit(0);
263 pid = READ();
265 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(220);
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);
271 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(224);
273 WRITE(0);
275 break;
277 case 3: /* attach by forking from child */
278 ppid = fork();
280 if (ppid < 0) my_e(225);
282 if (ppid == 0) {
283 child = 1;
285 if (ptrace(T_OK, 0, 0, 0) != 0) my_e(226);
287 WRITE(0);
289 if (READ() != 0) my_e(227);
291 pid = fork();
293 if (pid < 0) my_e(228);
295 if (pid == 0) {
296 c();
298 my_e(229);
301 WRITE(pid);
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);
307 exit(0);
310 if (READ() != 0) my_e(232);
312 if (kill(ppid, SIGSTOP) != 0) my_e(233);
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);
318 if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(237);
320 if (ptrace(T_RESUME, ppid, 0, 0) != 0) my_e(238);
322 WRITE(0);
324 pid = READ();
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);
330 if (ptrace(T_SETOPT, pid, 0, 0) != 0) my_e(242);
331 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(243);
333 detach_running(ppid);
335 break;
338 return pid;
341 pid_t traced_pfork(c)
342 void(*c) (void);
344 pid_t pid;
346 if (pipe(pfd) != 0) my_e(300);
347 if (pipe(&pfd[2]) != 0) my_e(301);
349 pid = fork();
351 if (pid < 0) my_e(302);
353 if (pid == 0) {
354 child = 1;
356 c();
358 my_e(303);
361 return pid;
364 void WRITE(value)
365 int value;
367 if (write(pfd[child*2+1], &value, sizeof(value)) != sizeof(value)) my_e(400);
370 int READ()
372 int value;
374 if (read(pfd[2-child*2], &value, sizeof(value)) != sizeof(value)) my_e(401);
376 return value;
379 void traced_wait()
381 int r, status;
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);
388 else {
389 /* Quick hack to clean up detached children */
390 waitpid(-1, NULL, WNOHANG);
393 close(pfd[0]);
394 close(pfd[1]);
395 close(pfd[2]);
396 close(pfd[3]);
399 void detach_running(pid)
400 pid_t pid;
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.
412 int status;
414 if (kill(pid, SIGSTOP) != 0) my_e(600);
416 if (waitpid(pid, &status, 0) != pid) my_e(601);
418 while (_WIFSTOPPED(status)) {
419 if (WSTOPSIG(status) == SIGSTOP) {
420 if (ptrace(T_DETACH, pid, 0, 0) != 0) my_e(602);
422 return;
425 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(603);
427 if (waitpid(pid, &status, 0) != pid) my_e(604);
430 /* Apparently the process exited. */
431 if (!_WIFEXITED(status) && !_WIFSIGNALED(status)) my_e(605);
433 /* In our tests, that should not happen. */
434 my_e(606);
437 void dummy_handler(sig)
438 int sig;
442 void exit_handler(sig)
443 int sig;
445 exit(42);
448 void count_handler(sig)
449 int sig;
451 sigs++;
454 void catch_handler(sig)
455 int sig;
457 sigset_t set;
458 int bit;
460 switch (sig) {
461 case SIGUSR1: bit = 1; break;
462 case SIGUSR2: bit = 2; break;
463 case SIGTERM: bit = 4; break;
464 default: my_e(100);
467 sigfillset(&set);
468 sigprocmask(SIG_SETMASK, &set, NULL);
470 if (caught & bit) my_e(101);
471 caught |= bit;
474 void test_wait_child()
476 exit(42);
479 void test_wait()
481 pid_t pid;
482 int status;
484 subtest = 1;
486 pid = traced_fork(test_wait_child);
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);
492 traced_wait();
495 void test_exec_child()
497 if (READ() != 0) my_e(100);
499 execl(executable, "DO CHECK", NULL);
501 my_e(101);
504 void test_exec()
506 pid_t pid;
507 int r, status;
509 /* This test covers the T_OK case. */
510 if (attach != 0) return;
512 subtest = 2;
514 pid = traced_fork(test_exec_child);
516 WRITE(0);
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);
523 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(4);
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);
529 traced_wait();
532 void test_step_child()
534 sigset_t set;
536 signal(SIGUSR1, SIG_IGN);
538 WRITE(0);
540 if (READ() != 0) my_e(100);
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);
547 exit(42);
550 void test_step()
552 pid_t pid;
553 int r, status, count;
555 subtest = 3;
557 pid = traced_fork(test_step_child);
559 if (READ() != 0) my_e(1);
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);
566 if (kill(pid, SIGUSR1) != 0) my_e(5);
568 WRITE(0);
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);
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);
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);
585 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
587 if (count < 10) my_e(13); /* in practice: hundreds */
589 traced_wait();
592 void test_sig_child()
594 signal(SIGUSR1, exit_handler);
596 if (READ() != 0) my_e(100);
598 pause();
600 my_e(101);
603 void test_sig()
605 pid_t pid;
606 int r, sig, status;
608 subtest = 4;
610 pid = traced_fork(test_sig_child);
612 WRITE(0);
614 /* allow the child to enter the pause */
615 sleep(1);
617 if (kill(pid, SIGUSR1) != 0) my_e(1);
618 if (kill(pid, SIGUSR2) != 0) my_e(2);
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);
625 /* The tracer should see kills arriving while the tracee is stopped. */
626 if (kill(pid, WSTOPSIG(status)) != 0) my_e(6);
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;
633 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(10);
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);
639 if (waitpid(pid, &status, WNOHANG) != 0) my_e(14);
641 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(15);
643 /* Ignored signals passed via ptrace() should be ignored. */
644 if (kill(pid, SIGUSR1) != 0) my_e(16);
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);
650 if (ptrace(T_RESUME, pid, 0, SIGCHLD) != 0) my_e(20);
652 /* if the pause has been aborted (shouldn't happen!), let the child exit */
653 sleep(1);
655 if (waitpid(pid, &status, WNOHANG) != 0) my_e(21);
657 /* Caught signals passed via ptrace() should invoke their signal handlers. */
658 if (kill(pid, SIGUSR1) != 0) my_e(22);
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);
664 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(26);
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);
670 traced_wait();
673 void test_exit_child()
675 WRITE(0);
677 for(;;);
680 void test_exit()
682 pid_t pid;
683 int r, status;
685 subtest = 5;
687 pid = traced_fork(test_exit_child);
689 if (READ() != 0) my_e(1);
691 sleep(1);
693 if (kill(pid, SIGSTOP) != 0) my_e(2);
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);
699 /* There should be no more signals pending for the tracer now. */
700 if (waitpid(pid, &status, WNOHANG) != 0) my_e(6);
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);
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);
709 traced_wait();
712 void test_term_child()
714 signal(SIGUSR1, SIG_DFL);
715 signal(SIGUSR2, dummy_handler);
717 WRITE(0);
719 pause();
721 my_e(100);
724 void test_term()
726 pid_t pid;
727 int status;
729 subtest = 6;
731 pid = traced_fork(test_term_child);
733 if (READ() != 0) my_e(1);
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.
739 if (kill(pid, SIGUSR1) != 0) my_e(2);
740 if (kill(pid, SIGUSR2) != 0) my_e(3);
742 if (waitpid(pid, &status, 0) != pid) my_e(4);
743 if (!_WIFSTOPPED(status)) my_e(5);
745 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(6);
747 if (waitpid(pid, &status, 0) != pid) my_e(7);
749 if (_WIFSTOPPED(status)) {
750 if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(8);
752 if (waitpid(pid, &status, 0) != pid) my_e(9);
755 if (!_WIFSIGNALED(status)) my_e(10);
756 if (WTERMSIG(status) != SIGUSR1) my_e(11);
758 traced_wait();
761 void test_catch_child()
763 struct sigaction sa;
764 sigset_t set, oset;
766 sa.sa_handler = catch_handler;
767 sigemptyset(&sa.sa_mask);
768 sa.sa_flags = SA_NODEFER;
770 sigaction(SIGUSR1, &sa, NULL);
771 sigaction(SIGUSR2, &sa, NULL);
772 sigaction(SIGTERM, &sa, NULL);
774 sigfillset(&set);
775 sigprocmask(SIG_SETMASK, &set, &oset);
777 caught = 0;
779 WRITE(0);
781 while (caught != 7) sigsuspend(&oset);
783 exit(42);
786 void test_catch()
788 pid_t pid;
789 int r, sig, status;
791 subtest = 7;
793 pid = traced_fork(test_catch_child);
795 if (READ() != 0) my_e(1);
797 if (kill(pid, SIGUSR1) != 0) my_e(2);
798 if (kill(pid, SIGUSR2) != 0) my_e(3);
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;
805 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(7);
807 if (kill(pid, SIGTERM) != 0) my_e(8);
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;
814 if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(12);
816 if (kill(pid, SIGBUS) != 0) my_e(13);
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);
822 if (ptrace(T_RESUME, pid, 0, sig) != 0) my_e(17);
824 if (WSTOPSIG(status) == sig) sig = SIGBUS;
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);
830 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(21);
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);
836 traced_wait();
839 void test_kill_child()
841 sigset_t set;
843 signal(SIGKILL, SIG_IGN);
844 sigfillset(&set);
845 sigprocmask(SIG_SETMASK, &set, NULL);
847 WRITE(0);
849 pause();
851 my_e(100);
854 void test_kill()
856 pid_t pid;
857 int status;
859 subtest = 8;
861 pid = traced_fork(test_kill_child);
863 if (READ() != 0) my_e(1);
865 /* SIGKILL must be unstoppable in every way. */
866 if (kill(pid, SIGKILL) != 0) my_e(2);
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);
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);
876 traced_wait();
879 void test_attach_child()
881 if (ptrace(T_OK, 0, 0, 0) != -1) my_e(100);
882 if (errno != EBUSY) my_e(101);
884 WRITE(0);
886 if (READ() != 0) my_e(102);
888 exit(42);
891 void test_attach()
893 pid_t pid;
895 subtest = 9;
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);
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);
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
912 pid = traced_fork(test_attach_child);
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);
918 if (READ() != 0) my_e(9);
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);
924 detach_running(pid);
926 WRITE(0);
928 traced_wait();
931 void test_detach_child()
933 struct sigaction sa;
934 sigset_t set, sset, oset;
936 sa.sa_handler = catch_handler;
937 sigemptyset(&sa.sa_mask);
938 sa.sa_flags = SA_NODEFER;
940 sigaction(SIGUSR1, &sa, NULL);
941 sigaction(SIGUSR2, &sa, NULL);
942 sigaction(SIGTERM, &sa, NULL);
944 sigfillset(&set);
945 sigprocmask(SIG_SETMASK, &set, &oset);
947 sigfillset(&sset);
948 sigdelset(&sset, SIGUSR1);
950 caught = 0;
952 WRITE(0);
954 if (sigsuspend(&sset) != -1) my_e(102);
955 if (errno != EINTR) my_e(103);
957 if (caught != 1) my_e(104);
959 if (READ() != 0) my_e(105);
961 while (caught != 7) sigsuspend(&oset);
963 exit(42);
966 void test_detach()
968 pid_t pid;
969 int r, status;
971 /* Can't use traced_fork(), so simplify a bit */
972 if (attach != 0) return;
974 subtest = 10;
976 pid = traced_pfork(test_detach_child);
978 if (READ() != 0) my_e(1);
980 /* The tracer should not see signals sent to the process before attaching. */
981 if (kill(pid, SIGUSR2) != 0) my_e(2);
983 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(3);
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);
989 if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(7);
991 if (kill(pid, SIGUSR1) != 0) my_e(8);
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);
997 /* Signals pending at the tracer should be passed on after detaching. */
998 if (kill(pid, SIGTERM) != 0) my_e(12);
1000 /* A signal may be passed with the detach request. */
1001 if (ptrace(T_DETACH, pid, 0, SIGUSR1) != 0) my_e(13);
1003 WRITE(0);
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);
1009 traced_wait();
1012 void test_death_child()
1014 pid_t pid;
1016 pid = fork();
1018 if (pid < 0) my_e(100);
1020 if (pid == 0) {
1021 ptrace(T_OK, 0, 0, 0);
1023 WRITE(getpid());
1025 for (;;) pause();
1028 if (READ() != 0) my_e(101);
1030 kill(getpid(), SIGKILL);
1032 my_e(102);
1035 void test_death()
1037 pid_t pid, cpid;
1038 int status;
1040 subtest = 11;
1042 pid = traced_fork(test_death_child);
1044 cpid = READ();
1046 if (kill(cpid, 0) != 0) my_e(1);
1048 WRITE(0);
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);
1054 /* The children of killed tracers should be terminated. */
1055 while (kill(cpid, 0) == 0) sleep(1);
1056 if (errno != ESRCH) my_e(5);
1058 traced_wait();
1061 void test_zdeath_child()
1063 if (READ() != 0) my_e(100);
1065 exit(42);
1068 void test_zdeath()
1070 pid_t pid, tpid;
1071 int r, status;
1073 /* Can't use traced_fork(), so simplify a bit */
1074 if (attach != 0) return;
1076 subtest = 12;
1078 pid = traced_pfork(test_zdeath_child);
1080 tpid = fork();
1082 if (tpid < 0) my_e(1);
1084 if (tpid == 0) {
1085 if (ptrace(T_ATTACH, pid, 0, 0) != 0) exit(101);
1087 if (waitpid(pid, &status, 0) != pid) exit(102);
1088 if (!_WIFSTOPPED(status)) exit(103);
1089 if (WSTOPSIG(status) != SIGSTOP) exit(104);
1091 if (ptrace(T_RESUME, pid, 0, 0) != 0) exit(105);
1093 WRITE(0);
1095 /* Unwaited-for traced zombies should be passed to their parent. */
1096 sleep(2);
1098 exit(84);
1101 sleep(1);
1103 /* However, that should only happen once the tracer has actually died. */
1104 if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
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);
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);
1114 traced_wait();
1117 void test_syscall_child()
1119 signal(SIGUSR1, count_handler);
1120 signal(SIGUSR2, count_handler);
1122 sigs = 0;
1124 WRITE(0);
1126 if (READ() != 0) my_e(100);
1128 /* Three calls (may fail) */
1129 setuid(0);
1130 close(123);
1131 getpid();
1133 if (sigs != 2) my_e(101);
1135 exit(42);
1138 void test_syscall()
1140 pid_t pid;
1141 int i, r, sig, status;
1143 subtest = 13;
1145 pid = traced_fork(test_syscall_child);
1147 if (READ() != 0) my_e(1);
1149 if (kill(pid, SIGSTOP) != 0) my_e(2);
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);
1155 WRITE(0);
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);
1160 if (waitpid(pid, &status, 0) != pid) my_e(7);
1162 for (i = 0; _WIFSTOPPED(status); i++) {
1163 if (WSTOPSIG(status) != SIGTRAP) my_e(8);
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;
1170 if (ptrace(T_SYSCALL, pid, 0, sig) != 0) my_e(9);
1172 if (waitpid(pid, &status, 0) != pid) my_e(10);
1175 if (!_WIFEXITED(status)) my_e(11);
1176 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1178 /* The number of events seen is deterministic but libc-dependent. */
1179 if (i < 10 || i > 100) my_e(12);
1181 /* The last system call event must be for entering exit(). */
1182 if (!(i % 2)) my_e(13);
1184 traced_wait();
1187 void test_tracefork_child()
1189 pid_t pid;
1191 signal(SIGHUP, SIG_IGN);
1193 pid = setsid();
1195 WRITE(pid);
1197 if (READ() != 0) my_e(100);
1199 if ((pid = fork()) < 0) my_e(101);
1201 exit(pid > 0 ? 42 : 84);
1204 void test_tracefork()
1206 pid_t pgrp, ppid, cpid, wpid;
1207 int r, status, gotstop, ptraps, ctraps;
1209 subtest = 14;
1211 ppid = traced_fork(test_tracefork_child);
1213 if ((pgrp = READ()) <= 0) my_e(1);
1215 if (kill(ppid, SIGSTOP) != 0) my_e(2);
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);
1221 if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(6);
1223 WRITE(0);
1225 if (ptrace(T_SYSCALL, ppid, 0, 0) != 0) my_e(7);
1227 cpid = -1;
1228 gotstop = -1;
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);
1234 if (wpid <= 0) my_e(8);
1235 if (cpid < 0 && wpid != ppid) {
1236 cpid = wpid;
1237 gotstop = 0;
1239 if (wpid != ppid && wpid != cpid) my_e(9);
1241 if (_WIFEXITED(status)) {
1242 if (wpid == ppid) {
1243 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1244 ppid = 0;
1246 else {
1247 if ((r = WEXITSTATUS(status)) != 84) my_e(r);
1248 cpid = 0;
1251 else {
1252 if (!_WIFSTOPPED(status)) my_e(10);
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);
1271 if (ptrace(T_SYSCALL, wpid, 0, 0) != 0) my_e(14);
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().
1279 if (ptraps < 3) my_e(15);
1280 if (!(ptraps % 2)) my_e(16);
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().
1286 if (ctraps < 2) my_e(17);
1287 if (ctraps % 2) my_e(18);
1289 traced_wait();
1292 void sigexec(setflag, opt, traps, stop)
1293 int setflag;
1294 int opt;
1295 int *traps;
1296 int *stop;
1298 pid_t pid;
1299 int r, status;
1301 pid = traced_fork(test_exec_child);
1303 if (kill(pid, SIGSTOP) != 0) my_e(1);
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);
1309 if (setflag && ptrace(T_SETOPT, pid, 0, opt) != 0) my_e(5);
1311 WRITE(0);
1313 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(6);
1315 *traps = 0;
1316 *stop = -1;
1318 for (;;) {
1319 if (waitpid(pid, &status, 0) != pid) my_e(7);
1321 if (_WIFEXITED(status)) break;
1323 if (!_WIFSTOPPED(status)) my_e(8);
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);
1337 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(11);
1340 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
1342 traced_wait();
1345 void test_trapexec()
1347 int traps, stop;
1349 subtest = 15;
1351 sigexec(1, 0, &traps, &stop);
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.
1358 if (traps < 4) my_e(12);
1359 if (traps % 2) my_e(13);
1360 if (stop >= 0) my_e(14);
1363 void test_altexec()
1365 int traps, stop;
1367 subtest = 16;
1369 sigexec(1, TO_ALTEXEC, &traps, &stop);
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.
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);
1381 void test_noexec()
1383 int traps, stop;
1385 subtest = 17;
1387 sigexec(1, TO_NOEXEC, &traps, &stop);
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);
1395 void test_defexec()
1397 int traps, stop;
1399 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1400 if (attach != 0 && attach != 1) return;
1402 subtest = 18;
1404 /* Do not set any options this time. */
1405 sigexec(0, 0, &traps, &stop);
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);
1413 else {
1414 if (traps < 3) my_e(15);
1415 if (!(traps % 2)) my_e(16);
1416 if (stop >= 0) my_e(17);
1420 void test_reattach_child()
1422 struct timeval tv;
1424 if (READ() != 0) my_e(100);
1426 tv.tv_sec = 2;
1427 tv.tv_usec = 0;
1428 if (select(0, NULL, NULL, NULL, &tv) != 0) my_e(101);
1430 exit(42);
1433 void test_reattach()
1435 pid_t pid;
1436 int r, status, count;
1438 subtest = 19;
1440 pid = traced_fork(test_reattach_child);
1442 if (kill(pid, SIGSTOP) != 0) my_e(1);
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);
1448 WRITE(0);
1450 signal(SIGALRM, dummy_handler);
1451 alarm(1);
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.
1459 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(5);
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);
1465 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(8);
1468 if (r != -1 || errno != EINTR) my_e(9);
1470 /* We always start with syscall enter event; the last event we should have
1471 * seen before the alarm was entering the select() call.
1473 if (!(count % 2)) my_e(10);
1475 /* Detach, and immediately attach again. */
1476 detach_running(pid);
1478 if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(11);
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);
1484 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(15);
1486 if (waitpid(pid, &status, 0) != pid) my_e(16);
1488 for (count = 0; _WIFSTOPPED(status); count++) {
1489 if (WSTOPSIG(status) != SIGTRAP) my_e(17);
1491 if (ptrace(T_SYSCALL, pid, 0, 0) != 0) my_e(18);
1493 if (waitpid(pid, &status, 0) != pid) my_e(19);
1496 if (!_WIFEXITED(status)) my_e(20);
1497 if ((r = WEXITSTATUS(status)) != 42) my_e(r);
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().
1502 if (!(count % 2)) my_e(21);
1504 traced_wait();