Namespaces icon indicating copy to clipboard operation
Namespaces copied to clipboard

Just a question/comment to the code.

Open wangtao13 opened this issue 9 years ago • 1 comments

Hi Nigel, Thank you very much for your sharing these excellent codes and blogs about Linux namespace, they are full of useful and helpful information for the guys want to know/learn Linux namespace, like me.

I found two things that I could NOT fully understand in the code.

  1. Say, in the invoke_ns6.c, the following line args.hostname = malloc(sizeof(char *) * (strlen(optarg) + 1)); is used to allocate a memory buffer to hold the user input 'hostname'. The length arg of malloc is 'sizeof(char *) * (strlen(optarg) + 1)'. So when "calculus" is used, malloc will allocate 72 bytes in my X86_64 (Ubuntu 16.04-AMD64) system, not 8+1=9 bytes. Should it be 'sizeof(char)' not 'sizeof(char *)' ??

  2. In the MNT namespace blog and code, if ((args->flags & CLONE_NEWPID) && (args->flags & CLONE_NEWNS)) {
    if (mount("none", "/proc", "", MS_REC|MS_PRIVATE, NULL) == -1) perror(" Child: mount"); if (mount("proc", "/proc", "proc", 0, NULL) == -1) perror(" Child: mount"); } You used 2 calls of mount, why? If without the first call, the MNT namespace in child process will mess up the /proc/ in the parent (host). And, you mentioned unshare() call, I did not find you used it in the blogs.

I tried to use it to replace the first call of mount() in child process, with CLONE_NEWNS, just before 'if (mount("proc", "/proc", "proc", 0, NULL) == -1)', but it could NOT work as expected. Then, I commented out the mounting of /proc in child process, just called unshare(CLONE_NEWNS) in child process, and mount /proc in child BASH, the /proc in child process can work well without messing up the host's /proc/

Can you shine your idea on this?

Many thanks,

-Tao

wangtao13 avatar May 05 '17 01:05 wangtao13

@wangtao13 I'm really sorry, your issue must have got lost in a sea of notifications! Thanks for finding the sizeof(char *) bug, it was in a couple of places.

On the mount issue; systemd makes all mounts shared by default, so the first call is to make the mount point private, before mounting a new procfs instance. I'm not entirely sure why this has to be completed in two discrete steps, but it doesn't work if you try and combine them.

I chose to use the clone syscall for the tutorials, rather than unshare, but wanted to be consistent throughout, that's why I haven't used unshare.

nbrownuk avatar Sep 12 '17 17:09 nbrownuk