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