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