This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathinit.c
More file actions
1670 lines (1411 loc) · 38.6 KB
/
init.c
File metadata and controls
1670 lines (1411 loc) · 38.6 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 <sys/eventfd.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"
#include "vsock.h"
#include "netlink.h"
static struct hyper_pod global_pod = {
.containers = LIST_HEAD_INIT(global_pod.containers),
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
};
#define MAXEVENTS 10
struct hyper_epoll hyper_epoll;
sigset_t orig_mask;
static int hyper_handle_exit(struct hyper_pod *pod);
static int hyper_set_win_size(struct hyper_pod *pod, char *json, int length)
{
struct winsize size;
struct hyper_exec *exec;
int ret = -1;
fprintf(stdout, "call hyper_set_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");
goto out;
}
const char *container = json_object_get_string(json_object(value), "container");
const char *process = json_object_get_string(json_object(value), "process");
if (!container || !process) {
fprintf(stderr, "call hyper_set_win_size, invalid config");
goto out;
}
exec = hyper_find_process(pod, container, process);
if (!exec) {
fprintf(stderr, "call hyper_set_win_size, can not find the process: %s\n", process);
goto out;
}
size.ws_row = (int)json_object_get_number(json_object(value), "row");
size.ws_col = (int)json_object_get_number(json_object(value), "column");
ret = ioctl(exec->ptyfd, TIOCSWINSZ, &size);
if (ret < 0)
perror("cannot ioctl to set pty device term size");
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 *pidsnew, *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) {
pidsnew = realloc(pids, npids + 16384);
if (pidsnew == NULL) {
free(pids);
closedir(dp);
return;
}
pids = pidsnew;
npids += 16384;
}
pids[index++] = pid;
}
fprintf(stdout, "Sending SIGTERM\n");
for (--index; index >= 0; --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 pod_inited_efd;
};
static int hyper_pod_init(void *data)
{
struct hyper_pod_arg *arg = data;
struct hyper_pod *pod = arg->pod;
sigset_t mask;
close(hyper_epoll.efd);
close(hyper_epoll.ctl.fd);
close(hyper_epoll.tty.fd);
close(hyper_epoll.dev.fd);
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) {
perror("sigprocmask SIGCHLD failed");
return -1;
}
signal(SIGCHLD, pod_init_sigchld);
/* mount new proc directory */
if (umount("/proc") < 0) {
perror("umount proc filesystem failed\n");
goto fail;
}
if (mount("proc", "/proc", "proc", MS_NOSUID| MS_NODEV| MS_NOEXEC, NULL) < 0) {
perror("mount proc filesystem failed\n");
goto fail;
}
if (sethostname(pod->hostname, strlen(pod->hostname)) < 0) {
perror("set host name failed");
goto fail;
}
fprintf(stdout, "hyper send pod inited event: normal\n");
if (hyper_eventfd_send(arg->pod_inited_efd, HYPER_EVENTFD_NORMAL) < 0) {
fprintf(stderr, "pod init send ready message failed\n");
goto fail;
}
close(arg->pod_inited_efd);
for (;;)
pause(); /* infinite loop and handle SIGCHLD */
out:
_exit(-1);
fail:
fprintf(stderr, "hyper send pod inited event: error\n");
hyper_eventfd_send(arg->pod_inited_efd, HYPER_EVENTFD_ERROR);
close(arg->pod_inited_efd);
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,
.pod_inited_efd = -1,
};
void *stack;
int ret = -1, init_pid;
arg.pod_inited_efd = eventfd(0, EFD_CLOEXEC);
if (arg.pod_inited_efd < 0) {
perror("create eventfd 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_eventfd_recv(arg.pod_inited_efd) < 0) {
perror("get pod init ready message failed");
goto out;
}
pod->init_pid = init_pid;
ret = 0;
out:
close(arg.pod_inited_efd);
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 pid_efd)
{
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) {
if (pid_efd > 0) {
fprintf(stderr, "hyper send exec process pid: error\n");
hyper_eventfd_send(pid_efd, HYPER_EVENTFD_ERROR);
}
perror("fail to fork");
goto out;
} else if (ret > 0) {
fprintf(stdout, "create child process pid=%d in the sandbox\n", ret);
if (pid_efd > 0) {
fprintf(stdout, "hyper send exec process pid: normal\n");
hyper_eventfd_send(pid_efd, ret);
}
_exit(0);
}
out:
close(pidns);
close(ipcns);
close(utsns);
return ret;
}
/*
* All containers in the pod share the same ipc namespace. However,
* posix ipc primitives are shm_open() family whose behaviors
* implemented in glibc are to create&share the shm objects within
* /dev/shm (or scans /proceed/mounts for any tmpfs if /dev/shm
* is not tmpfs).
* So we have to create the only one tmpfs mount and share it
* to all the containers.
*/
static int hyper_setup_shm(struct hyper_pod *pod)
{
if (hyper_mkdir("/tmp/hyper/shm", 0755) < 0) {
perror("create shared shm directory failed");
return -1;
}
if (mount("tmpfs", "/tmp/hyper/shm", "tmpfs", MS_NOSUID| MS_NODEV, NULL) < 0) {
perror("mount shm failed");
return -1;
}
return 0;
}
static bool is_serial = false;
static bool is_xen = false;
static int hyper_setup_shared(struct hyper_pod *pod)
{
int ret;
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;
}
if (is_xen)
ret = mount(pod->share_tag, SHARED_DIR, "9p", MS_NODEV, "trans=xen");
else
ret = mount(pod->share_tag, SHARED_DIR, "9p", MS_MGC_VAL| MS_NODEV, "trans=virtio");
if (ret < 0) {
perror("fail to mount shared dir");
return ret;
}
return 0;
}
static int hyper_setup_virtual_hyperstart_exec_container(struct hyper_pod *pod)
{
if (hyper_mkdir("/tmp/hyper/" HYPERSTART_EXEC_CONTAINER, 0755) < 0) {
perror("create virtual hyperstart-exec container directory failed");
return -1;
}
// for creating ptymaster when adding process with terminal=true
if (symlink("/dev/pts", "/tmp/hyper/" HYPERSTART_EXEC_CONTAINER "/devpts") < 0) {
perror("create virtual hyperstart-exec container's /dev symlink failed");
return -1;
}
return 0;
}
static int hyper_setup_pod(struct hyper_pod *pod)
{
/* create sandbox directory */
if (hyper_mkdir("/tmp/hyper", 0755) < 0) {
perror("create sandbox directory failed");
return -1;
}
if (hyper_setup_network(pod) < 0) {
fprintf(stderr, "setup network failed\n");
return -1;
}
if (hyper_setup_dns(pod) < 0) {
fprintf(stderr, "setup dns file failed\n");
return -1;
}
if (hyper_setup_hostname(pod) < 0) {
fprintf(stderr, "setup hostname file 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_shm(pod) < 0) {
fprintf(stderr, "setup shared shm failed\n");
return -1;
}
if (hyper_setup_pod_init(pod) < 0) {
fprintf(stderr, "start container failed\n");
return -1;
}
if (hyper_setup_virtual_hyperstart_exec_container(pod) < 0) {
return -1;
}
return 0;
}
static void hyper_print_uptime(void)
{
char buf[128];
int fd = open("/proc/uptime", O_RDONLY);
int n;
if (fd < 0)
return;
n = read(fd, buf, sizeof(buf)-1);
if (n > 0) {
buf[n] = 0;
fprintf(stdout, "uptime %s\n", buf);
}
close(fd);
}
static void hyper_flush_channel()
{
// Todo: remove this after we implement DESTROYVM message.
struct hyper_buf *ctl_buf = &hyper_epoll.ctl.wbuf;
struct hyper_buf *tty_buf = &hyper_epoll.tty.wbuf;
hyper_send_data_block(hyper_epoll.ctl.fd, ctl_buf->data, ctl_buf->get);
hyper_send_data_block(hyper_epoll.tty.fd, tty_buf->data, tty_buf->get);
}
void hyper_pod_destroyed(struct hyper_pod *pod, int failed)
{
hyper_cleanup_portmapping(pod);
hyper_cleanup_mounts(pod);
hyper_ctl_append_msg(&hyper_epoll.ctl, failed?ERROR:ACK, NULL, 0);
// Todo: this doesn't make sure peer receives the data
hyper_flush_channel();
// Todo: don't shutdown vm until hyperstart receives the DESTROYVM message,
// peer will send to DESTROYVM until receives the whole data of tty/ctl.
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(pod, error);
} else {
/* Kill pod */
hyper_term_all(pod);
}
return 0;
}
static int hyper_start_pod(struct hyper_pod *pod, char *json, int length)
{
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(struct hyper_pod *pod, char *json, int length)
{
int ret;
struct hyper_container *c;
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;
}
if (hyper_has_container(pod, c->id)) {
fprintf(stderr, "container id conflicts");
hyper_cleanup_container(c, pod);
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(struct hyper_pod *pod, char *json, int length, uint8_t **rmsg)
{
struct hyper_container *c;
int ret = -1;
size_t message_len = 0;
const char *emsg = NULL;
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);
emsg = "no such container";
goto out;
}
ret = kill(c->exec.pid, (int)json_object_get_number(json_object(value), "signal"));
if (ret <0) {
switch(errno) {
case EINVAL:
emsg = "invalid signal";
break;
case EPERM:
emsg = "no permission";
break;
case ESRCH:
emsg = "no such process";
break;
default:
emsg = "kill failed";
break;
}
}
out:
json_value_free(value);
if (emsg != NULL) {
message_len = strlen(emsg) + 1;
*rmsg = (uint8_t*)malloc(message_len);
if (*rmsg != NULL) {
strncpy((char*)*rmsg, emsg, message_len);
}
}
return ret;
}
static int hyper_signal_process(struct hyper_pod *pod, char *json, int length)
{
struct hyper_exec *exec;
int ret = -1;
JSON_Value *value = hyper_json_parse(json, length);
if (value == NULL) {
goto out;
}
const char *container = json_object_get_string(json_object(value), "container");
const char *process = json_object_get_string(json_object(value), "process");
exec = hyper_find_process(pod, container, process);
if (exec == NULL) {
fprintf(stderr, "can not find process");
goto out;
}
kill(-exec->pid, (int)json_object_get_number(json_object(value), "signal"));
ret = 0;
out:
json_value_free(value);
return ret;
}
static int hyper_remove_container(struct hyper_pod *pod, char *json, int length)
{
struct hyper_container *c;
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(struct hyper_pod *pod, 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;
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:
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 int hyper_ctl_send_ready(int fd)
{
uint8_t buf[8];
fprintf(stdout, "send ready message\n");
hyper_set_be32(buf, READY);
hyper_set_be32(buf + 4, 8);
if (hyper_send_data_block(fd, buf, 8) < 0) {
perror("send READY MESSAGE failed\n");
return -1;
}
return 0;
}
static int hyper_setup_ctl_channel(char *name)
{
int fd = hyper_open_channel(name, 0, is_serial);
if (fd < 0)
return fd;
if (hyper_ctl_send_ready(fd) < 0) {
close(fd);
return -1;
}
return fd;
}
static int hyper_setup_tty_channel(char *name)
{
int ret = hyper_open_channel(name, O_NONBLOCK, is_serial);
if (ret < 0)
return -1;
return ret;
}
static int hyper_setup_vsock_channel(void)
{
hyper_epoll.vsock_ctl_listener.fd = hyper_create_vsock_listener(HYPER_VSOCK_CTL_PORT);
if (hyper_epoll.vsock_ctl_listener.fd < 0)
goto out;
hyper_epoll.vsock_msg_listener.fd = hyper_create_vsock_listener(HYPER_VSOCK_MSG_PORT);
if (hyper_epoll.vsock_msg_listener.fd < 0)
goto out;
return 0;
out:
close(hyper_epoll.vsock_ctl_listener.fd);
close(hyper_epoll.vsock_msg_listener.fd);
return -1;
}
static int hyper_setup_normal_channel(void)
{
char *ctl_serial = NULL, *tty_serial = NULL;
if (is_serial) {
ctl_serial = strdup("/dev/ttyS1");
tty_serial = strdup("/dev/ttyS2");
} else if (is_xen) {
ctl_serial = strdup("/dev/hvc1");
tty_serial = strdup("/dev/hvc2");
is_serial = true;
} else {
ctl_serial = hyper_find_virtio_port("sh.hyper.channel.0");
if (ctl_serial == NULL) {
fprintf(stderr, "cannot find ctl channel\n");
goto out;
}
tty_serial = hyper_find_virtio_port("sh.hyper.channel.1");
if (tty_serial == NULL) {
fprintf(stderr, "cannot find tty channel\n");
goto out;
}
}
fprintf(stdout, "ctl: %s, tty: %s\n", ctl_serial, tty_serial);
hyper_epoll.ctl.fd = hyper_setup_ctl_channel(ctl_serial);
if (hyper_epoll.ctl.fd < 0) {
fprintf(stderr, "fail to setup hyper control serial port\n");
goto out;
}