Show More
@@ -1,647 +1,650 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | * A fast client for Mercurial command server |
|
3 | 3 | * |
|
4 | 4 | * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org> |
|
5 | 5 | * |
|
6 | 6 | * This software may be used and distributed according to the terms of the |
|
7 | 7 | * GNU General Public License version 2 or any later version. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #include <assert.h> |
|
11 | 11 | #include <errno.h> |
|
12 | 12 | #include <fcntl.h> |
|
13 | 13 | #include <signal.h> |
|
14 | 14 | #include <stdio.h> |
|
15 | 15 | #include <stdlib.h> |
|
16 | 16 | #include <string.h> |
|
17 | 17 | #include <sys/file.h> |
|
18 | 18 | #include <sys/stat.h> |
|
19 | 19 | #include <sys/types.h> |
|
20 | 20 | #include <sys/un.h> |
|
21 | 21 | #include <sys/wait.h> |
|
22 | 22 | #include <time.h> |
|
23 | 23 | #include <unistd.h> |
|
24 | 24 | |
|
25 | 25 | #include "hgclient.h" |
|
26 | 26 | #include "util.h" |
|
27 | 27 | |
|
28 | 28 | #ifndef UNIX_PATH_MAX |
|
29 | 29 | #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)NULL)->sun_path)) |
|
30 | 30 | #endif |
|
31 | 31 | |
|
32 | 32 | struct cmdserveropts { |
|
33 | 33 | char sockname[UNIX_PATH_MAX]; |
|
34 | 34 | char redirectsockname[UNIX_PATH_MAX]; |
|
35 | 35 | char lockfile[UNIX_PATH_MAX]; |
|
36 | 36 | size_t argsize; |
|
37 | 37 | const char **args; |
|
38 | 38 | int lockfd; |
|
39 | 39 | int sockdirfd; |
|
40 | 40 | }; |
|
41 | 41 | |
|
42 | 42 | static void initcmdserveropts(struct cmdserveropts *opts) { |
|
43 | 43 | memset(opts, 0, sizeof(struct cmdserveropts)); |
|
44 | 44 | opts->lockfd = -1; |
|
45 | 45 | opts->sockdirfd = -1; |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | static void freecmdserveropts(struct cmdserveropts *opts) { |
|
49 | 49 | free(opts->args); |
|
50 | 50 | opts->args = NULL; |
|
51 | 51 | opts->argsize = 0; |
|
52 | 52 | assert(opts->lockfd == -1 && "should be closed by unlockcmdserver()"); |
|
53 | 53 | if (opts->sockdirfd >= 0) { |
|
54 | 54 | close(opts->sockdirfd); |
|
55 | 55 | opts->sockdirfd = -1; |
|
56 | 56 | } |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | /* |
|
60 | 60 | * Test if an argument is a sensitive flag that should be passed to the server. |
|
61 | 61 | * Return 0 if not, otherwise the number of arguments starting from the current |
|
62 | 62 | * one that should be passed to the server. |
|
63 | 63 | */ |
|
64 | 64 | static size_t testsensitiveflag(const char *arg) |
|
65 | 65 | { |
|
66 | 66 | static const struct { |
|
67 | 67 | const char *name; |
|
68 | 68 | size_t narg; |
|
69 | 69 | } flags[] = { |
|
70 | 70 | {"--config", 1}, |
|
71 | 71 | {"--cwd", 1}, |
|
72 | 72 | {"--repo", 1}, |
|
73 | 73 | {"--repository", 1}, |
|
74 | 74 | {"--traceback", 0}, |
|
75 | 75 | {"-R", 1}, |
|
76 | 76 | }; |
|
77 | 77 | size_t i; |
|
78 | 78 | for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) { |
|
79 | 79 | size_t len = strlen(flags[i].name); |
|
80 | 80 | size_t narg = flags[i].narg; |
|
81 | 81 | if (memcmp(arg, flags[i].name, len) == 0) { |
|
82 | 82 | if (arg[len] == '\0') { |
|
83 | 83 | /* --flag (value) */ |
|
84 | 84 | return narg + 1; |
|
85 | 85 | } else if (arg[len] == '=' && narg > 0) { |
|
86 | 86 | /* --flag=value */ |
|
87 | 87 | return 1; |
|
88 | 88 | } else if (flags[i].name[1] != '-') { |
|
89 | 89 | /* short flag */ |
|
90 | 90 | return 1; |
|
91 | 91 | } |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | return 0; |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | /* |
|
98 | 98 | * Parse argv[] and put sensitive flags to opts->args |
|
99 | 99 | */ |
|
100 | 100 | static void setcmdserverargs(struct cmdserveropts *opts, |
|
101 | 101 | int argc, const char *argv[]) |
|
102 | 102 | { |
|
103 | 103 | size_t i, step; |
|
104 | 104 | opts->argsize = 0; |
|
105 | 105 | for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) { |
|
106 | 106 | if (!argv[i]) |
|
107 | 107 | continue; /* pass clang-analyse */ |
|
108 | 108 | if (strcmp(argv[i], "--") == 0) |
|
109 | 109 | break; |
|
110 | 110 | size_t n = testsensitiveflag(argv[i]); |
|
111 | 111 | if (n == 0 || i + n > (size_t)argc) |
|
112 | 112 | continue; |
|
113 | 113 | opts->args = reallocx(opts->args, |
|
114 | 114 | (n + opts->argsize) * sizeof(char *)); |
|
115 | 115 | memcpy(opts->args + opts->argsize, argv + i, |
|
116 | 116 | sizeof(char *) * n); |
|
117 | 117 | opts->argsize += n; |
|
118 | 118 | step = n; |
|
119 | 119 | } |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | static void preparesockdir(const char *sockdir) |
|
123 | 123 | { |
|
124 | 124 | int r; |
|
125 | 125 | r = mkdir(sockdir, 0700); |
|
126 | 126 | if (r < 0 && errno != EEXIST) |
|
127 | 127 | abortmsgerrno("cannot create sockdir %s", sockdir); |
|
128 | 128 | |
|
129 | 129 | struct stat st; |
|
130 | 130 | r = lstat(sockdir, &st); |
|
131 | 131 | if (r < 0) |
|
132 | 132 | abortmsgerrno("cannot stat %s", sockdir); |
|
133 | 133 | if (!S_ISDIR(st.st_mode)) |
|
134 | 134 | abortmsg("cannot create sockdir %s (file exists)", sockdir); |
|
135 | 135 | if (st.st_uid != geteuid() || st.st_mode & 0077) |
|
136 | 136 | abortmsg("insecure sockdir %s", sockdir); |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | static void setcmdserveropts(struct cmdserveropts *opts) |
|
140 | 140 | { |
|
141 | 141 | int r; |
|
142 | 142 | char sockdir[UNIX_PATH_MAX]; |
|
143 | 143 | const char *envsockname = getenv("CHGSOCKNAME"); |
|
144 | 144 | if (!envsockname) { |
|
145 | 145 | /* by default, put socket file in secure directory |
|
146 | 146 | * (permission of socket file may be ignored on some Unices) */ |
|
147 | 147 | const char *tmpdir = getenv("TMPDIR"); |
|
148 | 148 | if (!tmpdir) |
|
149 | 149 | tmpdir = "/tmp"; |
|
150 | 150 | r = snprintf(sockdir, sizeof(sockdir), "%s/chg%d", |
|
151 | 151 | tmpdir, geteuid()); |
|
152 | 152 | if (r < 0 || (size_t)r >= sizeof(sockdir)) |
|
153 | 153 | abortmsg("too long TMPDIR (r = %d)", r); |
|
154 | 154 | preparesockdir(sockdir); |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | const char *basename = (envsockname) ? envsockname : sockdir; |
|
158 | 158 | const char *sockfmt = (envsockname) ? "%s" : "%s/server"; |
|
159 | 159 | const char *lockfmt = (envsockname) ? "%s.lock" : "%s/lock"; |
|
160 | 160 | r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); |
|
161 | 161 | if (r < 0 || (size_t)r >= sizeof(opts->sockname)) |
|
162 | 162 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
163 | 163 | r = snprintf(opts->lockfile, sizeof(opts->lockfile), lockfmt, basename); |
|
164 | 164 | if (r < 0 || (size_t)r >= sizeof(opts->lockfile)) |
|
165 | 165 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | /* |
|
169 | 169 | * Acquire a file lock that indicates a client is trying to start and connect |
|
170 | 170 | * to a server, before executing a command. The lock is released upon exit or |
|
171 | 171 | * explicit unlock. Will block if the lock is held by another process. |
|
172 | 172 | */ |
|
173 | 173 | static void lockcmdserver(struct cmdserveropts *opts) |
|
174 | 174 | { |
|
175 | 175 | if (opts->lockfd == -1) { |
|
176 | 176 | opts->lockfd = open(opts->lockfile, |
|
177 | 177 | O_RDWR | O_CREAT | O_NOFOLLOW, 0600); |
|
178 | 178 | if (opts->lockfd == -1) |
|
179 | 179 | abortmsgerrno("cannot create lock file %s", |
|
180 | 180 | opts->lockfile); |
|
181 | 181 | fsetcloexec(opts->lockfd); |
|
182 | 182 | } |
|
183 | 183 | int r = flock(opts->lockfd, LOCK_EX); |
|
184 | 184 | if (r == -1) |
|
185 | 185 | abortmsgerrno("cannot acquire lock"); |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | /* |
|
189 | 189 | * Release the file lock held by calling lockcmdserver. Will do nothing if |
|
190 | 190 | * lockcmdserver is not called. |
|
191 | 191 | */ |
|
192 | 192 | static void unlockcmdserver(struct cmdserveropts *opts) |
|
193 | 193 | { |
|
194 | 194 | if (opts->lockfd == -1) |
|
195 | 195 | return; |
|
196 | 196 | flock(opts->lockfd, LOCK_UN); |
|
197 | 197 | close(opts->lockfd); |
|
198 | 198 | opts->lockfd = -1; |
|
199 | 199 | } |
|
200 | 200 | |
|
201 | 201 | static const char *gethgcmd(void) |
|
202 | 202 | { |
|
203 | 203 | static const char *hgcmd = NULL; |
|
204 | 204 | if (!hgcmd) { |
|
205 | 205 | hgcmd = getenv("CHGHG"); |
|
206 | 206 | if (!hgcmd || hgcmd[0] == '\0') |
|
207 | 207 | hgcmd = getenv("HG"); |
|
208 | 208 | if (!hgcmd || hgcmd[0] == '\0') |
|
209 | 209 | #ifdef HGPATH |
|
210 | 210 | hgcmd = (HGPATH); |
|
211 | 211 | #else |
|
212 | 212 | hgcmd = "hg"; |
|
213 | 213 | #endif |
|
214 | 214 | } |
|
215 | 215 | return hgcmd; |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | static void execcmdserver(const struct cmdserveropts *opts) |
|
219 | 219 | { |
|
220 | 220 | const char *hgcmd = gethgcmd(); |
|
221 | 221 | |
|
222 | 222 | const char *baseargv[] = { |
|
223 | 223 | hgcmd, |
|
224 | 224 | "serve", |
|
225 | 225 | "--cmdserver", "chgunix", |
|
226 | 226 | "--address", opts->sockname, |
|
227 | 227 | "--daemon-postexec", "chdir:/", |
|
228 | 228 | "--config", "extensions.chgserver=", |
|
229 | 229 | }; |
|
230 | 230 | size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); |
|
231 | 231 | size_t argsize = baseargvsize + opts->argsize + 1; |
|
232 | 232 | |
|
233 | 233 | const char **argv = mallocx(sizeof(char *) * argsize); |
|
234 | 234 | memcpy(argv, baseargv, sizeof(baseargv)); |
|
235 | 235 | memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); |
|
236 | 236 | argv[argsize - 1] = NULL; |
|
237 | 237 | |
|
238 | 238 | if (putenv("CHGINTERNALMARK=") != 0) |
|
239 | 239 | abortmsgerrno("failed to putenv"); |
|
240 | 240 | if (execvp(hgcmd, (char **)argv) < 0) |
|
241 | 241 | abortmsgerrno("failed to exec cmdserver"); |
|
242 | 242 | free(argv); |
|
243 | 243 | } |
|
244 | 244 | |
|
245 | 245 | /* Retry until we can connect to the server. Give up after some time. */ |
|
246 | 246 | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) |
|
247 | 247 | { |
|
248 | 248 | static const struct timespec sleepreq = {0, 10 * 1000000}; |
|
249 | 249 | int pst = 0; |
|
250 | 250 | |
|
251 | 251 | debugmsg("try connect to %s repeatedly", opts->sockname); |
|
252 | 252 | |
|
253 | 253 | unsigned int timeoutsec = 60; /* default: 60 seconds */ |
|
254 | 254 | const char *timeoutenv = getenv("CHGTIMEOUT"); |
|
255 | 255 | if (timeoutenv) |
|
256 | 256 | sscanf(timeoutenv, "%u", &timeoutsec); |
|
257 | 257 | |
|
258 | 258 | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { |
|
259 | 259 | hgclient_t *hgc = hgc_open(opts->sockname); |
|
260 | 260 | if (hgc) |
|
261 | 261 | return hgc; |
|
262 | 262 | |
|
263 | 263 | if (pid > 0) { |
|
264 | 264 | /* collect zombie if child process fails to start */ |
|
265 | 265 | int r = waitpid(pid, &pst, WNOHANG); |
|
266 | 266 | if (r != 0) |
|
267 | 267 | goto cleanup; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | nanosleep(&sleepreq, NULL); |
|
271 | 271 | } |
|
272 | 272 | |
|
273 | 273 | abortmsg("timed out waiting for cmdserver %s", opts->sockname); |
|
274 | 274 | return NULL; |
|
275 | 275 | |
|
276 | 276 | cleanup: |
|
277 | 277 | if (WIFEXITED(pst)) { |
|
278 | 278 | if (WEXITSTATUS(pst) == 0) |
|
279 | 279 | abortmsg("could not connect to cmdserver " |
|
280 | 280 | "(exited with status 0)"); |
|
281 | 281 | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); |
|
282 | 282 | exit(WEXITSTATUS(pst)); |
|
283 | 283 | } else if (WIFSIGNALED(pst)) { |
|
284 | 284 | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); |
|
285 | 285 | } else { |
|
286 | 286 | abortmsg("error while waiting for cmdserver"); |
|
287 | 287 | } |
|
288 | 288 | return NULL; |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | /* Connect to a cmdserver. Will start a new server on demand. */ |
|
292 | 292 | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) |
|
293 | 293 | { |
|
294 | 294 | const char *sockname = opts->redirectsockname[0] ? |
|
295 | 295 | opts->redirectsockname : opts->sockname; |
|
296 | 296 | debugmsg("try connect to %s", sockname); |
|
297 | 297 | hgclient_t *hgc = hgc_open(sockname); |
|
298 | 298 | if (hgc) |
|
299 | 299 | return hgc; |
|
300 | 300 | |
|
301 | 301 | lockcmdserver(opts); |
|
302 | 302 | hgc = hgc_open(sockname); |
|
303 | 303 | if (hgc) { |
|
304 | 304 | unlockcmdserver(opts); |
|
305 | 305 | debugmsg("cmdserver is started by another process"); |
|
306 | 306 | return hgc; |
|
307 | 307 | } |
|
308 | 308 | |
|
309 | 309 | /* prevent us from being connected to an outdated server: we were |
|
310 | 310 | * told by a server to redirect to opts->redirectsockname and that |
|
311 | 311 | * address does not work. we do not want to connect to the server |
|
312 | 312 | * again because it will probably tell us the same thing. */ |
|
313 | 313 | if (sockname == opts->redirectsockname) |
|
314 | 314 | unlink(opts->sockname); |
|
315 | 315 | |
|
316 | 316 | debugmsg("start cmdserver at %s", opts->sockname); |
|
317 | 317 | |
|
318 | 318 | pid_t pid = fork(); |
|
319 | 319 | if (pid < 0) |
|
320 | 320 | abortmsg("failed to fork cmdserver process"); |
|
321 | 321 | if (pid == 0) { |
|
322 | 322 | execcmdserver(opts); |
|
323 | 323 | } else { |
|
324 | 324 | hgc = retryconnectcmdserver(opts, pid); |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | unlockcmdserver(opts); |
|
328 | 328 | return hgc; |
|
329 | 329 | } |
|
330 | 330 | |
|
331 | 331 | static void killcmdserver(const struct cmdserveropts *opts) |
|
332 | 332 | { |
|
333 | 333 | /* resolve config hash */ |
|
334 | 334 | char *resolvedpath = realpath(opts->sockname, NULL); |
|
335 | 335 | if (resolvedpath) { |
|
336 | 336 | unlink(resolvedpath); |
|
337 | 337 | free(resolvedpath); |
|
338 | 338 | } |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | static pid_t peerpid = 0; |
|
342 | 342 | |
|
343 | 343 | static void forwardsignal(int sig) |
|
344 | 344 | { |
|
345 | 345 | assert(peerpid > 0); |
|
346 | 346 | if (kill(peerpid, sig) < 0) |
|
347 | 347 | abortmsgerrno("cannot kill %d", peerpid); |
|
348 | 348 | debugmsg("forward signal %d", sig); |
|
349 | 349 | } |
|
350 | 350 | |
|
351 | 351 | static void handlestopsignal(int sig) |
|
352 | 352 | { |
|
353 | 353 | sigset_t unblockset, oldset; |
|
354 | 354 | struct sigaction sa, oldsa; |
|
355 | 355 | if (sigemptyset(&unblockset) < 0) |
|
356 | 356 | goto error; |
|
357 | 357 | if (sigaddset(&unblockset, sig) < 0) |
|
358 | 358 | goto error; |
|
359 | 359 | memset(&sa, 0, sizeof(sa)); |
|
360 | 360 | sa.sa_handler = SIG_DFL; |
|
361 | 361 | sa.sa_flags = SA_RESTART; |
|
362 | 362 | if (sigemptyset(&sa.sa_mask) < 0) |
|
363 | 363 | goto error; |
|
364 | 364 | |
|
365 | 365 | forwardsignal(sig); |
|
366 | 366 | if (raise(sig) < 0) /* resend to self */ |
|
367 | 367 | goto error; |
|
368 | 368 | if (sigaction(sig, &sa, &oldsa) < 0) |
|
369 | 369 | goto error; |
|
370 | 370 | if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) |
|
371 | 371 | goto error; |
|
372 | 372 | /* resent signal will be handled before sigprocmask() returns */ |
|
373 | 373 | if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) |
|
374 | 374 | goto error; |
|
375 | 375 | if (sigaction(sig, &oldsa, NULL) < 0) |
|
376 | 376 | goto error; |
|
377 | 377 | return; |
|
378 | 378 | |
|
379 | 379 | error: |
|
380 | 380 | abortmsgerrno("failed to handle stop signal"); |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | static void setupsignalhandler(pid_t pid) |
|
384 | 384 | { |
|
385 | 385 | if (pid <= 0) |
|
386 | 386 | return; |
|
387 | 387 | peerpid = pid; |
|
388 | 388 | |
|
389 | 389 | struct sigaction sa; |
|
390 | 390 | memset(&sa, 0, sizeof(sa)); |
|
391 | 391 | sa.sa_handler = forwardsignal; |
|
392 | 392 | sa.sa_flags = SA_RESTART; |
|
393 | 393 | if (sigemptyset(&sa.sa_mask) < 0) |
|
394 | 394 | goto error; |
|
395 | 395 | |
|
396 | 396 | if (sigaction(SIGHUP, &sa, NULL) < 0) |
|
397 | 397 | goto error; |
|
398 | 398 | if (sigaction(SIGINT, &sa, NULL) < 0) |
|
399 | 399 | goto error; |
|
400 | 400 | |
|
401 | 401 | /* terminate frontend by double SIGTERM in case of server freeze */ |
|
402 | 402 | sa.sa_flags |= SA_RESETHAND; |
|
403 | 403 | if (sigaction(SIGTERM, &sa, NULL) < 0) |
|
404 | 404 | goto error; |
|
405 | 405 | |
|
406 | 406 | /* notify the worker about window resize events */ |
|
407 | 407 | sa.sa_flags = SA_RESTART; |
|
408 | 408 | if (sigaction(SIGWINCH, &sa, NULL) < 0) |
|
409 | 409 | goto error; |
|
410 | 410 | /* propagate job control requests to worker */ |
|
411 | 411 | sa.sa_handler = forwardsignal; |
|
412 | 412 | sa.sa_flags = SA_RESTART; |
|
413 | 413 | if (sigaction(SIGCONT, &sa, NULL) < 0) |
|
414 | 414 | goto error; |
|
415 | 415 | sa.sa_handler = handlestopsignal; |
|
416 | 416 | sa.sa_flags = SA_RESTART; |
|
417 | 417 | if (sigaction(SIGTSTP, &sa, NULL) < 0) |
|
418 | 418 | goto error; |
|
419 | 419 | |
|
420 | 420 | return; |
|
421 | 421 | |
|
422 | 422 | error: |
|
423 | 423 | abortmsgerrno("failed to set up signal handlers"); |
|
424 | 424 | } |
|
425 | 425 | |
|
426 | 426 | static void restoresignalhandler() |
|
427 | 427 | { |
|
428 | 428 | struct sigaction sa; |
|
429 | 429 | memset(&sa, 0, sizeof(sa)); |
|
430 | 430 | sa.sa_handler = SIG_DFL; |
|
431 | 431 | sa.sa_flags = SA_RESTART; |
|
432 | 432 | if (sigemptyset(&sa.sa_mask) < 0) |
|
433 | 433 | goto error; |
|
434 | 434 | |
|
435 | 435 | if (sigaction(SIGHUP, &sa, NULL) < 0) |
|
436 | 436 | goto error; |
|
437 | if (sigaction(SIGINT, &sa, NULL) < 0) | |
|
438 | goto error; | |
|
439 | 437 | if (sigaction(SIGTERM, &sa, NULL) < 0) |
|
440 | 438 | goto error; |
|
441 | 439 | if (sigaction(SIGWINCH, &sa, NULL) < 0) |
|
442 | 440 | goto error; |
|
443 | 441 | if (sigaction(SIGCONT, &sa, NULL) < 0) |
|
444 | 442 | goto error; |
|
445 | 443 | if (sigaction(SIGTSTP, &sa, NULL) < 0) |
|
446 | 444 | goto error; |
|
447 | 445 | |
|
446 | /* ignore Ctrl+C while shutting down to make pager exits cleanly */ | |
|
447 | sa.sa_handler = SIG_IGN; | |
|
448 | if (sigaction(SIGINT, &sa, NULL) < 0) | |
|
449 | goto error; | |
|
450 | ||
|
448 | 451 | peerpid = 0; |
|
449 | 452 | return; |
|
450 | 453 | |
|
451 | 454 | error: |
|
452 | 455 | abortmsgerrno("failed to restore signal handlers"); |
|
453 | 456 | } |
|
454 | 457 | |
|
455 | 458 | /* This implementation is based on hgext/pager.py (post 369741ef7253) |
|
456 | 459 | * Return 0 if pager is not started, or pid of the pager */ |
|
457 | 460 | static pid_t setuppager(hgclient_t *hgc, const char *const args[], |
|
458 | 461 | size_t argsize) |
|
459 | 462 | { |
|
460 | 463 | const char *pagercmd = hgc_getpager(hgc, args, argsize); |
|
461 | 464 | if (!pagercmd) |
|
462 | 465 | return 0; |
|
463 | 466 | |
|
464 | 467 | int pipefds[2]; |
|
465 | 468 | if (pipe(pipefds) < 0) |
|
466 | 469 | return 0; |
|
467 | 470 | pid_t pid = fork(); |
|
468 | 471 | if (pid < 0) |
|
469 | 472 | goto error; |
|
470 | 473 | if (pid > 0) { |
|
471 | 474 | close(pipefds[0]); |
|
472 | 475 | if (dup2(pipefds[1], fileno(stdout)) < 0) |
|
473 | 476 | goto error; |
|
474 | 477 | if (isatty(fileno(stderr))) { |
|
475 | 478 | if (dup2(pipefds[1], fileno(stderr)) < 0) |
|
476 | 479 | goto error; |
|
477 | 480 | } |
|
478 | 481 | close(pipefds[1]); |
|
479 | 482 | hgc_attachio(hgc); /* reattach to pager */ |
|
480 | 483 | return pid; |
|
481 | 484 | } else { |
|
482 | 485 | dup2(pipefds[0], fileno(stdin)); |
|
483 | 486 | close(pipefds[0]); |
|
484 | 487 | close(pipefds[1]); |
|
485 | 488 | |
|
486 | 489 | int r = execlp("/bin/sh", "/bin/sh", "-c", pagercmd, NULL); |
|
487 | 490 | if (r < 0) { |
|
488 | 491 | abortmsgerrno("cannot start pager '%s'", pagercmd); |
|
489 | 492 | } |
|
490 | 493 | return 0; |
|
491 | 494 | } |
|
492 | 495 | |
|
493 | 496 | error: |
|
494 | 497 | close(pipefds[0]); |
|
495 | 498 | close(pipefds[1]); |
|
496 | 499 | abortmsgerrno("failed to prepare pager"); |
|
497 | 500 | return 0; |
|
498 | 501 | } |
|
499 | 502 | |
|
500 | 503 | static void waitpager(pid_t pid) |
|
501 | 504 | { |
|
502 | 505 | /* close output streams to notify the pager its input ends */ |
|
503 | 506 | fclose(stdout); |
|
504 | 507 | fclose(stderr); |
|
505 | 508 | while (1) { |
|
506 | 509 | pid_t ret = waitpid(pid, NULL, 0); |
|
507 | 510 | if (ret == -1 && errno == EINTR) |
|
508 | 511 | continue; |
|
509 | 512 | break; |
|
510 | 513 | } |
|
511 | 514 | } |
|
512 | 515 | |
|
513 | 516 | /* Run instructions sent from the server like unlink and set redirect path |
|
514 | 517 | * Return 1 if reconnect is needed, otherwise 0 */ |
|
515 | 518 | static int runinstructions(struct cmdserveropts *opts, const char **insts) |
|
516 | 519 | { |
|
517 | 520 | int needreconnect = 0; |
|
518 | 521 | if (!insts) |
|
519 | 522 | return needreconnect; |
|
520 | 523 | |
|
521 | 524 | assert(insts); |
|
522 | 525 | opts->redirectsockname[0] = '\0'; |
|
523 | 526 | const char **pinst; |
|
524 | 527 | for (pinst = insts; *pinst; pinst++) { |
|
525 | 528 | debugmsg("instruction: %s", *pinst); |
|
526 | 529 | if (strncmp(*pinst, "unlink ", 7) == 0) { |
|
527 | 530 | unlink(*pinst + 7); |
|
528 | 531 | } else if (strncmp(*pinst, "redirect ", 9) == 0) { |
|
529 | 532 | int r = snprintf(opts->redirectsockname, |
|
530 | 533 | sizeof(opts->redirectsockname), |
|
531 | 534 | "%s", *pinst + 9); |
|
532 | 535 | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) |
|
533 | 536 | abortmsg("redirect path is too long (%d)", r); |
|
534 | 537 | needreconnect = 1; |
|
535 | 538 | } else if (strncmp(*pinst, "exit ", 5) == 0) { |
|
536 | 539 | int n = 0; |
|
537 | 540 | if (sscanf(*pinst + 5, "%d", &n) != 1) |
|
538 | 541 | abortmsg("cannot read the exit code"); |
|
539 | 542 | exit(n); |
|
540 | 543 | } else if (strcmp(*pinst, "reconnect") == 0) { |
|
541 | 544 | needreconnect = 1; |
|
542 | 545 | } else { |
|
543 | 546 | abortmsg("unknown instruction: %s", *pinst); |
|
544 | 547 | } |
|
545 | 548 | } |
|
546 | 549 | return needreconnect; |
|
547 | 550 | } |
|
548 | 551 | |
|
549 | 552 | /* |
|
550 | 553 | * Test whether the command is unsupported or not. This is not designed to |
|
551 | 554 | * cover all cases. But it's fast, does not depend on the server and does |
|
552 | 555 | * not return false positives. |
|
553 | 556 | */ |
|
554 | 557 | static int isunsupported(int argc, const char *argv[]) |
|
555 | 558 | { |
|
556 | 559 | enum { |
|
557 | 560 | SERVE = 1, |
|
558 | 561 | DAEMON = 2, |
|
559 | 562 | SERVEDAEMON = SERVE | DAEMON, |
|
560 | 563 | TIME = 4, |
|
561 | 564 | }; |
|
562 | 565 | unsigned int state = 0; |
|
563 | 566 | int i; |
|
564 | 567 | for (i = 0; i < argc; ++i) { |
|
565 | 568 | if (strcmp(argv[i], "--") == 0) |
|
566 | 569 | break; |
|
567 | 570 | if (i == 0 && strcmp("serve", argv[i]) == 0) |
|
568 | 571 | state |= SERVE; |
|
569 | 572 | else if (strcmp("-d", argv[i]) == 0 || |
|
570 | 573 | strcmp("--daemon", argv[i]) == 0) |
|
571 | 574 | state |= DAEMON; |
|
572 | 575 | else if (strcmp("--time", argv[i]) == 0) |
|
573 | 576 | state |= TIME; |
|
574 | 577 | } |
|
575 | 578 | return (state & TIME) == TIME || |
|
576 | 579 | (state & SERVEDAEMON) == SERVEDAEMON; |
|
577 | 580 | } |
|
578 | 581 | |
|
579 | 582 | static void execoriginalhg(const char *argv[]) |
|
580 | 583 | { |
|
581 | 584 | debugmsg("execute original hg"); |
|
582 | 585 | if (execvp(gethgcmd(), (char **)argv) < 0) |
|
583 | 586 | abortmsgerrno("failed to exec original hg"); |
|
584 | 587 | } |
|
585 | 588 | |
|
586 | 589 | int main(int argc, const char *argv[], const char *envp[]) |
|
587 | 590 | { |
|
588 | 591 | if (getenv("CHGDEBUG")) |
|
589 | 592 | enabledebugmsg(); |
|
590 | 593 | |
|
591 | 594 | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) |
|
592 | 595 | enablecolor(); |
|
593 | 596 | |
|
594 | 597 | if (getenv("CHGINTERNALMARK")) |
|
595 | 598 | abortmsg("chg started by chg detected.\n" |
|
596 | 599 | "Please make sure ${HG:-hg} is not a symlink or " |
|
597 | 600 | "wrapper to chg. Alternatively, set $CHGHG to the " |
|
598 | 601 | "path of real hg."); |
|
599 | 602 | |
|
600 | 603 | if (isunsupported(argc - 1, argv + 1)) |
|
601 | 604 | execoriginalhg(argv); |
|
602 | 605 | |
|
603 | 606 | struct cmdserveropts opts; |
|
604 | 607 | initcmdserveropts(&opts); |
|
605 | 608 | setcmdserveropts(&opts); |
|
606 | 609 | setcmdserverargs(&opts, argc, argv); |
|
607 | 610 | |
|
608 | 611 | if (argc == 2) { |
|
609 | 612 | if (strcmp(argv[1], "--kill-chg-daemon") == 0) { |
|
610 | 613 | killcmdserver(&opts); |
|
611 | 614 | return 0; |
|
612 | 615 | } |
|
613 | 616 | } |
|
614 | 617 | |
|
615 | 618 | hgclient_t *hgc; |
|
616 | 619 | size_t retry = 0; |
|
617 | 620 | while (1) { |
|
618 | 621 | hgc = connectcmdserver(&opts); |
|
619 | 622 | if (!hgc) |
|
620 | 623 | abortmsg("cannot open hg client"); |
|
621 | 624 | hgc_setenv(hgc, envp); |
|
622 | 625 | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); |
|
623 | 626 | int needreconnect = runinstructions(&opts, insts); |
|
624 | 627 | free(insts); |
|
625 | 628 | if (!needreconnect) |
|
626 | 629 | break; |
|
627 | 630 | hgc_close(hgc); |
|
628 | 631 | if (++retry > 10) |
|
629 | 632 | abortmsg("too many redirections.\n" |
|
630 | 633 | "Please make sure %s is not a wrapper which " |
|
631 | 634 | "changes sensitive environment variables " |
|
632 | 635 | "before executing hg. If you have to use a " |
|
633 | 636 | "wrapper, wrap chg instead of hg.", |
|
634 | 637 | gethgcmd()); |
|
635 | 638 | } |
|
636 | 639 | |
|
637 | 640 | setupsignalhandler(hgc_peerpid(hgc)); |
|
638 | 641 | pid_t pagerpid = setuppager(hgc, argv + 1, argc - 1); |
|
639 | 642 | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); |
|
640 | 643 | restoresignalhandler(); |
|
641 | 644 | hgc_close(hgc); |
|
642 | 645 | freecmdserveropts(&opts); |
|
643 | 646 | if (pagerpid) |
|
644 | 647 | waitpager(pagerpid); |
|
645 | 648 | |
|
646 | 649 | return exitcode; |
|
647 | 650 | } |
General Comments 0
You need to be logged in to leave comments.
Login now