summaryrefslogtreecommitdiff
path: root/tests/orphaned_process_group.c
blob: 13aacf36c1d44d4ef4ce64a43b285ecee0d57126 (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
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
/*
 * Check tracing of orphaned process group.
 *
 * Copyright (c) 2019-2020 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#define TIMEOUT 5

static void
alarm_handler(const int no)
{
	error_msg_and_skip("Orphaned process group semantics"
			   " is not supported by the kernel");
}

int
main(void)
{
	int status;

	/*
	 * Unblock all signals.
	 */
	static sigset_t mask;
	if (sigprocmask(SIG_SETMASK, &mask, NULL))
		perror_msg_and_fail("sigprocmask");

	/*
	 * Create a pipe to track termination of processes.
	 */
	int pipe_fds[2];
	if (pipe(pipe_fds))
		perror_msg_and_fail("pipe");

	/*
	 * Create a leader for its own new process group.
	 */
	pid_t leader = fork();
	if (leader < 0)
		perror_msg_and_fail("fork");

	if (leader) {
		/*
		 * Close the writing end of the pipe.
		 */
		close(pipe_fds[1]);

		/*
		 * Install the SIGALRM signal handler.
		 */
		static const struct sigaction sa = {
			.sa_handler = alarm_handler
		};
		if (sigaction(SIGALRM, &sa, NULL))
			perror_msg_and_fail("sigaction");

		/*
		 * Set an alarm clock.
		 */
		alarm(TIMEOUT);

		/*
		 * Wait for termination of the child process.
		 */
		if (waitpid(leader, &status, 0) != leader)
			perror_msg_and_fail("waitpid leader");
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
			error_msg_and_fail("waitpid leader: "
					   "unexpected wait status %d",
					   status);

		/*
		 * Wait for termination of all processes
		 * in the process group of the child process.
		 */
		if (read(pipe_fds[0], &status, sizeof(status)) != 0)
			perror_msg_and_fail("read");

		/*
		 * At this point all processes are gone.
		 * Let the tracer time to catch up.
		 */
		alarm(0);
		sleep(1);
		return 0;
	}

	/*
	 * Close the reading end of the pipe.
	 */
	close(pipe_fds[0]);

	/*
	 * Create a new process group.
	 */
	if (setpgid(0, 0))
		perror_msg_and_fail("setpgid");

	/*
	 * When the leader process terminates, the process group becomes orphaned.
	 * If any member of the orphaned process group is stopped, then
	 * a SIGHUP signal followed by a SIGCONT signal is sent to each process
	 * in the orphaned process group.
	 * Create a process in a stopped state to activate this behaviour.
	 */
	const pid_t stopped = fork();
	if (stopped < 0)
		perror_msg_and_fail("fork");
	if (!stopped) {
		static const struct sigaction sa = { .sa_handler = SIG_DFL };
		if (sigaction(SIGHUP, &sa, NULL))
			perror_msg_and_fail("sigaction");

		raise(SIGSTOP);
		_exit(0);
	}

	/*
	 * Wait for the process to stop.
	 */
	if (waitpid(stopped, &status, WUNTRACED) != stopped)
		perror_msg_and_fail("waitpid WUNTRACED");
	if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
                error_msg_and_fail("unexpected wait status %d", status);

	/*
	 * Print the expected output.
	 */
	leader = getpid();
	printf("%-5d --- %s {si_signo=%s, si_code=SI_TKILL"
	       ", si_pid=%d, si_uid=%d} ---\n",
	       stopped, "SIGSTOP", "SIGSTOP", stopped, geteuid());
	printf("%-5d --- stopped by SIGSTOP ---\n", stopped);
	printf("%-5d +++ exited with 0 +++\n", leader);
	printf("%-5d --- %s {si_signo=%s, si_code=SI_KERNEL} ---\n",
	       stopped, "SIGHUP", "SIGHUP");
	printf("%-5d +++ killed by %s +++\n", stopped, "SIGHUP");
	printf("%-5d +++ exited with 0 +++\n", getppid());

	/*
	 * Make the process group orphaned.
	 */
	return 0;
}