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