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