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