pctl(PR_SET_PDEATHSIG) 竞争条件

Bai*_*ker 5 c linux signals process

据我了解,在父进程死亡时终止子进程的最佳方法是通过prctl(PR_SET_PDEATHSIG)(至少在Linux上):How to make child process die afterparent exits?

对此有一个警告man prctl

当执行 set-user-ID 或 set-group-ID 二进制文件或具有相关功能的二进制文件时(自 Linux 2.4.36 / 2.6.23 起),该值会被 fork(2) 的子进程清除(请参阅能力(7))。该值在 execve(2) 中保留。

因此,以下代码存在竞争条件:

父级.c:

#include <unistd.h>

int main(int argc, char **argv) {
  int f = fork();
  if (fork() == 0) {
    execl("./child", "child", NULL, NULL);
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

孩子.c:

#include <sys/prctl.h>
#include <signal.h>

int main(int argc, char **argv) {
  prctl(PR_SET_PDEATHSIG, SIGKILL); // ignore error checking for now
  // ...
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

也就是说,父级计数 die beforeprctl()在子级中执行(因此子级将不会收到SIGKILL)。解决这个问题的正确方法是prctl()在父级之前exec()

父级.c:

#include <unistd.h>
#include <sys/prctl.h>
#include <signal.h>

int main(int argc, char **argv) {
  int f = fork();
  if (fork() == 0) {
    prctl(PR_SET_PDEATHSIG, SIGKILL); // ignore error checking for now
    execl("./child", "child", NULL, NULL);
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

孩子.c:

int main(int argc, char **argv) {
  // ...
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果./child是 setuid/setgid 二进制文件,那么避免竞争条件的这个技巧不起作用(exec()ing setuid/setgid 二进制文件会导致PDEATHSIG按照上面引用的手册页丢失),并且似乎您被迫采用第一个(活泼的)解决方案。

child如果setuid/setgid 二进制文件prctl(PR_SET_PDEATH_SIG)以非活泼的方式存在,有什么办法吗?

Nom*_*mal 4

让父进程设置管道更为常见。父进程保持写端打开(pipefd[1]),关闭读端(pipefd[0])。子进程关闭写端( pipefd[1]),并将读端( pipefd[1])设置为非阻塞。

这样,子进程可以用来read(pipefd[0], buffer, 1)检查父进程是否仍然存在。如果父进程仍在运行,它将返回-1(errno == EAGAINerrno == EINTR)。

SIGIO现在,在Linux中,子进程还可以设置读取结束异步,在这种情况下,当父进程退出时,它将发送一个信号(默认情况下):

fcntl(pipefd[0], F_SETSIG, desired_signal);
fcntl(pipefd[0], F_SETOWN, getpid());
fcntl(pipefd[0], F_SETFL, O_NONBLOCK | O_ASYNC);
Run Code Online (Sandbox Code Playgroud)

使用 siginfo 处理程序desired_signal。如果info->si_code == POLL_IN && info->si_fd == pipefd[0],则父进程要么退出,要么向管道写入内容。因为read()异步信号安全,并且管道是非阻塞的,所以您可以read(pipefd[0], &buffer, sizeof buffer)在信号处理程序中使用父级是否写入了某些内容,或者父级是否退出(关闭管道)。在后一种情况下,read()将返回0

据我所知,这种方法没有竞争条件(如果您使用实时信号,则信号不会因为用户发送的信号已经挂起而丢失),尽管它非常特定于 Linux。设置信号处理程序后,在子进程生命周期的任何时刻,子进程始终可以显式检查父进程是否仍然存在,而不会影响信号生成。

因此,用伪代码回顾一下:

Construct pipe
Fork child process

Child process:
    Close write end of pipe
    Install pipe signal handler (say, SIGRTMIN+0)
    Set read end of pipe to generate pipe signal (F_SETSIG)
    Set own PID as read end owner (F_SETOWN)
    Set read end of pipe nonblocking and async (F_SETFL, O_NONBLOCK | O_ASYNC)
    If read(pipefd[0], buffer, sizeof buffer) == 0,
        the parent process has already exited.

    Continue with normal work.

Child process pipe signal handler:
    If siginfo->si_code == POLL_IN and siginfo->si_fd == pipefd[0],
        parent process has exited.
        To immediately die, use e.g. raise(SIGKILL).    

Parent process:
    Close read end of pipe

    Continue with normal work.
Run Code Online (Sandbox Code Playgroud)

我不指望你相信我的话。

下面是一个粗略的示例程序,您可以使用它自己检查此行为。它很长,但只是因为我希望很容易看到运行时发生的情况。要在普通程序中实现这一点,您只需要几十行代码。示例.c

#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

static volatile sig_atomic_t done = 0;

static void handle_done(int signum)
{
    if (!done)
        done = signum;
}

static int install_done(const int signum)
{
    struct sigaction act;

    memset(&act, 0, sizeof act);
    sigemptyset(&act.sa_mask);
    act.sa_handler = handle_done;
    act.sa_flags = 0;
    if (sigaction(signum, &act, NULL) == -1)
        return errno;

    return 0;
}

static int  deathfd = -1;

static void death(int signum, siginfo_t *info, void *context)
{
    if (info->si_code == POLL_IN && info->si_fd == deathfd)
        raise(SIGTERM);
}

static int install_death(const int signum)
{
    struct sigaction act;

    memset(&act, 0, sizeof act);
    sigemptyset(&act.sa_mask);
    act.sa_sigaction = death;
    act.sa_flags = SA_SIGINFO;
    if (sigaction(signum, &act, NULL) == -1)
        return errno;

    return 0;
}

int main(void)
{
    pid_t  child, p;
    int    pipefd[2], status;
    char   buffer[8];

    if (install_done(SIGINT)) {
        fprintf(stderr, "Cannot set SIGINT handler: %s.\n", strerror(errno));
        return EXIT_FAILURE;
    }

    if (pipe(pipefd) == -1) {
        fprintf(stderr, "Cannot create control pipe: %s.\n", strerror(errno));
        return EXIT_FAILURE;
    }

    child = fork();
    if (child == (pid_t)-1) {
        fprintf(stderr, "Cannot fork child process: %s.\n", strerror(errno));
        return EXIT_FAILURE;
    }

    if (!child) {
        /*
         * Child process.
        */

        /* Close write end of pipe. */
        deathfd = pipefd[0];
        close(pipefd[1]);

        /* Set a SIGHUP signal handler. */
        if (install_death(SIGHUP)) {
            fprintf(stderr, "Child process: cannot set SIGHUP handler: %s.\n", strerror(errno));
            return EXIT_FAILURE;
        }

        /* Set SIGTERM signal handler. */
        if (install_done(SIGTERM)) {
            fprintf(stderr, "Child process: cannot set SIGTERM handler: %s.\n", strerror(errno));
            return EXIT_FAILURE;
        }

        /* We want a SIGHUP instead of SIGIO. */
        fcntl(deathfd, F_SETSIG, SIGHUP);

        /* We want the SIGHUP delivered when deathfd closes. */
        fcntl(deathfd, F_SETOWN, getpid());

        /* Make the deathfd (read end of pipe) nonblocking and async. */
        fcntl(deathfd, F_SETFL, O_NONBLOCK | O_ASYNC);

        /* Check if the parent process is dead. */
        if (read(deathfd, buffer, sizeof buffer) == 0) {
            printf("Child process (%ld): Parent process is already dead.\n", (long)getpid());
            return EXIT_FAILURE;
        }

        while (1) {
            status = __atomic_fetch_and(&done, 0, __ATOMIC_SEQ_CST);
            if (status == SIGINT)
                printf("Child process (%ld): SIGINT caught and ignored.\n", (long)getpid());
            else
            if (status)
                break;
            printf("Child process (%ld): Tick.\n", (long)getpid());
            fflush(stdout);
            sleep(1);

            status = __atomic_fetch_and(&done, 0, __ATOMIC_SEQ_CST);
            if (status == SIGINT)
                printf("Child process (%ld): SIGINT caught and ignored.\n", (long)getpid());
            else
            if (status)
                break;
            printf("Child process (%ld): Tock.\n", (long)getpid());
            fflush(stdout);
            sleep(1);
        }

        printf("Child process (%ld): Exited due to %s.\n", (long)getpid(),
               (status == SIGINT) ? "SIGINT" :
               (status == SIGHUP) ? "SIGHUP" :
               (status == SIGTERM) ? "SIGTERM" : "Unknown signal.\n");
        fflush(stdout);

        return EXIT_SUCCESS;
    }

    /*
     * Parent process.
    */

    /* Close read end of pipe. */
    close(pipefd[0]);

    while (!done) {
        fprintf(stderr, "Parent process (%ld): Tick.\n", (long)getpid());
        fflush(stderr);
        sleep(1);
        fprintf(stderr, "Parent process (%ld): Tock.\n", (long)getpid());
        fflush(stderr);
        sleep(1);

        /* Try reaping the child process. */
        p = waitpid(child, &status, WNOHANG);
        if (p == child || (p == (pid_t)-1 && errno == ECHILD)) {
            if (p == child && WIFSIGNALED(status))
                fprintf(stderr, "Child process died from %s. Parent will now exit, too.\n",
                        (WTERMSIG(status) == SIGINT) ? "SIGINT" :
                        (WTERMSIG(status) == SIGHUP) ? "SIGHUP" :
                        (WTERMSIG(status) == SIGTERM) ? "SIGTERM" : "an unknown signal");
            else
                fprintf(stderr, "Child process has exited, so the parent will too.\n");
            fflush(stderr);
            break;
        }
    }

    if (done) {
        fprintf(stderr, "Parent process (%ld): Exited due to %s.\n", (long)getpid(),
                   (done == SIGINT) ? "SIGINT" :
                   (done == SIGHUP) ? "SIGHUP" : "Unknown signal.\n");
        fflush(stderr);
    }

    /* Never reached! */
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

使用例如编译并运行上面的内容

gcc -Wall -O2 example.c -o example
./example
Run Code Online (Sandbox Code Playgroud)

父进程将打印到标准输出,子进程将打印到标准错误。如果按Ctrl+ ,父进程将退出C;子进程将忽略该信号。子进程使用SIGHUP而不是SIGIO(尽管实时信号,例如SIGRTMIN+0,会更安全);如果由退出的父进程生成,则信号处理程序将在子进程中SIGHUP引发。SIGTERM

为了使终止原因易于查看,子级捕获SIGTERM,并退出下一次迭代(一秒钟后)。如果需要,处理程序可以使用例如raise(SIGKILL)立即终止自身。

父进程和子进程都会显示它们的进程 ID,因此您可以轻松地从另一个终端窗口发送SIGINT//SIGHUP信号SIGTERM。(子进程忽略SIGINTSIGHUP从进程外部发送。)