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