[Lxc-users] PID namespace behavior

Serge Hallyn serge.hallyn at canonical.com
Wed Apr 11 19:52:44 UTC 2012


Quoting Arun M (arunmahadevaiyer at gmail.com):
> I guess the unshare and shell does not belong to the same process group. I
> am able to reproduce with the following code.
> 
> cat > pgrp.c <<EOF
> #include <stdio.h>
> #include <signal.h>
> #include <unistd.h>
> #include <alloca.h>
> #include <sched.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> 
> 
> void handle_usr1()
> {
> printf("PID = %d, got usr1\n", getpid());
> }
> 
> int do_child(void *a)
> {
> printf("Child pgrp = %d\n",getpgrp());
> sleep(10);
> kill(0, SIGUSR1);
> return 0;
> }
> 
> int main()
> {
> pid_t pid;
> int status;
> long stack_size = sysconf(_SC_PAGESIZE);
> void *stack = alloca(stack_size);
> 
> signal(SIGUSR1, handle_usr1);
> 
> if ( (pid = clone(do_child, stack + stack_size, CLONE_NEWPID, NULL)) == -1)
> {
> perror("clone");
> return -1;
> }
> 
> printf("Parent pgrp = %d\n",getpgrp());
> 
> waitpid(pid, &status, __WCLONE);
> 
> return 0;
> }
> EOF
> 
> $ gcc pgrp.c -o pgrp
> $ sudo ./pgrp
> Parent pgrp = 21135
> Child pgrp = 0
> PID = 1, got usr1
> PID = 21136, got usr1
> User defined signal 1
> 
> ----
> 
> Though the child pgrp is 0 in child, outside the namespace both the parent
> and child seem to be having the same pgrp id and hence the signal is
> delivered to the parent as well.
> 
> Thanks,
> Arun

Yup, fascinating.

I think this means we may want to do a setsid() or setpgrp() in
src/lxc/start:lxc_start().

-serge




More information about the lxc-users mailing list