Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 8bb0086

Browse files
author
Damien Lespiau
committed
Revert "Move containers to their own PID namespace"
Julio noticed a strange speed regression on exec with this commit. Revert for now. This reverts commit 9bf5e1b. Signed-off-by: Damien Lespiau <[email protected]>
1 parent 8dddce4 commit 8bb0086

File tree

5 files changed

+28
-60
lines changed

5 files changed

+28
-60
lines changed

src/container.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,6 @@ void hyper_cleanup_container(struct hyper_container *c, struct hyper_pod *pod)
785785
perror("umount devpts failed");
786786

787787
close(c->ns);
788-
close(c->pid_ns);
789788
hyper_cleanup_container_portmapping(c, pod);
790789
hyper_free_container(c);
791790
}

src/container.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ struct hyper_container {
3434
struct list_head list;
3535
struct hyper_exec exec;
3636
int ns;
37-
int pid_ns;
3837
uint32_t code;
3938

4039
// configs

src/exec.c

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <sys/stat.h>
77
#include <sys/epoll.h>
88
#include <sys/ioctl.h>
9-
#include <sys/mount.h>
109
#include <sys/wait.h>
1110
#include <sys/socket.h>
1211
#include <dirent.h>
@@ -479,7 +478,6 @@ static int hyper_setup_stdio_events(struct hyper_exec *exec, struct stdio_config
479478
static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_config *io)
480479
{
481480
struct hyper_container *c;
482-
int ret;
483481

484482
if (hyper_enter_sandbox(exec->pod, pipe) < 0) {
485483
perror("enter pidns of pod init failed");
@@ -497,46 +495,11 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con
497495
perror("fail to enter container ns");
498496
goto out;
499497
}
500-
501-
/* c->pid_ns is set in hyper_run_process based off the pid sent over the pipe */
502-
if (c->pid_ns > 0) {
503-
if (setns(c->pid_ns, CLONE_NEWPID) < 0) {
504-
perror("fail to enter container pid ns");
505-
goto out;
506-
}
507-
} else {
508-
if (unshare(CLONE_NEWPID) < 0) {
509-
perror("failed to create new pid ns");
510-
goto out;
511-
}
512-
}
513-
514-
/* current process isn't in the pidns even though setns(pidns, CLONE_NEWPID)
515-
* was called. fork() is needed, so that the child process will run in
516-
* the pidns, see man 2 setns */
517-
ret = fork();
518-
if (ret < 0) {
519-
perror("failed to fork");
520-
goto out;
521-
} else if (ret > 0) {
522-
fprintf(stdout, "created child process pid=%d in the sandbox\n", ret);
523-
if (pipe > 0) {
524-
hyper_send_type(pipe, ret);
525-
}
526-
_exit(0);
527-
}
528-
529498
if (chdir("/") < 0) {
530499
perror("fail to change to the root of the rootfs");
531500
goto out;
532501
}
533502

534-
/* iff creating new pid namespace remount /proc inside */
535-
if (c->pid_ns == -1 && mount("proc", "/proc", "proc", MS_NOSUID | MS_NODEV | MS_NOEXEC, NULL) < 0) {
536-
perror("failed to mount /proc after pid namespace switch");
537-
goto out;
538-
}
539-
540503
// Clear process environment
541504
clearenv();
542505

@@ -649,8 +612,6 @@ int hyper_run_process(struct hyper_exec *exec)
649612
int pid, ret = -1;
650613
uint32_t type;
651614
struct stdio_config io = {-1, -1,-1, -1,-1, -1};
652-
struct hyper_container *c;
653-
char path[128];
654615

655616
if (exec->argv == NULL || exec->seq == 0 || exec->container_id == NULL || strlen(exec->container_id) == 0) {
656617
fprintf(stderr, "cmd is %p, seq %" PRIu64 ", container %s\n",
@@ -687,21 +648,6 @@ int hyper_run_process(struct hyper_exec *exec)
687648
goto close_tty;
688649
}
689650

690-
c = hyper_find_container(exec->pod, exec->container_id);
691-
if (c == NULL) {
692-
fprintf(stderr, "can not find container %s\n", exec->container_id);
693-
goto out;
694-
}
695-
696-
if (c->pid_ns < 0) {
697-
sprintf(path, "/proc/%d/ns/pid", type);
698-
c->pid_ns = open(path, O_RDONLY | O_CLOEXEC);
699-
if (c->pid_ns < 0) {
700-
perror("open container pid ns failed");
701-
goto close_tty;
702-
}
703-
}
704-
705651
if (hyper_setup_stdio_events(exec, &io) < 0) {
706652
fprintf(stderr, "add pts master event failed\n");
707653
goto close_tty;

src/init.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,16 @@ static int hyper_setup_pod_init(struct hyper_pod *pod)
333333
// enter the sanbox and pass to the child, shouldn't call from the init process
334334
int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe)
335335
{
336-
int ret = -1, utsns = -1, ipcns = -1;
336+
int ret = -1, pidns = -1, utsns = -1, ipcns = -1;
337337
char path[512];
338338

339+
sprintf(path, "/proc/%d/ns/pid", pod->init_pid);
340+
pidns = open(path, O_RDONLY| O_CLOEXEC);
341+
if (pidns < 0) {
342+
perror("fail to open pidns of pod init");
343+
goto out;
344+
}
345+
339346
sprintf(path, "/proc/%d/ns/uts", pod->init_pid);
340347
utsns = open(path, O_RDONLY| O_CLOEXEC);
341348
if (utsns < 0) {
@@ -350,14 +357,32 @@ int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe)
350357
goto out;
351358
}
352359

353-
if (setns(utsns, CLONE_NEWUTS) < 0 ||
360+
if (setns(pidns, CLONE_NEWPID) < 0 ||
361+
setns(utsns, CLONE_NEWUTS) < 0 ||
354362
setns(ipcns, CLONE_NEWIPC) < 0) {
355363
perror("fail to enter the sandbox");
356364
goto out;
357365
}
358366

359-
ret = 0;
367+
/* current process isn't in the pidns even setns(pidns, CLONE_NEWPID)
368+
* was called. fork() is needed, so that the child process will run in
369+
* the pidns, see man 2 setns */
370+
ret = fork();
371+
if (ret < 0) {
372+
perror("fail to fork");
373+
goto out;
374+
} else if (ret > 0) {
375+
fprintf(stdout, "create child process pid=%d in the sandbox\n", ret);
376+
if (pidpipe > 0) {
377+
hyper_send_type(pidpipe, ret);
378+
}
379+
_exit(0);
380+
}
381+
360382
out:
383+
if (pidns >= 0)
384+
close(pidns);
385+
361386
if (ipcns >= 0)
362387
close(ipcns);
363388

src/parse.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
661661
c->exec.stderrev.fd = -1;
662662
c->exec.ptyfd = -1;
663663
c->ns = -1;
664-
c->pid_ns = -1;
665664
INIT_LIST_HEAD(&c->list);
666665

667666
next_container = toks[i].size;

0 commit comments

Comments
 (0)