3

I'm writing a shell with job control. The main process should ignore stop-signals and handle SIGCHLD. Child process after fork() should set signals to SIG_DFL. The problem is that my child process ignores signals too.

At the start of my program I set shell to foreground and initialize signals

...
tcsetpgrp(shell_terminal, shell_pgid);
set_signals();

void    chld_handler(int signum)
{
    if (signum == SIGCHLD)
        check_and_wait();
    return ;
}

void    set_signals() {
    sigset_t set;
    struct sigaction act;

    sigfillset(&set);
    sigprocmask(SIG_SETMASK, &set, NULL);

    ft_memset(&act, 0, sizeof(act));

    sigfillset(&act.sa_mask);
    act.sa_handler = SIG_IGN;

    sigaction(SIGINT, &act, NULL);
    sigaction(SIGQUIT, &act, NULL);
    sigaction(SIGTSTP, &act, NULL);
    sigaction(SIGTERM, &act, NULL);
    sigaction(SIGTTIN, &act, NULL);
    sigaction(SIGTTOU, &act, NULL);

    act.sa_handler = chld_handler;

    sigaction(SIGCHLD, &act, NULL);

    sigemptyset(&set);
    sigprocmask(SIG_SETMASK, &set, NULL);
    return;
}

after fork() in child process:

/* set to foreground */
pid = getpid();
if (!job->pgid)
    job->pgid = pid;
setpgid(pid, job->pgid);
tcsetpgrp(shell_terminal, job->pgid);

/* set signals */
sigset_t set;
struct sigaction act;

sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);

memset(&act, 0, sizeof(act));
sigfillset(&act.sa_mask);

act.sa_handler = SIG_DFL;

sigemptyset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);

execve(...);

But child process ignores signals

4

1 回答 1

1

sigaction()不通过引用存储 sigaction 对象。更改act.sa_handler为后act.sa_handler = SIG_DFL,您需要重复这些sigaction()呼叫。

sigaction(SIGINT, &act, NULL);
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGTSTP, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGTTIN, &act, NULL);
sigaction(SIGTTOU, &act, NULL);
于 2019-03-30T10:37:18.390 回答