On Tue, Mar 1, 2022 at 5:39 PM Denys Vlasenko <[email protected]> wrote:
> Meanwhile: what "timeout" is doing is it tries to get out
> of the way of the PROG to be launched so that timeout's parent
> sees PROG (not timeout) as a child. E.g. it can send signals
> to it, get waitpid notifications if PROG has been stopped
> with a signal, and such.
>
> And PROG also has no spurious "timeout" child.
> "timeout" exists as an orphaned granchild.
>
> Let's go with a solution with fd opened to /proc/PID?

This little test:

        int fd = open("/proc/self", O_RDONLY);
        int parent = getpid();
        int pid = fork();
        if (pid) { //parent
                sleep(1);
                exit(0);
        }
        sleep(2);
        printf("openat:%d\n", openat(fd, ".", O_RDONLY));
        while (openat(fd, ".", O_RDONLY) == -1)
                continue;

and a separate "spawn 40k 'sleep 0.03'" loop
seems to indicate that openat(fd, ".") on the exited
/proc/PID fails, and continues to fail
even if another process with this PID exists
again (pid was reused).

This would make it impossible that this
"does process exist?" test hooks on a different
process with the same pid:

        while (1) {
                sleep(1);
                if (--timeout <= 0)
                        break;
-               if (kill(pid, 0)) {
+               tfd = openat(fd, ".", O_RDONLY);
+               if (tfd == -1) {
                        /* process is gone */
                        return EXIT_SUCCESS;
                }
+               close(tfd);
        }

It would still be possible for the watched process to exit
right before "timeout" decided to SIGTERM it,
another process to fork and reuse this pid, and get
this SIGTERM instead.
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to