blob: 59af55b2779926d5c66e1be200261da00ade4708 [file] [log] [blame]
Denys Vlasenko8ed57272009-02-25 14:24:02 +00001#include <stdlib.h>
Denys Vlasenko26de70e2009-02-13 10:26:31 +00002#include <stddef.h>
3#include <unistd.h>
4#include <signal.h>
Denys Vlasenko26de70e2009-02-13 10:26:31 +00005#include <sys/types.h>
6#include <sys/socket.h>
Denys Vlasenko8158e772011-06-08 14:08:59 +02007#include <stdio.h>
Mike Frysingerb51ce622013-05-02 15:43:45 -04008#include <sys/wait.h>
Denys Vlasenko8ed57272009-02-25 14:24:02 +00009
Denys Vlasenko26de70e2009-02-13 10:26:31 +000010static const struct sockaddr sa;
11
Denys Vlasenko8ed57272009-02-25 14:24:02 +000012int main(int argc, char *argv[])
Denys Vlasenko26de70e2009-02-13 10:26:31 +000013{
14 int loops;
15 int pid;
16 sigset_t set;
17
Denys Vlasenko8158e772011-06-08 14:08:59 +020018 printf(
19"Please run me under 'strace -f -oLOG', and examine LOG file for incorrect\n"
20"decoding of interrupted syscalls: grep for 'sendto', '??" /* anti-trigraph gap */ "?', 'unavailable'.\n"
21"Pass number of iterations in argv[1] (default: 999).\n"
22 );
Denys Vlasenko7c57be82011-06-08 16:07:03 +020023 fflush(NULL);
Denys Vlasenko8158e772011-06-08 14:08:59 +020024
Denys Vlasenko26de70e2009-02-13 10:26:31 +000025 sigemptyset(&set);
26 sigaddset(&set, SIGCHLD);
27 sigprocmask(SIG_BLOCK, &set, NULL);
28
29 loops = 999;
30 if (argv[1])
31 loops = atoi(argv[1]);
32
33 while (--loops >= 0) {
34 pid = fork();
Denys Vlasenko8ed57272009-02-25 14:24:02 +000035
36 if (pid < 0)
37 exit(1);
38
Denys Vlasenko26de70e2009-02-13 10:26:31 +000039 if (!pid) {
40 /* child */
41 int child = getpid();
42
43 loops = 99;
44 while (--loops) {
45 pid = fork();
Denys Vlasenko8ed57272009-02-25 14:24:02 +000046
47 if (pid < 0)
48 exit(1);
49
Denys Vlasenko26de70e2009-02-13 10:26:31 +000050 if (!pid) {
51 /* grandchild: kill child */
52 kill(child, SIGKILL);
Denys Vlasenko8ed57272009-02-25 14:24:02 +000053 exit(0);
Denys Vlasenko26de70e2009-02-13 10:26:31 +000054 }
Denys Vlasenko8ed57272009-02-25 14:24:02 +000055
Denys Vlasenko26de70e2009-02-13 10:26:31 +000056 /* Add various syscalls you want to test here.
57 * strace will decode them and suddenly find
58 * process disappearing.
59 * But leave at least one case "empty", so that
60 * "kill grandchild" happens quicker.
61 * This produces cases when strace can't even
62 * decode syscall number before process dies.
63 */
64 switch (loops & 1) {
Denys Vlasenko8ed57272009-02-25 14:24:02 +000065 case 0:
Denys Vlasenko8158e772011-06-08 14:08:59 +020066 break; /* intentionally empty */
Denys Vlasenko8ed57272009-02-25 14:24:02 +000067 case 1:
68 sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa));
69 break;
Denys Vlasenko26de70e2009-02-13 10:26:31 +000070 }
Denys Vlasenko8ed57272009-02-25 14:24:02 +000071
Denys Vlasenko26de70e2009-02-13 10:26:31 +000072 /* kill grandchild */
73 kill(pid, SIGKILL);
74 }
Denys Vlasenko8ed57272009-02-25 14:24:02 +000075
76 exit(0);
Denys Vlasenko26de70e2009-02-13 10:26:31 +000077 }
Denys Vlasenko8ed57272009-02-25 14:24:02 +000078
Denys Vlasenko26de70e2009-02-13 10:26:31 +000079 /* parent */
80 wait(NULL);
81 }
Denys Vlasenko8ed57272009-02-25 14:24:02 +000082
Denys Vlasenko26de70e2009-02-13 10:26:31 +000083 return 0;
84}