Mercurial > hg
annotate contrib/chg/hgclient.c @ 36341:28a97cf212af
strip: don't reimplement any()
Differential Revision: https://phab.mercurial-scm.org/D2374
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Tue, 06 Feb 2018 08:54:36 -0800 |
parents | 9724f54923ec |
children | 413b6b10fdd5 |
rev | line source |
---|---|
28060 | 1 /* |
2 * A command server client that uses Unix domain socket | |
3 * | |
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org> | |
5 * | |
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. | |
8 */ | |
9 | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
10 #include <arpa/inet.h> /* for ntohl(), htonl() */ |
28060 | 11 #include <assert.h> |
12 #include <ctype.h> | |
13 #include <errno.h> | |
14 #include <fcntl.h> | |
15 #include <signal.h> | |
16 #include <stdint.h> | |
17 #include <stdio.h> | |
18 #include <stdlib.h> | |
19 #include <string.h> | |
20 #include <sys/socket.h> | |
21 #include <sys/stat.h> | |
22 #include <sys/un.h> | |
23 #include <unistd.h> | |
24 | |
25 #include "hgclient.h" | |
30738
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
26 #include "procutil.h" |
28060 | 27 #include "util.h" |
28 | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
29 enum { CAP_GETENCODING = 0x0001, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
30 CAP_RUNCOMMAND = 0x0002, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
31 /* cHg extension: */ |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
32 CAP_ATTACHIO = 0x0100, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
33 CAP_CHDIR = 0x0200, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
34 CAP_SETENV = 0x0800, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
35 CAP_SETUMASK = 0x1000, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
36 CAP_VALIDATE = 0x2000, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
37 CAP_SETPROCNAME = 0x4000, |
28060 | 38 }; |
39 | |
40 typedef struct { | |
41 const char *name; | |
42 unsigned int flag; | |
43 } cappair_t; | |
44 | |
45 static const cappair_t captable[] = { | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
46 {"getencoding", CAP_GETENCODING}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
47 {"runcommand", CAP_RUNCOMMAND}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
48 {"attachio", CAP_ATTACHIO}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
49 {"chdir", CAP_CHDIR}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
50 {"setenv", CAP_SETENV}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
51 {"setumask", CAP_SETUMASK}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
52 {"validate", CAP_VALIDATE}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
53 {"setprocname", CAP_SETPROCNAME}, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
54 {NULL, 0}, /* terminator */ |
28060 | 55 }; |
56 | |
57 typedef struct { | |
58 char ch; | |
59 char *data; | |
60 size_t maxdatasize; | |
61 size_t datasize; | |
62 } context_t; | |
63 | |
64 struct hgclient_tag_ { | |
65 int sockfd; | |
29581 | 66 pid_t pgid; |
28060 | 67 pid_t pid; |
68 context_t ctx; | |
69 unsigned int capflags; | |
70 }; | |
71 | |
72 static const size_t defaultdatasize = 4096; | |
73 | |
30738
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
74 static void attachio(hgclient_t *hgc); |
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
75 |
28060 | 76 static void initcontext(context_t *ctx) |
77 { | |
78 ctx->ch = '\0'; | |
79 ctx->data = malloc(defaultdatasize); | |
80 ctx->maxdatasize = (ctx->data) ? defaultdatasize : 0; | |
81 ctx->datasize = 0; | |
82 debugmsg("initialize context buffer with size %zu", ctx->maxdatasize); | |
83 } | |
84 | |
85 static void enlargecontext(context_t *ctx, size_t newsize) | |
86 { | |
87 if (newsize <= ctx->maxdatasize) | |
88 return; | |
89 | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
90 newsize = defaultdatasize * |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
91 ((newsize + defaultdatasize - 1) / defaultdatasize); |
28166
c6e0c2533e5f
chg: use mallocx and reallocx in hgclient
Jun Wu <quark@fb.com>
parents:
28160
diff
changeset
|
92 ctx->data = reallocx(ctx->data, newsize); |
28060 | 93 ctx->maxdatasize = newsize; |
94 debugmsg("enlarge context buffer to %zu", ctx->maxdatasize); | |
95 } | |
96 | |
97 static void freecontext(context_t *ctx) | |
98 { | |
99 debugmsg("free context buffer"); | |
100 free(ctx->data); | |
101 ctx->data = NULL; | |
102 ctx->maxdatasize = 0; | |
103 ctx->datasize = 0; | |
104 } | |
105 | |
106 /* Read channeled response from cmdserver */ | |
107 static void readchannel(hgclient_t *hgc) | |
108 { | |
109 assert(hgc); | |
110 | |
111 ssize_t rsize = recv(hgc->sockfd, &hgc->ctx.ch, sizeof(hgc->ctx.ch), 0); | |
28551
8e5312f8df30
chg: downgrade "failed to read channel" from abortmsg to debugmsg
Jun Wu <quark@fb.com>
parents:
28535
diff
changeset
|
112 if (rsize != sizeof(hgc->ctx.ch)) { |
8e5312f8df30
chg: downgrade "failed to read channel" from abortmsg to debugmsg
Jun Wu <quark@fb.com>
parents:
28535
diff
changeset
|
113 /* server would have exception and traceback would be printed */ |
8e5312f8df30
chg: downgrade "failed to read channel" from abortmsg to debugmsg
Jun Wu <quark@fb.com>
parents:
28535
diff
changeset
|
114 debugmsg("failed to read channel"); |
8e5312f8df30
chg: downgrade "failed to read channel" from abortmsg to debugmsg
Jun Wu <quark@fb.com>
parents:
28535
diff
changeset
|
115 exit(255); |
8e5312f8df30
chg: downgrade "failed to read channel" from abortmsg to debugmsg
Jun Wu <quark@fb.com>
parents:
28535
diff
changeset
|
116 } |
28060 | 117 |
118 uint32_t datasize_n; | |
119 rsize = recv(hgc->sockfd, &datasize_n, sizeof(datasize_n), 0); | |
120 if (rsize != sizeof(datasize_n)) | |
121 abortmsg("failed to read data size"); | |
122 | |
123 /* datasize denotes the maximum size to write if input request */ | |
124 hgc->ctx.datasize = ntohl(datasize_n); | |
125 enlargecontext(&hgc->ctx, hgc->ctx.datasize); | |
126 | |
127 if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S') | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
128 return; /* assumes input request */ |
28060 | 129 |
130 size_t cursize = 0; | |
131 while (cursize < hgc->ctx.datasize) { | |
132 rsize = recv(hgc->sockfd, hgc->ctx.data + cursize, | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
133 hgc->ctx.datasize - cursize, 0); |
29719
acf27be56d26
chg: just take it as EOF if recv() returns 0
Yuya Nishihara <yuya@tcha.org>
parents:
29602
diff
changeset
|
134 if (rsize < 1) |
28060 | 135 abortmsg("failed to read data block"); |
136 cursize += rsize; | |
137 } | |
138 } | |
139 | |
140 static void sendall(int sockfd, const void *data, size_t datasize) | |
141 { | |
142 const char *p = data; | |
143 const char *const endp = p + datasize; | |
144 while (p < endp) { | |
145 ssize_t r = send(sockfd, p, endp - p, 0); | |
146 if (r < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
147 abortmsgerrno("cannot communicate"); |
28060 | 148 p += r; |
149 } | |
150 } | |
151 | |
152 /* Write lengh-data block to cmdserver */ | |
153 static void writeblock(const hgclient_t *hgc) | |
154 { | |
155 assert(hgc); | |
156 | |
157 const uint32_t datasize_n = htonl(hgc->ctx.datasize); | |
158 sendall(hgc->sockfd, &datasize_n, sizeof(datasize_n)); | |
159 | |
160 sendall(hgc->sockfd, hgc->ctx.data, hgc->ctx.datasize); | |
161 } | |
162 | |
163 static void writeblockrequest(const hgclient_t *hgc, const char *chcmd) | |
164 { | |
165 debugmsg("request %s, block size %zu", chcmd, hgc->ctx.datasize); | |
166 | |
167 char buf[strlen(chcmd) + 1]; | |
168 memcpy(buf, chcmd, sizeof(buf) - 1); | |
169 buf[sizeof(buf) - 1] = '\n'; | |
170 sendall(hgc->sockfd, buf, sizeof(buf)); | |
171 | |
172 writeblock(hgc); | |
173 } | |
174 | |
175 /* Build '\0'-separated list of args. argsize < 0 denotes that args are | |
176 * terminated by NULL. */ | |
177 static void packcmdargs(context_t *ctx, const char *const args[], | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
178 ssize_t argsize) |
28060 | 179 { |
180 ctx->datasize = 0; | |
181 const char *const *const end = (argsize >= 0) ? args + argsize : NULL; | |
182 for (const char *const *it = args; it != end && *it; ++it) { | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
183 const size_t n = strlen(*it) + 1; /* include '\0' */ |
28060 | 184 enlargecontext(ctx, ctx->datasize + n); |
185 memcpy(ctx->data + ctx->datasize, *it, n); | |
186 ctx->datasize += n; | |
187 } | |
188 | |
189 if (ctx->datasize > 0) | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
190 --ctx->datasize; /* strip last '\0' */ |
28060 | 191 } |
192 | |
193 /* Extract '\0'-separated list of args to new buffer, terminated by NULL */ | |
194 static const char **unpackcmdargsnul(const context_t *ctx) | |
195 { | |
196 const char **args = NULL; | |
197 size_t nargs = 0, maxnargs = 0; | |
198 const char *s = ctx->data; | |
199 const char *e = ctx->data + ctx->datasize; | |
200 for (;;) { | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
201 if (nargs + 1 >= maxnargs) { /* including last NULL */ |
28060 | 202 maxnargs += 256; |
28166
c6e0c2533e5f
chg: use mallocx and reallocx in hgclient
Jun Wu <quark@fb.com>
parents:
28160
diff
changeset
|
203 args = reallocx(args, maxnargs * sizeof(args[0])); |
28060 | 204 } |
205 args[nargs] = s; | |
206 nargs++; | |
207 s = memchr(s, '\0', e - s); | |
208 if (!s) | |
209 break; | |
210 s++; | |
211 } | |
212 args[nargs] = NULL; | |
213 return args; | |
214 } | |
215 | |
216 static void handlereadrequest(hgclient_t *hgc) | |
217 { | |
218 context_t *ctx = &hgc->ctx; | |
219 size_t r = fread(ctx->data, sizeof(ctx->data[0]), ctx->datasize, stdin); | |
220 ctx->datasize = r; | |
221 writeblock(hgc); | |
222 } | |
223 | |
224 /* Read single-line */ | |
225 static void handlereadlinerequest(hgclient_t *hgc) | |
226 { | |
227 context_t *ctx = &hgc->ctx; | |
228 if (!fgets(ctx->data, ctx->datasize, stdin)) | |
229 ctx->data[0] = '\0'; | |
230 ctx->datasize = strlen(ctx->data); | |
231 writeblock(hgc); | |
232 } | |
233 | |
234 /* Execute the requested command and write exit code */ | |
235 static void handlesystemrequest(hgclient_t *hgc) | |
236 { | |
237 context_t *ctx = &hgc->ctx; | |
238 enlargecontext(ctx, ctx->datasize + 1); | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
239 ctx->data[ctx->datasize] = '\0'; /* terminate last string */ |
28060 | 240 |
241 const char **args = unpackcmdargsnul(ctx); | |
30726
dd897eb1699e
chg: send type information via S channel (BC)
Jun Wu <quark@fb.com>
parents:
30679
diff
changeset
|
242 if (!args[0] || !args[1] || !args[2]) |
dd897eb1699e
chg: send type information via S channel (BC)
Jun Wu <quark@fb.com>
parents:
30679
diff
changeset
|
243 abortmsg("missing type or command or cwd in system request"); |
30728
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
244 if (strcmp(args[0], "system") == 0) { |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
245 debugmsg("run '%s' at '%s'", args[1], args[2]); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
246 int32_t r = runshellcmd(args[1], args + 3, args[2]); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
247 free(args); |
28060 | 248 |
30728
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
249 uint32_t r_n = htonl(r); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
250 memcpy(ctx->data, &r_n, sizeof(r_n)); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
251 ctx->datasize = sizeof(r_n); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
252 writeblock(hgc); |
30738
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
253 } else if (strcmp(args[0], "pager") == 0) { |
31941
ac5527021097
chg: respect environment variables for pager
Jun Wu <quark@fb.com>
parents:
30756
diff
changeset
|
254 setuppager(args[1], args + 3); |
30738
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
255 if (hgc->capflags & CAP_ATTACHIO) |
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
256 attachio(hgc); |
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
257 /* unblock the server */ |
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
258 static const char emptycmd[] = "\n"; |
a45c0f42271f
chg: handle pager request client-side
Jun Wu <quark@fb.com>
parents:
30728
diff
changeset
|
259 sendall(hgc->sockfd, emptycmd, sizeof(emptycmd) - 1); |
30728
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
260 } else { |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
261 abortmsg("unknown type in system request: %s", args[0]); |
7438cb35979a
chg: check type read from S channel
Jun Wu <quark@fb.com>
parents:
30726
diff
changeset
|
262 } |
28060 | 263 } |
264 | |
265 /* Read response of command execution until receiving 'r'-esult */ | |
266 static void handleresponse(hgclient_t *hgc) | |
267 { | |
268 for (;;) { | |
269 readchannel(hgc); | |
270 context_t *ctx = &hgc->ctx; | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
271 debugmsg("response read from channel %c, size %zu", ctx->ch, |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
272 ctx->datasize); |
28060 | 273 switch (ctx->ch) { |
274 case 'o': | |
275 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | |
276 stdout); | |
277 break; | |
278 case 'e': | |
279 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | |
280 stderr); | |
281 break; | |
282 case 'd': | |
283 /* assumes last char is '\n' */ | |
284 ctx->data[ctx->datasize - 1] = '\0'; | |
285 debugmsg("server: %s", ctx->data); | |
286 break; | |
287 case 'r': | |
288 return; | |
289 case 'I': | |
290 handlereadrequest(hgc); | |
291 break; | |
292 case 'L': | |
293 handlereadlinerequest(hgc); | |
294 break; | |
295 case 'S': | |
296 handlesystemrequest(hgc); | |
297 break; | |
298 default: | |
299 if (isupper(ctx->ch)) | |
300 abortmsg("cannot handle response (ch = %c)", | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
301 ctx->ch); |
28060 | 302 } |
303 } | |
304 } | |
305 | |
306 static unsigned int parsecapabilities(const char *s, const char *e) | |
307 { | |
308 unsigned int flags = 0; | |
309 while (s < e) { | |
310 const char *t = strchr(s, ' '); | |
311 if (!t || t > e) | |
312 t = e; | |
313 const cappair_t *cap; | |
314 for (cap = captable; cap->flag; ++cap) { | |
315 size_t n = t - s; | |
316 if (strncmp(s, cap->name, n) == 0 && | |
317 strlen(cap->name) == n) { | |
318 flags |= cap->flag; | |
319 break; | |
320 } | |
321 } | |
322 s = t + 1; | |
323 } | |
324 return flags; | |
325 } | |
326 | |
327 static void readhello(hgclient_t *hgc) | |
328 { | |
329 readchannel(hgc); | |
330 context_t *ctx = &hgc->ctx; | |
28512
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
331 if (ctx->ch != 'o') { |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
332 char ch = ctx->ch; |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
333 if (ch == 'e') { |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
334 /* write early error and will exit */ |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
335 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
336 stderr); |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
337 handleresponse(hgc); |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
338 } |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
339 abortmsg("unexpected channel of hello message (ch = %c)", ch); |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
340 } |
28060 | 341 enlargecontext(ctx, ctx->datasize + 1); |
342 ctx->data[ctx->datasize] = '\0'; | |
343 debugmsg("hello received: %s (size = %zu)", ctx->data, ctx->datasize); | |
344 | |
345 const char *s = ctx->data; | |
346 const char *const dataend = ctx->data + ctx->datasize; | |
347 while (s < dataend) { | |
348 const char *t = strchr(s, ':'); | |
349 if (!t || t[1] != ' ') | |
350 break; | |
351 const char *u = strchr(t + 2, '\n'); | |
352 if (!u) | |
353 u = dataend; | |
354 if (strncmp(s, "capabilities:", t - s + 1) == 0) { | |
355 hgc->capflags = parsecapabilities(t + 2, u); | |
29581 | 356 } else if (strncmp(s, "pgid:", t - s + 1) == 0) { |
357 hgc->pgid = strtol(t + 2, NULL, 10); | |
28060 | 358 } else if (strncmp(s, "pid:", t - s + 1) == 0) { |
359 hgc->pid = strtol(t + 2, NULL, 10); | |
360 } | |
361 s = u + 1; | |
362 } | |
363 debugmsg("capflags=0x%04x, pid=%d", hgc->capflags, hgc->pid); | |
364 } | |
365 | |
30751 | 366 static void updateprocname(hgclient_t *hgc) |
367 { | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
368 int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize, "chg[worker/%d]", |
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
369 (int)getpid()); |
30756
1f9684fe94cc
chg: check snprintf result strictly
Jun Wu <quark@fb.com>
parents:
30751
diff
changeset
|
370 if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize) |
1f9684fe94cc
chg: check snprintf result strictly
Jun Wu <quark@fb.com>
parents:
30751
diff
changeset
|
371 abortmsg("insufficient buffer to write procname (r = %d)", r); |
1f9684fe94cc
chg: check snprintf result strictly
Jun Wu <quark@fb.com>
parents:
30751
diff
changeset
|
372 hgc->ctx.datasize = (size_t)r; |
30751 | 373 writeblockrequest(hgc, "setprocname"); |
374 } | |
375 | |
28060 | 376 static void attachio(hgclient_t *hgc) |
377 { | |
378 debugmsg("request attachio"); | |
379 static const char chcmd[] = "attachio\n"; | |
380 sendall(hgc->sockfd, chcmd, sizeof(chcmd) - 1); | |
381 readchannel(hgc); | |
382 context_t *ctx = &hgc->ctx; | |
383 if (ctx->ch != 'I') | |
384 abortmsg("unexpected response for attachio (ch = %c)", ctx->ch); | |
385 | |
386 static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; | |
387 struct msghdr msgh; | |
388 memset(&msgh, 0, sizeof(msgh)); | |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
389 struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */ |
28060 | 390 msgh.msg_iov = &iov; |
391 msgh.msg_iovlen = 1; | |
392 char fdbuf[CMSG_SPACE(sizeof(fds))]; | |
393 msgh.msg_control = fdbuf; | |
394 msgh.msg_controllen = sizeof(fdbuf); | |
395 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); | |
396 cmsg->cmsg_level = SOL_SOCKET; | |
397 cmsg->cmsg_type = SCM_RIGHTS; | |
398 cmsg->cmsg_len = CMSG_LEN(sizeof(fds)); | |
399 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds)); | |
400 msgh.msg_controllen = cmsg->cmsg_len; | |
401 ssize_t r = sendmsg(hgc->sockfd, &msgh, 0); | |
402 if (r < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
403 abortmsgerrno("sendmsg failed"); |
28060 | 404 |
405 handleresponse(hgc); | |
406 int32_t n; | |
407 if (ctx->datasize != sizeof(n)) | |
408 abortmsg("unexpected size of attachio result"); | |
409 memcpy(&n, ctx->data, sizeof(n)); | |
410 n = ntohl(n); | |
411 if (n != sizeof(fds) / sizeof(fds[0])) | |
412 abortmsg("failed to send fds (n = %d)", n); | |
413 } | |
414 | |
415 static void chdirtocwd(hgclient_t *hgc) | |
416 { | |
417 if (!getcwd(hgc->ctx.data, hgc->ctx.maxdatasize)) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
418 abortmsgerrno("failed to getcwd"); |
28060 | 419 hgc->ctx.datasize = strlen(hgc->ctx.data); |
420 writeblockrequest(hgc, "chdir"); | |
421 } | |
422 | |
28160
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
423 static void forwardumask(hgclient_t *hgc) |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
424 { |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
425 mode_t mask = umask(0); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
426 umask(mask); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
427 |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
428 static const char command[] = "setumask\n"; |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
429 sendall(hgc->sockfd, command, sizeof(command) - 1); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
430 uint32_t data = htonl(mask); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
431 sendall(hgc->sockfd, &data, sizeof(data)); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
432 } |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
433 |
28060 | 434 /*! |
435 * Open connection to per-user cmdserver | |
436 * | |
437 * If no background server running, returns NULL. | |
438 */ | |
439 hgclient_t *hgc_open(const char *sockname) | |
440 { | |
441 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
442 if (fd < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
443 abortmsgerrno("cannot create socket"); |
28060 | 444 |
445 /* don't keep fd on fork(), so that it can be closed when the parent | |
446 * process get terminated. */ | |
28855
f5764e177bbe
chg: extract the logic of setting FD_CLOEXEC to a utility function
Jun Wu <quark@fb.com>
parents:
28789
diff
changeset
|
447 fsetcloexec(fd); |
28060 | 448 |
449 struct sockaddr_un addr; | |
450 addr.sun_family = AF_UNIX; | |
30675
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
451 |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
452 /* use chdir to workaround small sizeof(sun_path) */ |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
453 int bakfd = -1; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
454 const char *basename = sockname; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
455 { |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
456 const char *split = strrchr(sockname, '/'); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
457 if (split && split != sockname) { |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
458 if (split[1] == '\0') |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
459 abortmsg("sockname cannot end with a slash"); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
460 size_t len = split - sockname; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
461 char sockdir[len + 1]; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
462 memcpy(sockdir, sockname, len); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
463 sockdir[len] = '\0'; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
464 |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
465 bakfd = open(".", O_DIRECTORY); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
466 if (bakfd == -1) |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
467 abortmsgerrno("cannot open cwd"); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
468 |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
469 int r = chdir(sockdir); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
470 if (r != 0) |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
471 abortmsgerrno("cannot chdir %s", sockdir); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
472 |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
473 basename = split + 1; |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
474 } |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
475 } |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
476 if (strlen(basename) >= sizeof(addr.sun_path)) |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
477 abortmsg("sockname is too long: %s", basename); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
478 strncpy(addr.sun_path, basename, sizeof(addr.sun_path)); |
28060 | 479 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; |
480 | |
30675
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
481 /* real connect */ |
28060 | 482 int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); |
30679
fe11f466880d
chg: handle connect failure before errno gets overridden
Jun Wu <quark@fb.com>
parents:
30675
diff
changeset
|
483 if (r < 0) { |
fe11f466880d
chg: handle connect failure before errno gets overridden
Jun Wu <quark@fb.com>
parents:
30675
diff
changeset
|
484 if (errno != ENOENT && errno != ECONNREFUSED) |
fe11f466880d
chg: handle connect failure before errno gets overridden
Jun Wu <quark@fb.com>
parents:
30675
diff
changeset
|
485 abortmsgerrno("cannot connect to %s", sockname); |
fe11f466880d
chg: handle connect failure before errno gets overridden
Jun Wu <quark@fb.com>
parents:
30675
diff
changeset
|
486 } |
30675
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
487 if (bakfd != -1) { |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
488 fchdirx(bakfd); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
489 close(bakfd); |
112915e9a363
chg: let hgc_open support long path
Jun Wu <quark@fb.com>
parents:
29719
diff
changeset
|
490 } |
28060 | 491 if (r < 0) { |
492 close(fd); | |
30679
fe11f466880d
chg: handle connect failure before errno gets overridden
Jun Wu <quark@fb.com>
parents:
30675
diff
changeset
|
493 return NULL; |
28060 | 494 } |
28769
222f482930c8
chg: make connect debug message less repetitive
Jun Wu <quark@fb.com>
parents:
28551
diff
changeset
|
495 debugmsg("connected to %s", addr.sun_path); |
28060 | 496 |
28166
c6e0c2533e5f
chg: use mallocx and reallocx in hgclient
Jun Wu <quark@fb.com>
parents:
28160
diff
changeset
|
497 hgclient_t *hgc = mallocx(sizeof(hgclient_t)); |
28060 | 498 memset(hgc, 0, sizeof(*hgc)); |
499 hgc->sockfd = fd; | |
500 initcontext(&hgc->ctx); | |
501 | |
502 readhello(hgc); | |
503 if (!(hgc->capflags & CAP_RUNCOMMAND)) | |
504 abortmsg("insufficient capability: runcommand"); | |
30751 | 505 if (hgc->capflags & CAP_SETPROCNAME) |
506 updateprocname(hgc); | |
28060 | 507 if (hgc->capflags & CAP_ATTACHIO) |
508 attachio(hgc); | |
509 if (hgc->capflags & CAP_CHDIR) | |
510 chdirtocwd(hgc); | |
28160
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
511 if (hgc->capflags & CAP_SETUMASK) |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
512 forwardumask(hgc); |
28060 | 513 |
514 return hgc; | |
515 } | |
516 | |
517 /*! | |
518 * Close connection and free allocated memory | |
519 */ | |
520 void hgc_close(hgclient_t *hgc) | |
521 { | |
522 assert(hgc); | |
523 freecontext(&hgc->ctx); | |
524 close(hgc->sockfd); | |
525 free(hgc); | |
526 } | |
527 | |
29581 | 528 pid_t hgc_peerpgid(const hgclient_t *hgc) |
529 { | |
530 assert(hgc); | |
531 return hgc->pgid; | |
532 } | |
533 | |
28060 | 534 pid_t hgc_peerpid(const hgclient_t *hgc) |
535 { | |
536 assert(hgc); | |
537 return hgc->pid; | |
538 } | |
539 | |
540 /*! | |
28356
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
541 * Send command line arguments to let the server load the repo config and check |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
542 * whether it can process our request directly or not. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
543 * Make sure hgc_setenv is called before calling this. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
544 * |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
545 * @return - NULL, the server believes it can handle our request, or does not |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
546 * support "validate" command. |
28535
aa082a8125da
chgserver: add an explicit "reconnect" instruction to validate
Jun Wu <quark@fb.com>
parents:
28512
diff
changeset
|
547 * - a list of strings, the server probably cannot handle our request |
aa082a8125da
chgserver: add an explicit "reconnect" instruction to validate
Jun Wu <quark@fb.com>
parents:
28512
diff
changeset
|
548 * and it sent instructions telling us what to do next. See |
28356
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
549 * chgserver.py for possible instruction formats. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
550 * the list should be freed by the caller. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
551 * the last string is guaranteed to be NULL. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
552 */ |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
553 const char **hgc_validate(hgclient_t *hgc, const char *const args[], |
35959
9724f54923ec
chg: enable clang-format on all .c and .h files
Augie Fackler <augie@google.com>
parents:
31941
diff
changeset
|
554 size_t argsize) |
28356
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
555 { |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
556 assert(hgc); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
557 if (!(hgc->capflags & CAP_VALIDATE)) |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
558 return NULL; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
559 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
560 packcmdargs(&hgc->ctx, args, argsize); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
561 writeblockrequest(hgc, "validate"); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
562 handleresponse(hgc); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
563 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
564 /* the server returns '\0' if it can handle our request */ |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
565 if (hgc->ctx.datasize <= 1) |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
566 return NULL; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
567 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
568 /* make sure the buffer is '\0' terminated */ |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
569 enlargecontext(&hgc->ctx, hgc->ctx.datasize + 1); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
570 hgc->ctx.data[hgc->ctx.datasize] = '\0'; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
571 return unpackcmdargsnul(&hgc->ctx); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
572 } |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
573 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
574 /*! |
28060 | 575 * Execute the specified Mercurial command |
576 * | |
577 * @return result code | |
578 */ | |
579 int hgc_runcommand(hgclient_t *hgc, const char *const args[], size_t argsize) | |
580 { | |
581 assert(hgc); | |
582 | |
583 packcmdargs(&hgc->ctx, args, argsize); | |
584 writeblockrequest(hgc, "runcommand"); | |
585 handleresponse(hgc); | |
586 | |
587 int32_t exitcode_n; | |
588 if (hgc->ctx.datasize != sizeof(exitcode_n)) { | |
589 abortmsg("unexpected size of exitcode"); | |
590 } | |
591 memcpy(&exitcode_n, hgc->ctx.data, sizeof(exitcode_n)); | |
592 return ntohl(exitcode_n); | |
593 } | |
594 | |
595 /*! | |
596 * (Re-)send client's stdio channels so that the server can access to tty | |
597 */ | |
598 void hgc_attachio(hgclient_t *hgc) | |
599 { | |
600 assert(hgc); | |
601 if (!(hgc->capflags & CAP_ATTACHIO)) | |
602 return; | |
603 attachio(hgc); | |
604 } | |
605 | |
606 /*! | |
607 * Update server's environment variables | |
608 * | |
609 * @param envp list of environment variables in "NAME=VALUE" format, | |
610 * terminated by NULL. | |
611 */ | |
612 void hgc_setenv(hgclient_t *hgc, const char *const envp[]) | |
613 { | |
614 assert(hgc && envp); | |
615 if (!(hgc->capflags & CAP_SETENV)) | |
616 return; | |
617 packcmdargs(&hgc->ctx, envp, /*argsize*/ -1); | |
618 writeblockrequest(hgc, "setenv"); | |
619 } |