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