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容器中的运行进程,故而无法退出。
来源:CSDN
作者:HELLO_蓝猫
链接:https://blog.csdn.net/BJUT_bluecat/article/details/103615887