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