在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。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值