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