Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 9ce56b7

Browse files
committed
podinit: wait until container environment is ready
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 72fc9ad commit 9ce56b7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/container.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ struct hyper_container_arg {
457457
struct hyper_pod *pod;
458458
int ipcns;
459459
int utsns;
460+
int pipe[2];
460461
};
461462

462463
static int hyper_container_init(void *data)
@@ -614,6 +615,7 @@ static int hyper_container_init(void *data)
614615
goto fail;
615616
}
616617

618+
hyper_send_type(arg->pipe[1], READY);
617619
fflush(stdout);
618620

619621
if (container_setup_tty(container) < 0) {
@@ -632,6 +634,7 @@ static int hyper_container_init(void *data)
632634
_exit(126);
633635

634636
fail:
637+
hyper_send_type(arg->pipe[1], ERROR);
635638
_exit(125);
636639
}
637640

@@ -669,10 +672,12 @@ int hyper_start_container(struct hyper_container *container,
669672
.pod = pod,
670673
.utsns = utsns,
671674
.ipcns = ipcns,
675+
.pipe = {-1, -1},
672676
};
673677
int flags = CLONE_NEWNS | SIGCHLD;
674678
char path[128];
675679
void *stack;
680+
uint32_t type;
676681
int pid;
677682

678683
if (container->image == NULL || container->exec.argv == NULL) {
@@ -681,6 +686,11 @@ int hyper_start_container(struct hyper_container *container,
681686
goto fail;
682687
}
683688

689+
if (pipe2(arg.pipe, O_CLOEXEC) < 0) {
690+
perror("create pipe between pod init execcmd failed");
691+
goto fail;
692+
}
693+
684694
if (hyper_setup_pty(container) < 0) {
685695
fprintf(stderr, "setup pty device for container failed\n");
686696
goto fail;
@@ -711,10 +721,18 @@ int hyper_start_container(struct hyper_container *container,
711721
goto fail;
712722
}
713723

724+
/* wait for ready message */
725+
if (hyper_get_type(arg.pipe[0], &type) < 0 || type != READY) {
726+
fprintf(stderr, "wait for container started failed\n");
727+
goto fail;
728+
}
729+
714730
container->exec.pid = pid;
715731
list_add_tail(&container->exec.list, &pod->exec_head);
716732
container->exec.ref++;
717733

734+
close(arg.pipe[0]);
735+
close(arg.pipe[1]);
718736
fprintf(stdout, "container %s,init pid %d,ref %d\n", container->id, pid, container->exec.ref);
719737
return 0;
720738
fail:
@@ -727,6 +745,8 @@ int hyper_start_container(struct hyper_container *container,
727745
container->exec.code = -1;
728746
container->exec.seq = 0;
729747
container->exec.ref = 0;
748+
close(arg.pipe[0]);
749+
close(arg.pipe[1]);
730750
return -1;
731751
}
732752

0 commit comments

Comments
 (0)