Re: [hackers] Re: [dwm][PATCH v2] Use sigaction(SA_NOCLDWAIT) for SIGCHLD handling

From: NRK <nrk_AT_disroot.org>
Date: Sat, 28 Jan 2023 15:04:50 +0600

Hi Hiltjo,

On Sat, Jan 28, 2023 at 12:11:44AM +0100, Hiltjo Posthuma wrote:
> We do not need to waitpid() on child processes. It is handled already
> by using sigaction().

Here's a test case. I see `cat` turning into a zombie on my system
without the `waitpid`:

        [/tmp]~> cat test.c
        #include <signal.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/wait.h>
        #include <unistd.h>
        
        int main(void)
        {
                struct sigaction sa;

                puts("waiting for cat to become a zombie");
                sleep(1);

                sigemptyset(&sa.sa_mask);
                sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
                sa.sa_handler = SIG_IGN;
                sigaction(SIGCHLD, &sa, NULL);

                puts("hello there");
                (void)getchar();
        }
        [/tmp]~> cat test.sh
        #!/bin/sh
        
        cat &
        exec ./test
        [/tmp]~> cc -o test test.c
        [/tmp]~> ./test.sh
        waiting for cat to become a zombie
        hello there

Just putting `while (waitpid(-1, NULL, WNOHANG) > 0);` after the
`sigaction` call (without worrying about EINTR) should still be better
than not calling `waitpid` at all IMO.

- NRK
Received on Sat Jan 28 2023 - 10:04:50 CET

This archive was generated by hypermail 2.3.0 : Sat Jan 28 2023 - 10:12:31 CET