Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug in interaction with pid namespaces

On Wed, Dec 17, 2014 at 8:54 AM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Dec 16, 11:50pm, Chirantan Ekbote wrote:
> }
> } We're not trying to use zsh as a replacement for init. I first noticed
> } this issue because the chrome os build system recently switched to
> } using pid namespaces in the build chroot. [...] In the actual workflow
> } where this is an issue for me, the pid is somewhere in the 240 to 270
> } range and zsh is only meant to behave like a regular shell.
> OK, so the issue is that zsh is started as a (possibly indirect) child
> of whatever was given "leadership" of the namespace, but at the time
> zsh starts GETPGRP() == 0.
> That sounds like a bug in one of the ancestor processes, then, because
> shouldn't whatever *that* is, have become a group leader, or assigned
> the process group to the fork() pid before execve() of zsh?

It quite possibly is a bug in the ancestor process.  I've also brought
this up with our build team and I think they may make the change you

> } So the problem is that on exit zsh gives a warning:
> }
> }     zsh: can't set tty pgrp: no such process
> }
> } which I thought was due to the call to setpgrp() but is actually in
> } the call to attachtty().
> Aha.  That means that one of
>         tcsetpgrp(SHTTY, pgrp)
> or
>         arg = &pgrp
>         ioctl(SHTTY, TIOCSPGRP, &arg)
> is returning -1 and errno == ESRCH.  Which makes sense if pgrp == 0.
> But that also means that zsh is being invoked as an interactive shell,
> because attachtty() is a no-op otherwise.  I can't imagine why the
> chrome os build would need to run an interactive shell.

I won't bore you with the details of the build process because they
would bore you.  The short version is to start working on chrome os we
run a command (cros_sdk) that downloads updates, sets up the
environment, mounts various directories inside a chroot, and at the
end runs

    exec chroot "${chroot_dir}" sudo -u ${username} -i

It is at this point that zsh is launched (since it is my login shell)
and from there, I can run commands to build different components or an
entire image of chrome os.

> However, I suggest the following.  The mypgrp = origpgrp assignment is
> questionable because the pgrp won't actually have been changed, but it
> may help elsewhere in tests that mypgrp != GETPGRP().  Shell exit is
> not the only place where release_pgrp() is called.
> diff --git a/Src/jobs.c b/Src/jobs.c
> index 8c4254a..c6e1bce 100644
> --- a/Src/jobs.c
> +++ b/Src/jobs.c
> @@ -2779,8 +2779,11 @@ void
>  release_pgrp(void)
>  {
>      if (origpgrp != mypgrp) {
> -       attachtty(origpgrp);
> -       setpgrp(0, origpgrp);
> +       /* in linux pid namespaces, origpgrp may never have been set */
> +       if (origpgrp) {
> +           attachtty(origpgrp);
> +           setpgrp(0, origpgrp);
> +       }
>         mypgrp = origpgrp;
>      }
>  }

This works for me.  Thanks very much for your help :-)

Messages sorted by: Reverse Date, Date, Thread, Author