This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
shim.c
1204 lines (1037 loc) · 27.1 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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include "config.h"
#include "utils.h"
#include "log.h"
#include "shim.h"
/* globals */
struct pollfd poll_fds[MAX_POLL_FDS] = {{-1}};
// File descriptors are added at specific index in the poll_fds array
#define SIGNAL_FD_INDEX 0
#define PROXY_SOCK_INDEX 1
#define STDIN_INDEX 2
/* Pipe used for capturing signal occurence */
int signal_pipe_fd[2] = { -1, -1 };
/* Pipe used for monitoring of the parent process from the child */
int monitor_pipe[2] = {-1, -1};
/* Byte sent from parent to notify its child it terminated properly */
const char end_byte = 'E';
static char *program_name;
struct termios *saved_term_settings;
/*!
* Add file descriptor to the array of polled descriptors
*
* \param poll_fds Array of polled fds
* \param index Index at which the fd is added
* \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 index, int fd, short events) {
struct pollfd pfd = { 0 };
if ( !poll_fds || fd < 0 || index >= MAX_POLL_FDS) {
shim_warning("Not able to add fd to poll_fds array\n");
return;
}
pfd.fd = fd;
pfd.events = events;
poll_fds[index] = pfd;
}
/*!
* 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;
}
void restore_terminal(void) {
if ( isatty(STDIN_FILENO) && saved_term_settings) {
if (tcsetattr (STDIN_FILENO, TCSANOW, saved_term_settings)) {
shim_warning("Unable to restore terminal: %s\n",
strerror(errno));
}
free(saved_term_settings);
saved_term_settings = NULL;
}
}
void send_end_byte(void) {
if (write(monitor_pipe[1], &end_byte, sizeof(end_byte)) <= 0) {
shim_warning("Could not write end byte to the monitor pipe");
}
shim_debug("End byte properly written to the monitor pipe");
}
/*!
* 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);
restore_terminal();
exit(EXIT_FAILURE);
}
/*
* Prepare byte stream in the proxy protocol format.
*
* \param fr \ref frame
* \param [out] total_size Total size of serialized byte stream
*/
uint8_t*
serialize_frame(struct frame *fr, ssize_t *total_size)
{
uint8_t *msg = NULL;
if (! (fr && total_size)) {
return NULL;
}
/* Header length is length of the header in number of 32 bit words.
* Converting the header length to byte length.
*/
*total_size = (ssize_t)((fr->header.header_len * sizeof(uint32_t)) + fr->header.payload_len);
msg = calloc((size_t)(*total_size), sizeof(uint8_t));
if (! msg) {
abort();
}
set_big_endian_16(msg, PROXY_VERSION);
msg[HEADER_LEN_OFFSET] = fr->header.header_len;
msg[RES_OFFSET] = fr->header.type;
msg[OPCODE_OFFSET] = fr->header.opcode;
set_big_endian_32(msg + PAYLOAD_LEN_OFFSET, fr->header.payload_len);
if (fr->header.payload_len && fr->payload) {
memcpy(msg + PAYLOAD_OFFSET, fr->payload,
fr->header.payload_len);
}
return msg;
}
/*!
* Write frame to the proxy socket fd.
*
* \param shim \ref cc_shim
* \param fr \ref frame
*
* \return true on success, false otherwise
*/
bool
write_frame(struct cc_shim *shim, struct frame *fr)
{
size_t offset = 0;
ssize_t ret;
ssize_t total_size = 0;
if (! (shim && fr)) {
return false;
}
uint8_t *msg = serialize_frame(fr, &total_size);
if (! msg ) {
return false;
}
while (offset < total_size) {
ret = write(shim->proxy_sock_fd, msg + offset, (size_t)total_size-offset);
if (ret == -1 && errno == EINTR) {
continue;
}
if (ret <= 0 ) {
free(msg);
shim_error("Error writing to proxy: %s\n", strerror(errno));
return false;
}
offset += (size_t)ret;
}
free(msg);
return true;
}
/*!
* Send message to proxy.
*
* \param shim \ref cc_shim
* \param type Type of message
* \param opcode Opcode of message type
* \param payload Payload to be sent
*
* \return true on success, false otherwise
*/
bool
send_proxy_message(struct cc_shim *shim, uint8_t type, uint8_t opcode,
const char *payload, size_t payload_len)
{
struct frame fr = {{ 0 }};
bool ret;
if ( !shim || shim->proxy_sock_fd < 0) {
return false;
}
fr.header.version = PROXY_VERSION;
fr.header.header_len = MIN_HEADER_WORD_SIZE;
fr.header.type = type;
fr.header.opcode = opcode;
shim_debug("Sending frame of type:%d, opcode:%d, payload:%s\n",
fr.header.type,
fr.header.opcode,
payload? payload:"(empty)");
if (payload) {
fr.header.payload_len = (uint32_t)payload_len;
fr.payload = (uint8_t*)payload;
}
ret = write_frame(shim, &fr);
return ret;
}
/*!
* Send Connect command to proxy.
*
* \param shim \ref cc_shim
*
* \return true on success, false otherwise
*/
bool
send_connect_command(struct cc_shim *shim)
{
char *payload = NULL;
bool ret;
if (! shim) {
return false;
}
if ((! shim->token) || (shim->proxy_sock_fd < 0)) {
return false;
}
if ( verify_base64url_format(shim->token) != 0) {
shim_error("Invalid token: %s, base64 encoded token expected\n",
shim->token);
return false;
}
// TODO: Verify the payload format
ret = asprintf(&payload,
"{\"token\":\"%s\"}", shim->token);
if (! payload) {
abort();
}
ret = send_proxy_message(shim, frametype_command, cmd_connectshim,
payload, strlen(payload));
if (! ret) {
shim_error("Could not send initial connect command to "
"proxy at %s\n", shim->proxy_address);
}
free(payload);
return ret;
}
/*!
* Read message received from proxy.
*
* \param fd File descriptor to read the data from.
* \param buf Buffer to store the data in.
* \param size Size in bytes to be read.
*
* \return true on success, false otherwise
*/
bool read_wire_data(int fd, uint8_t *buf, size_t size)
{
ssize_t ret;
size_t offset = 0;
if ( fd < 0 || ! buf ) {
return false;
}
while(offset < size) {
ret = recv(fd, buf+offset, size-offset, 0);
if (ret == 0) {
shim_debug("Received EOF on file descriptor\n");
// TODO: Exit for now, add logic to try to reconnect
// to proxy.
exit(EXIT_FAILURE);
} else if (ret < 0) {
shim_error("Failed to read from fd: %s\n",
strerror(errno));
return false;
}
offset += (size_t)ret;
}
return true;
}
/*!
* Read message received from proxy.
*
* \param shim \ref cc_shim
* \param header \ref frame
*
* \return newly allocated frame on success, NULL otherwise
*/
struct frame*
read_frame(struct cc_shim *shim)
{
uint8_t *buf = NULL;
size_t size = VERSION_SIZE + HEADER_LEN_SIZE;
size_t header_size_in_bytes;
struct frame *fr = NULL;
if ( !shim || shim->proxy_sock_fd < 0) {
return false;
}
buf = calloc(size, sizeof(uint8_t));
if ( !buf) {
abort();
}
fr = calloc(1, sizeof(struct frame));
if ( !fr) {
abort();
}
if (! read_wire_data(shim->proxy_sock_fd, buf, size)) {
goto error;
}
fr->header.header_len = buf[HEADER_LEN_OFFSET];
if (fr->header.header_len < MIN_HEADER_WORD_SIZE) {
shim_error("Header length cannot be less than %d\n",
MIN_HEADER_WORD_SIZE);
goto error;
}
fr->header.version = get_big_endian_16(buf);
if (fr->header.version != PROXY_VERSION) {
shim_error("Bad frame version: %d, Expected : %d\n",
fr->header.version,
PROXY_VERSION);
goto error;
}
/* Get the header size in bytes, as the proxy passes the header length
* as 32 bit word length.
*/
header_size_in_bytes = (size_t)fr->header.header_len * sizeof(uint32_t);
buf = realloc(buf, header_size_in_bytes);
if (! buf) {
abort();
}
if (! read_wire_data(shim->proxy_sock_fd, buf + size,
header_size_in_bytes - size)) {
shim_error("Error while reading frame from proxy at %s\n",
shim->proxy_address);
goto error;
}
fr->header.payload_len = get_big_endian_32(buf + PAYLOAD_LEN_OFFSET);
fr->header.type = buf[RES_OFFSET] & 0x0F;
fr->header.err = buf[RES_OFFSET] & 0x10 ;
fr->header.opcode = buf[OPCODE_OFFSET];
if (fr->header.payload_len) {
buf = realloc(buf, fr->header.payload_len + 1);
if (! buf) {
abort();
}
memset(buf, 0, fr->header.payload_len + 1);
if (! read_wire_data(shim->proxy_sock_fd, buf,
fr->header.payload_len)) {
goto error;
}
fr->payload = buf;
} else {
free(buf);
}
shim_debug("Frame read - HeaderLength: %d, Version: %d, "
"Payloadlen: %d, Type:%d, "
"Opcode: %d, Err:%d\n",
fr->header.header_len, fr->header.version,
fr->header.payload_len, fr->header.type,
fr->header.opcode, fr->header.err);
return fr;
error:
free(buf);
free(fr);
return NULL;
}
/*!
* Read signals received and send message in the hyperstart protocol
* format to the proxy ctl socket.
*
* \param shim \ref cc_shim
*/
void
handle_signals(struct cc_shim *shim) {
int sig;
int ret;
struct winsize ws;
char *payload = NULL;
if ( !shim || shim->proxy_sock_fd < 0) {
return;
}
while (read(signal_pipe_fd[0], &sig, sizeof(sig)) != -1) {
shim_debug("Handling signal : %d on fd %d\n", sig,
signal_pipe_fd[0]);
if (sig == SIGWINCH ) {
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
shim_warning("Error getting the current"\
"window size: %s\n",
strerror(errno));
continue;
}
ret = asprintf(&payload, "{\"signalNumber\": %d,"\
" \"rows\":%d, \"columns\":%d}",
sig, ws.ws_row, ws.ws_col);
shim_debug("handled SIGWINCH for container %s "
"(rows=%d, columns=%d)\n",
shim->container_id? shim->container_id: "",
ws.ws_row, ws.ws_col);
} else {
ret = asprintf(&payload, "{\"signalNumber\":%d}",
sig);
shim_debug("Sending signal %d to container %s\n",
sig,
shim->container_id? shim->container_id: "");
}
if (ret == -1) {
abort();
}
if (! send_proxy_message(shim, frametype_command,
cmd_signal, payload, strlen(payload))) {
shim_error("Could not send signal command "
"to proxy %s\n", shim->proxy_address);
}
free(payload);
}
}
/*!
* Read data from stdin and send it to proxy.
*
* \param shim \ref cc_shim
*/
void
handle_stdin(struct cc_shim *shim)
{
ssize_t nread;
char buf[BUFSIZ] = { 0 };
if (! shim || shim->proxy_sock_fd < 0) {
return;
}
nread = read(STDIN_FILENO , buf, BUFSIZ);
if (nread < 0) {
shim_warning("Error while reading stdin char :%s\n", strerror(errno));
return;
} else if (nread == 0) {
/* EOF received on stdin, send eof to hyperstart and remove stdin fd from
* the polled descriptors to prevent further eof events
*/
poll_fds[STDIN_INDEX].fd = -1;
}
if (! send_proxy_message(shim, frametype_stream, stream_stdin,
buf, (size_t)nread)) {
shim_error("Could not send stdin stream to proxy at %s\n",
shim->proxy_address);
}
}
/*!
* Handle response received from proxy
*
*\param shim \ref cc_shim
*\param fr \ref frame
*/
void
handle_proxy_response(struct cc_shim *shim, struct frame *fr)
{
if (! (shim && shim->proxy_address)) {
return;
}
if ( !fr) {
return;
}
// Reponses received from proxy are currently just logged.
if (fr->header.err) {
shim_error("Error response received from proxy at %s: %s\n",
shim->proxy_address,
fr->payload ? (char*)fr->payload:"");
/* If we receive an error with the ConnectShim response,
* the proxy could not validate the token.
*/
if (fr->header.opcode == cmd_connectshim) {
err_exit("Shim received an error in response"
"to ConnectShim command, exiting");
}
} else {
/* TODO: Currently logging response. Do we want to track responses
* to requests in future. Maybe useful for restarting connection with proxy
* and resending requests that were missed.
*/
shim_debug("Response received from proxy at %s: %s\n",
shim->proxy_address,
fr->payload ? (char*)fr->payload:"");
}
}
/*!
* Handle stream received from proxy
*
*\param shim \ref cc_shim
*\param fr \ref frame
*/
void
handle_proxy_stream(struct cc_shim *shim, struct frame *fr)
{
int outfd = -1;
size_t offset = 0;
ssize_t ret;
if (! (shim && shim->proxy_address)) {
return;
}
if ( fr->header.type != frametype_stream) {
return;
}
if (fr->header.opcode == stream_stdout) {
outfd = STDOUT_FILENO;
} else if (fr->header.opcode == stream_stderr) {
outfd = STDERR_FILENO;
} else {
shim_warning("Invalid stream with opcode %d received from proxy at %s\n",
fr->header.opcode, shim->proxy_address);
return;
}
while (offset < fr->header.payload_len) {
ret = write(outfd, fr->payload + offset,
(fr->header.payload_len - offset));
if (ret <= 0 ) {
shim_error("Could not write stream to fd %d for proxy %s: %s\n",
outfd, shim->proxy_address, strerror(errno));
return;
}
offset += (size_t)ret;
}
}
/*!
* Handle notification received from proxy
*
*\param shim \ref cc_shim
*\param fr \ref frame
*/
void
handle_proxy_notification(struct cc_shim *shim, struct frame *fr)
{
int code = 0;
if (! (shim && shim->proxy_address)) {
return;
}
if ( ! ( fr && fr->payload)) {
return;
}
if (fr->header.opcode == notification_exitcode) {
/* Send disconnect command to proxy and exit
* with the exit code
*/
code = *(fr->payload);
if (! send_proxy_message(shim, frametype_command,
cmd_disconnectshim, NULL, 0)) {
shim_error("Could not send Disconnect shim command "
"to proxy at %s\n", shim->proxy_address);
}
shim_debug("Exit status for container: %d\n", code);
restore_terminal();
exit(code);
} else {
shim_warning("Unknown notification received from proxy %s: %d\n",
shim->proxy_address, fr->header.opcode);
}
}
/*!
* Handle message received from proxy
*
*\param shim \ref cc_shim
*/
void
handle_proxy_message(struct cc_shim *shim)
{
struct frame *fr;
fr = read_frame(shim);
if ( !fr) {
return;
}
shim_debug("Received frame with type: %d, opcode: %d\n",
fr->header.type,
fr->header.opcode);
switch (fr->header.type) {
case frametype_response:
handle_proxy_response(shim, fr);
break;
case frametype_stream:
handle_proxy_stream(shim, fr);
break;
case frametype_notification:
handle_proxy_notification(shim, fr);
break;
case frametype_command:
shim_warning("Command received from proxy\n");
break;
default:
shim_warning("Unknown frame with type %d received\n",
fr->header.type);
}
free(fr->payload);
free(fr);
}
/*
* 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;
}
/*
* Parse proxy connection uri for connection address
* and port for tcp.
* Expected schemes are unix: and tcp:
*
* \param shim \ref cc_shim
* \param uri String containing the uri
*
* \return true on success, false on failure
*/
bool
parse_connection_uri(struct cc_shim *shim, char *uri)
{
const char *unix_uri = "unix://";
const char *tcp_uri = "tcp://";
size_t unix_uri_len = strlen(unix_uri);
size_t tcp_uri_len = strlen(tcp_uri);
bool ret = false;
ssize_t addr_len;
if (! (shim && uri)) {
return false;
}
if (! strncmp(uri, unix_uri, unix_uri_len)) {
shim->proxy_address = strdup(uri + unix_uri_len);
if ( !shim->proxy_address) {
abort();
}
if (strlen(shim->proxy_address) >= PATH_MAX ) {
shim_error("Path provided for the proxy exceeds"
"maximum allowed length on paths\n");
goto out;
}
ret = true;
} else if (! strncmp(uri, tcp_uri, tcp_uri_len)) {
char *port_offset = strstr(uri + tcp_uri_len, ":");
if ( !port_offset) {
shim_error("Missing port in uri %s\n", uri);
goto out;
}
shim->proxy_port = (int)parse_numeric_option(port_offset + 1);
if (shim->proxy_port == -1) {
shim_error("Could not parse port in uri %s: %s\n",
uri, strerror(errno));
goto out;
}
addr_len = port_offset - (uri + tcp_uri_len);
if (addr_len >= _POSIX_HOST_NAME_MAX) {
shim_error("Address provided for the proxy exceeds"
"maximum allowed length for hostname\n");
goto out;
}
if (addr_len == 0) {
shim_error("Missing tcp hostname in uri %s\n", uri);
goto out;
}
shim->proxy_address = calloc(sizeof(char),
(size_t)addr_len + 1);
if ( !shim->proxy_address) {
abort();
}
memcpy(shim->proxy_address, uri + tcp_uri_len,
(size_t)addr_len * sizeof(char));
ret = true;
} else {
shim_error("Invalid uri scheme : %s\n", uri);
}
out:
free(uri);
return ret;
}
/*
* Establish tcp/unix connection with proxy.
*
* \param shim \ref cc_shim
*
* \return true on success, false on failure
*/
bool
connect_to_proxy(struct cc_shim *shim)
{
int sockfd = -1;
char *port_str = NULL;
int ret;
if (! shim) {
return false;
}
/* Uninitialised port means the uri provided is a
* unix socket connection path
*/
if (shim->proxy_port == -1) {
struct sockaddr_un remote;
size_t path_size = sizeof(remote.sun_path);
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1 ) {
shim_error("Error while creating socket: %s\n",
strerror(errno));
goto out;
}
remote.sun_family = AF_UNIX;
remote.sun_path[path_size-1] = '\0';
strncpy(remote.sun_path, shim->proxy_address,
path_size - 2);
if (connect(sockfd, (struct sockaddr *)&remote,
sizeof(struct sockaddr_un)) == -1) {
shim_error("Error while connecting to proxy "
"with address %s: %s\n", shim->proxy_address,
strerror(errno));
goto out;
}
} else {
struct addrinfo hints;
struct addrinfo *servinfo, *addr;
/* Connect to proxy on tcp address+port
*/
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
/* Avoid service lookups as we provide a numeric port number.
*/
hints.ai_flags = AI_NUMERICSERV;
if ( asprintf(&port_str, "%d", shim->proxy_port) == -1) {
abort();
}
if ((ret = getaddrinfo(shim->proxy_address, port_str,
&hints, &servinfo)) != 0) {
shim_error("getaddrinfo error for %s:%d :%s\n",
shim->proxy_address,
shim->proxy_port,
strerror(errno));
goto out;
}
// loop through all the results and connect to the first we can
for(addr = servinfo; addr != NULL; addr = addr->ai_next) {
if ((sockfd = socket(addr->ai_family, addr->ai_socktype,
addr->ai_protocol)) == -1) {
shim_debug("Error in socket creation for "
"%s:%d :%s\n",
shim->proxy_address,
shim->proxy_port,
strerror(errno));
continue;
}
if (connect(sockfd, addr->ai_addr,
addr->ai_addrlen) == -1) {
close(sockfd);
sockfd = -1;
shim_debug("Error in client connection for "
"%s:%d : %s\n", shim->proxy_address,
shim->proxy_port, strerror(errno));
continue;
}
break;
}
if (addr == NULL) {
shim_error("Failed to connect to proxy with address"
" %s:%d : %s\n", shim->proxy_address,
shim->proxy_port, strerror(errno));
freeaddrinfo(servinfo);
goto out;
}
freeaddrinfo(servinfo);
}
free(port_str);
shim->proxy_sock_fd = sockfd;
return true;
out:
free(port_str);
if (sockfd >= 0) {
close(sockfd);
}
return false;
}
/*
* Print version information.
*/
void
show_version(void) {
printf("%s version: %s (commit: %s)\n", PACKAGE_NAME, PACKAGE_VERSION, GIT_COMMIT);
}
/*!
* Print program usage
*/
void
print_usage(void) {
printf("%s: Usage\n", program_name);
printf(" -c, --container-id Container id\n");
printf(" -d, --debug Enable debug output\n");
printf(" -t, --token Connection token passed by cc-proxy\n");
printf(" -u, --uri Connection uri. Supported schemes are tcp: and unix:\n");
printf(" -v, --version Show version\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,
.token = NULL,
.proxy_address = NULL,
.proxy_port = -1,
};
int ret;
struct sigaction sa;
int c;
bool debug = false;
char *uri = NULL;
int pid = -1;
program_name = argv[0];
struct option prog_opts[] = {
{"container-id", required_argument, 0, 'c'},
{"debug", no_argument, 0, 'd'},