forked from joahannes/minix
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest42.c
More file actions
1509 lines (1070 loc) · 30.6 KB
/
Copy pathtest42.c
File metadata and controls
1509 lines (1070 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/ptrace.h>
#include <sys/syslimits.h>
#define ITERATIONS 3
int max_error = 4;
#include "common.h"
#define my_e(n) { \
if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
#define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
#define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
#define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
#define timed_test(func) (timed_test_func(#func, func));
int main(int argc, char **argv);
void test(int m, int a);
void timed_test_func(const char *s, void (* func)(void));
void timed_test_timeout(int signum);
pid_t traced_fork(void (*c) (void));
pid_t traced_pfork(void (*c) (void));
void WRITE(int value);
int READ(void);
void traced_wait(void);
void detach_running(pid_t pid);
void dummy_handler(int sig);
void exit_handler(int sig);
void count_handler(int sig);
void catch_handler(int sig);
void test_wait_child(void);
void test_wait(void);
void test_exec_child(void);
void test_exec(void);
void test_step_child(void);
void test_step(void);
void test_sig_child(void);
void test_sig(void);
void test_exit_child(void);
void test_exit(void);
void test_term_child(void);
void test_term(void);
void test_catch_child(void);
void test_catch(void);
void test_kill_child(void);
void test_kill(void);
void test_attach_child(void);
void test_attach(void);
void test_detach_child(void);
void test_detach(void);
void test_death_child(void);
void test_death(void);
void test_zdeath_child(void);
void test_zdeath(void);
void test_syscall_child(void);
void test_syscall(void);
void test_tracefork_child(void);
void test_tracefork(void);
void sigexec(int setflag, int opt, int *traps, int *stop);
void test_trapexec(void);
void test_altexec(void);
void test_noexec(void);
void test_defexec(void);
void test_reattach_child(void);
void test_reattach(void);
static char *executable;
static int child = 0, attach;
static pid_t ppid;
static int pfd[4];
static int sigs, caught;
int main(argc, argv)
int argc;
char **argv;
{
int i, m = 0xFFFFFF, n = 0xF;
char cp_cmd[NAME_MAX + 10];
if (strcmp(argv[0], "DO CHECK") == 0) {
exit(42);
}
start(42);
executable = argv[0];
snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);
system(cp_cmd);
if (argc >= 2) m = atoi(argv[1]);
if (argc >= 3) n = atoi(argv[2]);
for (i = 0; i < ITERATIONS; i++) {
if (n & 001) test(m, 0);
if (n & 002) test(m, 1);
if (n & 004) test(m, 2);
if (n & 010) test(m, 3);
}
quit();
return(-1); /* impossible */
}
void test(m, a)
int m;
int a;
{
attach = a;
if (m & 00000001) timed_test(test_wait);
if (m & 00000002) timed_test(test_exec);
#if !defined(__arm__)
/* BJG: single-stepping isn't implemented on ARM */
if (m & 00000004) timed_test(test_step);
#endif
if (m & 00000010) timed_test(test_sig);
if (m & 00000020) timed_test(test_exit);
if (m & 00000040) timed_test(test_term);
if (m & 00000100) timed_test(test_catch);
if (m & 00000200) timed_test(test_kill);
if (m & 00000400) timed_test(test_attach);
if (m & 00001000) timed_test(test_detach);
if (m & 00002000) timed_test(test_death);
if (m & 00004000) timed_test(test_zdeath);
if (m & 00010000) timed_test(test_syscall);
if (m & 00020000) timed_test(test_tracefork);
if (m & 00040000) timed_test(test_trapexec);
if (m & 00100000) timed_test(test_altexec);
if (m & 00200000) timed_test(test_noexec);
if (m & 00400000) timed_test(test_defexec);
if (m & 01000000) test_reattach(); /* not timed, catches SIGALRM */
}
static jmp_buf timed_test_context;
void timed_test_timeout(int signum)
{
longjmp(timed_test_context, -1);
my_e(700);
quit();
exit(-1);
}
void timed_test_func(const char *s, void (* func)(void))
{
if (setjmp(timed_test_context) == 0)
{
/* the function gets 60 seconds to complete */
if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { my_e(701); return; }
alarm(60);
func();
alarm(0);
}
else
{
/* report timeout as error */
printf("timeout in %s\n", s);
my_e(702);
}
}
pid_t traced_fork(c)
void (*c)(void);
{
pid_t pid;
int r, status;
if (pipe(pfd) != 0) my_e(200);
if (pipe(&pfd[2]) != 0) my_e(201);
switch (attach) {
case 0: /* let child volunteer to be traced */
pid = fork();
if (pid < 0) my_e(202);
if (pid == 0) {
child = 1;
if (ptrace(T_OK, 0, 0, 0) != 0) my_e(203);
WRITE(0);
c();
my_e(204);
}
if (READ() != 0) my_e(205);
break;
case 1: /* attach to child process */
pid = fork();
if (pid < 0) my_e(206);
if (pid == 0) {
child = 1;
if (READ() != 0) my_e(207);
c();
my_e(208);
}
if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(209);
if (waitpid(pid, &status, 0) != pid) my_e(210);
if (!_WIFSTOPPED(status)) my_e(211);
if (WSTOPSIG(status) != SIGSTOP) my_e(212);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(213);
WRITE(0);
break;
case 2: /* attach to non-child process */
ppid = fork();
if (ppid < 0) my_e(214);
if (ppid == 0) {
pid = fork();
if (pid < 0) exit(215);
if (pid == 0) {
child = 1;
if (READ() != 0) my_e(216);
c();
my_e(217);
}
child = 1;
WRITE(pid);
if (waitpid(pid, &status, 0) != pid) my_e(218);
if (_WIFSTOPPED(status)) my_e(219);
if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
exit(0);
}
pid = READ();
if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(220);
if (waitpid(pid, &status, 0) != pid) my_e(221);
if (!_WIFSTOPPED(status)) my_e(222);
if (WSTOPSIG(status) != SIGSTOP) my_e(223);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(224);
WRITE(0);
break;
case 3: /* attach by forking from child */
ppid = fork();
if (ppid < 0) my_e(225);
if (ppid == 0) {
child = 1;
if (ptrace(T_OK, 0, 0, 0) != 0) my_e(226);
WRITE(0);
if (READ() != 0) my_e(227);
pid = fork();
if (pid < 0) my_e(228);
if (pid == 0) {
c();
my_e(229);
}
WRITE(pid);
if (waitpid(pid, &status, 0) != pid) my_e(230);
if (_WIFSTOPPED(status)) my_e(231);
if (_WIFEXITED(status) && (r = WEXITSTATUS(status)) != 42) my_e(r);
exit(0);
}
if (READ() != 0) my_e(232);
if (kill(ppid, SIGSTOP) != 0) my_e(233);
if (waitpid(ppid, &status, 0) != ppid) my_e(234);
if (!_WIFSTOPPED(status)) my_e(235);
if (WSTOPSIG(status) != SIGSTOP) my_e(236);
if (ptrace(T_SETOPT, ppid, 0, TO_TRACEFORK) != 0) my_e(237);
if (ptrace(T_RESUME, ppid, 0, 0) != 0) my_e(238);
WRITE(0);
pid = READ();
if (waitpid(pid, &status, 0) != pid) my_e(239);
if (!_WIFSTOPPED(status)) my_e(240);
if (WSTOPSIG(status) != SIGSTOP) my_e(241);
if (ptrace(T_SETOPT, pid, 0, 0) != 0) my_e(242);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(243);
detach_running(ppid);
break;
default:
abort();
}
return pid;
}
pid_t traced_pfork(c)
void(*c) (void);
{
pid_t pid;
if (pipe(pfd) != 0) my_e(300);
if (pipe(&pfd[2]) != 0) my_e(301);
pid = fork();
if (pid < 0) my_e(302);
if (pid == 0) {
child = 1;
c();
my_e(303);
}
return pid;
}
void WRITE(value)
int value;
{
if (write(pfd[child*2+1], &value, sizeof(value)) != sizeof(value)) my_e(400);
}
int READ()
{
int value;
if (read(pfd[2-child*2], &value, sizeof(value)) != sizeof(value)) my_e(401);
return value;
}
void traced_wait()
{
int r, status;
if (attach == 2) {
if (waitpid(ppid, &status, 0) != ppid) my_e(500);
if (!_WIFEXITED(status)) my_e(501);
if ((r = WEXITSTATUS(status)) != 0) my_e(r);
}
else {
/* Quick hack to clean up detached children */
waitpid(-1, NULL, WNOHANG);
}
close(pfd[0]);
close(pfd[1]);
close(pfd[2]);
close(pfd[3]);
}
void detach_running(pid)
pid_t pid;
{
/* Detach from a process that is not already stopped. This is the way to do it.
* We have to stop the child in order to detach from it, but as the child may
* have other signals pending for the tracer, we cannot assume we get our own
* signal back immediately. However, because we know that the kill is instant
* and resuming with pending signals will only stop the process immediately
* again, we can use T_RESUME for all the signals until we get our own signal,
* and then detach. A complicating factor is that anywhere during this
* procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
* will not happen.
*/
int status;
if (kill(pid, SIGSTOP) != 0) my_e(600);
if (waitpid(pid, &status, 0) != pid) my_e(601);
while (_WIFSTOPPED(status)) {
if (WSTOPSIG(status) == SIGSTOP) {
if (ptrace(T_DETACH, pid, 0, 0) != 0) my_e(602);
return;
}
if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(603);
if (waitpid(pid, &status, 0) != pid) my_e(604);
}
/* Apparently the process exited. */
if (!_WIFEXITED(status) && !_WIFSIGNALED(status)) my_e(605);
/* In our tests, that should not happen. */
my_e(606);
}
void dummy_handler(sig)
int sig;
{
}
void exit_handler(sig)
int sig;
{
exit(42);
}
void count_handler(sig)
int sig;
{
sigs++;
}
void catch_handler(sig)
int sig;
{
sigset_t set;
int bit;
switch (sig) {
case SIGUSR1: bit = 1; break;
case SIGUSR2: bit = 2; break;
case SIGTERM: bit = 4; break;
default: bit = 0; my_e(100);
}
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
if (caught & bit) my_e(101);
caught |= bit;
}
void test_wait_child()
{
exit(42);
}
void test_wait()
{
pid_t pid;
int status;
subtest = 1;
pid = traced_fork(test_wait_child);
if (waitpid(pid, &status, 0) != pid) my_e(1);
if (!_WIFEXITED(status)) my_e(2);
if (WEXITSTATUS(status) != 42) my_e(3);
traced_wait();
}
void test_exec_child()
{
if (READ() != 0) my_e(100);
execl(executable, "DO CHECK", NULL);
my_e(101);
}
void test_exec()
{
pid_t pid;
int r, status;
/* This test covers the T_OK case. */
if (attach != 0) return;
subtest = 2;
pid = traced_fork(test_exec_child);
WRITE(0);
/* An exec() should result in a trap signal. */
if (waitpid(pid, &status, 0) != pid) my_e(1);
if (!_WIFSTOPPED(status)) my_e(2);
if (WSTOPSIG(status) != SIGTRAP) my_e(3);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(4);
if (waitpid(pid, &status, 0) != pid) my_e(5);
if (!_WIFEXITED(status)) my_e(6);
if ((r = WEXITSTATUS(status)) != 42) my_e(r);
traced_wait();
}
void test_step_child()
{
sigset_t set;
signal(SIGUSR1, SIG_IGN);
WRITE(0);
if (READ() != 0) my_e(100);
/* It must not be possible for the child to stop the single-step signal. */
signal(SIGTRAP, SIG_IGN);
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
exit(42);
}
void test_step()
{
pid_t pid;
int r, status, count;
subtest = 3;
pid = traced_fork(test_step_child);
if (READ() != 0) my_e(1);
/* While the child is running, neither waitpid() nor ptrace() should work. */
if (waitpid(pid, &status, WNOHANG) != 0) my_e(2);
if (ptrace(T_RESUME, pid, 0, 0) != -1) my_e(3);
if (errno != EBUSY) my_e(4);
if (kill(pid, SIGUSR1) != 0) my_e(5);
WRITE(0);
/* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
if (waitpid(pid, &status, 0) != pid) my_e(6);
if (!_WIFSTOPPED(status)) my_e(7);
if (WSTOPSIG(status) != SIGUSR1) my_e(8);
/* ptrace(T_STEP) should result in instruction-wise progress. */
for (count = 0; ; count++) {
if (ptrace(T_STEP, pid, 0, 0) != 0) my_e(9);
if (waitpid(pid, &status, 0) != pid) my_e(10);
if (_WIFEXITED(status)) break;
if (!_WIFSTOPPED(status)) my_e(11);
if (WSTOPSIG(status) != SIGTRAP) my_e(12);
}
if ((r = WEXITSTATUS(status)) != 42) my_e(r);
if (count < 10) my_e(13); /* in practice: hundreds */
traced_wait();
}
void test_sig_child()
{
signal(SIGUSR1, exit_handler);
if (READ() != 0) my_e(100);
pause();
my_e(101);
}
void test_sig()
{
pid_t pid;
int r, sig, status;
subtest = 4;
pid = traced_fork(test_sig_child);
WRITE(0);
/* allow the child to enter the pause */
sleep(1);
if (kill(pid, SIGUSR1) != 0) my_e(1);
if (kill(pid, SIGUSR2) != 0) my_e(2);
/* All signals should arrive at the tracer, although in "random" order. */
if (waitpid(pid, &status, 0) != pid) my_e(3);
if (!_WIFSTOPPED(status)) my_e(4);
if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(5);
/* The tracer should see kills arriving while the tracee is stopped. */
if (kill(pid, WSTOPSIG(status)) != 0) my_e(6);
if (waitpid(pid, &status, WNOHANG) != pid) my_e(7);
if (!_WIFSTOPPED(status)) my_e(8);
if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(9);
sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(10);
if (waitpid(pid, &status, 0) != pid) my_e(11);
if (!_WIFSTOPPED(status)) my_e(12);
if (WSTOPSIG(status) != sig) my_e(13);
if (waitpid(pid, &status, WNOHANG) != 0) my_e(14);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(15);
/* Ignored signals passed via ptrace() should be ignored. */
if (kill(pid, SIGUSR1) != 0) my_e(16);
if (waitpid(pid, &status, 0) != pid) my_e(17);
if (!_WIFSTOPPED(status)) my_e(18);
if (WSTOPSIG(status) != SIGUSR1) my_e(19);
if (ptrace(T_RESUME, pid, 0, SIGCHLD) != 0) my_e(20);
/* if the pause has been aborted (shouldn't happen!), let the child exit */
sleep(1);
if (waitpid(pid, &status, WNOHANG) != 0) my_e(21);
/* Caught signals passed via ptrace() should invoke their signal handlers. */
if (kill(pid, SIGUSR1) != 0) my_e(22);
if (waitpid(pid, &status, 0) != pid) my_e(23);
if (!_WIFSTOPPED(status)) my_e(24);
if (WSTOPSIG(status) != SIGUSR1) my_e(25);
if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(26);
if (waitpid(pid, &status, 0) != pid) my_e(27);
if (!_WIFEXITED(status)) my_e(28);
if ((r = WEXITSTATUS(status)) != 42) my_e(29);
traced_wait();
}
void test_exit_child()
{
WRITE(0);
for(;;);
}
void test_exit()
{
pid_t pid;
int r, status;
subtest = 5;
pid = traced_fork(test_exit_child);
if (READ() != 0) my_e(1);
sleep(1);
if (kill(pid, SIGSTOP) != 0) my_e(2);
if (waitpid(pid, &status, 0) != pid) my_e(3);
if (!_WIFSTOPPED(status)) my_e(4);
if (WSTOPSIG(status) != SIGSTOP) my_e(5);
/* There should be no more signals pending for the tracer now. */
if (waitpid(pid, &status, WNOHANG) != 0) my_e(6);
/* ptrace(T_EXIT) should terminate the process with the given exit value. */
if (ptrace(T_EXIT, pid, 0, 42) != 0) my_e(7);
if (waitpid(pid, &status, 0) != pid) my_e(8);
if (!_WIFEXITED(status)) my_e(9);
if ((r = WEXITSTATUS(status)) != 42) my_e(r);
traced_wait();
}
void test_term_child()
{
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, dummy_handler);
WRITE(0);
pause();
my_e(100);
}
void test_term()
{
pid_t pid;
int status;
subtest = 6;
pid = traced_fork(test_term_child);
if (READ() != 0) my_e(1);
/* If the first of two signals terminates the traced child, the second signal
* may or may not be delivered to the tracer - this is merely a policy issue.
* However, nothing unexpected should happen.
*/
if (kill(pid, SIGUSR1) != 0) my_e(2);
if (kill(pid, SIGUSR2) != 0) my_e(3);
if (waitpid(pid, &status, 0) != pid) my_e(4);
if (!_WIFSTOPPED(status)) my_e(5);
if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(6);
if (waitpid(pid, &status, 0) != pid) my_e(7);
if (_WIFSTOPPED(status)) {
if (ptrace(T_RESUME, pid, 0, SIGUSR1) != 0) my_e(8);
if (waitpid(pid, &status, 0) != pid) my_e(9);
}
if (!_WIFSIGNALED(status)) my_e(10);
if (WTERMSIG(status) != SIGUSR1) my_e(11);
traced_wait();
}
void test_catch_child()
{
struct sigaction sa;
sigset_t set, oset;
sa.sa_handler = catch_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NODEFER;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, &oset);
caught = 0;
WRITE(0);
while (caught != 7) sigsuspend(&oset);
exit(42);
}
void test_catch()
{
pid_t pid;
int r, sig, status;
subtest = 7;
pid = traced_fork(test_catch_child);
if (READ() != 0) my_e(1);
if (kill(pid, SIGUSR1) != 0) my_e(2);
if (kill(pid, SIGUSR2) != 0) my_e(3);
if (waitpid(pid, &status, 0) != pid) my_e(4);
if (!_WIFSTOPPED(status)) my_e(5);
if (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGUSR2) my_e(6);
sig = (WSTOPSIG(status) == SIGUSR1) ? SIGUSR2 : SIGUSR1;
if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(7);
if (kill(pid, SIGTERM) != 0) my_e(8);
if (waitpid(pid, &status, 0) != pid) my_e(9);
if (!_WIFSTOPPED(status)) my_e(10);
if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGTERM) my_e(11);
if (WSTOPSIG(status) == sig) sig = SIGTERM;
if (ptrace(T_RESUME, pid, 0, WSTOPSIG(status)) != 0) my_e(12);
if (kill(pid, SIGBUS) != 0) my_e(13);
if (waitpid(pid, &status, 0) != pid) my_e(14);
if (!_WIFSTOPPED(status)) my_e(15);
if (WSTOPSIG(status) != sig && WSTOPSIG(status) != SIGBUS) my_e(16);
if (ptrace(T_RESUME, pid, 0, sig) != 0) my_e(17);
if (WSTOPSIG(status) == sig) sig = SIGBUS;
if (waitpid(pid, &status, 0) != pid) my_e(18);
if (!_WIFSTOPPED(status)) my_e(19);
if (WSTOPSIG(status) != sig) my_e(20);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(21);
if (waitpid(pid, &status, 0) != pid) my_e(22);
if (!_WIFEXITED(status)) my_e(23);
if ((r = WEXITSTATUS(status)) != 42) my_e(r);
traced_wait();
}
void test_kill_child()
{
sigset_t set;
signal(SIGKILL, SIG_IGN);
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
WRITE(0);
pause();
my_e(100);
}
void test_kill()
{
pid_t pid;
int status;
subtest = 8;
pid = traced_fork(test_kill_child);
if (READ() != 0) my_e(1);
/* SIGKILL must be unstoppable in every way. */
if (kill(pid, SIGKILL) != 0) my_e(2);
if (waitpid(pid, &status, 0) != pid) my_e(3);
if (!_WIFSIGNALED(status)) my_e(4);
if (WTERMSIG(status) != SIGKILL) my_e(5);
/* After termination, the child must no longer be visible to the tracer. */
if (waitpid(pid, &status, WNOHANG) != -1) my_e(6);
if (errno != ECHILD) my_e(7);
traced_wait();
}
void test_attach_child()
{
if (ptrace(T_OK, 0, 0, 0) != -1) my_e(100);
if (errno != EBUSY) my_e(101);
WRITE(0);
if (READ() != 0) my_e(102);
exit(42);
}
void test_attach()
{
pid_t pid;
subtest = 9;
/* Attaching to kernel processes is not allowed. */
if (ptrace(T_ATTACH, -1, 0, 0) != -1) my_e(1);
if (errno != ESRCH) my_e(2);
/* Attaching to self is not allowed. */
if (ptrace(T_ATTACH, getpid(), 0, 0) != -1) my_e(3);
if (errno != EPERM) my_e(4);
/* Attaching to PM is not allowed. */
#if 0
/* FIXME: disabled until we can reliably determine PM's pid */
if (ptrace(T_ATTACH, 0, 0, 0) != -1) my_e(5);
if (errno != EPERM) my_e(6);
#endif
pid = traced_fork(test_attach_child);
/* Attaching more than once is not allowed. */
if (ptrace(T_ATTACH, pid, 0, 0) != -1) my_e(7);
if (errno != EBUSY) my_e(8);
if (READ() != 0) my_e(9);
/* Detaching a running child should not succeed. */
if (ptrace(T_DETACH, pid, 0, 0) == 0) my_e(10);
if (errno != EBUSY) my_e(11);
detach_running(pid);
WRITE(0);
traced_wait();
}
void test_detach_child()
{
struct sigaction sa;
sigset_t set, sset, oset;
sa.sa_handler = catch_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NODEFER;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, &oset);
sigfillset(&sset);
sigdelset(&sset, SIGUSR1);
caught = 0;
WRITE(0);
if (sigsuspend(&sset) != -1) my_e(102);
if (errno != EINTR) my_e(103);
if (caught != 1) my_e(104);
if (READ() != 0) my_e(105);
while (caught != 7) sigsuspend(&oset);
exit(42);
}
void test_detach()
{
pid_t pid;
int r, status;
/* Can't use traced_fork(), so simplify a bit */
if (attach != 0) return;
subtest = 10;
pid = traced_pfork(test_detach_child);
if (READ() != 0) my_e(1);
/* The tracer should not see signals sent to the process before attaching. */
if (kill(pid, SIGUSR2) != 0) my_e(2);
if (ptrace(T_ATTACH, pid, 0, 0) != 0) my_e(3);
if (waitpid(pid, &status, 0) != pid) my_e(4);
if (!_WIFSTOPPED(status)) my_e(5);
if (WSTOPSIG(status) != SIGSTOP) my_e(6);
if (ptrace(T_RESUME, pid, 0, 0) != 0) my_e(7);
if (kill(pid, SIGUSR1) != 0) my_e(8);
if (waitpid(pid, &status, 0) != pid) my_e(9);
if (!_WIFSTOPPED(status)) my_e(10);
if (WSTOPSIG(status) != SIGUSR1) my_e(11);
/* Signals pending at the tracer should be passed on after detaching. */