##// END OF EJS Templates
chg: fix typo
Manuel Jacob -
r45529:2c920c4d default
parent child Browse files
Show More
@@ -1,470 +1,470
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 "procutil.h"
26 #include "procutil.h"
27 #include "util.h"
27 #include "util.h"
28
28
29 #ifndef PATH_MAX
29 #ifndef PATH_MAX
30 #define PATH_MAX 4096
30 #define PATH_MAX 4096
31 #endif
31 #endif
32
32
33 struct cmdserveropts {
33 struct cmdserveropts {
34 char sockname[PATH_MAX];
34 char sockname[PATH_MAX];
35 char initsockname[PATH_MAX];
35 char initsockname[PATH_MAX];
36 char redirectsockname[PATH_MAX];
36 char redirectsockname[PATH_MAX];
37 size_t argsize;
37 size_t argsize;
38 const char **args;
38 const char **args;
39 };
39 };
40
40
41 static void initcmdserveropts(struct cmdserveropts *opts)
41 static void initcmdserveropts(struct cmdserveropts *opts)
42 {
42 {
43 memset(opts, 0, sizeof(struct cmdserveropts));
43 memset(opts, 0, sizeof(struct cmdserveropts));
44 }
44 }
45
45
46 static void freecmdserveropts(struct cmdserveropts *opts)
46 static void freecmdserveropts(struct cmdserveropts *opts)
47 {
47 {
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}, {"--cwd", 1}, {"--repo", 1},
64 {"--config", 1}, {"--cwd", 1}, {"--repo", 1},
65 {"--repository", 1}, {"--traceback", 0}, {"-R", 1},
65 {"--repository", 1}, {"--traceback", 0}, {"-R", 1},
66 };
66 };
67 size_t i;
67 size_t i;
68 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) {
68 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) {
69 size_t len = strlen(flags[i].name);
69 size_t len = strlen(flags[i].name);
70 size_t narg = flags[i].narg;
70 size_t narg = flags[i].narg;
71 if (memcmp(arg, flags[i].name, len) == 0) {
71 if (memcmp(arg, flags[i].name, len) == 0) {
72 if (arg[len] == '\0') {
72 if (arg[len] == '\0') {
73 /* --flag (value) */
73 /* --flag (value) */
74 return narg + 1;
74 return narg + 1;
75 } else if (arg[len] == '=' && narg > 0) {
75 } else if (arg[len] == '=' && narg > 0) {
76 /* --flag=value */
76 /* --flag=value */
77 return 1;
77 return 1;
78 } else if (flags[i].name[1] != '-') {
78 } else if (flags[i].name[1] != '-') {
79 /* short flag */
79 /* short flag */
80 return 1;
80 return 1;
81 }
81 }
82 }
82 }
83 }
83 }
84 return 0;
84 return 0;
85 }
85 }
86
86
87 /*
87 /*
88 * Parse argv[] and put sensitive flags to opts->args
88 * Parse argv[] and put sensitive flags to opts->args
89 */
89 */
90 static void setcmdserverargs(struct cmdserveropts *opts, int argc,
90 static void setcmdserverargs(struct cmdserveropts *opts, int argc,
91 const char *argv[])
91 const char *argv[])
92 {
92 {
93 size_t i, step;
93 size_t i, step;
94 opts->argsize = 0;
94 opts->argsize = 0;
95 for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) {
95 for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) {
96 if (!argv[i])
96 if (!argv[i])
97 continue; /* pass clang-analyse */
97 continue; /* pass clang-analyse */
98 if (strcmp(argv[i], "--") == 0)
98 if (strcmp(argv[i], "--") == 0)
99 break;
99 break;
100 size_t n = testsensitiveflag(argv[i]);
100 size_t n = testsensitiveflag(argv[i]);
101 if (n == 0 || i + n > (size_t)argc)
101 if (n == 0 || i + n > (size_t)argc)
102 continue;
102 continue;
103 opts->args =
103 opts->args =
104 reallocx(opts->args, (n + opts->argsize) * sizeof(char *));
104 reallocx(opts->args, (n + opts->argsize) * sizeof(char *));
105 memcpy(opts->args + opts->argsize, argv + i,
105 memcpy(opts->args + opts->argsize, argv + i,
106 sizeof(char *) * n);
106 sizeof(char *) * n);
107 opts->argsize += n;
107 opts->argsize += n;
108 step = n;
108 step = n;
109 }
109 }
110 }
110 }
111
111
112 static void preparesockdir(const char *sockdir)
112 static void preparesockdir(const char *sockdir)
113 {
113 {
114 int r;
114 int r;
115 r = mkdir(sockdir, 0700);
115 r = mkdir(sockdir, 0700);
116 if (r < 0 && errno != EEXIST)
116 if (r < 0 && errno != EEXIST)
117 abortmsgerrno("cannot create sockdir %s", sockdir);
117 abortmsgerrno("cannot create sockdir %s", sockdir);
118
118
119 struct stat st;
119 struct stat st;
120 r = lstat(sockdir, &st);
120 r = lstat(sockdir, &st);
121 if (r < 0)
121 if (r < 0)
122 abortmsgerrno("cannot stat %s", sockdir);
122 abortmsgerrno("cannot stat %s", sockdir);
123 if (!S_ISDIR(st.st_mode))
123 if (!S_ISDIR(st.st_mode))
124 abortmsg("cannot create sockdir %s (file exists)", sockdir);
124 abortmsg("cannot create sockdir %s (file exists)", sockdir);
125 if (st.st_uid != geteuid() || st.st_mode & 0077)
125 if (st.st_uid != geteuid() || st.st_mode & 0077)
126 abortmsg("insecure sockdir %s", sockdir);
126 abortmsg("insecure sockdir %s", sockdir);
127 }
127 }
128
128
129 /*
129 /*
130 * Check if a socket directory exists and is only owned by the current user.
130 * Check if a socket directory exists and is only owned by the current user.
131 * Return 1 if so, 0 if not. This is used to check if XDG_RUNTIME_DIR can be
131 * Return 1 if so, 0 if not. This is used to check if XDG_RUNTIME_DIR can be
132 * used or not. According to the specification [1], XDG_RUNTIME_DIR should be
132 * used or not. According to the specification [1], XDG_RUNTIME_DIR should be
133 * ignored if the directory is not owned by the user with mode 0700.
133 * ignored if the directory is not owned by the user with mode 0700.
134 * [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
134 * [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
135 */
135 */
136 static int checkruntimedir(const char *sockdir)
136 static int checkruntimedir(const char *sockdir)
137 {
137 {
138 struct stat st;
138 struct stat st;
139 int r = lstat(sockdir, &st);
139 int r = lstat(sockdir, &st);
140 if (r < 0) /* ex. does not exist */
140 if (r < 0) /* ex. does not exist */
141 return 0;
141 return 0;
142 if (!S_ISDIR(st.st_mode)) /* ex. is a file, not a directory */
142 if (!S_ISDIR(st.st_mode)) /* ex. is a file, not a directory */
143 return 0;
143 return 0;
144 return st.st_uid == geteuid() && (st.st_mode & 0777) == 0700;
144 return st.st_uid == geteuid() && (st.st_mode & 0777) == 0700;
145 }
145 }
146
146
147 static void getdefaultsockdir(char sockdir[], size_t size)
147 static void getdefaultsockdir(char sockdir[], size_t size)
148 {
148 {
149 /* by default, put socket file in secure directory
149 /* by default, put socket file in secure directory
150 * (${XDG_RUNTIME_DIR}/chg, or /${TMPDIR:-tmp}/chg$UID)
150 * (${XDG_RUNTIME_DIR}/chg, or /${TMPDIR:-tmp}/chg$UID)
151 * (permission of socket file may be ignored on some Unices) */
151 * (permission of socket file may be ignored on some Unices) */
152 const char *runtimedir = getenv("XDG_RUNTIME_DIR");
152 const char *runtimedir = getenv("XDG_RUNTIME_DIR");
153 int r;
153 int r;
154 if (runtimedir && checkruntimedir(runtimedir)) {
154 if (runtimedir && checkruntimedir(runtimedir)) {
155 r = snprintf(sockdir, size, "%s/chg", runtimedir);
155 r = snprintf(sockdir, size, "%s/chg", runtimedir);
156 } else {
156 } else {
157 const char *tmpdir = getenv("TMPDIR");
157 const char *tmpdir = getenv("TMPDIR");
158 if (!tmpdir)
158 if (!tmpdir)
159 tmpdir = "/tmp";
159 tmpdir = "/tmp";
160 r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
160 r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
161 }
161 }
162 if (r < 0 || (size_t)r >= size)
162 if (r < 0 || (size_t)r >= size)
163 abortmsg("too long TMPDIR (r = %d)", r);
163 abortmsg("too long TMPDIR (r = %d)", r);
164 }
164 }
165
165
166 static void setcmdserveropts(struct cmdserveropts *opts)
166 static void setcmdserveropts(struct cmdserveropts *opts)
167 {
167 {
168 int r;
168 int r;
169 char sockdir[PATH_MAX];
169 char sockdir[PATH_MAX];
170 const char *envsockname = getenv("CHGSOCKNAME");
170 const char *envsockname = getenv("CHGSOCKNAME");
171 if (!envsockname) {
171 if (!envsockname) {
172 getdefaultsockdir(sockdir, sizeof(sockdir));
172 getdefaultsockdir(sockdir, sizeof(sockdir));
173 preparesockdir(sockdir);
173 preparesockdir(sockdir);
174 }
174 }
175
175
176 const char *basename = (envsockname) ? envsockname : sockdir;
176 const char *basename = (envsockname) ? envsockname : sockdir;
177 const char *sockfmt = (envsockname) ? "%s" : "%s/server";
177 const char *sockfmt = (envsockname) ? "%s" : "%s/server";
178 r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename);
178 r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename);
179 if (r < 0 || (size_t)r >= sizeof(opts->sockname))
179 if (r < 0 || (size_t)r >= sizeof(opts->sockname))
180 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
180 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
181 r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u",
181 r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u",
182 opts->sockname, (unsigned)getpid());
182 opts->sockname, (unsigned)getpid());
183 if (r < 0 || (size_t)r >= sizeof(opts->initsockname))
183 if (r < 0 || (size_t)r >= sizeof(opts->initsockname))
184 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
184 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
185 }
185 }
186
186
187 static const char *gethgcmd(void)
187 static const char *gethgcmd(void)
188 {
188 {
189 static const char *hgcmd = NULL;
189 static const char *hgcmd = NULL;
190 if (!hgcmd) {
190 if (!hgcmd) {
191 hgcmd = getenv("CHGHG");
191 hgcmd = getenv("CHGHG");
192 if (!hgcmd || hgcmd[0] == '\0')
192 if (!hgcmd || hgcmd[0] == '\0')
193 hgcmd = getenv("HG");
193 hgcmd = getenv("HG");
194 if (!hgcmd || hgcmd[0] == '\0')
194 if (!hgcmd || hgcmd[0] == '\0')
195 #ifdef HGPATH
195 #ifdef HGPATH
196 hgcmd = (HGPATH);
196 hgcmd = (HGPATH);
197 #else
197 #else
198 hgcmd = "hg";
198 hgcmd = "hg";
199 #endif
199 #endif
200 }
200 }
201 return hgcmd;
201 return hgcmd;
202 }
202 }
203
203
204 static void execcmdserver(const struct cmdserveropts *opts)
204 static void execcmdserver(const struct cmdserveropts *opts)
205 {
205 {
206 const char *hgcmd = gethgcmd();
206 const char *hgcmd = gethgcmd();
207
207
208 const char *baseargv[] = {
208 const char *baseargv[] = {
209 hgcmd,
209 hgcmd,
210 "serve",
210 "serve",
211 "--cmdserver",
211 "--cmdserver",
212 "chgunix",
212 "chgunix",
213 "--address",
213 "--address",
214 opts->initsockname,
214 opts->initsockname,
215 "--daemon-postexec",
215 "--daemon-postexec",
216 "chdir:/",
216 "chdir:/",
217 };
217 };
218 size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]);
218 size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]);
219 size_t argsize = baseargvsize + opts->argsize + 1;
219 size_t argsize = baseargvsize + opts->argsize + 1;
220
220
221 const char **argv = mallocx(sizeof(char *) * argsize);
221 const char **argv = mallocx(sizeof(char *) * argsize);
222 memcpy(argv, baseargv, sizeof(baseargv));
222 memcpy(argv, baseargv, sizeof(baseargv));
223 if (opts->args) {
223 if (opts->args) {
224 size_t size = sizeof(char *) * opts->argsize;
224 size_t size = sizeof(char *) * opts->argsize;
225 memcpy(argv + baseargvsize, opts->args, size);
225 memcpy(argv + baseargvsize, opts->args, size);
226 }
226 }
227 argv[argsize - 1] = NULL;
227 argv[argsize - 1] = NULL;
228
228
229 const char *lc_ctype_env = getenv("LC_CTYPE");
229 const char *lc_ctype_env = getenv("LC_CTYPE");
230 if (lc_ctype_env == NULL) {
230 if (lc_ctype_env == NULL) {
231 if (putenv("CHG_CLEAR_LC_CTYPE=") != 0)
231 if (putenv("CHG_CLEAR_LC_CTYPE=") != 0)
232 abortmsgerrno("failed to putenv CHG_CLEAR_LC_CTYPE");
232 abortmsgerrno("failed to putenv CHG_CLEAR_LC_CTYPE");
233 } else {
233 } else {
234 if (setenv("CHGORIG_LC_CTYPE", lc_ctype_env, 1) != 0) {
234 if (setenv("CHGORIG_LC_CTYPE", lc_ctype_env, 1) != 0) {
235 abortmsgerrno("failed to setenv CHGORIG_LC_CTYYPE");
235 abortmsgerrno("failed to setenv CHGORIG_LC_CTYPE");
236 }
236 }
237 }
237 }
238
238
239 if (putenv("CHGINTERNALMARK=") != 0)
239 if (putenv("CHGINTERNALMARK=") != 0)
240 abortmsgerrno("failed to putenv");
240 abortmsgerrno("failed to putenv");
241 if (execvp(hgcmd, (char **)argv) < 0)
241 if (execvp(hgcmd, (char **)argv) < 0)
242 abortmsgerrno("failed to exec cmdserver");
242 abortmsgerrno("failed to exec cmdserver");
243 free(argv);
243 free(argv);
244 }
244 }
245
245
246 /* Retry until we can connect to the server. Give up after some time. */
246 /* Retry until we can connect to the server. Give up after some time. */
247 static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid)
247 static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid)
248 {
248 {
249 static const struct timespec sleepreq = {0, 10 * 1000000};
249 static const struct timespec sleepreq = {0, 10 * 1000000};
250 int pst = 0;
250 int pst = 0;
251
251
252 debugmsg("try connect to %s repeatedly", opts->initsockname);
252 debugmsg("try connect to %s repeatedly", opts->initsockname);
253
253
254 unsigned int timeoutsec = 60; /* default: 60 seconds */
254 unsigned int timeoutsec = 60; /* default: 60 seconds */
255 const char *timeoutenv = getenv("CHGTIMEOUT");
255 const char *timeoutenv = getenv("CHGTIMEOUT");
256 if (timeoutenv)
256 if (timeoutenv)
257 sscanf(timeoutenv, "%u", &timeoutsec);
257 sscanf(timeoutenv, "%u", &timeoutsec);
258
258
259 for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) {
259 for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) {
260 hgclient_t *hgc = hgc_open(opts->initsockname);
260 hgclient_t *hgc = hgc_open(opts->initsockname);
261 if (hgc) {
261 if (hgc) {
262 debugmsg("rename %s to %s", opts->initsockname,
262 debugmsg("rename %s to %s", opts->initsockname,
263 opts->sockname);
263 opts->sockname);
264 int r = rename(opts->initsockname, opts->sockname);
264 int r = rename(opts->initsockname, opts->sockname);
265 if (r != 0)
265 if (r != 0)
266 abortmsgerrno("cannot rename");
266 abortmsgerrno("cannot rename");
267 return hgc;
267 return hgc;
268 }
268 }
269
269
270 if (pid > 0) {
270 if (pid > 0) {
271 /* collect zombie if child process fails to start */
271 /* collect zombie if child process fails to start */
272 int r = waitpid(pid, &pst, WNOHANG);
272 int r = waitpid(pid, &pst, WNOHANG);
273 if (r != 0)
273 if (r != 0)
274 goto cleanup;
274 goto cleanup;
275 }
275 }
276
276
277 nanosleep(&sleepreq, NULL);
277 nanosleep(&sleepreq, NULL);
278 }
278 }
279
279
280 abortmsg("timed out waiting for cmdserver %s", opts->initsockname);
280 abortmsg("timed out waiting for cmdserver %s", opts->initsockname);
281 return NULL;
281 return NULL;
282
282
283 cleanup:
283 cleanup:
284 if (WIFEXITED(pst)) {
284 if (WIFEXITED(pst)) {
285 if (WEXITSTATUS(pst) == 0)
285 if (WEXITSTATUS(pst) == 0)
286 abortmsg("could not connect to cmdserver "
286 abortmsg("could not connect to cmdserver "
287 "(exited with status 0)");
287 "(exited with status 0)");
288 debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst));
288 debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst));
289 exit(WEXITSTATUS(pst));
289 exit(WEXITSTATUS(pst));
290 } else if (WIFSIGNALED(pst)) {
290 } else if (WIFSIGNALED(pst)) {
291 abortmsg("cmdserver killed by signal %d", WTERMSIG(pst));
291 abortmsg("cmdserver killed by signal %d", WTERMSIG(pst));
292 } else {
292 } else {
293 abortmsg("error while waiting for cmdserver");
293 abortmsg("error while waiting for cmdserver");
294 }
294 }
295 return NULL;
295 return NULL;
296 }
296 }
297
297
298 /* Connect to a cmdserver. Will start a new server on demand. */
298 /* Connect to a cmdserver. Will start a new server on demand. */
299 static hgclient_t *connectcmdserver(struct cmdserveropts *opts)
299 static hgclient_t *connectcmdserver(struct cmdserveropts *opts)
300 {
300 {
301 const char *sockname =
301 const char *sockname =
302 opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname;
302 opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname;
303 debugmsg("try connect to %s", sockname);
303 debugmsg("try connect to %s", sockname);
304 hgclient_t *hgc = hgc_open(sockname);
304 hgclient_t *hgc = hgc_open(sockname);
305 if (hgc)
305 if (hgc)
306 return hgc;
306 return hgc;
307
307
308 /* prevent us from being connected to an outdated server: we were
308 /* prevent us from being connected to an outdated server: we were
309 * told by a server to redirect to opts->redirectsockname and that
309 * told by a server to redirect to opts->redirectsockname and that
310 * address does not work. we do not want to connect to the server
310 * address does not work. we do not want to connect to the server
311 * again because it will probably tell us the same thing. */
311 * again because it will probably tell us the same thing. */
312 if (sockname == opts->redirectsockname)
312 if (sockname == opts->redirectsockname)
313 unlink(opts->sockname);
313 unlink(opts->sockname);
314
314
315 debugmsg("start cmdserver at %s", opts->initsockname);
315 debugmsg("start cmdserver at %s", opts->initsockname);
316
316
317 pid_t pid = fork();
317 pid_t pid = fork();
318 if (pid < 0)
318 if (pid < 0)
319 abortmsg("failed to fork cmdserver process");
319 abortmsg("failed to fork cmdserver process");
320 if (pid == 0) {
320 if (pid == 0) {
321 execcmdserver(opts);
321 execcmdserver(opts);
322 } else {
322 } else {
323 hgc = retryconnectcmdserver(opts, pid);
323 hgc = retryconnectcmdserver(opts, pid);
324 }
324 }
325
325
326 return hgc;
326 return hgc;
327 }
327 }
328
328
329 static void killcmdserver(const struct cmdserveropts *opts)
329 static void killcmdserver(const struct cmdserveropts *opts)
330 {
330 {
331 /* resolve config hash */
331 /* resolve config hash */
332 char *resolvedpath = realpath(opts->sockname, NULL);
332 char *resolvedpath = realpath(opts->sockname, NULL);
333 if (resolvedpath) {
333 if (resolvedpath) {
334 unlink(resolvedpath);
334 unlink(resolvedpath);
335 free(resolvedpath);
335 free(resolvedpath);
336 }
336 }
337 }
337 }
338
338
339 /* Run instructions sent from the server like unlink and set redirect path
339 /* Run instructions sent from the server like unlink and set redirect path
340 * Return 1 if reconnect is needed, otherwise 0 */
340 * Return 1 if reconnect is needed, otherwise 0 */
341 static int runinstructions(struct cmdserveropts *opts, const char **insts)
341 static int runinstructions(struct cmdserveropts *opts, const char **insts)
342 {
342 {
343 int needreconnect = 0;
343 int needreconnect = 0;
344 if (!insts)
344 if (!insts)
345 return needreconnect;
345 return needreconnect;
346
346
347 assert(insts);
347 assert(insts);
348 opts->redirectsockname[0] = '\0';
348 opts->redirectsockname[0] = '\0';
349 const char **pinst;
349 const char **pinst;
350 for (pinst = insts; *pinst; pinst++) {
350 for (pinst = insts; *pinst; pinst++) {
351 debugmsg("instruction: %s", *pinst);
351 debugmsg("instruction: %s", *pinst);
352 if (strncmp(*pinst, "unlink ", 7) == 0) {
352 if (strncmp(*pinst, "unlink ", 7) == 0) {
353 unlink(*pinst + 7);
353 unlink(*pinst + 7);
354 } else if (strncmp(*pinst, "redirect ", 9) == 0) {
354 } else if (strncmp(*pinst, "redirect ", 9) == 0) {
355 int r = snprintf(opts->redirectsockname,
355 int r = snprintf(opts->redirectsockname,
356 sizeof(opts->redirectsockname), "%s",
356 sizeof(opts->redirectsockname), "%s",
357 *pinst + 9);
357 *pinst + 9);
358 if (r < 0 || r >= (int)sizeof(opts->redirectsockname))
358 if (r < 0 || r >= (int)sizeof(opts->redirectsockname))
359 abortmsg("redirect path is too long (%d)", r);
359 abortmsg("redirect path is too long (%d)", r);
360 needreconnect = 1;
360 needreconnect = 1;
361 } else if (strncmp(*pinst, "exit ", 5) == 0) {
361 } else if (strncmp(*pinst, "exit ", 5) == 0) {
362 int n = 0;
362 int n = 0;
363 if (sscanf(*pinst + 5, "%d", &n) != 1)
363 if (sscanf(*pinst + 5, "%d", &n) != 1)
364 abortmsg("cannot read the exit code");
364 abortmsg("cannot read the exit code");
365 exit(n);
365 exit(n);
366 } else if (strcmp(*pinst, "reconnect") == 0) {
366 } else if (strcmp(*pinst, "reconnect") == 0) {
367 needreconnect = 1;
367 needreconnect = 1;
368 } else {
368 } else {
369 abortmsg("unknown instruction: %s", *pinst);
369 abortmsg("unknown instruction: %s", *pinst);
370 }
370 }
371 }
371 }
372 return needreconnect;
372 return needreconnect;
373 }
373 }
374
374
375 /*
375 /*
376 * Test whether the command is unsupported or not. This is not designed to
376 * Test whether the command is unsupported or not. This is not designed to
377 * cover all cases. But it's fast, does not depend on the server.
377 * cover all cases. But it's fast, does not depend on the server.
378 */
378 */
379 static int isunsupported(int argc, const char *argv[])
379 static int isunsupported(int argc, const char *argv[])
380 {
380 {
381 enum { SERVE = 1,
381 enum { SERVE = 1,
382 DAEMON = 2,
382 DAEMON = 2,
383 SERVEDAEMON = SERVE | DAEMON,
383 SERVEDAEMON = SERVE | DAEMON,
384 };
384 };
385 unsigned int state = 0;
385 unsigned int state = 0;
386 int i;
386 int i;
387 for (i = 0; i < argc; ++i) {
387 for (i = 0; i < argc; ++i) {
388 if (strcmp(argv[i], "--") == 0)
388 if (strcmp(argv[i], "--") == 0)
389 break;
389 break;
390 /*
390 /*
391 * there can be false positives but no false negative
391 * there can be false positives but no false negative
392 * we cannot assume `serve` will always be first argument
392 * we cannot assume `serve` will always be first argument
393 * because global options can be passed before the command name
393 * because global options can be passed before the command name
394 */
394 */
395 if (strcmp("serve", argv[i]) == 0)
395 if (strcmp("serve", argv[i]) == 0)
396 state |= SERVE;
396 state |= SERVE;
397 else if (strcmp("-d", argv[i]) == 0 ||
397 else if (strcmp("-d", argv[i]) == 0 ||
398 strcmp("--daemon", argv[i]) == 0)
398 strcmp("--daemon", argv[i]) == 0)
399 state |= DAEMON;
399 state |= DAEMON;
400 }
400 }
401 return (state & SERVEDAEMON) == SERVEDAEMON;
401 return (state & SERVEDAEMON) == SERVEDAEMON;
402 }
402 }
403
403
404 static void execoriginalhg(const char *argv[])
404 static void execoriginalhg(const char *argv[])
405 {
405 {
406 debugmsg("execute original hg");
406 debugmsg("execute original hg");
407 if (execvp(gethgcmd(), (char **)argv) < 0)
407 if (execvp(gethgcmd(), (char **)argv) < 0)
408 abortmsgerrno("failed to exec original hg");
408 abortmsgerrno("failed to exec original hg");
409 }
409 }
410
410
411 int main(int argc, const char *argv[], const char *envp[])
411 int main(int argc, const char *argv[], const char *envp[])
412 {
412 {
413 if (getenv("CHGDEBUG"))
413 if (getenv("CHGDEBUG"))
414 enabledebugmsg();
414 enabledebugmsg();
415
415
416 if (!getenv("HGPLAIN") && isatty(fileno(stderr)))
416 if (!getenv("HGPLAIN") && isatty(fileno(stderr)))
417 enablecolor();
417 enablecolor();
418
418
419 if (getenv("CHGINTERNALMARK"))
419 if (getenv("CHGINTERNALMARK"))
420 abortmsg("chg started by chg detected.\n"
420 abortmsg("chg started by chg detected.\n"
421 "Please make sure ${HG:-hg} is not a symlink or "
421 "Please make sure ${HG:-hg} is not a symlink or "
422 "wrapper to chg. Alternatively, set $CHGHG to the "
422 "wrapper to chg. Alternatively, set $CHGHG to the "
423 "path of real hg.");
423 "path of real hg.");
424
424
425 if (isunsupported(argc - 1, argv + 1))
425 if (isunsupported(argc - 1, argv + 1))
426 execoriginalhg(argv);
426 execoriginalhg(argv);
427
427
428 struct cmdserveropts opts;
428 struct cmdserveropts opts;
429 initcmdserveropts(&opts);
429 initcmdserveropts(&opts);
430 setcmdserveropts(&opts);
430 setcmdserveropts(&opts);
431 setcmdserverargs(&opts, argc, argv);
431 setcmdserverargs(&opts, argc, argv);
432
432
433 if (argc == 2) {
433 if (argc == 2) {
434 if (strcmp(argv[1], "--kill-chg-daemon") == 0) {
434 if (strcmp(argv[1], "--kill-chg-daemon") == 0) {
435 killcmdserver(&opts);
435 killcmdserver(&opts);
436 return 0;
436 return 0;
437 }
437 }
438 }
438 }
439
439
440 hgclient_t *hgc;
440 hgclient_t *hgc;
441 size_t retry = 0;
441 size_t retry = 0;
442 while (1) {
442 while (1) {
443 hgc = connectcmdserver(&opts);
443 hgc = connectcmdserver(&opts);
444 if (!hgc)
444 if (!hgc)
445 abortmsg("cannot open hg client");
445 abortmsg("cannot open hg client");
446 hgc_setenv(hgc, envp);
446 hgc_setenv(hgc, envp);
447 const char **insts = hgc_validate(hgc, argv + 1, argc - 1);
447 const char **insts = hgc_validate(hgc, argv + 1, argc - 1);
448 int needreconnect = runinstructions(&opts, insts);
448 int needreconnect = runinstructions(&opts, insts);
449 free(insts);
449 free(insts);
450 if (!needreconnect)
450 if (!needreconnect)
451 break;
451 break;
452 hgc_close(hgc);
452 hgc_close(hgc);
453 if (++retry > 10)
453 if (++retry > 10)
454 abortmsg("too many redirections.\n"
454 abortmsg("too many redirections.\n"
455 "Please make sure %s is not a wrapper which "
455 "Please make sure %s is not a wrapper which "
456 "changes sensitive environment variables "
456 "changes sensitive environment variables "
457 "before executing hg. If you have to use a "
457 "before executing hg. If you have to use a "
458 "wrapper, wrap chg instead of hg.",
458 "wrapper, wrap chg instead of hg.",
459 gethgcmd());
459 gethgcmd());
460 }
460 }
461
461
462 setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc));
462 setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc));
463 atexit(waitpager);
463 atexit(waitpager);
464 int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1);
464 int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1);
465 restoresignalhandler();
465 restoresignalhandler();
466 hgc_close(hgc);
466 hgc_close(hgc);
467 freecmdserveropts(&opts);
467 freecmdserveropts(&opts);
468
468
469 return exitcode;
469 return exitcode;
470 }
470 }
General Comments 0
You need to be logged in to leave comments. Login now