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