##// END OF EJS Templates
chg: initialize sigaction fields more reliably...
Yuya Nishihara -
r28084:3fc45956 default
parent child Browse files
Show More
@@ -1,331 +1,332 b''
1 /*
1 /*
2 * A fast client for Mercurial command server
2 * A fast client for Mercurial command server
3 *
3 *
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 *
5 *
6 * This software may be used and distributed according to the terms of the
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.
7 * GNU General Public License version 2 or any later version.
8 */
8 */
9
9
10 #include <assert.h>
10 #include <assert.h>
11 #include <errno.h>
11 #include <errno.h>
12 #include <fcntl.h>
12 #include <fcntl.h>
13 #include <signal.h>
13 #include <signal.h>
14 #include <stdio.h>
14 #include <stdio.h>
15 #include <stdlib.h>
15 #include <stdlib.h>
16 #include <string.h>
16 #include <string.h>
17 #include <sys/stat.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
18 #include <sys/types.h>
19 #include <sys/un.h>
19 #include <sys/un.h>
20 #include <sys/wait.h>
20 #include <sys/wait.h>
21 #include <time.h>
21 #include <time.h>
22 #include <unistd.h>
22 #include <unistd.h>
23
23
24 #include "hgclient.h"
24 #include "hgclient.h"
25 #include "util.h"
25 #include "util.h"
26
26
27 #ifndef UNIX_PATH_MAX
27 #ifndef UNIX_PATH_MAX
28 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)NULL)->sun_path))
28 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)NULL)->sun_path))
29 #endif
29 #endif
30
30
31 struct cmdserveropts {
31 struct cmdserveropts {
32 char sockname[UNIX_PATH_MAX];
32 char sockname[UNIX_PATH_MAX];
33 char lockfile[UNIX_PATH_MAX];
33 char lockfile[UNIX_PATH_MAX];
34 char pidfile[UNIX_PATH_MAX];
34 char pidfile[UNIX_PATH_MAX];
35 };
35 };
36
36
37 static void preparesockdir(const char *sockdir)
37 static void preparesockdir(const char *sockdir)
38 {
38 {
39 int r;
39 int r;
40 r = mkdir(sockdir, 0700);
40 r = mkdir(sockdir, 0700);
41 if (r < 0 && errno != EEXIST)
41 if (r < 0 && errno != EEXIST)
42 abortmsg("cannot create sockdir %s (errno = %d)",
42 abortmsg("cannot create sockdir %s (errno = %d)",
43 sockdir, errno);
43 sockdir, errno);
44
44
45 struct stat st;
45 struct stat st;
46 r = lstat(sockdir, &st);
46 r = lstat(sockdir, &st);
47 if (r < 0)
47 if (r < 0)
48 abortmsg("cannot stat %s (errno = %d)", sockdir, errno);
48 abortmsg("cannot stat %s (errno = %d)", sockdir, errno);
49 if (!S_ISDIR(st.st_mode))
49 if (!S_ISDIR(st.st_mode))
50 abortmsg("cannot create sockdir %s (file exists)", sockdir);
50 abortmsg("cannot create sockdir %s (file exists)", sockdir);
51 if (st.st_uid != geteuid() || st.st_mode & 0077)
51 if (st.st_uid != geteuid() || st.st_mode & 0077)
52 abortmsg("insecure sockdir %s", sockdir);
52 abortmsg("insecure sockdir %s", sockdir);
53 }
53 }
54
54
55 static void setcmdserveropts(struct cmdserveropts *opts)
55 static void setcmdserveropts(struct cmdserveropts *opts)
56 {
56 {
57 int r;
57 int r;
58 char sockdir[UNIX_PATH_MAX];
58 char sockdir[UNIX_PATH_MAX];
59 const char *envsockname = getenv("CHGSOCKNAME");
59 const char *envsockname = getenv("CHGSOCKNAME");
60 if (!envsockname) {
60 if (!envsockname) {
61 /* by default, put socket file in secure directory
61 /* by default, put socket file in secure directory
62 * (permission of socket file may be ignored on some Unices) */
62 * (permission of socket file may be ignored on some Unices) */
63 const char *tmpdir = getenv("TMPDIR");
63 const char *tmpdir = getenv("TMPDIR");
64 if (!tmpdir)
64 if (!tmpdir)
65 tmpdir = "/tmp";
65 tmpdir = "/tmp";
66 r = snprintf(sockdir, sizeof(sockdir), "%s/chg%d",
66 r = snprintf(sockdir, sizeof(sockdir), "%s/chg%d",
67 tmpdir, geteuid());
67 tmpdir, geteuid());
68 if (r < 0 || (size_t)r >= sizeof(sockdir))
68 if (r < 0 || (size_t)r >= sizeof(sockdir))
69 abortmsg("too long TMPDIR (r = %d)", r);
69 abortmsg("too long TMPDIR (r = %d)", r);
70 preparesockdir(sockdir);
70 preparesockdir(sockdir);
71 }
71 }
72
72
73 const char *basename = (envsockname) ? envsockname : sockdir;
73 const char *basename = (envsockname) ? envsockname : sockdir;
74 const char *sockfmt = (envsockname) ? "%s" : "%s/server";
74 const char *sockfmt = (envsockname) ? "%s" : "%s/server";
75 const char *lockfmt = (envsockname) ? "%s.lock" : "%s/lock";
75 const char *lockfmt = (envsockname) ? "%s.lock" : "%s/lock";
76 const char *pidfmt = (envsockname) ? "%s.pid" : "%s/pid";
76 const char *pidfmt = (envsockname) ? "%s.pid" : "%s/pid";
77 r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename);
77 r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename);
78 if (r < 0 || (size_t)r >= sizeof(opts->sockname))
78 if (r < 0 || (size_t)r >= sizeof(opts->sockname))
79 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
79 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
80 r = snprintf(opts->lockfile, sizeof(opts->lockfile), lockfmt, basename);
80 r = snprintf(opts->lockfile, sizeof(opts->lockfile), lockfmt, basename);
81 if (r < 0 || (size_t)r >= sizeof(opts->lockfile))
81 if (r < 0 || (size_t)r >= sizeof(opts->lockfile))
82 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
82 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
83 r = snprintf(opts->pidfile, sizeof(opts->pidfile), pidfmt, basename);
83 r = snprintf(opts->pidfile, sizeof(opts->pidfile), pidfmt, basename);
84 if (r < 0 || (size_t)r >= sizeof(opts->pidfile))
84 if (r < 0 || (size_t)r >= sizeof(opts->pidfile))
85 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
85 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
86 }
86 }
87
87
88 /*
88 /*
89 * Make lock file that indicates cmdserver process is about to start. Created
89 * Make lock file that indicates cmdserver process is about to start. Created
90 * lock file will be deleted by server. (0: success, -1: lock exists)
90 * lock file will be deleted by server. (0: success, -1: lock exists)
91 */
91 */
92 static int lockcmdserver(const struct cmdserveropts *opts)
92 static int lockcmdserver(const struct cmdserveropts *opts)
93 {
93 {
94 int r;
94 int r;
95 char info[32];
95 char info[32];
96 r = snprintf(info, sizeof(info), "%d", getpid());
96 r = snprintf(info, sizeof(info), "%d", getpid());
97 if (r < 0 || (size_t)r >= sizeof(info))
97 if (r < 0 || (size_t)r >= sizeof(info))
98 abortmsg("failed to format lock info");
98 abortmsg("failed to format lock info");
99 r = symlink(info, opts->lockfile);
99 r = symlink(info, opts->lockfile);
100 if (r < 0 && errno != EEXIST)
100 if (r < 0 && errno != EEXIST)
101 abortmsg("failed to make lock %s (errno = %d)",
101 abortmsg("failed to make lock %s (errno = %d)",
102 opts->lockfile, errno);
102 opts->lockfile, errno);
103 return r;
103 return r;
104 }
104 }
105
105
106 static void execcmdserver(const struct cmdserveropts *opts)
106 static void execcmdserver(const struct cmdserveropts *opts)
107 {
107 {
108 const char *hgcmd = getenv("CHGHG");
108 const char *hgcmd = getenv("CHGHG");
109 if (!hgcmd || hgcmd[0] == '\0')
109 if (!hgcmd || hgcmd[0] == '\0')
110 hgcmd = getenv("HG");
110 hgcmd = getenv("HG");
111 if (!hgcmd || hgcmd[0] == '\0')
111 if (!hgcmd || hgcmd[0] == '\0')
112 hgcmd = "hg";
112 hgcmd = "hg";
113
113
114 const char *argv[] = {
114 const char *argv[] = {
115 hgcmd,
115 hgcmd,
116 "serve",
116 "serve",
117 "--cwd", "/",
117 "--cwd", "/",
118 "--cmdserver", "chgunix",
118 "--cmdserver", "chgunix",
119 "--address", opts->sockname,
119 "--address", opts->sockname,
120 "--daemon-pipefds", opts->lockfile,
120 "--daemon-pipefds", opts->lockfile,
121 "--pid-file", opts->pidfile,
121 "--pid-file", opts->pidfile,
122 "--config", "extensions.chgserver=",
122 "--config", "extensions.chgserver=",
123 /* wrap root ui so that it can be disabled/enabled by config */
123 /* wrap root ui so that it can be disabled/enabled by config */
124 "--config", "progress.assume-tty=1",
124 "--config", "progress.assume-tty=1",
125 NULL,
125 NULL,
126 };
126 };
127 if (execvp(hgcmd, (char **)argv) < 0)
127 if (execvp(hgcmd, (char **)argv) < 0)
128 abortmsg("failed to exec cmdserver (errno = %d)", errno);
128 abortmsg("failed to exec cmdserver (errno = %d)", errno);
129 }
129 }
130
130
131 /*
131 /*
132 * Sleep until lock file is deleted, i.e. cmdserver process starts listening.
132 * Sleep until lock file is deleted, i.e. cmdserver process starts listening.
133 * If pid is given, it also checks if the child process fails to start.
133 * If pid is given, it also checks if the child process fails to start.
134 */
134 */
135 static void waitcmdserver(const struct cmdserveropts *opts, pid_t pid)
135 static void waitcmdserver(const struct cmdserveropts *opts, pid_t pid)
136 {
136 {
137 static const struct timespec sleepreq = {0, 10 * 1000000};
137 static const struct timespec sleepreq = {0, 10 * 1000000};
138 int pst = 0;
138 int pst = 0;
139
139
140 for (unsigned int i = 0; i < 10 * 100; i++) {
140 for (unsigned int i = 0; i < 10 * 100; i++) {
141 int r;
141 int r;
142 struct stat lst;
142 struct stat lst;
143
143
144 r = lstat(opts->lockfile, &lst);
144 r = lstat(opts->lockfile, &lst);
145 if (r < 0 && errno == ENOENT)
145 if (r < 0 && errno == ENOENT)
146 return; /* lock file deleted by server */
146 return; /* lock file deleted by server */
147 if (r < 0)
147 if (r < 0)
148 goto cleanup;
148 goto cleanup;
149
149
150 if (pid > 0) {
150 if (pid > 0) {
151 /* collect zombie if child process fails to start */
151 /* collect zombie if child process fails to start */
152 r = waitpid(pid, &pst, WNOHANG);
152 r = waitpid(pid, &pst, WNOHANG);
153 if (r != 0)
153 if (r != 0)
154 goto cleanup;
154 goto cleanup;
155 }
155 }
156
156
157 nanosleep(&sleepreq, NULL);
157 nanosleep(&sleepreq, NULL);
158 }
158 }
159
159
160 abortmsg("timed out waiting for cmdserver %s", opts->lockfile);
160 abortmsg("timed out waiting for cmdserver %s", opts->lockfile);
161 return;
161 return;
162
162
163 cleanup:
163 cleanup:
164 if (pid > 0)
164 if (pid > 0)
165 /* lockfile should be made by this process */
165 /* lockfile should be made by this process */
166 unlink(opts->lockfile);
166 unlink(opts->lockfile);
167 if (WIFEXITED(pst)) {
167 if (WIFEXITED(pst)) {
168 abortmsg("cmdserver exited with status %d", WEXITSTATUS(pst));
168 abortmsg("cmdserver exited with status %d", WEXITSTATUS(pst));
169 } else if (WIFSIGNALED(pst)) {
169 } else if (WIFSIGNALED(pst)) {
170 abortmsg("cmdserver killed by signal %d", WTERMSIG(pst));
170 abortmsg("cmdserver killed by signal %d", WTERMSIG(pst));
171 } else {
171 } else {
172 abortmsg("error white waiting cmdserver");
172 abortmsg("error white waiting cmdserver");
173 }
173 }
174 }
174 }
175
175
176 /* Spawn new background cmdserver */
176 /* Spawn new background cmdserver */
177 static void startcmdserver(const struct cmdserveropts *opts)
177 static void startcmdserver(const struct cmdserveropts *opts)
178 {
178 {
179 debugmsg("start cmdserver at %s", opts->sockname);
179 debugmsg("start cmdserver at %s", opts->sockname);
180
180
181 if (lockcmdserver(opts) < 0) {
181 if (lockcmdserver(opts) < 0) {
182 debugmsg("lock file exists, waiting...");
182 debugmsg("lock file exists, waiting...");
183 waitcmdserver(opts, 0);
183 waitcmdserver(opts, 0);
184 return;
184 return;
185 }
185 }
186
186
187 /* remove dead cmdserver socket if any */
187 /* remove dead cmdserver socket if any */
188 unlink(opts->sockname);
188 unlink(opts->sockname);
189
189
190 pid_t pid = fork();
190 pid_t pid = fork();
191 if (pid < 0)
191 if (pid < 0)
192 abortmsg("failed to fork cmdserver process");
192 abortmsg("failed to fork cmdserver process");
193 if (pid == 0) {
193 if (pid == 0) {
194 /* bypass uisetup() of pager extension */
194 /* bypass uisetup() of pager extension */
195 int nullfd = open("/dev/null", O_WRONLY);
195 int nullfd = open("/dev/null", O_WRONLY);
196 if (nullfd >= 0) {
196 if (nullfd >= 0) {
197 dup2(nullfd, fileno(stdout));
197 dup2(nullfd, fileno(stdout));
198 close(nullfd);
198 close(nullfd);
199 }
199 }
200 execcmdserver(opts);
200 execcmdserver(opts);
201 } else {
201 } else {
202 waitcmdserver(opts, pid);
202 waitcmdserver(opts, pid);
203 }
203 }
204 }
204 }
205
205
206 static void killcmdserver(const struct cmdserveropts *opts, int sig)
206 static void killcmdserver(const struct cmdserveropts *opts, int sig)
207 {
207 {
208 FILE *fp = fopen(opts->pidfile, "r");
208 FILE *fp = fopen(opts->pidfile, "r");
209 if (!fp)
209 if (!fp)
210 abortmsg("cannot open %s (errno = %d)", opts->pidfile, errno);
210 abortmsg("cannot open %s (errno = %d)", opts->pidfile, errno);
211 int pid = 0;
211 int pid = 0;
212 int n = fscanf(fp, "%d", &pid);
212 int n = fscanf(fp, "%d", &pid);
213 fclose(fp);
213 fclose(fp);
214 if (n != 1 || pid <= 0)
214 if (n != 1 || pid <= 0)
215 abortmsg("cannot read pid from %s", opts->pidfile);
215 abortmsg("cannot read pid from %s", opts->pidfile);
216
216
217 if (kill((pid_t)pid, sig) < 0) {
217 if (kill((pid_t)pid, sig) < 0) {
218 if (errno == ESRCH)
218 if (errno == ESRCH)
219 return;
219 return;
220 abortmsg("cannot kill %d (errno = %d)", pid, errno);
220 abortmsg("cannot kill %d (errno = %d)", pid, errno);
221 }
221 }
222 }
222 }
223
223
224 static pid_t peerpid = 0;
224 static pid_t peerpid = 0;
225
225
226 static void forwardsignal(int sig)
226 static void forwardsignal(int sig)
227 {
227 {
228 assert(peerpid > 0);
228 assert(peerpid > 0);
229 if (kill(peerpid, sig) < 0)
229 if (kill(peerpid, sig) < 0)
230 abortmsg("cannot kill %d (errno = %d)", peerpid, errno);
230 abortmsg("cannot kill %d (errno = %d)", peerpid, errno);
231 debugmsg("forward signal %d", sig);
231 debugmsg("forward signal %d", sig);
232 }
232 }
233
233
234 static void setupsignalhandler(pid_t pid)
234 static void setupsignalhandler(pid_t pid)
235 {
235 {
236 if (pid <= 0)
236 if (pid <= 0)
237 return;
237 return;
238 peerpid = pid;
238 peerpid = pid;
239
239
240 struct sigaction sa;
240 struct sigaction sa;
241 memset(&sa, 0, sizeof(sa));
241 memset(&sa, 0, sizeof(sa));
242 sa.sa_handler = forwardsignal;
242 sa.sa_handler = forwardsignal;
243 sa.sa_flags = SA_RESTART;
243 sa.sa_flags = SA_RESTART;
244 sigemptyset(&sa.sa_mask);
244
245
245 sigaction(SIGHUP, &sa, NULL);
246 sigaction(SIGHUP, &sa, NULL);
246 sigaction(SIGINT, &sa, NULL);
247 sigaction(SIGINT, &sa, NULL);
247
248
248 /* terminate frontend by double SIGTERM in case of server freeze */
249 /* terminate frontend by double SIGTERM in case of server freeze */
249 sa.sa_flags |= SA_RESETHAND;
250 sa.sa_flags |= SA_RESETHAND;
250 sigaction(SIGTERM, &sa, NULL);
251 sigaction(SIGTERM, &sa, NULL);
251 }
252 }
252
253
253 /* This implementation is based on hgext/pager.py (pre 369741ef7253) */
254 /* This implementation is based on hgext/pager.py (pre 369741ef7253) */
254 static void setuppager(hgclient_t *hgc, const char *const args[],
255 static void setuppager(hgclient_t *hgc, const char *const args[],
255 size_t argsize)
256 size_t argsize)
256 {
257 {
257 const char *pagercmd = hgc_getpager(hgc, args, argsize);
258 const char *pagercmd = hgc_getpager(hgc, args, argsize);
258 if (!pagercmd)
259 if (!pagercmd)
259 return;
260 return;
260
261
261 int pipefds[2];
262 int pipefds[2];
262 if (pipe(pipefds) < 0)
263 if (pipe(pipefds) < 0)
263 return;
264 return;
264 pid_t pid = fork();
265 pid_t pid = fork();
265 if (pid < 0)
266 if (pid < 0)
266 goto error;
267 goto error;
267 if (pid == 0) {
268 if (pid == 0) {
268 close(pipefds[0]);
269 close(pipefds[0]);
269 if (dup2(pipefds[1], fileno(stdout)) < 0)
270 if (dup2(pipefds[1], fileno(stdout)) < 0)
270 goto error;
271 goto error;
271 if (isatty(fileno(stderr))) {
272 if (isatty(fileno(stderr))) {
272 if (dup2(pipefds[1], fileno(stderr)) < 0)
273 if (dup2(pipefds[1], fileno(stderr)) < 0)
273 goto error;
274 goto error;
274 }
275 }
275 close(pipefds[1]);
276 close(pipefds[1]);
276 hgc_attachio(hgc); /* reattach to pager */
277 hgc_attachio(hgc); /* reattach to pager */
277 return;
278 return;
278 } else {
279 } else {
279 dup2(pipefds[0], fileno(stdin));
280 dup2(pipefds[0], fileno(stdin));
280 close(pipefds[0]);
281 close(pipefds[0]);
281 close(pipefds[1]);
282 close(pipefds[1]);
282
283
283 int r = execlp("/bin/sh", "/bin/sh", "-c", pagercmd, NULL);
284 int r = execlp("/bin/sh", "/bin/sh", "-c", pagercmd, NULL);
284 if (r < 0) {
285 if (r < 0) {
285 abortmsg("cannot start pager '%s' (errno = %d)",
286 abortmsg("cannot start pager '%s' (errno = %d)",
286 pagercmd, errno);
287 pagercmd, errno);
287 }
288 }
288 return;
289 return;
289 }
290 }
290
291
291 error:
292 error:
292 close(pipefds[0]);
293 close(pipefds[0]);
293 close(pipefds[1]);
294 close(pipefds[1]);
294 abortmsg("failed to prepare pager (errno = %d)", errno);
295 abortmsg("failed to prepare pager (errno = %d)", errno);
295 }
296 }
296
297
297 int main(int argc, const char *argv[], const char *envp[])
298 int main(int argc, const char *argv[], const char *envp[])
298 {
299 {
299 if (getenv("CHGDEBUG"))
300 if (getenv("CHGDEBUG"))
300 enabledebugmsg();
301 enabledebugmsg();
301
302
302 struct cmdserveropts opts;
303 struct cmdserveropts opts;
303 setcmdserveropts(&opts);
304 setcmdserveropts(&opts);
304
305
305 if (argc == 2) {
306 if (argc == 2) {
306 int sig = 0;
307 int sig = 0;
307 if (strcmp(argv[1], "--kill-chg-daemon") == 0)
308 if (strcmp(argv[1], "--kill-chg-daemon") == 0)
308 sig = SIGTERM;
309 sig = SIGTERM;
309 if (strcmp(argv[1], "--reload-chg-daemon") == 0)
310 if (strcmp(argv[1], "--reload-chg-daemon") == 0)
310 sig = SIGHUP;
311 sig = SIGHUP;
311 if (sig > 0) {
312 if (sig > 0) {
312 killcmdserver(&opts, sig);
313 killcmdserver(&opts, sig);
313 return 0;
314 return 0;
314 }
315 }
315 }
316 }
316
317
317 hgclient_t *hgc = hgc_open(opts.sockname);
318 hgclient_t *hgc = hgc_open(opts.sockname);
318 if (!hgc) {
319 if (!hgc) {
319 startcmdserver(&opts);
320 startcmdserver(&opts);
320 hgc = hgc_open(opts.sockname);
321 hgc = hgc_open(opts.sockname);
321 }
322 }
322 if (!hgc)
323 if (!hgc)
323 abortmsg("cannot open hg client");
324 abortmsg("cannot open hg client");
324
325
325 setupsignalhandler(hgc_peerpid(hgc));
326 setupsignalhandler(hgc_peerpid(hgc));
326 hgc_setenv(hgc, envp);
327 hgc_setenv(hgc, envp);
327 setuppager(hgc, argv + 1, argc - 1);
328 setuppager(hgc, argv + 1, argc - 1);
328 int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1);
329 int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1);
329 hgc_close(hgc);
330 hgc_close(hgc);
330 return exitcode;
331 return exitcode;
331 }
332 }
@@ -1,121 +1,123 b''
1 /*
1 /*
2 * Utility functions
2 * Utility functions
3 *
3 *
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 *
5 *
6 * This software may be used and distributed according to the terms of the
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.
7 * GNU General Public License version 2 or any later version.
8 */
8 */
9
9
10 #include <signal.h>
10 #include <signal.h>
11 #include <stdarg.h>
11 #include <stdarg.h>
12 #include <stdio.h>
12 #include <stdio.h>
13 #include <stdlib.h>
13 #include <stdlib.h>
14 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/wait.h>
16 #include <unistd.h>
17 #include <unistd.h>
17
18
18 #include "util.h"
19 #include "util.h"
19
20
20 void abortmsg(const char *fmt, ...)
21 void abortmsg(const char *fmt, ...)
21 {
22 {
22 va_list args;
23 va_list args;
23 va_start(args, fmt);
24 va_start(args, fmt);
24 fputs("\033[1;31mchg: abort: ", stderr);
25 fputs("\033[1;31mchg: abort: ", stderr);
25 vfprintf(stderr, fmt, args);
26 vfprintf(stderr, fmt, args);
26 fputs("\033[m\n", stderr);
27 fputs("\033[m\n", stderr);
27 va_end(args);
28 va_end(args);
28
29
29 exit(255);
30 exit(255);
30 }
31 }
31
32
32 static int debugmsgenabled = 0;
33 static int debugmsgenabled = 0;
33
34
34 void enabledebugmsg(void)
35 void enabledebugmsg(void)
35 {
36 {
36 debugmsgenabled = 1;
37 debugmsgenabled = 1;
37 }
38 }
38
39
39 void debugmsg(const char *fmt, ...)
40 void debugmsg(const char *fmt, ...)
40 {
41 {
41 if (!debugmsgenabled)
42 if (!debugmsgenabled)
42 return;
43 return;
43
44
44 va_list args;
45 va_list args;
45 va_start(args, fmt);
46 va_start(args, fmt);
46 fputs("\033[1;30mchg: debug: ", stderr);
47 fputs("\033[1;30mchg: debug: ", stderr);
47 vfprintf(stderr, fmt, args);
48 vfprintf(stderr, fmt, args);
48 fputs("\033[m\n", stderr);
49 fputs("\033[m\n", stderr);
49 va_end(args);
50 va_end(args);
50 }
51 }
51
52
52 /*
53 /*
53 * Execute a shell command in mostly the same manner as system(), with the
54 * Execute a shell command in mostly the same manner as system(), with the
54 * give environment variables, after chdir to the given cwd. Returns a status
55 * give environment variables, after chdir to the given cwd. Returns a status
55 * code compatible with the Python subprocess module.
56 * code compatible with the Python subprocess module.
56 */
57 */
57 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
58 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
58 {
59 {
59 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
60 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
60 unsigned int doneflags = 0;
61 unsigned int doneflags = 0;
61 int status = 0;
62 int status = 0;
62 struct sigaction newsa, oldsaint, oldsaquit;
63 struct sigaction newsa, oldsaint, oldsaquit;
63 sigset_t oldmask;
64 sigset_t oldmask;
64
65
65 /* block or mask signals just as system() does */
66 /* block or mask signals just as system() does */
67 memset(&newsa, 0, sizeof(newsa));
66 newsa.sa_handler = SIG_IGN;
68 newsa.sa_handler = SIG_IGN;
67 newsa.sa_flags = 0;
69 newsa.sa_flags = 0;
68 if (sigemptyset(&newsa.sa_mask) < 0)
70 if (sigemptyset(&newsa.sa_mask) < 0)
69 goto done;
71 goto done;
70 if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
72 if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
71 goto done;
73 goto done;
72 doneflags |= F_SIGINT;
74 doneflags |= F_SIGINT;
73 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
75 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
74 goto done;
76 goto done;
75 doneflags |= F_SIGQUIT;
77 doneflags |= F_SIGQUIT;
76
78
77 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
79 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
78 goto done;
80 goto done;
79 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
81 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
80 goto done;
82 goto done;
81 doneflags |= F_SIGMASK;
83 doneflags |= F_SIGMASK;
82
84
83 pid_t pid = fork();
85 pid_t pid = fork();
84 if (pid < 0)
86 if (pid < 0)
85 goto done;
87 goto done;
86 if (pid == 0) {
88 if (pid == 0) {
87 sigaction(SIGINT, &oldsaint, NULL);
89 sigaction(SIGINT, &oldsaint, NULL);
88 sigaction(SIGQUIT, &oldsaquit, NULL);
90 sigaction(SIGQUIT, &oldsaquit, NULL);
89 sigprocmask(SIG_SETMASK, &oldmask, NULL);
91 sigprocmask(SIG_SETMASK, &oldmask, NULL);
90 if (cwd && chdir(cwd) < 0)
92 if (cwd && chdir(cwd) < 0)
91 _exit(127);
93 _exit(127);
92 const char *argv[] = {"sh", "-c", cmd, NULL};
94 const char *argv[] = {"sh", "-c", cmd, NULL};
93 if (envp) {
95 if (envp) {
94 execve("/bin/sh", (char **)argv, (char **)envp);
96 execve("/bin/sh", (char **)argv, (char **)envp);
95 } else {
97 } else {
96 execv("/bin/sh", (char **)argv);
98 execv("/bin/sh", (char **)argv);
97 }
99 }
98 _exit(127);
100 _exit(127);
99 } else {
101 } else {
100 if (waitpid(pid, &status, 0) < 0)
102 if (waitpid(pid, &status, 0) < 0)
101 goto done;
103 goto done;
102 doneflags |= F_WAITPID;
104 doneflags |= F_WAITPID;
103 }
105 }
104
106
105 done:
107 done:
106 if (doneflags & F_SIGINT)
108 if (doneflags & F_SIGINT)
107 sigaction(SIGINT, &oldsaint, NULL);
109 sigaction(SIGINT, &oldsaint, NULL);
108 if (doneflags & F_SIGQUIT)
110 if (doneflags & F_SIGQUIT)
109 sigaction(SIGQUIT, &oldsaquit, NULL);
111 sigaction(SIGQUIT, &oldsaquit, NULL);
110 if (doneflags & F_SIGMASK)
112 if (doneflags & F_SIGMASK)
111 sigprocmask(SIG_SETMASK, &oldmask, NULL);
113 sigprocmask(SIG_SETMASK, &oldmask, NULL);
112
114
113 /* no way to report other errors, use 127 (= shell termination) */
115 /* no way to report other errors, use 127 (= shell termination) */
114 if (!(doneflags & F_WAITPID))
116 if (!(doneflags & F_WAITPID))
115 return 127;
117 return 127;
116 if (WIFEXITED(status))
118 if (WIFEXITED(status))
117 return WEXITSTATUS(status);
119 return WEXITSTATUS(status);
118 if (WIFSIGNALED(status))
120 if (WIFSIGNALED(status))
119 return -WTERMSIG(status);
121 return -WTERMSIG(status);
120 return 127;
122 return 127;
121 }
123 }
General Comments 0
You need to be logged in to leave comments. Login now