summaryrefslogtreecommitdiff
path: root/test/sigkill_rain.c
blob: 59af55b2779926d5c66e1be200261da00ade4708 (plain)
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
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/wait.h>

static const struct sockaddr sa;

int main(int argc, char *argv[])
{
	int loops;
	int pid;
	sigset_t set;

	printf(
"Please run me under 'strace -f -oLOG', and examine LOG file for incorrect\n"
"decoding of interrupted syscalls: grep for 'sendto', '??" /* anti-trigraph gap */ "?', 'unavailable'.\n"
"Pass number of iterations in argv[1] (default: 999).\n"
	);
	fflush(NULL);

	sigemptyset(&set);
	sigaddset(&set, SIGCHLD);
	sigprocmask(SIG_BLOCK, &set, NULL);

	loops = 999;
	if (argv[1])
		loops = atoi(argv[1]);

	while (--loops >= 0) {
		pid = fork();

		if (pid < 0)
			exit(1);

		if (!pid) {
			/* child */
			int child = getpid();

			loops = 99;
			while (--loops) {
				pid = fork();

				if (pid < 0)
					exit(1);

				if (!pid) {
					/* grandchild: kill child */
					kill(child, SIGKILL);
					exit(0);
				}

				/* Add various syscalls you want to test here.
				 * strace will decode them and suddenly find
				 * process disappearing.
				 * But leave at least one case "empty", so that
				 * "kill grandchild" happens quicker.
				 * This produces cases when strace can't even
				 * decode syscall number before process dies.
				 */
				switch (loops & 1) {
				case 0:
					break; /* intentionally empty */
				case 1:
					sendto(-1, "Hello cruel world", 17, 0, &sa, sizeof(sa));
					break;
				}

				/* kill grandchild */
				kill(pid, SIGKILL);
			}

			exit(0);
		}

		/* parent */
		wait(NULL);
	}

	return 0;
}