Show More
@@ -1,453 +1,456 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 | { |
|
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 | memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); |
|
223 | if (opts->args) { | |
|
224 | size_t size = sizeof(char *) * opts->argsize; | |||
|
225 | memcpy(argv + baseargvsize, opts->args, size); | |||
|
226 | } | |||
224 | argv[argsize - 1] = NULL; |
|
227 | argv[argsize - 1] = NULL; | |
225 |
|
228 | |||
226 | if (putenv("CHGINTERNALMARK=") != 0) |
|
229 | if (putenv("CHGINTERNALMARK=") != 0) | |
227 | abortmsgerrno("failed to putenv"); |
|
230 | abortmsgerrno("failed to putenv"); | |
228 | if (execvp(hgcmd, (char **)argv) < 0) |
|
231 | if (execvp(hgcmd, (char **)argv) < 0) | |
229 | abortmsgerrno("failed to exec cmdserver"); |
|
232 | abortmsgerrno("failed to exec cmdserver"); | |
230 | free(argv); |
|
233 | free(argv); | |
231 | } |
|
234 | } | |
232 |
|
235 | |||
233 | /* 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. */ | |
234 | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) |
|
237 | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) | |
235 | { |
|
238 | { | |
236 | static const struct timespec sleepreq = {0, 10 * 1000000}; |
|
239 | static const struct timespec sleepreq = {0, 10 * 1000000}; | |
237 | int pst = 0; |
|
240 | int pst = 0; | |
238 |
|
241 | |||
239 | debugmsg("try connect to %s repeatedly", opts->initsockname); |
|
242 | debugmsg("try connect to %s repeatedly", opts->initsockname); | |
240 |
|
243 | |||
241 | unsigned int timeoutsec = 60; /* default: 60 seconds */ |
|
244 | unsigned int timeoutsec = 60; /* default: 60 seconds */ | |
242 | const char *timeoutenv = getenv("CHGTIMEOUT"); |
|
245 | const char *timeoutenv = getenv("CHGTIMEOUT"); | |
243 | if (timeoutenv) |
|
246 | if (timeoutenv) | |
244 | sscanf(timeoutenv, "%u", &timeoutsec); |
|
247 | sscanf(timeoutenv, "%u", &timeoutsec); | |
245 |
|
248 | |||
246 | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { |
|
249 | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { | |
247 | hgclient_t *hgc = hgc_open(opts->initsockname); |
|
250 | hgclient_t *hgc = hgc_open(opts->initsockname); | |
248 | if (hgc) { |
|
251 | if (hgc) { | |
249 | debugmsg("rename %s to %s", opts->initsockname, |
|
252 | debugmsg("rename %s to %s", opts->initsockname, | |
250 | opts->sockname); |
|
253 | opts->sockname); | |
251 | int r = rename(opts->initsockname, opts->sockname); |
|
254 | int r = rename(opts->initsockname, opts->sockname); | |
252 | if (r != 0) |
|
255 | if (r != 0) | |
253 | abortmsgerrno("cannot rename"); |
|
256 | abortmsgerrno("cannot rename"); | |
254 | return hgc; |
|
257 | return hgc; | |
255 | } |
|
258 | } | |
256 |
|
259 | |||
257 | if (pid > 0) { |
|
260 | if (pid > 0) { | |
258 | /* collect zombie if child process fails to start */ |
|
261 | /* collect zombie if child process fails to start */ | |
259 | int r = waitpid(pid, &pst, WNOHANG); |
|
262 | int r = waitpid(pid, &pst, WNOHANG); | |
260 | if (r != 0) |
|
263 | if (r != 0) | |
261 | goto cleanup; |
|
264 | goto cleanup; | |
262 | } |
|
265 | } | |
263 |
|
266 | |||
264 | nanosleep(&sleepreq, NULL); |
|
267 | nanosleep(&sleepreq, NULL); | |
265 | } |
|
268 | } | |
266 |
|
269 | |||
267 | abortmsg("timed out waiting for cmdserver %s", opts->initsockname); |
|
270 | abortmsg("timed out waiting for cmdserver %s", opts->initsockname); | |
268 | return NULL; |
|
271 | return NULL; | |
269 |
|
272 | |||
270 | cleanup: |
|
273 | cleanup: | |
271 | if (WIFEXITED(pst)) { |
|
274 | if (WIFEXITED(pst)) { | |
272 | if (WEXITSTATUS(pst) == 0) |
|
275 | if (WEXITSTATUS(pst) == 0) | |
273 | abortmsg("could not connect to cmdserver " |
|
276 | abortmsg("could not connect to cmdserver " | |
274 | "(exited with status 0)"); |
|
277 | "(exited with status 0)"); | |
275 | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); |
|
278 | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); | |
276 | exit(WEXITSTATUS(pst)); |
|
279 | exit(WEXITSTATUS(pst)); | |
277 | } else if (WIFSIGNALED(pst)) { |
|
280 | } else if (WIFSIGNALED(pst)) { | |
278 | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); |
|
281 | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); | |
279 | } else { |
|
282 | } else { | |
280 | abortmsg("error while waiting for cmdserver"); |
|
283 | abortmsg("error while waiting for cmdserver"); | |
281 | } |
|
284 | } | |
282 | return NULL; |
|
285 | return NULL; | |
283 | } |
|
286 | } | |
284 |
|
287 | |||
285 | /* Connect to a cmdserver. Will start a new server on demand. */ |
|
288 | /* Connect to a cmdserver. Will start a new server on demand. */ | |
286 | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) |
|
289 | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) | |
287 | { |
|
290 | { | |
288 | const char *sockname = |
|
291 | const char *sockname = | |
289 | opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname; |
|
292 | opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname; | |
290 | debugmsg("try connect to %s", sockname); |
|
293 | debugmsg("try connect to %s", sockname); | |
291 | hgclient_t *hgc = hgc_open(sockname); |
|
294 | hgclient_t *hgc = hgc_open(sockname); | |
292 | if (hgc) |
|
295 | if (hgc) | |
293 | return hgc; |
|
296 | return hgc; | |
294 |
|
297 | |||
295 | /* prevent us from being connected to an outdated server: we were |
|
298 | /* prevent us from being connected to an outdated server: we were | |
296 | * told by a server to redirect to opts->redirectsockname and that |
|
299 | * told by a server to redirect to opts->redirectsockname and that | |
297 | * address does not work. we do not want to connect to the server |
|
300 | * address does not work. we do not want to connect to the server | |
298 | * again because it will probably tell us the same thing. */ |
|
301 | * again because it will probably tell us the same thing. */ | |
299 | if (sockname == opts->redirectsockname) |
|
302 | if (sockname == opts->redirectsockname) | |
300 | unlink(opts->sockname); |
|
303 | unlink(opts->sockname); | |
301 |
|
304 | |||
302 | debugmsg("start cmdserver at %s", opts->initsockname); |
|
305 | debugmsg("start cmdserver at %s", opts->initsockname); | |
303 |
|
306 | |||
304 | pid_t pid = fork(); |
|
307 | pid_t pid = fork(); | |
305 | if (pid < 0) |
|
308 | if (pid < 0) | |
306 | abortmsg("failed to fork cmdserver process"); |
|
309 | abortmsg("failed to fork cmdserver process"); | |
307 | if (pid == 0) { |
|
310 | if (pid == 0) { | |
308 | execcmdserver(opts); |
|
311 | execcmdserver(opts); | |
309 | } else { |
|
312 | } else { | |
310 | hgc = retryconnectcmdserver(opts, pid); |
|
313 | hgc = retryconnectcmdserver(opts, pid); | |
311 | } |
|
314 | } | |
312 |
|
315 | |||
313 | return hgc; |
|
316 | return hgc; | |
314 | } |
|
317 | } | |
315 |
|
318 | |||
316 | static void killcmdserver(const struct cmdserveropts *opts) |
|
319 | static void killcmdserver(const struct cmdserveropts *opts) | |
317 | { |
|
320 | { | |
318 | /* resolve config hash */ |
|
321 | /* resolve config hash */ | |
319 | char *resolvedpath = realpath(opts->sockname, NULL); |
|
322 | char *resolvedpath = realpath(opts->sockname, NULL); | |
320 | if (resolvedpath) { |
|
323 | if (resolvedpath) { | |
321 | unlink(resolvedpath); |
|
324 | unlink(resolvedpath); | |
322 | free(resolvedpath); |
|
325 | free(resolvedpath); | |
323 | } |
|
326 | } | |
324 | } |
|
327 | } | |
325 |
|
328 | |||
326 | /* Run instructions sent from the server like unlink and set redirect path |
|
329 | /* Run instructions sent from the server like unlink and set redirect path | |
327 | * Return 1 if reconnect is needed, otherwise 0 */ |
|
330 | * Return 1 if reconnect is needed, otherwise 0 */ | |
328 | static int runinstructions(struct cmdserveropts *opts, const char **insts) |
|
331 | static int runinstructions(struct cmdserveropts *opts, const char **insts) | |
329 | { |
|
332 | { | |
330 | int needreconnect = 0; |
|
333 | int needreconnect = 0; | |
331 | if (!insts) |
|
334 | if (!insts) | |
332 | return needreconnect; |
|
335 | return needreconnect; | |
333 |
|
336 | |||
334 | assert(insts); |
|
337 | assert(insts); | |
335 | opts->redirectsockname[0] = '\0'; |
|
338 | opts->redirectsockname[0] = '\0'; | |
336 | const char **pinst; |
|
339 | const char **pinst; | |
337 | for (pinst = insts; *pinst; pinst++) { |
|
340 | for (pinst = insts; *pinst; pinst++) { | |
338 | debugmsg("instruction: %s", *pinst); |
|
341 | debugmsg("instruction: %s", *pinst); | |
339 | if (strncmp(*pinst, "unlink ", 7) == 0) { |
|
342 | if (strncmp(*pinst, "unlink ", 7) == 0) { | |
340 | unlink(*pinst + 7); |
|
343 | unlink(*pinst + 7); | |
341 | } else if (strncmp(*pinst, "redirect ", 9) == 0) { |
|
344 | } else if (strncmp(*pinst, "redirect ", 9) == 0) { | |
342 | int r = snprintf(opts->redirectsockname, |
|
345 | int r = snprintf(opts->redirectsockname, | |
343 | sizeof(opts->redirectsockname), "%s", |
|
346 | sizeof(opts->redirectsockname), "%s", | |
344 | *pinst + 9); |
|
347 | *pinst + 9); | |
345 | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) |
|
348 | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) | |
346 | abortmsg("redirect path is too long (%d)", r); |
|
349 | abortmsg("redirect path is too long (%d)", r); | |
347 | needreconnect = 1; |
|
350 | needreconnect = 1; | |
348 | } else if (strncmp(*pinst, "exit ", 5) == 0) { |
|
351 | } else if (strncmp(*pinst, "exit ", 5) == 0) { | |
349 | int n = 0; |
|
352 | int n = 0; | |
350 | if (sscanf(*pinst + 5, "%d", &n) != 1) |
|
353 | if (sscanf(*pinst + 5, "%d", &n) != 1) | |
351 | abortmsg("cannot read the exit code"); |
|
354 | abortmsg("cannot read the exit code"); | |
352 | exit(n); |
|
355 | exit(n); | |
353 | } else if (strcmp(*pinst, "reconnect") == 0) { |
|
356 | } else if (strcmp(*pinst, "reconnect") == 0) { | |
354 | needreconnect = 1; |
|
357 | needreconnect = 1; | |
355 | } else { |
|
358 | } else { | |
356 | abortmsg("unknown instruction: %s", *pinst); |
|
359 | abortmsg("unknown instruction: %s", *pinst); | |
357 | } |
|
360 | } | |
358 | } |
|
361 | } | |
359 | return needreconnect; |
|
362 | return needreconnect; | |
360 | } |
|
363 | } | |
361 |
|
364 | |||
362 | /* |
|
365 | /* | |
363 | * Test whether the command is unsupported or not. This is not designed to |
|
366 | * Test whether the command is unsupported or not. This is not designed to | |
364 | * cover all cases. But it's fast, does not depend on the server and does |
|
367 | * cover all cases. But it's fast, does not depend on the server and does | |
365 | * not return false positives. |
|
368 | * not return false positives. | |
366 | */ |
|
369 | */ | |
367 | static int isunsupported(int argc, const char *argv[]) |
|
370 | static int isunsupported(int argc, const char *argv[]) | |
368 | { |
|
371 | { | |
369 | enum { SERVE = 1, |
|
372 | enum { SERVE = 1, | |
370 | DAEMON = 2, |
|
373 | DAEMON = 2, | |
371 | SERVEDAEMON = SERVE | DAEMON, |
|
374 | SERVEDAEMON = SERVE | DAEMON, | |
372 | }; |
|
375 | }; | |
373 | unsigned int state = 0; |
|
376 | unsigned int state = 0; | |
374 | int i; |
|
377 | int i; | |
375 | for (i = 0; i < argc; ++i) { |
|
378 | for (i = 0; i < argc; ++i) { | |
376 | if (strcmp(argv[i], "--") == 0) |
|
379 | if (strcmp(argv[i], "--") == 0) | |
377 | break; |
|
380 | break; | |
378 | if (i == 0 && strcmp("serve", argv[i]) == 0) |
|
381 | if (i == 0 && strcmp("serve", argv[i]) == 0) | |
379 | state |= SERVE; |
|
382 | state |= SERVE; | |
380 | else if (strcmp("-d", argv[i]) == 0 || |
|
383 | else if (strcmp("-d", argv[i]) == 0 || | |
381 | strcmp("--daemon", argv[i]) == 0) |
|
384 | strcmp("--daemon", argv[i]) == 0) | |
382 | state |= DAEMON; |
|
385 | state |= DAEMON; | |
383 | } |
|
386 | } | |
384 | return (state & SERVEDAEMON) == SERVEDAEMON; |
|
387 | return (state & SERVEDAEMON) == SERVEDAEMON; | |
385 | } |
|
388 | } | |
386 |
|
389 | |||
387 | static void execoriginalhg(const char *argv[]) |
|
390 | static void execoriginalhg(const char *argv[]) | |
388 | { |
|
391 | { | |
389 | debugmsg("execute original hg"); |
|
392 | debugmsg("execute original hg"); | |
390 | if (execvp(gethgcmd(), (char **)argv) < 0) |
|
393 | if (execvp(gethgcmd(), (char **)argv) < 0) | |
391 | abortmsgerrno("failed to exec original hg"); |
|
394 | abortmsgerrno("failed to exec original hg"); | |
392 | } |
|
395 | } | |
393 |
|
396 | |||
394 | int main(int argc, const char *argv[], const char *envp[]) |
|
397 | int main(int argc, const char *argv[], const char *envp[]) | |
395 | { |
|
398 | { | |
396 | if (getenv("CHGDEBUG")) |
|
399 | if (getenv("CHGDEBUG")) | |
397 | enabledebugmsg(); |
|
400 | enabledebugmsg(); | |
398 |
|
401 | |||
399 | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) |
|
402 | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) | |
400 | enablecolor(); |
|
403 | enablecolor(); | |
401 |
|
404 | |||
402 | if (getenv("CHGINTERNALMARK")) |
|
405 | if (getenv("CHGINTERNALMARK")) | |
403 | abortmsg("chg started by chg detected.\n" |
|
406 | abortmsg("chg started by chg detected.\n" | |
404 | "Please make sure ${HG:-hg} is not a symlink or " |
|
407 | "Please make sure ${HG:-hg} is not a symlink or " | |
405 | "wrapper to chg. Alternatively, set $CHGHG to the " |
|
408 | "wrapper to chg. Alternatively, set $CHGHG to the " | |
406 | "path of real hg."); |
|
409 | "path of real hg."); | |
407 |
|
410 | |||
408 | if (isunsupported(argc - 1, argv + 1)) |
|
411 | if (isunsupported(argc - 1, argv + 1)) | |
409 | execoriginalhg(argv); |
|
412 | execoriginalhg(argv); | |
410 |
|
413 | |||
411 | struct cmdserveropts opts; |
|
414 | struct cmdserveropts opts; | |
412 | initcmdserveropts(&opts); |
|
415 | initcmdserveropts(&opts); | |
413 | setcmdserveropts(&opts); |
|
416 | setcmdserveropts(&opts); | |
414 | setcmdserverargs(&opts, argc, argv); |
|
417 | setcmdserverargs(&opts, argc, argv); | |
415 |
|
418 | |||
416 | if (argc == 2) { |
|
419 | if (argc == 2) { | |
417 | if (strcmp(argv[1], "--kill-chg-daemon") == 0) { |
|
420 | if (strcmp(argv[1], "--kill-chg-daemon") == 0) { | |
418 | killcmdserver(&opts); |
|
421 | killcmdserver(&opts); | |
419 | return 0; |
|
422 | return 0; | |
420 | } |
|
423 | } | |
421 | } |
|
424 | } | |
422 |
|
425 | |||
423 | hgclient_t *hgc; |
|
426 | hgclient_t *hgc; | |
424 | size_t retry = 0; |
|
427 | size_t retry = 0; | |
425 | while (1) { |
|
428 | while (1) { | |
426 | hgc = connectcmdserver(&opts); |
|
429 | hgc = connectcmdserver(&opts); | |
427 | if (!hgc) |
|
430 | if (!hgc) | |
428 | abortmsg("cannot open hg client"); |
|
431 | abortmsg("cannot open hg client"); | |
429 | hgc_setenv(hgc, envp); |
|
432 | hgc_setenv(hgc, envp); | |
430 | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); |
|
433 | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); | |
431 | int needreconnect = runinstructions(&opts, insts); |
|
434 | int needreconnect = runinstructions(&opts, insts); | |
432 | free(insts); |
|
435 | free(insts); | |
433 | if (!needreconnect) |
|
436 | if (!needreconnect) | |
434 | break; |
|
437 | break; | |
435 | hgc_close(hgc); |
|
438 | hgc_close(hgc); | |
436 | if (++retry > 10) |
|
439 | if (++retry > 10) | |
437 | abortmsg("too many redirections.\n" |
|
440 | abortmsg("too many redirections.\n" | |
438 | "Please make sure %s is not a wrapper which " |
|
441 | "Please make sure %s is not a wrapper which " | |
439 | "changes sensitive environment variables " |
|
442 | "changes sensitive environment variables " | |
440 | "before executing hg. If you have to use a " |
|
443 | "before executing hg. If you have to use a " | |
441 | "wrapper, wrap chg instead of hg.", |
|
444 | "wrapper, wrap chg instead of hg.", | |
442 | gethgcmd()); |
|
445 | gethgcmd()); | |
443 | } |
|
446 | } | |
444 |
|
447 | |||
445 | setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); |
|
448 | setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); | |
446 | atexit(waitpager); |
|
449 | atexit(waitpager); | |
447 | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); |
|
450 | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); | |
448 | restoresignalhandler(); |
|
451 | restoresignalhandler(); | |
449 | hgc_close(hgc); |
|
452 | hgc_close(hgc); | |
450 | freecmdserveropts(&opts); |
|
453 | freecmdserveropts(&opts); | |
451 |
|
454 | |||
452 | return exitcode; |
|
455 | return exitcode; | |
453 | } |
|
456 | } |
General Comments 0
You need to be logged in to leave comments.
Login now