Mercurial > hg
annotate contrib/chg/hgclient.c @ 29648:94c5273c7d5d stable
doc: fix incorrect use of rst hg role in help text
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Mon, 01 Aug 2016 06:08:27 +0900 |
parents | 4fc4b8cc9957 |
children | acf27be56d26 |
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; | |
29602 | 129 int emptycount = 0; |
28060 | 130 while (cursize < hgc->ctx.datasize) { |
131 rsize = recv(hgc->sockfd, hgc->ctx.data + cursize, | |
132 hgc->ctx.datasize - cursize, 0); | |
29602 | 133 /* rsize == 0 normally indicates EOF, while it's also a valid |
134 * packet size for unix socket. treat it as EOF and abort if | |
135 * we get many empty responses in a row. */ | |
136 emptycount = (rsize == 0 ? emptycount + 1 : 0); | |
137 if (rsize < 0 || emptycount > 20) | |
28060 | 138 abortmsg("failed to read data block"); |
139 cursize += rsize; | |
140 } | |
141 } | |
142 | |
143 static void sendall(int sockfd, const void *data, size_t datasize) | |
144 { | |
145 const char *p = data; | |
146 const char *const endp = p + datasize; | |
147 while (p < endp) { | |
148 ssize_t r = send(sockfd, p, endp - p, 0); | |
149 if (r < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
150 abortmsgerrno("cannot communicate"); |
28060 | 151 p += r; |
152 } | |
153 } | |
154 | |
155 /* Write lengh-data block to cmdserver */ | |
156 static void writeblock(const hgclient_t *hgc) | |
157 { | |
158 assert(hgc); | |
159 | |
160 const uint32_t datasize_n = htonl(hgc->ctx.datasize); | |
161 sendall(hgc->sockfd, &datasize_n, sizeof(datasize_n)); | |
162 | |
163 sendall(hgc->sockfd, hgc->ctx.data, hgc->ctx.datasize); | |
164 } | |
165 | |
166 static void writeblockrequest(const hgclient_t *hgc, const char *chcmd) | |
167 { | |
168 debugmsg("request %s, block size %zu", chcmd, hgc->ctx.datasize); | |
169 | |
170 char buf[strlen(chcmd) + 1]; | |
171 memcpy(buf, chcmd, sizeof(buf) - 1); | |
172 buf[sizeof(buf) - 1] = '\n'; | |
173 sendall(hgc->sockfd, buf, sizeof(buf)); | |
174 | |
175 writeblock(hgc); | |
176 } | |
177 | |
178 /* Build '\0'-separated list of args. argsize < 0 denotes that args are | |
179 * terminated by NULL. */ | |
180 static void packcmdargs(context_t *ctx, const char *const args[], | |
181 ssize_t argsize) | |
182 { | |
183 ctx->datasize = 0; | |
184 const char *const *const end = (argsize >= 0) ? args + argsize : NULL; | |
185 for (const char *const *it = args; it != end && *it; ++it) { | |
186 const size_t n = strlen(*it) + 1; /* include '\0' */ | |
187 enlargecontext(ctx, ctx->datasize + n); | |
188 memcpy(ctx->data + ctx->datasize, *it, n); | |
189 ctx->datasize += n; | |
190 } | |
191 | |
192 if (ctx->datasize > 0) | |
193 --ctx->datasize; /* strip last '\0' */ | |
194 } | |
195 | |
196 /* Extract '\0'-separated list of args to new buffer, terminated by NULL */ | |
197 static const char **unpackcmdargsnul(const context_t *ctx) | |
198 { | |
199 const char **args = NULL; | |
200 size_t nargs = 0, maxnargs = 0; | |
201 const char *s = ctx->data; | |
202 const char *e = ctx->data + ctx->datasize; | |
203 for (;;) { | |
204 if (nargs + 1 >= maxnargs) { /* including last NULL */ | |
205 maxnargs += 256; | |
28166
c6e0c2533e5f
chg: use mallocx and reallocx in hgclient
Jun Wu <quark@fb.com>
parents:
28160
diff
changeset
|
206 args = reallocx(args, maxnargs * sizeof(args[0])); |
28060 | 207 } |
208 args[nargs] = s; | |
209 nargs++; | |
210 s = memchr(s, '\0', e - s); | |
211 if (!s) | |
212 break; | |
213 s++; | |
214 } | |
215 args[nargs] = NULL; | |
216 return args; | |
217 } | |
218 | |
219 static void handlereadrequest(hgclient_t *hgc) | |
220 { | |
221 context_t *ctx = &hgc->ctx; | |
222 size_t r = fread(ctx->data, sizeof(ctx->data[0]), ctx->datasize, stdin); | |
223 ctx->datasize = r; | |
224 writeblock(hgc); | |
225 } | |
226 | |
227 /* Read single-line */ | |
228 static void handlereadlinerequest(hgclient_t *hgc) | |
229 { | |
230 context_t *ctx = &hgc->ctx; | |
231 if (!fgets(ctx->data, ctx->datasize, stdin)) | |
232 ctx->data[0] = '\0'; | |
233 ctx->datasize = strlen(ctx->data); | |
234 writeblock(hgc); | |
235 } | |
236 | |
237 /* Execute the requested command and write exit code */ | |
238 static void handlesystemrequest(hgclient_t *hgc) | |
239 { | |
240 context_t *ctx = &hgc->ctx; | |
241 enlargecontext(ctx, ctx->datasize + 1); | |
242 ctx->data[ctx->datasize] = '\0'; /* terminate last string */ | |
243 | |
244 const char **args = unpackcmdargsnul(ctx); | |
245 if (!args[0] || !args[1]) | |
246 abortmsg("missing command or cwd in system request"); | |
247 debugmsg("run '%s' at '%s'", args[0], args[1]); | |
248 int32_t r = runshellcmd(args[0], args + 2, args[1]); | |
249 free(args); | |
250 | |
251 uint32_t r_n = htonl(r); | |
252 memcpy(ctx->data, &r_n, sizeof(r_n)); | |
253 ctx->datasize = sizeof(r_n); | |
254 writeblock(hgc); | |
255 } | |
256 | |
257 /* Read response of command execution until receiving 'r'-esult */ | |
258 static void handleresponse(hgclient_t *hgc) | |
259 { | |
260 for (;;) { | |
261 readchannel(hgc); | |
262 context_t *ctx = &hgc->ctx; | |
263 debugmsg("response read from channel %c, size %zu", | |
264 ctx->ch, ctx->datasize); | |
265 switch (ctx->ch) { | |
266 case 'o': | |
267 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | |
268 stdout); | |
269 break; | |
270 case 'e': | |
271 fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | |
272 stderr); | |
273 break; | |
274 case 'd': | |
275 /* assumes last char is '\n' */ | |
276 ctx->data[ctx->datasize - 1] = '\0'; | |
277 debugmsg("server: %s", ctx->data); | |
278 break; | |
279 case 'r': | |
280 return; | |
281 case 'I': | |
282 handlereadrequest(hgc); | |
283 break; | |
284 case 'L': | |
285 handlereadlinerequest(hgc); | |
286 break; | |
287 case 'S': | |
288 handlesystemrequest(hgc); | |
289 break; | |
290 default: | |
291 if (isupper(ctx->ch)) | |
292 abortmsg("cannot handle response (ch = %c)", | |
293 ctx->ch); | |
294 } | |
295 } | |
296 } | |
297 | |
298 static unsigned int parsecapabilities(const char *s, const char *e) | |
299 { | |
300 unsigned int flags = 0; | |
301 while (s < e) { | |
302 const char *t = strchr(s, ' '); | |
303 if (!t || t > e) | |
304 t = e; | |
305 const cappair_t *cap; | |
306 for (cap = captable; cap->flag; ++cap) { | |
307 size_t n = t - s; | |
308 if (strncmp(s, cap->name, n) == 0 && | |
309 strlen(cap->name) == n) { | |
310 flags |= cap->flag; | |
311 break; | |
312 } | |
313 } | |
314 s = t + 1; | |
315 } | |
316 return flags; | |
317 } | |
318 | |
319 static void readhello(hgclient_t *hgc) | |
320 { | |
321 readchannel(hgc); | |
322 context_t *ctx = &hgc->ctx; | |
28512
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
323 if (ctx->ch != 'o') { |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
324 char ch = ctx->ch; |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
325 if (ch == 'e') { |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
326 /* write early error and will exit */ |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
327 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
|
328 stderr); |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
329 handleresponse(hgc); |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
330 } |
b957b4c6cad8
chg: provide early exception to user
Yuya Nishihara <yuya@tcha.org>
parents:
28356
diff
changeset
|
331 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
|
332 } |
28060 | 333 enlargecontext(ctx, ctx->datasize + 1); |
334 ctx->data[ctx->datasize] = '\0'; | |
335 debugmsg("hello received: %s (size = %zu)", ctx->data, ctx->datasize); | |
336 | |
337 const char *s = ctx->data; | |
338 const char *const dataend = ctx->data + ctx->datasize; | |
339 while (s < dataend) { | |
340 const char *t = strchr(s, ':'); | |
341 if (!t || t[1] != ' ') | |
342 break; | |
343 const char *u = strchr(t + 2, '\n'); | |
344 if (!u) | |
345 u = dataend; | |
346 if (strncmp(s, "capabilities:", t - s + 1) == 0) { | |
347 hgc->capflags = parsecapabilities(t + 2, u); | |
29581 | 348 } else if (strncmp(s, "pgid:", t - s + 1) == 0) { |
349 hgc->pgid = strtol(t + 2, NULL, 10); | |
28060 | 350 } else if (strncmp(s, "pid:", t - s + 1) == 0) { |
351 hgc->pid = strtol(t + 2, NULL, 10); | |
352 } | |
353 s = u + 1; | |
354 } | |
355 debugmsg("capflags=0x%04x, pid=%d", hgc->capflags, hgc->pid); | |
356 } | |
357 | |
358 static void attachio(hgclient_t *hgc) | |
359 { | |
360 debugmsg("request attachio"); | |
361 static const char chcmd[] = "attachio\n"; | |
362 sendall(hgc->sockfd, chcmd, sizeof(chcmd) - 1); | |
363 readchannel(hgc); | |
364 context_t *ctx = &hgc->ctx; | |
365 if (ctx->ch != 'I') | |
366 abortmsg("unexpected response for attachio (ch = %c)", ctx->ch); | |
367 | |
368 static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; | |
369 struct msghdr msgh; | |
370 memset(&msgh, 0, sizeof(msgh)); | |
371 struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */ | |
372 msgh.msg_iov = &iov; | |
373 msgh.msg_iovlen = 1; | |
374 char fdbuf[CMSG_SPACE(sizeof(fds))]; | |
375 msgh.msg_control = fdbuf; | |
376 msgh.msg_controllen = sizeof(fdbuf); | |
377 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); | |
378 cmsg->cmsg_level = SOL_SOCKET; | |
379 cmsg->cmsg_type = SCM_RIGHTS; | |
380 cmsg->cmsg_len = CMSG_LEN(sizeof(fds)); | |
381 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds)); | |
382 msgh.msg_controllen = cmsg->cmsg_len; | |
383 ssize_t r = sendmsg(hgc->sockfd, &msgh, 0); | |
384 if (r < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
385 abortmsgerrno("sendmsg failed"); |
28060 | 386 |
387 handleresponse(hgc); | |
388 int32_t n; | |
389 if (ctx->datasize != sizeof(n)) | |
390 abortmsg("unexpected size of attachio result"); | |
391 memcpy(&n, ctx->data, sizeof(n)); | |
392 n = ntohl(n); | |
393 if (n != sizeof(fds) / sizeof(fds[0])) | |
394 abortmsg("failed to send fds (n = %d)", n); | |
395 } | |
396 | |
397 static void chdirtocwd(hgclient_t *hgc) | |
398 { | |
399 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
|
400 abortmsgerrno("failed to getcwd"); |
28060 | 401 hgc->ctx.datasize = strlen(hgc->ctx.data); |
402 writeblockrequest(hgc, "chdir"); | |
403 } | |
404 | |
28160
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
405 static void forwardumask(hgclient_t *hgc) |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
406 { |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
407 mode_t mask = umask(0); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
408 umask(mask); |
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 static const char command[] = "setumask\n"; |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
411 sendall(hgc->sockfd, command, sizeof(command) - 1); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
412 uint32_t data = htonl(mask); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
413 sendall(hgc->sockfd, &data, sizeof(data)); |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
414 } |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
415 |
28060 | 416 /*! |
417 * Open connection to per-user cmdserver | |
418 * | |
419 * If no background server running, returns NULL. | |
420 */ | |
421 hgclient_t *hgc_open(const char *sockname) | |
422 { | |
423 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
424 if (fd < 0) | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
425 abortmsgerrno("cannot create socket"); |
28060 | 426 |
427 /* don't keep fd on fork(), so that it can be closed when the parent | |
428 * 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
|
429 fsetcloexec(fd); |
28060 | 430 |
431 struct sockaddr_un addr; | |
432 addr.sun_family = AF_UNIX; | |
433 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)); | |
434 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; | |
435 | |
436 int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); | |
437 if (r < 0) { | |
438 close(fd); | |
439 if (errno == ENOENT || errno == ECONNREFUSED) | |
440 return NULL; | |
28789
7f6e0a15189b
chg: replace abortmsg showing errno with abortmsgerrno
Jun Wu <quark@fb.com>
parents:
28769
diff
changeset
|
441 abortmsgerrno("cannot connect to %s", addr.sun_path); |
28060 | 442 } |
28769
222f482930c8
chg: make connect debug message less repetitive
Jun Wu <quark@fb.com>
parents:
28551
diff
changeset
|
443 debugmsg("connected to %s", addr.sun_path); |
28060 | 444 |
28166
c6e0c2533e5f
chg: use mallocx and reallocx in hgclient
Jun Wu <quark@fb.com>
parents:
28160
diff
changeset
|
445 hgclient_t *hgc = mallocx(sizeof(hgclient_t)); |
28060 | 446 memset(hgc, 0, sizeof(*hgc)); |
447 hgc->sockfd = fd; | |
448 initcontext(&hgc->ctx); | |
449 | |
450 readhello(hgc); | |
451 if (!(hgc->capflags & CAP_RUNCOMMAND)) | |
452 abortmsg("insufficient capability: runcommand"); | |
453 if (hgc->capflags & CAP_ATTACHIO) | |
454 attachio(hgc); | |
455 if (hgc->capflags & CAP_CHDIR) | |
456 chdirtocwd(hgc); | |
28160
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
457 if (hgc->capflags & CAP_SETUMASK) |
098cb7bd46a7
chg: forward umask from client to server
Jun Wu <quark@fb.com>
parents:
28060
diff
changeset
|
458 forwardumask(hgc); |
28060 | 459 |
460 return hgc; | |
461 } | |
462 | |
463 /*! | |
464 * Close connection and free allocated memory | |
465 */ | |
466 void hgc_close(hgclient_t *hgc) | |
467 { | |
468 assert(hgc); | |
469 freecontext(&hgc->ctx); | |
470 close(hgc->sockfd); | |
471 free(hgc); | |
472 } | |
473 | |
29581 | 474 pid_t hgc_peerpgid(const hgclient_t *hgc) |
475 { | |
476 assert(hgc); | |
477 return hgc->pgid; | |
478 } | |
479 | |
28060 | 480 pid_t hgc_peerpid(const hgclient_t *hgc) |
481 { | |
482 assert(hgc); | |
483 return hgc->pid; | |
484 } | |
485 | |
486 /*! | |
28356
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
487 * 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
|
488 * whether it can process our request directly or not. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
489 * Make sure hgc_setenv is called before calling this. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
490 * |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
491 * @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
|
492 * support "validate" command. |
28535
aa082a8125da
chgserver: add an explicit "reconnect" instruction to validate
Jun Wu <quark@fb.com>
parents:
28512
diff
changeset
|
493 * - 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
|
494 * 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
|
495 * chgserver.py for possible instruction formats. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
496 * the list should be freed by the caller. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
497 * the last string is guaranteed to be NULL. |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
498 */ |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
499 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
|
500 size_t argsize) |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
501 { |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
502 assert(hgc); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
503 if (!(hgc->capflags & CAP_VALIDATE)) |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
504 return NULL; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
505 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
506 packcmdargs(&hgc->ctx, args, argsize); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
507 writeblockrequest(hgc, "validate"); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
508 handleresponse(hgc); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
509 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
510 /* 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
|
511 if (hgc->ctx.datasize <= 1) |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
512 return NULL; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
513 |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
514 /* make sure the buffer is '\0' terminated */ |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
515 enlargecontext(&hgc->ctx, hgc->ctx.datasize + 1); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
516 hgc->ctx.data[hgc->ctx.datasize] = '\0'; |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
517 return unpackcmdargsnul(&hgc->ctx); |
a5c773acb018
chg: implement validate in hgclient
Jun Wu <quark@fb.com>
parents:
28166
diff
changeset
|
518 } |
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 /*! |
28060 | 521 * Execute the specified Mercurial command |
522 * | |
523 * @return result code | |
524 */ | |
525 int hgc_runcommand(hgclient_t *hgc, const char *const args[], size_t argsize) | |
526 { | |
527 assert(hgc); | |
528 | |
529 packcmdargs(&hgc->ctx, args, argsize); | |
530 writeblockrequest(hgc, "runcommand"); | |
531 handleresponse(hgc); | |
532 | |
533 int32_t exitcode_n; | |
534 if (hgc->ctx.datasize != sizeof(exitcode_n)) { | |
535 abortmsg("unexpected size of exitcode"); | |
536 } | |
537 memcpy(&exitcode_n, hgc->ctx.data, sizeof(exitcode_n)); | |
538 return ntohl(exitcode_n); | |
539 } | |
540 | |
541 /*! | |
542 * (Re-)send client's stdio channels so that the server can access to tty | |
543 */ | |
544 void hgc_attachio(hgclient_t *hgc) | |
545 { | |
546 assert(hgc); | |
547 if (!(hgc->capflags & CAP_ATTACHIO)) | |
548 return; | |
549 attachio(hgc); | |
550 } | |
551 | |
552 /*! | |
553 * Get pager command for the given Mercurial command args | |
554 * | |
555 * If no pager enabled, returns NULL. The return value becomes invalid | |
556 * once you run another request to hgc. | |
557 */ | |
558 const char *hgc_getpager(hgclient_t *hgc, const char *const args[], | |
559 size_t argsize) | |
560 { | |
561 assert(hgc); | |
562 | |
563 if (!(hgc->capflags & CAP_GETPAGER)) | |
564 return NULL; | |
565 | |
566 packcmdargs(&hgc->ctx, args, argsize); | |
567 writeblockrequest(hgc, "getpager"); | |
568 handleresponse(hgc); | |
569 | |
570 if (hgc->ctx.datasize < 1 || hgc->ctx.data[0] == '\0') | |
571 return NULL; | |
572 enlargecontext(&hgc->ctx, hgc->ctx.datasize + 1); | |
573 hgc->ctx.data[hgc->ctx.datasize] = '\0'; | |
574 return hgc->ctx.data; | |
575 } | |
576 | |
577 /*! | |
578 * Update server's environment variables | |
579 * | |
580 * @param envp list of environment variables in "NAME=VALUE" format, | |
581 * terminated by NULL. | |
582 */ | |
583 void hgc_setenv(hgclient_t *hgc, const char *const envp[]) | |
584 { | |
585 assert(hgc && envp); | |
586 if (!(hgc->capflags & CAP_SETENV)) | |
587 return; | |
588 packcmdargs(&hgc->ctx, envp, /*argsize*/ -1); | |
589 writeblockrequest(hgc, "setenv"); | |
590 } |