在linux中使用setns()设置pid namespace

以下代码展示了 setns() 的用法:

#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#define STACK_SIZE	(1024 * 1024)	

int idle(void *args)
{
	printf("I'm child process, and my pid is: %d\n", getpid());
	for (;;) {
		sleep(1);
	}

	return 0;
}

pid_t clone_wrapper(int (*func)(void *), int flag, void *args)
{
	char *stack, *stack_top;
	
	stack = (char *)malloc(STACK_SIZE);
	if (stack == NULL) {
		printf("alloc stack for child failed!\n");
		return -1;
	}
	stack_top = stack + STACK_SIZE;  /* Assume stack grows downward */

	return clone(func, stack_top, flag , args);
}

char *get_pid_ns(int pid)
{
	char bytes[32];
	
	sprintf(bytes, "/proc/%d/ns/pid", pid);
	return strdup(bytes);
}

int main(void)
{
	pid_t childs[2];
	char *ns_file;
	int fd;

	printf("I'm parent, and my pid is: %d\n", getpid());

	childs[0] = clone_wrapper(idle, CLONE_NEWPID, NULL);
	if (childs[0] == -1) {
		printf("error: create child thread failed!\n");
		return ;
	}
	printf("first child's pid is: %d\n", childs[0]);

	ns_file = get_pid_ns(childs[0]);
	if (!ns_file) {
		printf("get child pid ns failed!\n");
		return -1;
	}

	fd = open(ns_file, O_RDONLY);
	if (fd == -1) {
		printf("open child pid ns failed!\n");
		return -1;
	}

	if (setns(fd, 0) == -1) {
		printf("set ns failed!\n");
		return -1;
	}

	printf("I'm parent, and my pid is: %d\n", getpid());

	childs[1] = clone_wrapper(idle, 0, NULL);
	if (childs[1] == -1) {
		printf("error: create child thread failed!\n");
		return -1;
	}
	printf("second child's pid is: %d\n", childs[1]);

	sleep(3);

	kill(childs[0], SIGTERM);
	kill(childs[1], SIGTERM);

	waitpid(childs[0], NULL, 0);
	waitpid(childs[1], NULL, 0);

	return 0;
}

运行结果:

$ gcc -o main ./main.c
$ sudo ./main 
I'm parent, and my pid is: 18611
first child's pid is: 18612
I'm child process, and my pid is: 1
I'm parent, and my pid is: 18611
second child's pid is: 18613
I'm child process, and my pid is: 2

其中,父进程在创建第一个子进程时指定了 CLONE_NEWPID,然后父进程调用 setns(), 将其 pid namespace 设置为第一个子进程的 pid namespace,接下来父进程又创建了第二个子进程,则此时,第一个进程和第二个处在同一个 pid namespace 中,在父进程的 pid namespace 中,它们的 PID 分别是:18612、18613,在子进程的 pid namespace 中,它们的 PID 分别是:1、2。

上一篇:香洲区湾仔镇哪条街有一条龙全套服务特殊大保健sdf


下一篇:2021-03-04