This repository was archived by the owner on Jan 6, 2023. It is now read-only.
forked from hyperhq/hyperstart
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinit.c
More file actions
1429 lines (1190 loc) · 30.9 KB
/
init.c
File metadata and controls
1429 lines (1190 loc) · 30.9 KB
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
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <dirent.h>
#include <sched.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <mntent.h>
#include <sys/epoll.h>
#include <inttypes.h>
#include <ctype.h>
#include <sys/prctl.h>
#include "../config.h"
#include "hyper.h"
#include "net.h"
#include "util.h"
#include "exec.h"
#include "event.h"
#include "parse.h"
#include "container.h"
#include "syscall.h"
struct hyper_pod global_pod = {
.containers = LIST_HEAD_INIT(global_pod.containers),
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
};
#define MAXEVENTS 10
#define PROC_UPTIME_PATH "/proc/uptime"
struct hyper_ctl ctl;
sigset_t orig_mask;
/* is_init is true when hyperstart is run as the Linux init process */
static bool is_init;
static int hyper_handle_exit(struct hyper_pod *pod);
static int hyper_set_win_size(char *json, int length)
{
struct hyper_exec *exec;
int ret, columns, rows;
fprintf(stdout, "call hyper_win_size, json %s, len %d\n", json, length);
JSON_Value *value = hyper_json_parse(json, length);
if (value == NULL) {
fprintf(stderr, "set term size failed\n");
ret = -1;
goto out;
}
const uint64_t seq = (uint64_t)json_object_get_number(json_object(value), "seq");
exec = hyper_find_exec_by_seq(&global_pod, seq);
if (exec == NULL) {
fprintf(stdout, "can not find exec whose seq is %" PRIu64"\n", seq);
ret = 0;
goto out;
}
rows = (int)json_object_get_number(json_object(value), "row");
columns = (int)json_object_get_number(json_object(value), "column");
ret = set_win_size(exec->ptyfd, rows, columns);
out:
json_value_free(value);
return ret;
}
static void hyper_kill_process(int pid)
{
char path[64];
char *line = NULL, *ignore = "SigIgn:";
size_t len = 0;
ssize_t read;
FILE *file;
char *sub;
sprintf(path, "/proc/%u/status", pid);
fprintf(stdout, "fopen %s\n", path);
file = fopen(path, "r");
if (file == NULL) {
perror("can not open process proc status file");
return;
}
while ((read = getline(&line, &len, file)) != -1) {
long mask;
if (strstr(line, ignore) == NULL)
continue;
sub = line + strlen(ignore);
fprintf(stdout, "find sigign %s", sub);
mask = atol(sub);
fprintf(stdout, "mask is %ld\n", mask);
if ((mask >> (SIGTERM - 1)) & 0x1) {
fprintf(stdout, "signal term is ignored, kill it\n");
kill(pid, SIGKILL);
}
break;
}
fclose(file);
free(line);
}
static void hyper_term_all(struct hyper_pod *pod)
{
int npids = 0;
int index = 0;
int pid;
DIR *dp;
struct dirent *de;
pid_t *pids = NULL;
struct hyper_exec *e;
pid_t hyperstart_pid;
dp = opendir("/proc");
if (dp == NULL)
return;
hyperstart_pid = getpid();
while ((de = readdir(dp)) && de != NULL) {
if (!isdigit(de->d_name[0]))
continue;
pid = atoi(de->d_name);
if (pid == 1)
continue;
if (pid == hyperstart_pid)
continue;
if (index <= npids) {
pids = realloc(pids, npids + 16384);
if (pids == NULL) {
closedir(dp);
return;
}
npids += 16384;
}
pids[index++] = pid;
}
fprintf(stdout, "Sending SIGTERM\n");
for (--index; index >= 0; --index) {
fprintf(stdout, "kill process %d\n", pids[index]);
kill(pids[index], SIGTERM);
}
free(pids);
closedir(dp);
list_for_each_entry(e, &pod->exec_head, list)
hyper_kill_process(e->pid);
}
static int hyper_handle_exit(struct hyper_pod *pod)
{
int pid, status;
/* pid + exit code */
uint8_t data[5];
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
data[4] = 0;
if (WIFEXITED(status)) {
data[4] = WEXITSTATUS(status);
fprintf(stdout, "pid %d exit normally, status %" PRIu8 "\n",
pid, data[4]);
} else if (WIFSIGNALED(status)) {
fprintf(stdout, "pid %d exit by signal, status %d\n",
pid, WTERMSIG(status));
}
if (pod && hyper_handle_exec_exit(pod, pid, data[4]) < 0)
fprintf(stderr, "signal_loop send eof failed\n");
}
return 0;
}
static void pod_init_sigchld(int sig)
{
hyper_handle_exit(NULL);
}
static void hyper_init_sigchld(int sig)
{
hyper_handle_exit(&global_pod);
}
struct hyper_pod_arg {
struct hyper_pod *pod;
int ctl_pipe[2];
};
static int hyper_pod_init(void *data)
{
struct hyper_pod_arg *arg = data;
struct hyper_pod *pod = arg->pod;
sigset_t mask;
close(arg->ctl_pipe[0]);
close(ctl.efd);
close(ctl.chan.fd);
close(ctl.tty.fd);
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) {
perror("sigprocmask SIGCHLD failed");
return -1;
}
signal(SIGCHLD, pod_init_sigchld);
if (sethostname(pod->hostname, strlen(pod->hostname)) < 0) {
perror("set host name failed");
goto fail;
}
if (hyper_send_type(arg->ctl_pipe[1], READY) < 0) {
fprintf(stderr, "pod init send ready message failed\n");
goto fail;
}
close(arg->ctl_pipe[1]);
for (;;)
pause(); /* infinite loop and handle SIGCHLD */
out:
_exit(-1);
fail:
hyper_send_type(arg->ctl_pipe[1], ERROR);
close(arg->ctl_pipe[1]);
goto out;
}
static int hyper_start_containers(struct hyper_pod *pod)
{
struct hyper_container *c;
// TODO: setup containers and run container init processes
// via separated hyperstart APIs
list_for_each_entry(c, &pod->containers, list) {
if (hyper_setup_container(c, pod) < 0)
return -1;
if (hyper_run_process(&c->exec) < 0)
return -1;
pod->remains++;
}
return 0;
}
static int hyper_setup_pod_init(struct hyper_pod *pod)
{
int stacksize = getpagesize() * 4;
int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC |
CLONE_NEWUTS;
struct hyper_pod_arg arg = {
.pod = NULL,
.ctl_pipe = {-1, -1},
};
uint32_t type;
void *stack;
int ret = -1, init_pid;
if (pipe2(arg.ctl_pipe, O_CLOEXEC) < 0) {
perror("create pipe between hyper init and pod init failed");
goto out;
}
stack = malloc(stacksize);
if (stack == NULL) {
perror("fail to allocate stack for pod init");
goto out;
}
arg.pod = pod;
init_pid = clone(hyper_pod_init, stack + stacksize, flags, &arg);
free(stack);
if (init_pid < 0) {
perror("create pod init process failed");
goto out;
}
fprintf(stdout, "pod init pid %d\n", init_pid);
/* Wait for pod init start */
if (hyper_get_type(arg.ctl_pipe[0], &type) < 0) {
perror("get pod init ready message failed");
goto out;
}
if (type != READY) {
fprintf(stderr, "get incorrect message type %d, expect READY\n", type);
goto out;
}
pod->init_pid = init_pid;
ret = 0;
out:
close(arg.ctl_pipe[1]);
close(arg.ctl_pipe[0]);
return ret;
}
// enter the sanbox and pass to the child, shouldn't call from the init process
int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe)
{
int ret = -1, pidns = -1, utsns = -1, ipcns = -1;
char path[512];
sprintf(path, "/proc/%d/ns/pid", pod->init_pid);
pidns = open(path, O_RDONLY| O_CLOEXEC);
if (pidns < 0) {
perror("fail to open pidns of pod init");
goto out;
}
sprintf(path, "/proc/%d/ns/uts", pod->init_pid);
utsns = open(path, O_RDONLY| O_CLOEXEC);
if (utsns < 0) {
perror("fail to open utsns of pod init");
goto out;
}
sprintf(path, "/proc/%d/ns/ipc", pod->init_pid);
ipcns = open(path, O_RDONLY| O_CLOEXEC);
if (ipcns < 0) {
perror("fail to open ipcns of pod init");
goto out;
}
if (setns(pidns, CLONE_NEWPID) < 0 ||
setns(utsns, CLONE_NEWUTS) < 0 ||
setns(ipcns, CLONE_NEWIPC) < 0) {
perror("fail to enter the sandbox");
goto out;
}
/* current process isn't in the pidns even setns(pidns, CLONE_NEWPID)
* was called. fork() is needed, so that the child process will run in
* the pidns, see man 2 setns */
ret = fork();
if (ret < 0) {
perror("fail to fork");
goto out;
} else if (ret > 0) {
fprintf(stdout, "create child process pid=%d in the sandbox\n", ret);
if (pidpipe > 0) {
hyper_send_type(pidpipe, ret);
}
_exit(0);
}
out:
if (pidns >= 0)
close(pidns);
if (ipcns >= 0)
close(ipcns);
if (utsns >= 0)
close(utsns);
return ret;
}
#ifdef WITH_VBOX
#define MAX_HOST_NAME 256
#define MAX_NLS_NAME 32
#define VBSF_MOUNT_SIGNATURE_BYTE_0 '\377'
#define VBSF_MOUNT_SIGNATURE_BYTE_1 '\376'
#define VBSF_MOUNT_SIGNATURE_BYTE_2 '\375'
struct vbsf_mount_info_new
{
char nullchar; /* name cannot be '\0' -- we use this field
to distinguish between the old structure
and the new structure */
char signature[3]; /* signature */
int length; /* length of the whole structure */
char name[MAX_HOST_NAME]; /* share name */
char nls_name[MAX_NLS_NAME]; /* name of an I/O charset */
int uid; /* user ID for all entries, default 0=root */
int gid; /* group ID for all entries, default 0=root */
int ttl; /* time to live */
int dmode; /* mode for directories if != 0xffffffff */
int fmode; /* mode for regular files if != 0xffffffff */
int dmask; /* umask applied to directories */
int fmask; /* umask applied to regular files */
};
static int hyper_setup_shared(struct hyper_pod *pod)
{
struct vbsf_mount_info_new mntinf;
if (pod->share_tag == NULL) {
fprintf(stdout, "no shared directory\n");
return 0;
}
if (hyper_mkdir(SHARED_DIR, 0755) < 0) {
perror("fail to create " SHARED_DIR);
return -1;
}
bzero(&mntinf, sizeof(mntinf));
mntinf.nullchar = '\0';
mntinf.signature[0] = VBSF_MOUNT_SIGNATURE_BYTE_0;
mntinf.signature[1] = VBSF_MOUNT_SIGNATURE_BYTE_1;
mntinf.signature[2] = VBSF_MOUNT_SIGNATURE_BYTE_2;
mntinf.length = sizeof(mntinf);
mntinf.dmode = ~0U;
mntinf.fmode = ~0U;
strcpy(mntinf.name, pod->share_tag);
if (mount(NULL, SHARED_DIR, "vboxsf",
MS_NODEV, &mntinf) < 0) {
perror("fail to mount shared dir");
return -1;
}
return 0;
}
#else
static int hyper_setup_shared(struct hyper_pod *pod)
{
if (pod->share_tag == NULL) {
fprintf(stdout, "no shared directory\n");
return 0;
}
if (hyper_mkdir(SHARED_DIR, 0755) < 0) {
perror("fail to create " SHARED_DIR);
return -1;
}
// Adding 524288 (512K) value to msize, provides
// an enhancement in I/O storage operations.
// This value was tested using different block sizes
// and I/O operations.
if (mount(pod->share_tag, SHARED_DIR, "9p",
MS_MGC_VAL| MS_NODEV, "trans=virtio,msize=524288") < 0) {
perror("fail to mount shared dir");
return -1;
}
return 0;
}
#endif
static int hyper_setup_pod(struct hyper_pod *pod)
{
/* create tmp proc directory */
if (hyper_mkdir("/tmp/hyper/proc", 0755) < 0) {
perror("create tmp proc failed");
return -1;
}
if (hyper_setup_network(pod) < 0) {
fprintf(stderr, "setup network failed\n");
return -1;
}
if (hyper_setup_iptables(pod) < 0) {
fprintf(stderr, "iptable rules setup failed\n");
return -1;
}
if (hyper_setup_dns(pod) < 0) {
fprintf(stderr, "setup network failed\n");
return -1;
}
if (hyper_setup_shared(pod) < 0) {
fprintf(stderr, "setup shared directory failed\n");
return -1;
}
if (hyper_setup_portmapping(pod) < 0) {
fprintf(stderr, "setup port mapping failed\n");
return -1;
}
if (hyper_setup_pod_init(pod) < 0) {
fprintf(stderr, "start container failed\n");
return -1;
}
return 0;
}
static void hyper_print_uptime(void)
{
char buf[128];
int fd = open(PROC_UPTIME_PATH, O_RDONLY);
ssize_t bytes_read;
if (fd < 0)
return;
int buffer_size = sizeof(buf) - 1;
memset(buf, 0, buffer_size + 1);
buf[buffer_size] = '\0';
bytes_read = read(fd, buf, buffer_size);
if (bytes_read < 0) {
fprintf(stdout, "reading %s failed: %s\n",
PROC_UPTIME_PATH, strerror(errno));
} else if (bytes_read == 0) {
fprintf(stderr, "EOF reading %s\n", PROC_UPTIME_PATH);
} else {
if (bytes_read > buffer_size) {
bytes_read = buffer_size;
}
buf[bytes_read] = '\0';
fprintf(stdout, "uptime %s\n", buf);
}
close(fd);
}
void hyper_pod_destroyed(int failed)
{
hyper_send_msg_block(ctl.chan.fd, failed?ERROR:ACK, 0, NULL);
hyper_shutdown();
}
static int hyper_destroy_pod(struct hyper_pod *pod, int error)
{
if (pod->init_pid == 0 || pod->remains == 0) {
/* Pod stopped, just shutdown */
hyper_pod_destroyed(error);
} else {
/* Kill pod */
hyper_term_all(pod);
}
return 0;
}
static int hyper_start_pod(char *json, int length)
{
struct hyper_pod *pod = &global_pod;
fprintf(stdout, "call hyper_start_pod, json %s, len %d\n", json, length);
if (pod->init_pid)
fprintf(stdout, "pod init_pid exist %d\n", pod->init_pid);
hyper_sync_time_hctosys();
if (hyper_parse_pod(pod, json, length) < 0) {
fprintf(stderr, "parse pod json failed\n");
return -1;
}
if (hyper_setup_pod(pod) < 0) {
hyper_destroy_pod(pod, 1);
return -1;
}
if (hyper_start_containers(pod) < 0) {
fprintf(stderr, "start containers failed\n");
hyper_destroy_pod(pod, 1);
return -1;
}
return 0;
}
static int hyper_new_container(char *json, int length)
{
int ret;
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length);
if (!pod->init_pid) {
fprintf(stdout, "the pod is not created yet\n");
return -1;
}
c = hyper_parse_new_container(pod, json, length);
if (c == NULL) {
fprintf(stderr, "parse container json failed\n");
return -1;
}
list_add_tail(&c->list, &pod->containers);
ret = hyper_setup_container(c, pod);
if (ret >= 0)
ret = hyper_run_process(&c->exec);
if (ret < 0) {
//TODO full grace cleanup
hyper_cleanup_container(c, pod);
} else {
pod->remains++;
}
return ret;
}
static int hyper_kill_container(char *json, int length)
{
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
int ret = -1;
JSON_Value *value = hyper_json_parse(json, length);
if (value == NULL) {
goto out;
}
const char *id = json_object_get_string(json_object(value), "container");
c = hyper_find_container(pod, id);
if (c == NULL) {
fprintf(stderr, "can not find container whose id is %s\n", id);
/* if pod's init pid is 0 means container was already
* cleaned there are not processes running, kill container
* is not needed
*/
if (pod->init_pid == 0) {
fprintf(stdout, "container %s was already cleaned\n", id);
ret = 0;
}
goto out;
}
const int signal = (int)json_object_get_number(json_object(value), "signal");
const int all_processes = json_object_get_boolean(json_object(value),
"allProcesses");
/*
* json_object_get_boolean returns '1' only when
* 'allProcesses' exists and is true
*/
if (all_processes == 1) {
struct hyper_exec *exec;
list_for_each_entry(exec, &pod->exec_head, list) {
/*
* Kill exec processes before container workload
* to ensure all processes receive the signal
*/
if (strcmp(exec->container_id, c->id) == 0
&& exec->pid != c->exec.pid ) {
fprintf(stdout, "killing exec process %d\n", exec->pid);
kill(exec->pid, signal);
}
}
}
fprintf(stdout, "killing container workload %d\n", c->exec.pid);
/* killing container workload */
kill(c->exec.pid, signal);
ret = 0;
out:
json_value_free(value);
return ret;
}
static int hyper_remove_container(char *json, int length)
{
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
int ret = -1;
JSON_Value *value = hyper_json_parse(json, length);
if (value == NULL) {
goto out;
}
const char *id = json_object_get_string(json_object(value), "container");
c = hyper_find_container(pod, id);
if (c == NULL) {
fprintf(stderr, "can not find container whose id is %s\n", id);
goto out;
}
if (c->exec.exit != 1) {
fprintf(stderr, "container %s has not been stopped\n", id);
goto out;
}
hyper_cleanup_container(c, pod);
ret = 0;
out:
json_value_free(value);
return ret;
}
struct hyper_file_arg {
int rw;
int mntns;
int pipe[2];
char *file;
};
static int hyper_open_container_file(void *data)
{
struct hyper_file_arg *arg = data;
int fd = -1, ret = -1, size;
if (setns(arg->mntns, CLONE_NEWNS) < 0) {
perror("fail to enter container ns");
goto exit;
}
if (arg->rw == WRITEFILE) {
fd = open(arg->file, O_CREAT| O_TRUNC| O_WRONLY, 0644);
} else {
fd = open(arg->file, O_RDONLY);
}
if (fd < 0) {
perror("fail to open target file");
goto exit;
}
ret = 0;
exit:
size = write(arg->pipe[1], &fd, sizeof(fd));
if (size != sizeof(fd) && ret == 0) {
ret = -1;
}
exit(ret);
}
static int hyper_cmd_rw_file(char *json, int length, uint32_t *rdatalen, uint8_t **rdata, int rw)
{
struct file_command cmd = {
.id = NULL,
.file = NULL,
};
struct hyper_file_arg arg = {
.pipe = {-1, -1},
.rw = rw,
};
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
char *data = NULL;
void *stack = NULL;
int stacksize = getpagesize() * 4;
int fd = -1, len = 0, ret = -1, datalen = 0;
int pid, size;
fprintf(stdout, "%s: %s\n", __func__, rw == WRITEFILE ? "write" : "read");
if (rw == WRITEFILE) {
// TODO: send the data via hyperstream rather than append it at the end of the command
data = strchr(json, '}');
if (data == NULL) {
goto out;
}
data++;
datalen = length - (data - json);
length = data - json;
}
if (hyper_parse_file_command(&cmd, json, length) < 0) {
goto out;
}
arg.file = cmd.file;
c = hyper_find_container(pod, cmd.id);
if (c == NULL) {
fprintf(stderr, "can not find container whose id is %s\n", cmd.id);
goto out;
}
arg.mntns = c->ns;
if (arg.mntns < 0) {
perror("fail to open mnt ns");
goto out;
}
if (pipe2(arg.pipe, O_CLOEXEC) < 0) {
perror("create pipe failed");
goto out;
}
stack = malloc(stacksize);
if (stack == NULL) {
perror("fail to allocate stack for container init");
goto out;
}
pid = clone(hyper_open_container_file, stack + stacksize, CLONE_FILES | SIGCHLD, &arg);
if (pid < 0) {
perror("fail to fork child process");
goto out;
}
size = read(arg.pipe[0], &fd, sizeof(fd));
if (size != sizeof(fd)) {
perror("fail to read fd from pipe");
goto out;
}
if (fd < 0) {
perror("child open target file failed");
goto out;
}
if (rw == READFILE) {
struct stat st;
if(fstat(fd, &st) < 0) {
perror("fail to state file");
goto out;
}
*rdatalen = datalen = st.st_size;
data = malloc(datalen);
*rdata = (uint8_t *) data;
if (*rdata == NULL) {
fprintf(stderr, "allocate memory for reading file failed\n");
goto out;
}
fprintf(stdout, "file length %d\n", *rdatalen);
}
while(len < datalen) {
if (rw == WRITEFILE) {
size = write(fd, data + len, datalen - len);
} else {
size = read(fd, data + len, datalen - len);
}
if (size < 0) {
if (errno == EINTR)
continue;
perror("fail to operate data to file");
goto out;
}
len += size;
}
ret = 0;
out:
if (fd >= 0)
close(fd);
close(arg.pipe[0]);
close(arg.pipe[1]);
free(cmd.id);
free(cmd.file);
free(stack);
return ret;
}
static void hyper_cmd_online_cpu_mem()
{
int pid = fork();
if (pid < 0) {
perror("fail to fork online process");
} else if (pid == 0) {
online_cpu();
online_memory();
exit(0);
}
}
static void hyper_cleanup_hostname(struct hyper_pod *pod)
{
free(pod->hostname);
pod->hostname = NULL;
}
static void hyper_cleanup_shared(struct hyper_pod *pod)
{
if (pod->share_tag == NULL) {
fprintf(stdout, "no shared directory\n");
return;
}
free(pod->share_tag);
pod->share_tag = NULL;
if (umount(SHARED_DIR) < 0 &&
umount2(SHARED_DIR, MNT_DETACH)) {
perror("fail to umount shared dir");
return;
}
if (rmdir(SHARED_DIR) < 0)
perror("fail to delete " SHARED_DIR);
sync();
}
void hyper_cleanup_pod(struct hyper_pod *pod)
{
if (pod->init_pid) {
hyper_kill_process(pod->init_pid);
pod->init_pid = 0;
}
hyper_cleanup_containers(pod);
hyper_cleanup_network(pod);
hyper_cleanup_shared(pod);
hyper_cleanup_dns(pod);
hyper_cleanup_portmapping(pod);
hyper_cleanup_hostname(pod);
}
static int hyper_setup_ctl_channel(char *name)
{
int ret = hyper_open_channel(name, 0);
if (ret < 0)
return ret;
fprintf(stdout, "send ready message\n");
if (hyper_send_type(ret, READY) < 0) {
perror("send READY MESSAGE failed\n");
goto out;
}
return ret;
out:
close(ret);
return -1;
}
static int hyper_setup_tty_channel(char *name)
{
int ret = hyper_open_channel(name, O_NONBLOCK);
if (ret < 0)
return -1;
return ret;
}
static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len)
{
struct hyper_buf *rbuf = &de->rbuf;
struct hyper_pod *pod = de->ptr;
struct hyper_exec *exec;
struct hyper_buf *wbuf;
uint64_t seq = 0;
int size;
seq = hyper_get_be64(rbuf->data);
fprintf(stdout, "\n%s seq %" PRIu64", len %" PRIu32"\n", __func__, seq, len - 12);
exec = hyper_find_exec_by_seq(pod, seq);
if (exec == NULL) {
wbuf = &de->wbuf;
fprintf(stderr, "can't find exec whose seq is %" PRIu64 "\n", seq);
/* goodbye */
if (wbuf->get + 12 > wbuf->size)
return 0;
hyper_set_be64(wbuf->data + wbuf->get, seq);
hyper_set_be32(wbuf->data + wbuf->get + 8, 12);
wbuf->get += 12;
if (hyper_modify_event(ctl.efd, de, EPOLLIN | EPOLLOUT) < 0) {
fprintf(stderr, "modify ctl tty event to in & out failed\n");
return -1;
}
return 0;
}
fprintf(stdout, "find exec %s pid %d, seq is %" PRIu64 "\n",