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