bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FEATURE REQUEST : shell option to automatically terminate child proc


From: Corto Beau
Subject: Re: FEATURE REQUEST : shell option to automatically terminate child processes on parent death
Date: Sat, 11 Nov 2023 23:50:38 +0100
User-agent: Mozilla Thunderbird

Except a wrapper program would be subject to the race condition I mentioned in my own message. Sorry for the noise.

On 11/11/2023 21:54, Corto Beau wrote:

 I think that a loadable builtin might be overkill for the second case, because one could write a wrapper program to set PDEATHSIG and call execve on the command-line arguments.

This way you'd just have to run "noreparent foo" instead of writing a shell function that calls the noreparent builtin and exec into the child afterwards. It wouldn't allow to unset the flag though, but I can't think of a use-case for that.

It would also eliminate the need for a "fork_noreparent" builtin because "noreparent foo &" would do just that.

On the other hand, the shell option to automatically set PDEATHSIG on all newly created children allows to enable this behavior without prefixing every line with "noreparent". I would understand if this use-case was considered too narrow though.

On 11/11/2023 21:32, Corto Beau wrote:

Do you mean something like a "fork_noreparent" builtin that would call make_child and set PDEATHSIG afterwards, or a "noreparent" builtin that the child would have to call ? I do agree that it wouldn't make sense to implement the later as a shell option, but this particular use of prctl is subject to a race condition that can only be avoided by comparing the PID of the parent before calling fork and after calling prctl (in case the process got reparented before setting PDEATHSIG), because checking for ppid = 1 is not enough on systems like Linux where the process might have been reparented by a subreaper.

On 11/11/2023 20:49, Oğuz wrote:
On Saturday, November 11, 2023, Corto Beau <corto.beau@laposte.net> wrote:

    Configuration Information [Automatically generated, do not change]:
    Machine: x86_64
    OS: linux-gnu
    Compiler: gcc
    Compilation CFLAGS: -g -O2
    uname output: Linux zinc 6.6.1-arch1-1 #1 SMP PREEMPT_DYNAMIC
    Wed, 08 Nov 2023 16:05:38 +0000 x86_64 GNU/Linux
    Machine Type: x86_64-pc-linux-gnu

    Bash Version: 5.2 Patch
    Level: 21
    Release Status: release

    Description:
    Hi,

    I would like to suggest a new shell option to ensure child
    processes are automatically killed when the parent dies.

    Right now, it's already possible to emulate this feature by
    setting a trap to kill all child processes on exit (trap "kill
    0" EXIT), but obviously it doesn't work if the process is
    terminated by a signal that cannot be caught (like SIGKILL).

    On Linux, it can be done by setting the PR_SET_PDEATHSIG flag to
    propagate the parent termination signal to the child, regardless
    of whether the signal can be caught or not.

    The rationale for this feature is that scripts spawning
    background processes to listen to various events (udev events,
    window manager events, etc) often leave orphan processes behind
    when terminated forcefully.

    I've attached a proof-of-concept patch.


I think it'd make more sense to implement this as a loadable builtin.


--
Oğuz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]