Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 2 | #include <stddef.h> |
| 3 | #include <unistd.h> |
| 4 | #include <signal.h> |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/socket.h> |
Denys Vlasenko | 8158e77 | 2011-06-08 14:08:59 +0200 | [diff] [blame] | 7 | #include <stdio.h> |
Mike Frysinger | b51ce62 | 2013-05-02 15:43:45 -0400 | [diff] [blame] | 8 | #include <sys/wait.h> |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 9 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 10 | static const struct sockaddr sa; |
| 11 | |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 12 | int main(int argc, char *argv[]) |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 13 | { |
| 14 | int loops; |
| 15 | int pid; |
| 16 | sigset_t set; |
| 17 | |
Denys Vlasenko | 8158e77 | 2011-06-08 14:08:59 +0200 | [diff] [blame] | 18 | 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 Vlasenko | 7c57be8 | 2011-06-08 16:07:03 +0200 | [diff] [blame] | 23 | fflush(NULL); |
Denys Vlasenko | 8158e77 | 2011-06-08 14:08:59 +0200 | [diff] [blame] | 24 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 25 | 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 Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 35 | |
| 36 | if (pid < 0) |
| 37 | exit(1); |
| 38 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 39 | if (!pid) { |
| 40 | /* child */ |
| 41 | int child = getpid(); |
| 42 | |
| 43 | loops = 99; |
| 44 | while (--loops) { |
| 45 | pid = fork(); |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 46 | |
| 47 | if (pid < 0) |
| 48 | exit(1); |
| 49 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 50 | if (!pid) { |
| 51 | /* grandchild: kill child */ |
| 52 | kill(child, SIGKILL); |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 53 | exit(0); |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 54 | } |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 55 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 56 | /* 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 Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 65 | case 0: |
Denys Vlasenko | 8158e77 | 2011-06-08 14:08:59 +0200 | [diff] [blame] | 66 | break; /* intentionally empty */ |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 67 | case 1: |
| 68 | sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa)); |
| 69 | break; |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 70 | } |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 71 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 72 | /* kill grandchild */ |
| 73 | kill(pid, SIGKILL); |
| 74 | } |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 75 | |
| 76 | exit(0); |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 77 | } |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 78 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 79 | /* parent */ |
| 80 | wait(NULL); |
| 81 | } |
Denys Vlasenko | 8ed5727 | 2009-02-25 14:24:02 +0000 | [diff] [blame] | 82 | |
Denys Vlasenko | 26de70e | 2009-02-13 10:26:31 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |