Show More
@@ -1,543 +1,547 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 <dirent.h> |
|
11 | #include <dirent.h> | |
12 | #include <errno.h> |
|
12 | #include <errno.h> | |
13 | #include <fcntl.h> |
|
13 | #include <fcntl.h> | |
14 | #include <signal.h> |
|
14 | #include <signal.h> | |
15 | #include <stdio.h> |
|
15 | #include <stdio.h> | |
16 | #include <stdlib.h> |
|
16 | #include <stdlib.h> | |
17 | #include <string.h> |
|
17 | #include <string.h> | |
18 | #include <sys/file.h> |
|
18 | #include <sys/file.h> | |
19 | #include <sys/stat.h> |
|
19 | #include <sys/stat.h> | |
20 | #include <sys/types.h> |
|
20 | #include <sys/types.h> | |
21 | #include <sys/un.h> |
|
21 | #include <sys/un.h> | |
22 | #include <sys/wait.h> |
|
22 | #include <sys/wait.h> | |
23 | #include <time.h> |
|
23 | #include <time.h> | |
24 | #include <unistd.h> |
|
24 | #include <unistd.h> | |
25 |
|
25 | |||
26 | #include "hgclient.h" |
|
26 | #include "hgclient.h" | |
27 | #include "procutil.h" |
|
27 | #include "procutil.h" | |
28 | #include "util.h" |
|
28 | #include "util.h" | |
29 |
|
29 | |||
30 | #ifndef PATH_MAX |
|
30 | #ifndef PATH_MAX | |
31 | #define PATH_MAX 4096 |
|
31 | #define PATH_MAX 4096 | |
32 | #endif |
|
32 | #endif | |
33 |
|
33 | |||
34 | struct cmdserveropts { |
|
34 | struct cmdserveropts { | |
35 | char sockname[PATH_MAX]; |
|
35 | char sockname[PATH_MAX]; | |
36 | char initsockname[PATH_MAX]; |
|
36 | char initsockname[PATH_MAX]; | |
37 | char redirectsockname[PATH_MAX]; |
|
37 | char redirectsockname[PATH_MAX]; | |
38 | size_t argsize; |
|
38 | size_t argsize; | |
39 | const char **args; |
|
39 | const char **args; | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | static void initcmdserveropts(struct cmdserveropts *opts) |
|
42 | static void initcmdserveropts(struct cmdserveropts *opts) | |
43 | { |
|
43 | { | |
44 | memset(opts, 0, sizeof(struct cmdserveropts)); |
|
44 | memset(opts, 0, sizeof(struct cmdserveropts)); | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | static void freecmdserveropts(struct cmdserveropts *opts) |
|
47 | static void freecmdserveropts(struct cmdserveropts *opts) | |
48 | { |
|
48 | { | |
49 | free(opts->args); |
|
49 | free(opts->args); | |
50 | opts->args = NULL; |
|
50 | opts->args = NULL; | |
51 | opts->argsize = 0; |
|
51 | opts->argsize = 0; | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | /* |
|
54 | /* | |
55 | * Test if an argument is a sensitive flag that should be passed to the server. |
|
55 | * Test if an argument is a sensitive flag that should be passed to the server. | |
56 | * Return 0 if not, otherwise the number of arguments starting from the current |
|
56 | * Return 0 if not, otherwise the number of arguments starting from the current | |
57 | * one that should be passed to the server. |
|
57 | * one that should be passed to the server. | |
58 | */ |
|
58 | */ | |
59 | static size_t testsensitiveflag(const char *arg) |
|
59 | static size_t testsensitiveflag(const char *arg) | |
60 | { |
|
60 | { | |
61 | static const struct { |
|
61 | static const struct { | |
62 | const char *name; |
|
62 | const char *name; | |
63 | size_t narg; |
|
63 | size_t narg; | |
64 | } flags[] = { |
|
64 | } flags[] = { | |
65 | {"--config", 1}, {"--cwd", 1}, {"--repo", 1}, |
|
65 | {"--config", 1}, {"--cwd", 1}, {"--repo", 1}, | |
66 | {"--repository", 1}, {"--traceback", 0}, {"-R", 1}, |
|
66 | {"--repository", 1}, {"--traceback", 0}, {"-R", 1}, | |
67 | }; |
|
67 | }; | |
68 | size_t i; |
|
68 | size_t i; | |
69 | for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) { |
|
69 | for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) { | |
70 | size_t len = strlen(flags[i].name); |
|
70 | size_t len = strlen(flags[i].name); | |
71 | size_t narg = flags[i].narg; |
|
71 | size_t narg = flags[i].narg; | |
72 | if (memcmp(arg, flags[i].name, len) == 0) { |
|
72 | if (memcmp(arg, flags[i].name, len) == 0) { | |
73 | if (arg[len] == '\0') { |
|
73 | if (arg[len] == '\0') { | |
74 | /* --flag (value) */ |
|
74 | /* --flag (value) */ | |
75 | return narg + 1; |
|
75 | return narg + 1; | |
76 | } else if (arg[len] == '=' && narg > 0) { |
|
76 | } else if (arg[len] == '=' && narg > 0) { | |
77 | /* --flag=value */ |
|
77 | /* --flag=value */ | |
78 | return 1; |
|
78 | return 1; | |
79 | } else if (flags[i].name[1] != '-') { |
|
79 | } else if (flags[i].name[1] != '-') { | |
80 | /* short flag */ |
|
80 | /* 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, int argc, |
|
91 | static void setcmdserverargs(struct cmdserveropts *opts, int argc, | |
92 | const char *argv[]) |
|
92 | 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 = |
|
104 | opts->args = | |
105 | reallocx(opts->args, (n + opts->argsize) * sizeof(char *)); |
|
105 | reallocx(opts->args, (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 | abortmsgerrno("cannot create sockdir %s", sockdir); |
|
118 | abortmsgerrno("cannot create sockdir %s", sockdir); | |
119 |
|
119 | |||
120 | struct stat st; |
|
120 | struct stat st; | |
121 | r = lstat(sockdir, &st); |
|
121 | r = lstat(sockdir, &st); | |
122 | if (r < 0) |
|
122 | if (r < 0) | |
123 | abortmsgerrno("cannot stat %s", sockdir); |
|
123 | abortmsgerrno("cannot stat %s", sockdir); | |
124 | if (!S_ISDIR(st.st_mode)) |
|
124 | if (!S_ISDIR(st.st_mode)) | |
125 | abortmsg("cannot create sockdir %s (file exists)", sockdir); |
|
125 | abortmsg("cannot create sockdir %s (file exists)", sockdir); | |
126 | if (st.st_uid != geteuid() || st.st_mode & 0077) |
|
126 | if (st.st_uid != geteuid() || st.st_mode & 0077) | |
127 | abortmsg("insecure sockdir %s", sockdir); |
|
127 | abortmsg("insecure sockdir %s", sockdir); | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | /* |
|
130 | /* | |
131 | * Check if a socket directory exists and is only owned by the current user. |
|
131 | * Check if a socket directory exists and is only owned by the current user. | |
132 | * Return 1 if so, 0 if not. This is used to check if XDG_RUNTIME_DIR can be |
|
132 | * Return 1 if so, 0 if not. This is used to check if XDG_RUNTIME_DIR can be | |
133 | * used or not. According to the specification [1], XDG_RUNTIME_DIR should be |
|
133 | * used or not. According to the specification [1], XDG_RUNTIME_DIR should be | |
134 | * ignored if the directory is not owned by the user with mode 0700. |
|
134 | * ignored if the directory is not owned by the user with mode 0700. | |
135 | * [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
|
135 | * [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | |
136 | */ |
|
136 | */ | |
137 | static int checkruntimedir(const char *sockdir) |
|
137 | static int checkruntimedir(const char *sockdir) | |
138 | { |
|
138 | { | |
139 | struct stat st; |
|
139 | struct stat st; | |
140 | int r = lstat(sockdir, &st); |
|
140 | int r = lstat(sockdir, &st); | |
141 | if (r < 0) /* ex. does not exist */ |
|
141 | if (r < 0) /* ex. does not exist */ | |
142 | return 0; |
|
142 | return 0; | |
143 | if (!S_ISDIR(st.st_mode)) /* ex. is a file, not a directory */ |
|
143 | if (!S_ISDIR(st.st_mode)) /* ex. is a file, not a directory */ | |
144 | return 0; |
|
144 | return 0; | |
145 | return st.st_uid == geteuid() && (st.st_mode & 0777) == 0700; |
|
145 | return st.st_uid == geteuid() && (st.st_mode & 0777) == 0700; | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | static void getdefaultsockdir(char sockdir[], size_t size) |
|
148 | static void getdefaultsockdir(char sockdir[], size_t size) | |
149 | { |
|
149 | { | |
150 | /* by default, put socket file in secure directory |
|
150 | /* by default, put socket file in secure directory | |
151 | * (${XDG_RUNTIME_DIR}/chg, or /${TMPDIR:-tmp}/chg$UID) |
|
151 | * (${XDG_RUNTIME_DIR}/chg, or /${TMPDIR:-tmp}/chg$UID) | |
152 | * (permission of socket file may be ignored on some Unices) */ |
|
152 | * (permission of socket file may be ignored on some Unices) */ | |
153 | const char *runtimedir = getenv("XDG_RUNTIME_DIR"); |
|
153 | const char *runtimedir = getenv("XDG_RUNTIME_DIR"); | |
154 | int r; |
|
154 | int r; | |
155 | if (runtimedir && checkruntimedir(runtimedir)) { |
|
155 | if (runtimedir && checkruntimedir(runtimedir)) { | |
156 | r = snprintf(sockdir, size, "%s/chg", runtimedir); |
|
156 | r = snprintf(sockdir, size, "%s/chg", runtimedir); | |
157 | } else { |
|
157 | } else { | |
158 | const char *tmpdir = getenv("TMPDIR"); |
|
158 | const char *tmpdir = getenv("TMPDIR"); | |
159 | if (!tmpdir) |
|
159 | if (!tmpdir) | |
160 | tmpdir = "/tmp"; |
|
160 | tmpdir = "/tmp"; | |
161 | r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid()); |
|
161 | r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid()); | |
162 | } |
|
162 | } | |
163 | if (r < 0 || (size_t)r >= size) |
|
163 | if (r < 0 || (size_t)r >= size) | |
164 | abortmsg("too long TMPDIR (r = %d)", r); |
|
164 | abortmsg("too long TMPDIR (r = %d)", r); | |
165 | } |
|
165 | } | |
166 |
|
166 | |||
167 | static void setcmdserveropts(struct cmdserveropts *opts) |
|
167 | static void setcmdserveropts(struct cmdserveropts *opts) | |
168 | { |
|
168 | { | |
169 | int r; |
|
169 | int r; | |
170 | char sockdir[PATH_MAX]; |
|
170 | char sockdir[PATH_MAX]; | |
171 | const char *envsockname = getenv("CHGSOCKNAME"); |
|
171 | const char *envsockname = getenv("CHGSOCKNAME"); | |
172 | if (!envsockname) { |
|
172 | if (!envsockname) { | |
173 | getdefaultsockdir(sockdir, sizeof(sockdir)); |
|
173 | getdefaultsockdir(sockdir, sizeof(sockdir)); | |
174 | preparesockdir(sockdir); |
|
174 | preparesockdir(sockdir); | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | const char *basename = (envsockname) ? envsockname : sockdir; |
|
177 | const char *basename = (envsockname) ? envsockname : sockdir; | |
178 | const char *sockfmt = (envsockname) ? "%s" : "%s/server"; |
|
178 | const char *sockfmt = (envsockname) ? "%s" : "%s/server"; | |
179 | r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); |
|
179 | r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); | |
180 | if (r < 0 || (size_t)r >= sizeof(opts->sockname)) |
|
180 | if (r < 0 || (size_t)r >= sizeof(opts->sockname)) | |
181 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
181 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | |
182 | r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u", |
|
182 | r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u", | |
183 | opts->sockname, (unsigned)getpid()); |
|
183 | opts->sockname, (unsigned)getpid()); | |
184 | if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) |
|
184 | if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) | |
185 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
185 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | /* If the current program is, say, /a/b/c/chg, returns /a/b/c/hg. */ |
|
188 | /* If the current program is, say, /a/b/c/chg, returns /a/b/c/hg. */ | |
189 | static char *getrelhgcmd(void) |
|
189 | static char *getrelhgcmd(void) | |
190 | { |
|
190 | { | |
191 | ssize_t n; |
|
191 | ssize_t n; | |
192 | char *res, *slash; |
|
192 | char *res, *slash; | |
193 | int maxsize = 4096; |
|
193 | int maxsize = 4096; | |
194 | res = malloc(maxsize); |
|
194 | res = malloc(maxsize); | |
195 | if (res == NULL) |
|
195 | if (res == NULL) | |
196 | goto cleanup; |
|
196 | goto cleanup; | |
197 | n = readlink("/proc/self/exe", res, maxsize); |
|
197 | n = readlink("/proc/self/exe", res, maxsize); | |
198 | if (n < 0 || n >= maxsize) |
|
198 | if (n < 0 || n >= maxsize) | |
199 | goto cleanup; |
|
199 | goto cleanup; | |
200 | res[n] = '\0'; |
|
200 | res[n] = '\0'; | |
201 | slash = strrchr(res, '/'); |
|
201 | slash = strrchr(res, '/'); | |
202 | if (slash == NULL) |
|
202 | if (slash == NULL) | |
203 | goto cleanup; |
|
203 | goto cleanup; | |
204 | /* 4 is strlen("/hg") + nul byte */ |
|
204 | /* 4 is strlen("/hg") + nul byte */ | |
205 | if (slash + 4 >= res + maxsize) |
|
205 | if (slash + 4 >= res + maxsize) | |
206 | goto cleanup; |
|
206 | goto cleanup; | |
207 | memcpy(slash, "/hg", 4); |
|
207 | memcpy(slash, "/hg", 4); | |
208 | return res; |
|
208 | return res; | |
209 | cleanup: |
|
209 | cleanup: | |
210 | free(res); |
|
210 | free(res); | |
211 | return NULL; |
|
211 | return NULL; | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 | static const char *gethgcmd(void) |
|
214 | static const char *gethgcmd(void) | |
215 | { |
|
215 | { | |
216 | static const char *hgcmd = NULL; |
|
216 | static const char *hgcmd = NULL; | |
217 | #ifdef HGPATHREL |
|
217 | #ifdef HGPATHREL | |
218 | int tryrelhgcmd = 1; |
|
218 | int tryrelhgcmd = 1; | |
219 | #else |
|
219 | #else | |
220 | int tryrelhgcmd = 0; |
|
220 | int tryrelhgcmd = 0; | |
221 | #endif |
|
221 | #endif | |
222 | if (!hgcmd) { |
|
222 | if (!hgcmd) { | |
223 | hgcmd = getenv("CHGHG"); |
|
223 | hgcmd = getenv("CHGHG"); | |
224 | if (!hgcmd || hgcmd[0] == '\0') |
|
224 | if (!hgcmd || hgcmd[0] == '\0') | |
225 | hgcmd = getenv("HG"); |
|
225 | hgcmd = getenv("HG"); | |
226 | if (tryrelhgcmd && (!hgcmd || hgcmd[0] == '\0')) |
|
226 | if (tryrelhgcmd && (!hgcmd || hgcmd[0] == '\0')) | |
227 | hgcmd = getrelhgcmd(); |
|
227 | hgcmd = getrelhgcmd(); | |
228 | if (!hgcmd || hgcmd[0] == '\0') |
|
228 | if (!hgcmd || hgcmd[0] == '\0') | |
229 | #ifdef HGPATH |
|
229 | #ifdef HGPATH | |
230 | hgcmd = (HGPATH); |
|
230 | hgcmd = (HGPATH); | |
231 | #else |
|
231 | #else | |
232 | hgcmd = "hg"; |
|
232 | hgcmd = "hg"; | |
233 | #endif |
|
233 | #endif | |
234 | } |
|
234 | } | |
235 | return hgcmd; |
|
235 | return hgcmd; | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | static void execcmdserver(const struct cmdserveropts *opts) |
|
238 | static void execcmdserver(const struct cmdserveropts *opts) | |
239 | { |
|
239 | { | |
240 | const char *hgcmd = gethgcmd(); |
|
240 | const char *hgcmd = gethgcmd(); | |
241 |
|
241 | |||
242 | const char *baseargv[] = { |
|
242 | const char *baseargv[] = { | |
243 | hgcmd, |
|
243 | hgcmd, | |
244 | "serve", |
|
244 | "serve", | |
245 | "--cmdserver", |
|
245 | "--cmdserver", | |
246 | "chgunix", |
|
246 | "chgunix", | |
247 | "--address", |
|
247 | "--address", | |
248 | opts->initsockname, |
|
248 | opts->initsockname, | |
249 | "--daemon-postexec", |
|
249 | "--daemon-postexec", | |
250 | "chdir:/", |
|
250 | "chdir:/", | |
251 | }; |
|
251 | }; | |
252 | size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); |
|
252 | size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); | |
253 | size_t argsize = baseargvsize + opts->argsize + 1; |
|
253 | size_t argsize = baseargvsize + opts->argsize + 1; | |
254 |
|
254 | |||
255 | const char **argv = mallocx(sizeof(char *) * argsize); |
|
255 | const char **argv = mallocx(sizeof(char *) * argsize); | |
256 | memcpy(argv, baseargv, sizeof(baseargv)); |
|
256 | memcpy(argv, baseargv, sizeof(baseargv)); | |
257 | if (opts->args) { |
|
257 | if (opts->args) { | |
258 | size_t size = sizeof(char *) * opts->argsize; |
|
258 | size_t size = sizeof(char *) * opts->argsize; | |
259 | memcpy(argv + baseargvsize, opts->args, size); |
|
259 | memcpy(argv + baseargvsize, opts->args, size); | |
260 | } |
|
260 | } | |
261 | argv[argsize - 1] = NULL; |
|
261 | argv[argsize - 1] = NULL; | |
262 |
|
262 | |||
263 | const char *lc_ctype_env = getenv("LC_CTYPE"); |
|
263 | const char *lc_ctype_env = getenv("LC_CTYPE"); | |
264 | if (lc_ctype_env == NULL) { |
|
264 | if (lc_ctype_env == NULL) { | |
265 | if (putenv("CHG_CLEAR_LC_CTYPE=") != 0) |
|
265 | if (putenv("CHG_CLEAR_LC_CTYPE=") != 0) | |
266 | abortmsgerrno("failed to putenv CHG_CLEAR_LC_CTYPE"); |
|
266 | abortmsgerrno("failed to putenv CHG_CLEAR_LC_CTYPE"); | |
267 | } else { |
|
267 | } else { | |
268 | if (setenv("CHGORIG_LC_CTYPE", lc_ctype_env, 1) != 0) { |
|
268 | if (setenv("CHGORIG_LC_CTYPE", lc_ctype_env, 1) != 0) { | |
269 | abortmsgerrno("failed to setenv CHGORIG_LC_CTYPE"); |
|
269 | abortmsgerrno("failed to setenv CHGORIG_LC_CTYPE"); | |
270 | } |
|
270 | } | |
271 | } |
|
271 | } | |
272 |
|
272 | |||
273 | /* close any open files to avoid hanging locks */ |
|
273 | /* close any open files to avoid hanging locks */ | |
274 | DIR *dp = opendir("/proc/self/fd"); |
|
274 | DIR *dp = opendir("/proc/self/fd"); | |
275 | if (dp != NULL) { |
|
275 | if (dp != NULL) { | |
276 | debugmsg("closing files based on /proc contents"); |
|
276 | debugmsg("closing files based on /proc contents"); | |
277 | struct dirent *de; |
|
277 | struct dirent *de; | |
278 | while ((de = readdir(dp))) { |
|
278 | while ((de = readdir(dp))) { | |
279 | char *end; |
|
279 | char *end; | |
280 | long fd_value = strtol(de->d_name, &end, 10); |
|
280 | long fd_value = strtol(de->d_name, &end, 10); | |
281 | if (end == de->d_name) { |
|
281 | if (end == de->d_name) { | |
282 | /* unable to convert to int (. or ..) */ |
|
282 | /* unable to convert to int (. or ..) */ | |
283 | continue; |
|
283 | continue; | |
284 | } |
|
284 | } | |
285 | if (errno == ERANGE) { |
|
285 | if (errno == ERANGE) { | |
286 |
debugmsg("tried to parse %s, but range error |
|
286 | debugmsg("tried to parse %s, but range error " | |
|
287 | "occurred", | |||
|
288 | de->d_name); | |||
287 | continue; |
|
289 | continue; | |
288 | } |
|
290 | } | |
289 | if (fd_value > STDERR_FILENO) { |
|
291 | if (fd_value > STDERR_FILENO) { | |
290 | int res = close(fd_value); |
|
292 | int res = close(fd_value); | |
291 | if (res) { |
|
293 | if (res) { | |
292 |
debugmsg("tried to close fd %ld: %d |
|
294 | debugmsg("tried to close fd %ld: %d " | |
|
295 | "(errno: %d)", | |||
|
296 | fd_value, res, errno); | |||
293 | } |
|
297 | } | |
294 | } |
|
298 | } | |
295 | } |
|
299 | } | |
296 | } |
|
300 | } | |
297 |
|
301 | |||
298 | if (putenv("CHGINTERNALMARK=") != 0) |
|
302 | if (putenv("CHGINTERNALMARK=") != 0) | |
299 | abortmsgerrno("failed to putenv"); |
|
303 | abortmsgerrno("failed to putenv"); | |
300 | if (execvp(hgcmd, (char **)argv) < 0) |
|
304 | if (execvp(hgcmd, (char **)argv) < 0) | |
301 | abortmsgerrno("failed to exec cmdserver"); |
|
305 | abortmsgerrno("failed to exec cmdserver"); | |
302 | free(argv); |
|
306 | free(argv); | |
303 | } |
|
307 | } | |
304 |
|
308 | |||
305 | /* Retry until we can connect to the server. Give up after some time. */ |
|
309 | /* Retry until we can connect to the server. Give up after some time. */ | |
306 | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) |
|
310 | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) | |
307 | { |
|
311 | { | |
308 | static const struct timespec sleepreq = {0, 10 * 1000000}; |
|
312 | static const struct timespec sleepreq = {0, 10 * 1000000}; | |
309 | int pst = 0; |
|
313 | int pst = 0; | |
310 |
|
314 | |||
311 | debugmsg("try connect to %s repeatedly", opts->initsockname); |
|
315 | debugmsg("try connect to %s repeatedly", opts->initsockname); | |
312 |
|
316 | |||
313 | unsigned int timeoutsec = 60; /* default: 60 seconds */ |
|
317 | unsigned int timeoutsec = 60; /* default: 60 seconds */ | |
314 | const char *timeoutenv = getenv("CHGTIMEOUT"); |
|
318 | const char *timeoutenv = getenv("CHGTIMEOUT"); | |
315 | if (timeoutenv) |
|
319 | if (timeoutenv) | |
316 | sscanf(timeoutenv, "%u", &timeoutsec); |
|
320 | sscanf(timeoutenv, "%u", &timeoutsec); | |
317 |
|
321 | |||
318 | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { |
|
322 | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { | |
319 | hgclient_t *hgc = hgc_open(opts->initsockname); |
|
323 | hgclient_t *hgc = hgc_open(opts->initsockname); | |
320 | if (hgc) { |
|
324 | if (hgc) { | |
321 | debugmsg("rename %s to %s", opts->initsockname, |
|
325 | debugmsg("rename %s to %s", opts->initsockname, | |
322 | opts->sockname); |
|
326 | opts->sockname); | |
323 | int r = rename(opts->initsockname, opts->sockname); |
|
327 | int r = rename(opts->initsockname, opts->sockname); | |
324 | if (r != 0) |
|
328 | if (r != 0) | |
325 | abortmsgerrno("cannot rename"); |
|
329 | abortmsgerrno("cannot rename"); | |
326 | return hgc; |
|
330 | return hgc; | |
327 | } |
|
331 | } | |
328 |
|
332 | |||
329 | if (pid > 0) { |
|
333 | if (pid > 0) { | |
330 | /* collect zombie if child process fails to start */ |
|
334 | /* collect zombie if child process fails to start */ | |
331 | int r = waitpid(pid, &pst, WNOHANG); |
|
335 | int r = waitpid(pid, &pst, WNOHANG); | |
332 | if (r != 0) |
|
336 | if (r != 0) | |
333 | goto cleanup; |
|
337 | goto cleanup; | |
334 | } |
|
338 | } | |
335 |
|
339 | |||
336 | nanosleep(&sleepreq, NULL); |
|
340 | nanosleep(&sleepreq, NULL); | |
337 | } |
|
341 | } | |
338 |
|
342 | |||
339 | abortmsg("timed out waiting for cmdserver %s", opts->initsockname); |
|
343 | abortmsg("timed out waiting for cmdserver %s", opts->initsockname); | |
340 | return NULL; |
|
344 | return NULL; | |
341 |
|
345 | |||
342 | cleanup: |
|
346 | cleanup: | |
343 | if (WIFEXITED(pst)) { |
|
347 | if (WIFEXITED(pst)) { | |
344 | if (WEXITSTATUS(pst) == 0) |
|
348 | if (WEXITSTATUS(pst) == 0) | |
345 | abortmsg("could not connect to cmdserver " |
|
349 | abortmsg("could not connect to cmdserver " | |
346 | "(exited with status 0)"); |
|
350 | "(exited with status 0)"); | |
347 | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); |
|
351 | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); | |
348 | exit(WEXITSTATUS(pst)); |
|
352 | exit(WEXITSTATUS(pst)); | |
349 | } else if (WIFSIGNALED(pst)) { |
|
353 | } else if (WIFSIGNALED(pst)) { | |
350 | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); |
|
354 | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); | |
351 | } else { |
|
355 | } else { | |
352 | abortmsg("error while waiting for cmdserver"); |
|
356 | abortmsg("error while waiting for cmdserver"); | |
353 | } |
|
357 | } | |
354 | return NULL; |
|
358 | return NULL; | |
355 | } |
|
359 | } | |
356 |
|
360 | |||
357 | /* Connect to a cmdserver. Will start a new server on demand. */ |
|
361 | /* Connect to a cmdserver. Will start a new server on demand. */ | |
358 | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) |
|
362 | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) | |
359 | { |
|
363 | { | |
360 | const char *sockname = |
|
364 | const char *sockname = | |
361 | opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname; |
|
365 | opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname; | |
362 | debugmsg("try connect to %s", sockname); |
|
366 | debugmsg("try connect to %s", sockname); | |
363 | hgclient_t *hgc = hgc_open(sockname); |
|
367 | hgclient_t *hgc = hgc_open(sockname); | |
364 | if (hgc) |
|
368 | if (hgc) | |
365 | return hgc; |
|
369 | return hgc; | |
366 |
|
370 | |||
367 | /* prevent us from being connected to an outdated server: we were |
|
371 | /* prevent us from being connected to an outdated server: we were | |
368 | * told by a server to redirect to opts->redirectsockname and that |
|
372 | * told by a server to redirect to opts->redirectsockname and that | |
369 | * address does not work. we do not want to connect to the server |
|
373 | * address does not work. we do not want to connect to the server | |
370 | * again because it will probably tell us the same thing. */ |
|
374 | * again because it will probably tell us the same thing. */ | |
371 | if (sockname == opts->redirectsockname) |
|
375 | if (sockname == opts->redirectsockname) | |
372 | unlink(opts->sockname); |
|
376 | unlink(opts->sockname); | |
373 |
|
377 | |||
374 | debugmsg("start cmdserver at %s", opts->initsockname); |
|
378 | debugmsg("start cmdserver at %s", opts->initsockname); | |
375 |
|
379 | |||
376 | pid_t pid = fork(); |
|
380 | pid_t pid = fork(); | |
377 | if (pid < 0) |
|
381 | if (pid < 0) | |
378 | abortmsg("failed to fork cmdserver process"); |
|
382 | abortmsg("failed to fork cmdserver process"); | |
379 | if (pid == 0) { |
|
383 | if (pid == 0) { | |
380 | execcmdserver(opts); |
|
384 | execcmdserver(opts); | |
381 | } else { |
|
385 | } else { | |
382 | hgc = retryconnectcmdserver(opts, pid); |
|
386 | hgc = retryconnectcmdserver(opts, pid); | |
383 | } |
|
387 | } | |
384 |
|
388 | |||
385 | return hgc; |
|
389 | return hgc; | |
386 | } |
|
390 | } | |
387 |
|
391 | |||
388 | static void killcmdserver(const struct cmdserveropts *opts) |
|
392 | static void killcmdserver(const struct cmdserveropts *opts) | |
389 | { |
|
393 | { | |
390 | /* resolve config hash */ |
|
394 | /* resolve config hash */ | |
391 | char *resolvedpath = realpath(opts->sockname, NULL); |
|
395 | char *resolvedpath = realpath(opts->sockname, NULL); | |
392 | if (resolvedpath) { |
|
396 | if (resolvedpath) { | |
393 | unlink(resolvedpath); |
|
397 | unlink(resolvedpath); | |
394 | free(resolvedpath); |
|
398 | free(resolvedpath); | |
395 | } |
|
399 | } | |
396 | } |
|
400 | } | |
397 |
|
401 | |||
398 | /* Run instructions sent from the server like unlink and set redirect path |
|
402 | /* Run instructions sent from the server like unlink and set redirect path | |
399 | * Return 1 if reconnect is needed, otherwise 0 */ |
|
403 | * Return 1 if reconnect is needed, otherwise 0 */ | |
400 | static int runinstructions(struct cmdserveropts *opts, const char **insts) |
|
404 | static int runinstructions(struct cmdserveropts *opts, const char **insts) | |
401 | { |
|
405 | { | |
402 | int needreconnect = 0; |
|
406 | int needreconnect = 0; | |
403 | if (!insts) |
|
407 | if (!insts) | |
404 | return needreconnect; |
|
408 | return needreconnect; | |
405 |
|
409 | |||
406 | assert(insts); |
|
410 | assert(insts); | |
407 | opts->redirectsockname[0] = '\0'; |
|
411 | opts->redirectsockname[0] = '\0'; | |
408 | const char **pinst; |
|
412 | const char **pinst; | |
409 | for (pinst = insts; *pinst; pinst++) { |
|
413 | for (pinst = insts; *pinst; pinst++) { | |
410 | debugmsg("instruction: %s", *pinst); |
|
414 | debugmsg("instruction: %s", *pinst); | |
411 | if (strncmp(*pinst, "unlink ", 7) == 0) { |
|
415 | if (strncmp(*pinst, "unlink ", 7) == 0) { | |
412 | unlink(*pinst + 7); |
|
416 | unlink(*pinst + 7); | |
413 | } else if (strncmp(*pinst, "redirect ", 9) == 0) { |
|
417 | } else if (strncmp(*pinst, "redirect ", 9) == 0) { | |
414 | int r = snprintf(opts->redirectsockname, |
|
418 | int r = snprintf(opts->redirectsockname, | |
415 | sizeof(opts->redirectsockname), "%s", |
|
419 | sizeof(opts->redirectsockname), "%s", | |
416 | *pinst + 9); |
|
420 | *pinst + 9); | |
417 | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) |
|
421 | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) | |
418 | abortmsg("redirect path is too long (%d)", r); |
|
422 | abortmsg("redirect path is too long (%d)", r); | |
419 | needreconnect = 1; |
|
423 | needreconnect = 1; | |
420 | } else if (strncmp(*pinst, "exit ", 5) == 0) { |
|
424 | } else if (strncmp(*pinst, "exit ", 5) == 0) { | |
421 | int n = 0; |
|
425 | int n = 0; | |
422 | if (sscanf(*pinst + 5, "%d", &n) != 1) |
|
426 | if (sscanf(*pinst + 5, "%d", &n) != 1) | |
423 | abortmsg("cannot read the exit code"); |
|
427 | abortmsg("cannot read the exit code"); | |
424 | exit(n); |
|
428 | exit(n); | |
425 | } else if (strcmp(*pinst, "reconnect") == 0) { |
|
429 | } else if (strcmp(*pinst, "reconnect") == 0) { | |
426 | needreconnect = 1; |
|
430 | needreconnect = 1; | |
427 | } else { |
|
431 | } else { | |
428 | abortmsg("unknown instruction: %s", *pinst); |
|
432 | abortmsg("unknown instruction: %s", *pinst); | |
429 | } |
|
433 | } | |
430 | } |
|
434 | } | |
431 | return needreconnect; |
|
435 | return needreconnect; | |
432 | } |
|
436 | } | |
433 |
|
437 | |||
434 | /* |
|
438 | /* | |
435 | * Test whether the command and the environment is unsupported or not. |
|
439 | * Test whether the command and the environment is unsupported or not. | |
436 | * |
|
440 | * | |
437 | * If any of the stdio file descriptors are not present (rare, but some tools |
|
441 | * If any of the stdio file descriptors are not present (rare, but some tools | |
438 | * might spawn new processes without stdio instead of redirecting them to the |
|
442 | * might spawn new processes without stdio instead of redirecting them to the | |
439 | * null device), then mark it as not supported because attachio won't work |
|
443 | * null device), then mark it as not supported because attachio won't work | |
440 | * correctly. |
|
444 | * correctly. | |
441 | * |
|
445 | * | |
442 | * The command list is not designed to cover all cases. But it's fast, and does |
|
446 | * The command list is not designed to cover all cases. But it's fast, and does | |
443 | * not depend on the server. |
|
447 | * not depend on the server. | |
444 | */ |
|
448 | */ | |
445 | static int isunsupported(int argc, const char *argv[]) |
|
449 | static int isunsupported(int argc, const char *argv[]) | |
446 | { |
|
450 | { | |
447 | enum { SERVE = 1, |
|
451 | enum { SERVE = 1, | |
448 | DAEMON = 2, |
|
452 | DAEMON = 2, | |
449 | SERVEDAEMON = SERVE | DAEMON, |
|
453 | SERVEDAEMON = SERVE | DAEMON, | |
450 | }; |
|
454 | }; | |
451 | unsigned int state = 0; |
|
455 | unsigned int state = 0; | |
452 | int i; |
|
456 | int i; | |
453 | /* use fcntl to test missing stdio fds */ |
|
457 | /* use fcntl to test missing stdio fds */ | |
454 | if (fcntl(STDIN_FILENO, F_GETFD) == -1 || |
|
458 | if (fcntl(STDIN_FILENO, F_GETFD) == -1 || | |
455 | fcntl(STDOUT_FILENO, F_GETFD) == -1 || |
|
459 | fcntl(STDOUT_FILENO, F_GETFD) == -1 || | |
456 | fcntl(STDERR_FILENO, F_GETFD) == -1) { |
|
460 | fcntl(STDERR_FILENO, F_GETFD) == -1) { | |
457 | debugmsg("stdio fds are missing"); |
|
461 | debugmsg("stdio fds are missing"); | |
458 | return 1; |
|
462 | return 1; | |
459 | } |
|
463 | } | |
460 | for (i = 0; i < argc; ++i) { |
|
464 | for (i = 0; i < argc; ++i) { | |
461 | if (strcmp(argv[i], "--") == 0) |
|
465 | if (strcmp(argv[i], "--") == 0) | |
462 | break; |
|
466 | break; | |
463 | /* |
|
467 | /* | |
464 | * there can be false positives but no false negative |
|
468 | * there can be false positives but no false negative | |
465 | * we cannot assume `serve` will always be first argument |
|
469 | * we cannot assume `serve` will always be first argument | |
466 | * because global options can be passed before the command name |
|
470 | * because global options can be passed before the command name | |
467 | */ |
|
471 | */ | |
468 | if (strcmp("serve", argv[i]) == 0) |
|
472 | if (strcmp("serve", argv[i]) == 0) | |
469 | state |= SERVE; |
|
473 | state |= SERVE; | |
470 | else if (strcmp("-d", argv[i]) == 0 || |
|
474 | else if (strcmp("-d", argv[i]) == 0 || | |
471 | strcmp("--daemon", argv[i]) == 0) |
|
475 | strcmp("--daemon", argv[i]) == 0) | |
472 | state |= DAEMON; |
|
476 | state |= DAEMON; | |
473 | } |
|
477 | } | |
474 | return (state & SERVEDAEMON) == SERVEDAEMON; |
|
478 | return (state & SERVEDAEMON) == SERVEDAEMON; | |
475 | } |
|
479 | } | |
476 |
|
480 | |||
477 | static void execoriginalhg(const char *argv[]) |
|
481 | static void execoriginalhg(const char *argv[]) | |
478 | { |
|
482 | { | |
479 | debugmsg("execute original hg"); |
|
483 | debugmsg("execute original hg"); | |
480 | if (execvp(gethgcmd(), (char **)argv) < 0) |
|
484 | if (execvp(gethgcmd(), (char **)argv) < 0) | |
481 | abortmsgerrno("failed to exec original hg"); |
|
485 | abortmsgerrno("failed to exec original hg"); | |
482 | } |
|
486 | } | |
483 |
|
487 | |||
484 | int main(int argc, const char *argv[], const char *envp[]) |
|
488 | int main(int argc, const char *argv[], const char *envp[]) | |
485 | { |
|
489 | { | |
486 | if (getenv("CHGDEBUG")) |
|
490 | if (getenv("CHGDEBUG")) | |
487 | enabledebugmsg(); |
|
491 | enabledebugmsg(); | |
488 |
|
492 | |||
489 | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) |
|
493 | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) | |
490 | enablecolor(); |
|
494 | enablecolor(); | |
491 |
|
495 | |||
492 | if (getenv("CHGINTERNALMARK")) |
|
496 | if (getenv("CHGINTERNALMARK")) | |
493 | abortmsg("chg started by chg detected.\n" |
|
497 | abortmsg("chg started by chg detected.\n" | |
494 | "Please make sure ${HG:-hg} is not a symlink or " |
|
498 | "Please make sure ${HG:-hg} is not a symlink or " | |
495 | "wrapper to chg. Alternatively, set $CHGHG to the " |
|
499 | "wrapper to chg. Alternatively, set $CHGHG to the " | |
496 | "path of real hg."); |
|
500 | "path of real hg."); | |
497 |
|
501 | |||
498 | if (isunsupported(argc - 1, argv + 1)) |
|
502 | if (isunsupported(argc - 1, argv + 1)) | |
499 | execoriginalhg(argv); |
|
503 | execoriginalhg(argv); | |
500 |
|
504 | |||
501 | struct cmdserveropts opts; |
|
505 | struct cmdserveropts opts; | |
502 | initcmdserveropts(&opts); |
|
506 | initcmdserveropts(&opts); | |
503 | setcmdserveropts(&opts); |
|
507 | setcmdserveropts(&opts); | |
504 | setcmdserverargs(&opts, argc, argv); |
|
508 | setcmdserverargs(&opts, argc, argv); | |
505 |
|
509 | |||
506 | if (argc == 2) { |
|
510 | if (argc == 2) { | |
507 | if (strcmp(argv[1], "--kill-chg-daemon") == 0) { |
|
511 | if (strcmp(argv[1], "--kill-chg-daemon") == 0) { | |
508 | killcmdserver(&opts); |
|
512 | killcmdserver(&opts); | |
509 | return 0; |
|
513 | return 0; | |
510 | } |
|
514 | } | |
511 | } |
|
515 | } | |
512 |
|
516 | |||
513 | hgclient_t *hgc; |
|
517 | hgclient_t *hgc; | |
514 | size_t retry = 0; |
|
518 | size_t retry = 0; | |
515 | while (1) { |
|
519 | while (1) { | |
516 | hgc = connectcmdserver(&opts); |
|
520 | hgc = connectcmdserver(&opts); | |
517 | if (!hgc) |
|
521 | if (!hgc) | |
518 | abortmsg("cannot open hg client"); |
|
522 | abortmsg("cannot open hg client"); | |
519 | hgc_setenv(hgc, envp); |
|
523 | hgc_setenv(hgc, envp); | |
520 | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); |
|
524 | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); | |
521 | int needreconnect = runinstructions(&opts, insts); |
|
525 | int needreconnect = runinstructions(&opts, insts); | |
522 | free(insts); |
|
526 | free(insts); | |
523 | if (!needreconnect) |
|
527 | if (!needreconnect) | |
524 | break; |
|
528 | break; | |
525 | hgc_close(hgc); |
|
529 | hgc_close(hgc); | |
526 | if (++retry > 10) |
|
530 | if (++retry > 10) | |
527 | abortmsg("too many redirections.\n" |
|
531 | abortmsg("too many redirections.\n" | |
528 | "Please make sure %s is not a wrapper which " |
|
532 | "Please make sure %s is not a wrapper which " | |
529 | "changes sensitive environment variables " |
|
533 | "changes sensitive environment variables " | |
530 | "before executing hg. If you have to use a " |
|
534 | "before executing hg. If you have to use a " | |
531 | "wrapper, wrap chg instead of hg.", |
|
535 | "wrapper, wrap chg instead of hg.", | |
532 | gethgcmd()); |
|
536 | gethgcmd()); | |
533 | } |
|
537 | } | |
534 |
|
538 | |||
535 | setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); |
|
539 | setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); | |
536 | atexit(waitpager); |
|
540 | atexit(waitpager); | |
537 | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); |
|
541 | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); | |
538 | restoresignalhandler(); |
|
542 | restoresignalhandler(); | |
539 | hgc_close(hgc); |
|
543 | hgc_close(hgc); | |
540 | freecmdserveropts(&opts); |
|
544 | freecmdserveropts(&opts); | |
541 |
|
545 | |||
542 | return exitcode; |
|
546 | return exitcode; | |
543 | } |
|
547 | } |
General Comments 0
You need to be logged in to leave comments.
Login now