This repository was archived by the owner on Mar 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathshim.c
664 lines (582 loc) · 15.7 KB
/
shim.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <poll.h>
#include <assert.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <inttypes.h>
#include <getopt.h>
#include <stdbool.h>
#include <limits.h>
#include "utils.h"
#include "log.h"
#include "shim.h"
/* globals */
/* Pipe used for capturing signal occurence */
int signal_pipe_fd[2] = { -1, -1 };
static char *program_name;
/*!
* Add file descriptor to the array of polled descriptors
*
* \param poll_fds Array of polled fds
* \param nfds Number of fds present in the array
* \param fd File descriptor to add
* \param events Events for the fd that should be polled
*/
void add_pollfd(struct pollfd *poll_fds, nfds_t *nfds, int fd, short events) {
struct pollfd pfd = { 0 };
if ( !poll_fds || !nfds || fd < 0) {
return;
}
assert(*nfds < MAX_POLL_FDS);
pfd.fd = fd;
pfd.events = events;
poll_fds[*nfds] = pfd;
(*nfds)++;
}
/*!
* Signal handler for the signals that should be caught and
* forwarded to the proxy
*
* \param signal_no Signal number of the signal caught
*/
static void
signal_handler(int signal_no)
{
int savedErrno; /* In case we change 'errno' */
savedErrno = errno;
/* Write signal number to pipe, so that the signal can be identfied later */
if (write(signal_pipe_fd[1], &signal_no, sizeof(signal_no)) == -1 && errno != EAGAIN) {
return;
}
errno = savedErrno;
}
/*!
* Assign signal handler for all the signals that should be
* forwarded by the shim to the proxy.
*
* \param sa Signal handler
* \return true on success, false otherwise
*/
bool
assign_all_signals(struct sigaction *sa)
{
if (! sa) {
return false;
}
for (int i = 0; shim_signal_table[i]; i++) {
if (sigaction(shim_signal_table[i], sa, NULL) == -1) {
shim_error("Error assigning signal handler for %d : %s\n",
shim_signal_table[i], strerror(errno));
return false;
}
}
return true;
}
/*!
* Print formatted message to stderr and exit with EXIT_FAILURE
*
* \param format Format that specifies how subsequent arguments are
* converted for output
*/
void
err_exit(const char *format, ...)
{
va_list args;
if ( !format) {
return;
}
fprintf(stderr, "%s: ", program_name);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
/*!
* Construct message in the proxy ctl rpc protocol format
* Proxy control message format:
*
* | length | Reserved| Data(Request/Response |
* | . . . . | . . . . | . . . . . . . . . . . . |
* 0 4 8 length
*
* \param json Json Payload
* \param[out] len Length of the message
*
* \return Newly allocated string on sucess, NULL on failure
*/
char*
get_proxy_ctl_msg(char *json, size_t *len) {
char *proxy_ctl_msg = NULL;
if (! (json && len)) {
return NULL;
}
*len = strlen(json) + PROXY_CTL_HEADER_SIZE + 1;
proxy_ctl_msg = calloc(*len, sizeof(char));
if (! proxy_ctl_msg) {
abort();
}
set_big_endian_32((uint8_t*)proxy_ctl_msg + PROXY_CTL_HEADER_LENGTH_OFFSET,
(uint32_t)(strlen(json) + PROXY_CTL_HEADER_SIZE));
strcpy(proxy_ctl_msg + PROXY_CTL_HEADER_SIZE, json);
return proxy_ctl_msg;
}
/*!
* Send "hyper" payload to cc-proxy. This will be forwarded to hyperstart.
*
* \param fd File descriptor to send the message to(should be proxy ctl socket fd)
* \param Hyperstart cmd id
* \param json Json payload
*/
void
send_proxy_hyper_message(int fd, const char *hyper_cmd, char *json) {
char *proxy_payload = NULL;
char *proxy_command_id = "hyper";
char *proxy_ctl_msg = NULL;
size_t len = 0, offset = 0;
ssize_t ret;
/* cc-proxy has the following format for "hyper" payload:
* {
* "id": "hyper",
* "data": {
* "hyperName": "ping",
* "data": "json payload",
* }
* }
*/
if ( !json || fd < 0) {
return;
}
ret = asprintf(&proxy_payload,
"{\"id\":\"%s\",\"data\":{\"hyperName\":\"%s\",\"data\":%s}}",
proxy_command_id, hyper_cmd, json);
if (ret == -1) {
abort();
}
proxy_ctl_msg = get_proxy_ctl_msg(proxy_payload, &len);
free(proxy_payload);
while (offset < len) {
ret = write(fd, proxy_ctl_msg + offset, len-offset);
if (ret == EINTR) {
continue;
}
if (ret <= 0 ) {
free(proxy_ctl_msg);
shim_error("Error writing to proxy: %s\n", strerror(errno));
return;
}
offset += (size_t)ret;
}
free(proxy_ctl_msg);
}
/*!
* Read signals received and send message in the hyperstart protocol
* format to outfd
*
* \param container_id Container id
* \param outfd File descriptor to send the message to
*/
void
handle_signals(char *container_id, int outfd) {
int sig;
char *buf;
int ret;
char *cmd = NULL;
struct winsize ws;
static char* cmds[] = { "winsize", "killcontainer"};
if (! container_id || outfd < 0) {
return;
}
while (read(signal_pipe_fd[0], &sig, sizeof(sig)) != -1) {
printf("Handling signal : %d on fd %d\n", sig, signal_pipe_fd[0]);
if (sig == SIGWINCH ) {
cmd = cmds[0];
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
shim_warning("Error getting the current window size: %s\n",
strerror(errno));
continue;
}
ret = asprintf(&buf, "{\"container_id\":\"%s\", \"row\":\"%d\", \"col\":\"%d\"}",
container_id, ws.ws_row, ws.ws_col);
shim_debug("handled SIGWINCH for container %s (row=%d, col=%d)\n",
container_id, ws.ws_row, ws.ws_col);
} else {
cmd = cmds[1];
ret = asprintf(&buf, "{\"container_id\":\"%s\", \"signal\":\"%d\"}",
container_id, sig);
shim_debug("Killed container %s with signal %d\n", container_id, sig);
}
if (ret == -1) {
abort();
}
send_proxy_hyper_message(outfd, cmd, buf);
free(buf);
}
}
/*!
* Read data from infd and write to outfd
*
* \param infd File descriptor to read from
* \param outfd File descriptor to write to
*/
void
handle_stdin(struct cc_shim *shim)
{
ssize_t nread;
char buf[BUFSIZ-12];
char wbuf[BUFSIZ];
int ret;
ssize_t len;
if (! shim || shim->proxy_io_fd < 0) {
return;
}
// write data to I/O fd
while ((nread = read(STDIN_FILENO, buf, BUFSIZ-12)) != -1) {
set_big_endian_64 ((uint8_t*)wbuf, shim->io_seq_no);
len = nread + STREAM_HEADER_SIZE;
set_big_endian_32 ((uint8_t*)wbuf, (uint32_t)len);
strncpy(wbuf, buf, (size_t)nread);
// TODO: handle write in the poll loop to account for write blocking
ret = (int)write(shim->proxy_io_fd, wbuf, (size_t)nread);
if (ret == -1) {
shim_warning("Error writing from fd %d to fd %d: %s\n",
STDIN_FILENO, shim->proxy_io_fd, strerror(errno));
return;
}
}
}
/*!
* Read and parse I/O message on proxy I/O fd
*
* \param shim \ref cc_shim
* \param[out] seq Seqence number of the I/O stream
* \param[out] stream_len Length of the data received
*
* \return newly allocated string on success, else \c NULL.
*/
char*
read_IO_message(struct cc_shim *shim, uint64_t *seq, ssize_t *stream_len) {
char *buf = NULL;
ssize_t need_read = STREAM_HEADER_SIZE;
ssize_t bytes_read = 0, want, ret;
if (! (shim && seq && stream_len)) {
return NULL;
}
*stream_len = 0;
buf = calloc(STREAM_HEADER_SIZE, 1);
if (! buf ) {
abort();
}
while (bytes_read < need_read) {
want = need_read - bytes_read;
if (want > BUFSIZ) {
want = BUFSIZ;
}
ret = read(shim->proxy_io_fd, buf+bytes_read, (size_t)want);
if (ret == -1) {
shim_warning("Error reading from proxy I/O fd: %s\n", strerror(errno));
free(buf);
return NULL;
} else if (ret == 0) {
/* EOF received on proxy I/O fd*/
shim_warning("EOF received on proxy I/O fd\n");
free(buf);
return NULL;
}
bytes_read += ret;
if (*stream_len == 0 && bytes_read >=12) {
*stream_len = get_big_endian_32(buf+STREAM_HEADER_LENGTH_OFFSET);
// length is 12 when hyperstart sends eof before sending exit code
if (*stream_len == STREAM_HEADER_SIZE) {
break;
}
if (*stream_len > STREAM_HEADER_SIZE) {
need_read = *stream_len;
buf = realloc(buf, (size_t)*stream_len);
if (! buf) {
abort();
}
}
}
}
*seq = get_big_endian_64(buf);
return buf;
}
/*!
* Handle output on the proxy I/O fd
*
*\param shim \ref cc_shim
*/
void
handle_proxy_output(struct cc_shim *shim)
{
uint64_t seq;
char *buf;
int outfd;
ssize_t stream_len = 0;
ssize_t ret;
ssize_t offset;
int code = 0;
if (shim == NULL) {
return;
}
buf = read_IO_message(shim, &seq, &stream_len);
if ( !buf) {
/*TODO: is exiting here more appropriate, since this denotes
* error communicating with proxy or proxy has exited
*/
return;
}
if (seq == shim->io_seq_no) {
outfd = STDOUT_FILENO;
} else if (seq == shim->io_seq_no + 1) {//proxy allocates errseq 1 higher
outfd = STDERR_FILENO;
} else {
shim_warning("Seq no %"PRIu64 " received from proxy does not match with\
shim seq %"PRIu64 "\n", seq, shim->io_seq_no);
free(buf);
return;
}
if (!shim->exiting && stream_len == STREAM_HEADER_SIZE) {
shim->exiting = true;
free(buf);
return;
} else if (shim->exiting && stream_len == (STREAM_HEADER_SIZE+1)) {
code = atoi(buf + STREAM_HEADER_SIZE); // hyperstart has sent the exit status
free(buf);
exit(code);
}
/* TODO: what if writing to stdout/err blocks? Add this to the poll loop
* to watch out for EPOLLOUT
*/
offset = STREAM_HEADER_SIZE;
while (offset < stream_len) {
ret = write(outfd, buf+offset, (size_t)(stream_len-offset));
if (ret <= 0 ) {
free(buf);
return;
}
offset += (ssize_t)ret;
}
free(buf);
}
/*!
* Handle data on the proxy ctl socket fd
*
*\param shim \ref cc_shim
*/
void
handle_proxy_ctl(struct cc_shim *shim)
{
char buf[LINE_MAX] = { 0 };
ssize_t ret;
if (! shim) {
return;
}
ret = read(shim->proxy_sock_fd, buf, LINE_MAX-1);
if (ret == -1) {
shim_warning("Error reading from the proxy ctl socket: %s\n", strerror(errno));
exit(EXIT_FAILURE);
} else if (ret == 0) {
shim_warning("EOF received on proxy ctl socket. Proxy has exited\n");
exit(EXIT_FAILURE);
}
//TODO: Parse the json and log error responses explicitly
shim_debug("Proxy response:%s\n", buf + PROXY_CTL_HEADER_SIZE);
}
/*
* Parse number from input
*
* \return Long long integer on success, -1 on failure
*/
long long
parse_numeric_option(char *input) {
char *endptr;
long long num;
if ( !input) {
return -1;
}
errno = 0;
num = strtoll(input, &endptr, 10);
if ( errno || *endptr ) {
return -1;
}
return num;
}
/*!
* Print program usage
*/
void
print_usage(void) {
printf("%s: Usage\n", program_name);
printf(" -c, --container-id Container id\n");
printf(" -p, --proxy-sock-fd File descriptor of the socket connected to cc-proxy\n");
printf(" -o, --proxy-io-fd File descriptor of I/0 fd sent by the cc-proxy\n");
printf(" -s, --seq-no Sequence no for stdin and stdout\n");
printf(" -e, --err-seq-no Sequence no for stderr\n");
printf(" -d, --debug Enable debug output\n");
printf(" -h, --help Display this help message\n");
}
int
main(int argc, char **argv)
{
struct cc_shim shim = {
.container_id = NULL,
.proxy_sock_fd = -1,
.proxy_io_fd = -1,
.io_seq_no = 0,
.err_seq_no = 0,
.exiting = false,
};
struct pollfd poll_fds[MAX_POLL_FDS] = {{-1}};
nfds_t nfds = 0;
int ret;
struct sigaction sa;
int c;
bool debug = false;
long long val;
program_name = argv[0];
struct option prog_opts[] = {
{"container-id", required_argument, 0, 'c'},
{"proxy-sock-fd", required_argument, 0, 'p'},
{"proxy-io-fd", required_argument, 0, 'o'},
{"seq-no", required_argument, 0, 's'},
{"err-seq-no", required_argument, 0, 'e'},
{"debug", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{ 0, 0, 0, 0},
};
while ((c = getopt_long(argc, argv, "c:p:o:s:e:dh", prog_opts, NULL))!= -1) {
switch (c) {
case 'c':
shim.container_id = strdup(optarg);
break;
case 'p':
shim.proxy_sock_fd = (int)parse_numeric_option(optarg);
if (shim.proxy_sock_fd < 0) {
err_exit("Invalid value for proxy socket fd\n");
}
break;
case 'o':
shim.proxy_io_fd = (int)parse_numeric_option(optarg);
if (shim.proxy_io_fd < 0) {
err_exit("Invalid value for proxy IO fd\n");
}
break;
case 's':
val = parse_numeric_option(optarg);
if (val == -1) {
err_exit("Invalid value for I/O seqence no\n");
}
shim.io_seq_no = (uint64_t)val;
break;
case 'e':
val = parse_numeric_option(optarg);
if (val == -1) {
err_exit("Invalid value for error sequence no\n");
}
shim.err_seq_no = (uint64_t)val;
break;
case 'd':
debug = true;
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
default:
print_usage();
exit(EXIT_FAILURE);
}
}
if ( !shim.container_id) {
err_exit("Missing container id\n");
}
if ( shim.proxy_sock_fd == -1) {
err_exit("Missing proxy socket file descriptor\n");
}
if ( shim.proxy_io_fd == -1) {
err_exit("Missing proxy I/O file descriptor\n");
}
if (shim.io_seq_no == 0) {
err_exit("Missing I/O sequence number\n");
}
shim_log_init(debug);
/* Using self pipe trick to handle signals in the main loop, other strategy
* would be to clock signals and use signalfd()/ to handle signals synchronously
*/
if (pipe(signal_pipe_fd) == -1) {
err_exit("Error creating pipe\n");
}
// Add read end of pipe to pollfd list and make it non-bocking
add_pollfd(poll_fds, &nfds, signal_pipe_fd[0], POLLIN | POLLPRI);
if (! set_fd_nonblocking(signal_pipe_fd[0])) {
exit(EXIT_FAILURE);
}
if (! set_fd_nonblocking(signal_pipe_fd[1])) {
exit(EXIT_FAILURE);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; /* Restart interrupted reads()s */
sa.sa_handler = signal_handler;
// Change the default action of all signals that should be forwarded to proxy
if (! assign_all_signals(&sa)) {
err_exit("sigaction");
}
if ( !set_fd_nonblocking(STDIN_FILENO)) {
exit(EXIT_FAILURE);
}
add_pollfd(poll_fds, &nfds, STDIN_FILENO, POLLIN | POLLPRI);
add_pollfd(poll_fds, &nfds, shim.proxy_io_fd, POLLIN | POLLPRI);
add_pollfd(poll_fds, &nfds, shim.proxy_sock_fd, POLLIN | POLLPRI);
/* 0 =>signal_pipe_fd[0]
1 =>stdin
2 =>proxy_io_fd
3 =>sockfd
*/
while (1) {
ret = poll(poll_fds, nfds, -1);
if (ret == -1 && errno != EINTR) {
shim_error("Error in poll : %s\n", strerror(errno));
break;
}
/* check if signal was received first */
if (poll_fds[0].revents != 0) {
handle_signals(shim.container_id, shim.proxy_sock_fd);
}
// check stdin fd
if (poll_fds[1].revents != 0) {
handle_stdin(&shim);
}
//check proxy_io_fd
if (poll_fds[2].revents != 0) {
handle_proxy_output(&shim);
}
// check for proxy sockfd
if (poll_fds[3].revents != 0) {
handle_proxy_ctl(&shim);
}
}
free(shim.container_id);
return 0;
}