]> granicus.if.org Git - strace/blob - test/sigkill_rain.c
Add argument to tprint_iov() specifying whether to decode each iovec
[strace] / test / sigkill_rain.c
1 #include <stdlib.h>
2 #include <stddef.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <stdio.h>
8
9 static const struct sockaddr sa;
10
11 int main(int argc, char *argv[])
12 {
13         int loops;
14         int pid;
15         sigset_t set;
16
17         printf(
18 "Please run me under 'strace -f -oLOG', and examine LOG file for incorrect\n"
19 "decoding of interrupted syscalls: grep for 'sendto', '??" /* anti-trigraph gap */ "?', 'unavailable'.\n"
20 "Pass number of iterations in argv[1] (default: 999).\n"
21         );
22         fflush(NULL);
23
24         sigemptyset(&set);
25         sigaddset(&set, SIGCHLD);
26         sigprocmask(SIG_BLOCK, &set, NULL);
27
28         loops = 999;
29         if (argv[1])
30                 loops = atoi(argv[1]);
31
32         while (--loops >= 0) {
33                 pid = fork();
34
35                 if (pid < 0)
36                         exit(1);
37
38                 if (!pid) {
39                         /* child */
40                         int child = getpid();
41
42                         loops = 99;
43                         while (--loops) {
44                                 pid = fork();
45
46                                 if (pid < 0)
47                                         exit(1);
48
49                                 if (!pid) {
50                                         /* grandchild: kill child */
51                                         kill(child, SIGKILL);
52                                         exit(0);
53                                 }
54
55                                 /* Add various syscalls you want to test here.
56                                  * strace will decode them and suddenly find
57                                  * process disappearing.
58                                  * But leave at least one case "empty", so that
59                                  * "kill grandchild" happens quicker.
60                                  * This produces cases when strace can't even
61                                  * decode syscall number before process dies.
62                                  */
63                                 switch (loops & 1) {
64                                 case 0:
65                                         break; /* intentionally empty */
66                                 case 1:
67                                         sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa));
68                                         break;
69                                 }
70
71                                 /* kill grandchild */
72                                 kill(pid, SIGKILL);
73                         }
74
75                         exit(0);
76                 }
77
78                 /* parent */
79                 wait(NULL);
80         }
81
82         return 0;
83 }