I am trying to create a process with new namespaces, and for that i need to use clone(2) with the appropriate flags, the following is the clone system call and a printf() statement to print the parent PID:
printf("clone() = %ld\n", (long)getpid());
printf("PID: %ld\n", (long)getpid());
struct utsname utsname;
uname(&utsname);
printf("parent namespace hostname: %s\n", utsname.nodename);
clone(child_main(&process_struct, checkpoint), stack + process_struct.Stack,
CLONE_NEWCGROUP
|CLONE_NEWIPC
|CLONE_NEWNET
|CLONE_NEWNS
|CLONE_NEWPID
|CLONE_NEWUTS|SIGCHLD, &process_struct)
and the child_main() function, the child function will set the hostname and then will print the child's PID, the problem is that the hostname of the system changes not just the namespace's hostname, and the child's PID is the same of the parent, this wrong and the child PID from within the namespace should be 1 and its PPID is 0 (meaning no parent), also sethostname(2) must only affect the child process namespace
int child_main(struct process *process, int *checkpoint){
char c;
fprintf(stderr,"=> IPC setup...");
//double check the IPC
close(checkpoint[1]);
fprintf(stderr,"Done\n");
if ( sethostname(process->Hostname, strlen(process->Hostname)){
//close(process->File_descriptor);
return -1;
}
printf("PID: %ld\n", (long)getpid());
struct utsname utsname;
uname(&utsname);
printf("child namespace hostname: %s\n", utsname.nodename);
// startup the IPC pipes
read(checkpoint[0], &c, 1);
char* argv[]={(char*)0};
if(execve("/bin/bash", argv, NULL) == -1 ){
fprintf(stderr,"--> Launching process Failed %m\n");
return -1;
}
return 0;
}
int
(the return fromchild_main
).