利用namespace修改主机名后的问题

∥☆過路亽.° 提交于 2020-01-27 01:42:29

1 背景

学习docker隔离机制的过程中完成一个uts隔离的demo,程序运行状态良好,之后关掉了执行程序的终端,新开了终端,主机名仍然没变,通过exit无法退出程序了。

2 namespace实现主机名隔离

参考:https://blog.csdn.net/weixin_34273046/article/details/92464192

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
 
#define STACK_SIZE (1024*1024)
 
static char child_stack[STACK_SIZE];
char* const child_args[] = {
	"/bin/bash",
	NULL
};
 
int child_main(void* args){
	printf("Now in child process!\n");
	sethostname("123456",12);
	execv(child_args[0],child_args);
	return 1;
}
 
int main(){
	printf("Program start: \n");
	int child_pid = clone(child_main,child_stack + STACK_SIZE,CLONE_NEWUTS | SIGCHLD,NULL);
	waitpid(child_pid,NULL,0);
	printf("Already exit!\n");
	return 0;
}


3 原因

在这里插入图片描述

左上角执行了uts隔离程序,其本质是一个守护进程,即使终端关闭,守护进程也不会终止,在其他终端中,无法查看lxc容器中的运行进程,故而无法退出。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!