-
Notifications
You must be signed in to change notification settings - Fork 65
/
wait_pgrp.c
232 lines (218 loc) · 7.48 KB
/
wait_pgrp.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "wait_pgrp.h"
#include <errno.h> // for errno, ECHILD, EINTR, ESRCH
#include <signal.h> // for kill, sigaddset, sigemptyset, sigprocmask,
// sigsuspend, SIGCHLD, SIGTERM
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS, WEXITSTATUS, WIFEXITED, WIFSIGNALED
#include <string.h>
#include <sys/wait.h> // for waitpid, WNOHANG
#include <unistd.h> // for pid_t
#include "logging.h" // for Log, LogErrno
static void HandleSIGCHLD(int unused_signo) {
// No handling needed - we just want to interrupt select() or sigsuspend()
// calls.
(void)unused_signo;
}
void InitWaitPgrp(void) {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = HandleSIGCHLD; // To interrupt select().
if (sigaction(SIGCHLD, &sa, NULL) != 0) {
LogErrno("sigaction(SIGCHLD)");
}
}
pid_t ForkWithoutSigHandlers(void) {
// Before forking, block all signals we may have handlers for.
sigset_t oldset, set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGCHLD);
sigemptyset(&oldset);
if (sigprocmask(SIG_BLOCK, &set, &oldset)) {
LogErrno("Unable to block signals");
}
pid_t pid = fork();
int fork_errno = errno;
if (pid == 0) {
// Clear all our custom signal handlers in the subprocess.
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = SIG_DFL;
if (sigaction(SIGUSR1, &sa, NULL)) {
LogErrno("sigaction(SIGUSR1)");
}
if (sigaction(SIGTERM, &sa, NULL)) {
LogErrno("sigaction(SIGTERM)");
}
if (sigaction(SIGCHLD, &sa, NULL)) {
LogErrno("sigaction(SIGCHLD)");
}
}
// Now we can unmask signals.
if (sigprocmask(SIG_SETMASK, &oldset, NULL)) {
LogErrno("Unable to restore signal mask");
}
errno = fork_errno;
return pid;
}
void StartPgrp(void) {
if (setsid() == (pid_t)-1) {
LogErrno("setsid");
}
// To avoid a race condition when killing the process group after the leader
// is already dead (which could then kill another new process group with the
// same ID), we'll create a dummy process that never dies until we signal the
// process group explicitly.
pid_t pid = fork();
if (pid == -1) {
LogErrno("StartPgrp -> fork; expect potential race in KillPgrp");
// We ignore this error, as everything else can still work in this case;
// however in this case the aforementioned race condition in KillPgrp can
// happen.
} else if (pid == 0) {
// Child process.
// Just wait forever. We'll get SIGTERM'd when it's time to go.
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = SIG_IGN; // Don't die of SIGUSR1 (saver reset).
if (sigaction(SIGUSR1, &sa, NULL) != 0) {
LogErrno("sigaction(SIGUSR1)");
}
{
const char *args[2] = {"pgrp_placeholder", NULL};
ExecvHelper("pgrp_placeholder", args);
sleep(2); // Reduce log spam or other effects from failed execv.
_exit(EXIT_FAILURE);
}
}
}
int ExecvHelper(const char *path, const char *const argv[]) {
// Helpers expect to always run inside HELPER_PATH. Also, path names may be
// relative to this directory.
if (chdir(HELPER_PATH)) {
LogErrno("chdir %s", HELPER_PATH);
return -1;
}
// Now execute the program.
execv(path, (char *const *)argv);
// If we get here, we know it failed. We still log the error and free the
// allocated path if any.
int saved_errno = errno;
LogErrno("execv %s", path);
errno = saved_errno;
return -1;
}
int KillPgrp(pid_t pid, int signo) {
int ret = kill(-pid, signo);
if (ret < 0 && errno == ESRCH) {
// Note: this shouldn't happen as StartPgrp() should ensure that we never
// get here. Remove this workaround once we made sure this really does not
// happen. TODO(divVerent).
LogErrno("Unable to kill process group %d - falling back to leader only",
(int)pid);
// Might mean the process is not a process group leader - but might also
// mean that the process is already dead. Try killing just the process
// then.
ret = kill(pid, signo);
}
return ret;
}
int WaitPgrp(const char *name, pid_t *pid, int do_block, int already_killed,
int *exit_status) {
int pid_saved = *pid;
int result = WaitProc(name, pid, do_block, already_killed, exit_status);
if (result && !already_killed) {
if (KillPgrp(pid_saved, SIGTERM) < 0) {
LogErrno("KillPgrp %s", name);
}
}
return result;
}
int WaitProc(const char *name, pid_t *pid, int do_block, int already_killed,
int *exit_status) {
sigset_t oldset, set;
sigemptyset(&set);
// We're blocking the signals we may have forwarding handlers for as their
// handling reads the pid variable we are changing here.
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGTERM);
// If we want to wait for a process to die, we must also block SIGCHLD
// so we can reliably wait for another child in case waitpid returned 0.
// Why can't we just use 0 instead of WNOHANG? Because then we can't block
// above signal handlers anymore, which use the pid variable.
if (do_block) {
sigaddset(&set, SIGCHLD);
}
sigemptyset(&oldset);
if (sigprocmask(SIG_BLOCK, &set, &oldset)) {
LogErrno("Unable to block signals");
}
int result = -1;
while (result == -1) {
int status;
pid_t gotpid = waitpid(*pid, &status, WNOHANG);
if (gotpid < 0) {
switch (errno) {
case ECHILD:
// The process is already dead. Fine. Although this shouldn't happen.
Log("%s child died without us noticing - please fix", name);
*exit_status = WAIT_ALREADY_DEAD;
*pid = 0;
result = 1;
break;
case EINTR:
// Waitpid was interrupted. Need to retry.
break;
default:
// Assume the child still lives. Shouldn't ever happen.
LogErrno("%s child could not be waited upon", name);
break;
}
} else if (gotpid == *pid) {
if (WIFSIGNALED(status)) {
int signo = WTERMSIG(status);
if (!already_killed || signo != SIGTERM) {
Log("%s child killed by signal %d", name, signo);
}
*exit_status = (signo > 0) ? -signo : WAIT_NONPOSITIVE_SIGNAL;
*pid = 0;
result = 1;
} else if (WIFEXITED(status)) {
*exit_status = WEXITSTATUS(status);
if (*exit_status != EXIT_SUCCESS) {
Log("%s child failed with status %d", name, *exit_status);
}
*pid = 0;
result = 1;
}
// Otherwise it was suspended or whatever. We need to keep waiting.
} else if (gotpid != 0) {
Log("Unexpectedly woke up for PID %d", (int)*pid);
} else if (do_block) {
// Block for SIGCHLD, then waitpid again.
sigsuspend(&oldset);
} else {
result = 0; // Child still lives.
}
}
if (sigprocmask(SIG_SETMASK, &oldset, NULL)) {
LogErrno("Unable to restore signal mask");
}
return result;
}