-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathvcmd_main.c
439 lines (351 loc) · 8.89 KB
/
vcmd_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* CORE
* Copyright (c)2010-2012 the Boeing Company.
* See the LICENSE file included in this distribution.
*
* author: Tom Goff <thomas.goff@boeing.com>
*
* vcmd_main.c
*
* vcmd utility program for executing programs in an existing namespace
* specified by the given channel.
*
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include "version.h"
#include "vnode_chnl.h"
#include "vnode_cmd.h"
#include "vnode_client.h"
#include "myerr.h"
#define FORWARD_SIGNALS
#define VCMD_DEFAULT_CMD "/bin/bash"
int verbose;
typedef struct {
vnode_client_t *client;
vnode_client_cmdio_t *cmdio;
int argc;
char **argv;
int cmdid;
int cmdstatus;
ev_io stdin_watcher;
int stdin_fwdfd;
ev_io ptymaster_watcher;
int ptymaster_fwdfd;
} vcmd_t;
static vcmd_t vcmd;
static struct termios saveattr;
static int saveattr_set;
struct option longopts[] =
{
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{ 0 }
};
void usage(int status, char *fmt, ...)
{
extern const char *__progname;
va_list ap;
FILE *output;
va_start(ap, fmt);
output = status ? stderr : stdout;
fprintf(output, "\n");
if (fmt != NULL)
{
vfprintf(output, fmt, ap);
fprintf(output, "\n\n");
}
fprintf(output,
"Usage: %s [-h|-V] [-v] [-q|-i|-I] -c <channel name> -- command args"
"...\n\n"
"Run the specified command in the Linux namespace container "
"specified by the \ncontrol <channel name>, with the specified "
"arguments.\n\nOptions:\n"
" -h, --help show this help message and exit\n"
" -V, --version show version number and exit\n"
" -v enable verbose logging\n"
" -q run the command quietly, without local input or output\n"
" -i run the command interactively (use PTY)\n"
" -I run the command non-interactively (without PTY)\n"
" -c control channel name (e.g. '/tmp/pycore.45647/n3')\n",
__progname);
va_end(ap);
exit(status);
}
static void vcmd_rwcb(struct ev_loop *loop, ev_io *w, int revents)
{
int outfd = *(int *)w->data;
char buf[BUFSIZ];
ssize_t rcount, wcount;
rcount = read(w->fd, buf, sizeof(buf));
if (rcount <= 0)
{
ev_io_stop(loop, w);
}
else
{
wcount = write(outfd, buf, rcount);
if (wcount != rcount)
WARN("write() error: wrote %d of %d bytes", wcount, rcount);
}
return;
}
static void vcmd_cmddonecb(int32_t cmdid, pid_t pid, int status, void *data)
{
vcmd_t *vcmd = data;
if (vcmd->cmdio->iotype == VCMD_IO_PTY)
{
ev_io_stop(vcmd->client->loop, &vcmd->stdin_watcher);
ev_io_stop(vcmd->client->loop, &vcmd->ptymaster_watcher);
/* drain command output */
for (;;)
{
char buf[BUFSIZ];
ssize_t rcount, wcount;
rcount = read(vcmd->ptymaster_watcher.fd, buf, sizeof(buf));
if (rcount <= 0)
break;
wcount = write(STDOUT_FILENO, buf, rcount);
if (wcount != rcount)
WARN("write() error: %d of %d bytes", wcount, rcount);
}
}
vnode_close_clientcmdio(vcmd->cmdio);
#ifdef DEBUG
WARNX("cmdid %u; pid %d; status: 0x%x", cmdid, pid, status);
#endif
if (WIFEXITED(status))
/* normal terminataion */
vcmd->cmdstatus = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
if (verbose)
INFO("command %u terminated by signal: %d", cmdid, WTERMSIG(status));
vcmd->cmdstatus = 255;
}
else
{
INFO("unexpected termination status for command %u: 0x%x", cmdid, status);
vcmd->cmdstatus = 255;
}
vcmd->cmdid = -1;
ev_unloop(vcmd->client->loop, EVUNLOOP_ALL);
return;
}
static void vcmd_cmdreqcb(struct ev_loop *loop, ev_timer *w, int revents)
{
vcmd_t *vcmd = w->data;
#ifdef DEBUG
WARNX("sending command request: serverfd %d; vcmd %p",
vcmd->client->serverfd, vcmd);
#endif
if (vcmd->cmdio->iotype == VCMD_IO_PTY)
{
/* setup forwarding i/o */
vcmd->stdin_fwdfd = vcmd->cmdio->stdiopty.masterfd;
vcmd->stdin_watcher.data = &vcmd->stdin_fwdfd;
ev_io_init(&vcmd->stdin_watcher, vcmd_rwcb, STDIN_FILENO, EV_READ);
ev_io_start(loop, &vcmd->stdin_watcher);
vcmd->ptymaster_fwdfd = STDOUT_FILENO;
vcmd->ptymaster_watcher.data = &vcmd->ptymaster_fwdfd;
ev_io_init(&vcmd->ptymaster_watcher, vcmd_rwcb,
vcmd->cmdio->stdiopty.masterfd, EV_READ);
ev_io_start(loop, &vcmd->ptymaster_watcher);
}
vcmd->cmdid = vnode_client_cmdreq(vcmd->client, vcmd->cmdio,
vcmd_cmddonecb, vcmd,
vcmd->argc, vcmd->argv);
if (vcmd->cmdid < 0)
{
WARNX("vnode_client_cmdreq() failed");
vnode_delclient(vcmd->client);
vcmd->client = NULL;
exit(255);
}
return;
}
static void vcmd_ioerrorcb(vnode_client_t *client)
{
vcmd_t *vcmd = client->data;
WARNX("i/o error");
vnode_delclient(client);
vcmd->client = NULL;
exit(1);
return;
}
#ifdef FORWARD_SIGNALS
static void sighandler(int signum)
{
if (!vcmd.client || vcmd.cmdid < 0)
return;
#ifdef DEBUG
WARNX("sending command signal: serverfd %d; cmdid %u; signum: %d",
vcmd.client->serverfd, vcmd.cmdid, signum);
#endif
if (vnode_send_cmdsignal(vcmd.client->serverfd, vcmd.cmdid, signum))
WARN("vnode_send_cmdsignal() failed");
return;
}
#endif /* FORWARD_SIGNALS */
static void sigwinch_handler(int signum)
{
struct winsize wsiz;
if (signum != SIGWINCH)
{
WARNX("unexpected signal number: %d", signum);
return;
}
if (!vcmd.cmdio || vcmd.cmdio->iotype != VCMD_IO_PTY)
return;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsiz))
{
WARN("ioctl() failed");
return;
}
if (ioctl(vcmd.cmdio->stdiopty.masterfd, TIOCSWINSZ, &wsiz))
WARN("ioctl() failed");
return;
}
static int termioraw(int fd, struct termios *saveattr)
{
int err;
struct termios raw = {};
err = tcgetattr(fd, saveattr);
if (err)
{
WARN("tcgetattr() failed");
return err;
}
cfmakeraw(&raw);
err = tcsetattr(fd, TCSADRAIN, &raw);
if (err)
{
WARN("tcsetattr() failed");
return err;
}
return 0;
}
static void cleanup(void)
{
if (saveattr_set)
if (tcsetattr(STDOUT_FILENO, TCSADRAIN, &saveattr))
WARN("tcsetattr() failed");
return;
}
int main(int argc, char *argv[])
{
char *ctrlchnlname = NULL;
vnode_client_cmdiotype_t iotype = VCMD_IO_FD;
ev_timer cmdreq;
extern const char *__progname;
#ifdef FORWARD_SIGNALS
int i;
struct sigaction sig_action = {
.sa_handler = sighandler,
};
#endif /* FORWARD_SIGNALS */
char *def_argv[2] = { VCMD_DEFAULT_CMD, 0 };
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) &&
isatty(STDERR_FILENO) && getpgrp() == tcgetpgrp(STDOUT_FILENO))
iotype = VCMD_IO_PTY;
/* Parse command line argument list */
for (;;)
{
int opt;
if ((opt = getopt_long(argc, argv, "c:hiIqvV", longopts, NULL)) == -1)
break;
switch (opt)
{
case 'c':
ctrlchnlname = optarg;
break;
case 'i':
iotype = VCMD_IO_PTY;
break;
case 'I':
iotype = VCMD_IO_FD;
break;
case 'q':
iotype = VCMD_IO_NONE;
break;
case 'v':
verbose++;
break;
case 'V':
printf("%s version %s\n", __progname, CORE_VERSION);
exit(0);
case 'h':
/* pass through */
default:
usage(0, NULL);
}
}
argc -= optind;
argv += optind;
if (ctrlchnlname == NULL)
usage(1, "no control channel name given");
if (!argc)
{
argc = 1;
argv = def_argv;
}
if (argc >= VNODE_ARGMAX)
usage(1, "too many command arguments");
if (atexit(cleanup))
ERR(1, "atexit() failed");
#ifdef FORWARD_SIGNALS
for (i = 1; i < _NSIG; i++)
if (sigaction(i, &sig_action, NULL))
if (verbose && i != SIGKILL && i != SIGSTOP)
WARN("sigaction() failed for %d", i);
#endif /* FORWARD_SIGNALS */
vcmd.cmdio = vnode_open_clientcmdio(iotype);
if (!vcmd.cmdio)
ERR(1, "vnode_open_clientcmdio() failed");
vcmd.argc = argc;
vcmd.argv = argv;
vcmd.cmdstatus = 255;
switch (vcmd.cmdio->iotype)
{
case VCMD_IO_NONE:
break;
case VCMD_IO_FD:
SET_STDIOFD(vcmd.cmdio, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
break;
case VCMD_IO_PTY:
{
struct sigaction sigwinch_action = {
.sa_handler = sigwinch_handler,
};
if (sigaction(SIGWINCH, &sigwinch_action, NULL))
WARN("sigaction() failed for SIGWINCH");
sigwinch_handler(SIGWINCH);
if (termioraw(STDOUT_FILENO, &saveattr))
WARNX("termioraw() failed");
else
saveattr_set = 1;
}
break;
default:
ERR(1, "unsupported i/o type: %u", vcmd.cmdio->iotype);
break;
}
vcmd.client = vnode_client(ev_default_loop(0), ctrlchnlname,
vcmd_ioerrorcb, &vcmd);
if (!vcmd.client)
ERR(1, "vnode_client() failed");
cmdreq.data = &vcmd;
ev_timer_init(&cmdreq, vcmd_cmdreqcb, 0, 0);
ev_timer_start(vcmd.client->loop, &cmdreq);
ev_loop(vcmd.client->loop, 0);
vnode_delclient(vcmd.client);
exit(vcmd.cmdstatus);
}