Debian Codegraph @

contrib/chg/procutil.c

raw
1 /*
2 * Utilities about process handling - signal and subprocess (ex. pager)
3 *
4 * Copyright (c) 2011 Yuya Nishihara <[email protected]>
5 *
6 * This software may be used and distributed according to the terms of the
7 * GNU General Public License version 2 or any later version.
8 */
9
10 #include <assert.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/wait.h>
16 #include <unistd.h>
17
18 #include "procutil.h"
19 #include "util.h"
20
21 static pid_t pagerpid = 0;
22 static pid_t peerpgid = 0;
23 static pid_t peerpid = 0;
24
25 static void forwardsignal(int sig)
26 {
27 assert(peerpid > 0);
28 if (kill(peerpid, sig) < 0) {
29 abortmsgerrno("cannot kill %d", peerpid);
30 }
31 debugmsg("forward signal %d", sig);
32 }
33
34 static void forwardsignaltogroup(int sig)
35 {
36 /* prefer kill(-pgid, sig), fallback to pid if pgid is invalid */
37 pid_t killpid = peerpgid > 1 ? -peerpgid : peerpid;
38 if (kill(killpid, sig) < 0) {
39 abortmsgerrno("cannot kill %d", killpid);
40 }
41 debugmsg("forward signal %d to %d", sig, killpid);
42 }
43
44 static void handlestopsignal(int sig)
45 {
46 sigset_t unblockset, oldset;
47 struct sigaction sa, oldsa;
48 if (sigemptyset(&unblockset) < 0) {
49 goto error;
50 }
51 if (sigaddset(&unblockset, sig) < 0) {
52 goto error;
53 }
54 memset(&sa, 0, sizeof(sa));
55 sa.sa_handler = SIG_DFL;
56 sa.sa_flags = SA_RESTART;
57 if (sigemptyset(&sa.sa_mask) < 0) {
58 goto error;
59 }
60
61 forwardsignal(sig);
62 if (raise(sig) < 0) { /* resend to self */
63 goto error;
64 }
65 if (sigaction(sig, &sa, &oldsa) < 0) {
66 goto error;
67 }
68 if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) {
69 goto error;
70 }
71 /* resent signal will be handled before sigprocmask() returns */
72 if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) {
73 goto error;
74 }
75 if (sigaction(sig, &oldsa, NULL) < 0) {
76 goto error;
77 }
78 return;
79
80 error:
81 abortmsgerrno("failed to handle stop signal");
82 }
83
84 static void handlechildsignal(int sig UNUSED_)
85 {
86 if (peerpid == 0 || pagerpid == 0) {
87 return;
88 }
89 /* if pager exits, notify the server with SIGPIPE immediately.
90 * otherwise the server won't get SIGPIPE if it does not write
91 * anything. (issue5278) */
92 if (waitpid(pagerpid, NULL, WNOHANG) == pagerpid) {
93 kill(peerpid, SIGPIPE);
94 }
95 }
96
97 void setupsignalhandler(pid_t pid, pid_t pgid)
98 {
99 if (pid <= 0) {
100 return;
101 }
102 peerpid = pid;
103 peerpgid = (pgid <= 1 ? 0 : pgid);
104
105 struct sigaction sa;
106 memset(&sa, 0, sizeof(sa));
107
108 /* deadly signals meant to be sent to a process group:
109 * - SIGHUP: usually generated by the kernel, when termination of a
110 * process causes that process group to become orphaned
111 * - SIGINT: usually generated by the terminal */
112 sa.sa_handler = forwardsignaltogroup;
113 sa.sa_flags = SA_RESTART;
114 if (sigemptyset(&sa.sa_mask) < 0) {
115 goto error;
116 }
117 if (sigaction(SIGHUP, &sa, NULL) < 0) {
118 goto error;
119 }
120 if (sigaction(SIGINT, &sa, NULL) < 0) {
121 goto error;
122 }
123
124 /* terminate frontend by double SIGTERM in case of server freeze */
125 sa.sa_handler = forwardsignal;
126 sa.sa_flags |= SA_RESETHAND;
127 if (sigaction(SIGTERM, &sa, NULL) < 0) {
128 goto error;
129 }
130
131 /* notify the worker about window resize events */
132 sa.sa_flags = SA_RESTART;
133 if (sigaction(SIGWINCH, &sa, NULL) < 0) {
134 goto error;
135 }
136 /* forward user-defined signals */
137 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
138 goto error;
139 }
140 if (sigaction(SIGUSR2, &sa, NULL) < 0) {
141 goto error;
142 }
143 /* propagate job control requests to worker */
144 sa.sa_handler = forwardsignal;
145 sa.sa_flags = SA_RESTART;
146 if (sigaction(SIGCONT, &sa, NULL) < 0) {
147 goto error;
148 }
149 sa.sa_handler = handlestopsignal;
150 sa.sa_flags = SA_RESTART;
151 if (sigaction(SIGTSTP, &sa, NULL) < 0) {
152 goto error;
153 }
154 /* get notified when pager exits */
155 sa.sa_handler = handlechildsignal;
156 sa.sa_flags = SA_RESTART;
157 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
158 goto error;
159 }
160
161 return;
162
163 error:
164 abortmsgerrno("failed to set up signal handlers");
165 }
166
167 void restoresignalhandler(void)
168 {
169 struct sigaction sa;
170 memset(&sa, 0, sizeof(sa));
171 sa.sa_handler = SIG_DFL;
172 sa.sa_flags = SA_RESTART;
173 if (sigemptyset(&sa.sa_mask) < 0) {
174 goto error;
175 }
176
177 if (sigaction(SIGHUP, &sa, NULL) < 0) {
178 goto error;
179 }
180 if (sigaction(SIGTERM, &sa, NULL) < 0) {
181 goto error;
182 }
183 if (sigaction(SIGWINCH, &sa, NULL) < 0) {
184 goto error;
185 }
186 if (sigaction(SIGCONT, &sa, NULL) < 0) {
187 goto error;
188 }
189 if (sigaction(SIGTSTP, &sa, NULL) < 0) {
190 goto error;
191 }
192 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
193 goto error;
194 }
195
196 /* ignore Ctrl+C while shutting down to make pager exits cleanly */
197 sa.sa_handler = SIG_IGN;
198 if (sigaction(SIGINT, &sa, NULL) < 0) {
199 goto error;
200 }
201
202 peerpid = 0;
203 return;
204
205 error:
206 abortmsgerrno("failed to restore signal handlers");
207 }
208
209 /* This implementation is based on hgext/pager.py (post 369741ef7253)
210 * Return 0 if pager is not started, or pid of the pager */
211 pid_t setuppager(const char *pagercmd, const char *envp[])
212 {
213 assert(pagerpid == 0);
214 if (!pagercmd) {
215 return 0;
216 }
217
218 int pipefds[2];
219 if (pipe(pipefds) < 0) {
220 return 0;
221 }
222 pid_t pid = fork();
223 if (pid < 0) {
224 goto error;
225 }
226 if (pid > 0) {
227 close(pipefds[0]);
228 if (dup2(pipefds[1], fileno(stdout)) < 0) {
229 goto error;
230 }
231 if (isatty(fileno(stderr))) {
232 if (dup2(pipefds[1], fileno(stderr)) < 0) {
233 goto error;
234 }
235 }
236 close(pipefds[1]);
237 pagerpid = pid;
238 return pid;
239 } else {
240 dup2(pipefds[0], fileno(stdin));
241 close(pipefds[0]);
242 close(pipefds[1]);
243
244 int r =
245 execle("/bin/sh", "/bin/sh", "-c", pagercmd, NULL, envp);
246 if (r < 0) {
247 abortmsgerrno("cannot start pager '%s'", pagercmd);
248 }
249 return 0;
250 }
251
252 error:
253 close(pipefds[0]);
254 close(pipefds[1]);
255 abortmsgerrno("failed to prepare pager");
256 return 0;
257 }
258
259 void waitpager(void)
260 {
261 if (pagerpid == 0) {
262 return;
263 }
264
265 /* close output streams to notify the pager its input ends */
266 fclose(stdout);
267 fclose(stderr);
268 while (1) {
269 pid_t ret = waitpid(pagerpid, NULL, 0);
270 if (ret == -1 && errno == EINTR) {
271 continue;
272 }
273 break;
274 }
275 }