c - 为什么我的程序在使用 sigaction 时会出现异常行为?

标签 c linux signals fork posix

我正在编写自己的简单 shell 作为练习。我需要注册到 SIGCHLD 信号以处理僵尸进程。出于某种原因,当我使用 sigaction 添加处理程序时,程序退出,我不明白为什么。

您可以在 main() 中看到,如果 process_arglist() 返回 0 但我返回 1,我们将退出> 我看不出信号处理会如何影响它。

这是我的代码。它应该处理以 & 结尾的命令(我们 fork() 并在子代码中使用 execvp)。 例如:ping 127.0.0.1 -c 5 &

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

void sigchld_handler(int signal) {
  int my_errno = errno;
  while (waitpid(-1, 0, WNOHANG) > 0); // WNOHANG so handler will be non-blocking.
  errno = my_errno;
}

int handle_background_command(int count, char** arglist) {

  pid_t pid;
  arglist[count - 1] = NULL; // remove "&" from arglist

  //Handle SIGCHLD signal
  struct sigaction sa, sa_dft;
  sa.sa_handler = sigchld_handler;
  sa.sa_flags = SA_NOCLDSTOP;

  if (sigaction(SIGCHLD, &sa, &sa_dft) == -1) {
    perror("error when trying to set signal action");
    exit(-1);
  }

  if((pid = fork()) == -1) {
    perror("error when trying to fork() from handle_background_command()");
    exit(1);
  }

  if(pid == 0) {

    // Child code

    sigaction(SIGCHLD, &sa_dft, NULL);

    if(execvp(arglist[0], arglist) == -1) {
      perror("error when trying to execvp() from handle_background_command()");
      exit(1);
    }

  }

  // Parent code
  return 1;
}


int process_arglist(int count, char** arglist)
{
  return handle_background_command(count, arglist);
}

int main(void)
{
    while (1)
    {
        char** arglist = NULL;
        char* line = NULL;
        size_t size;
        int count = 0;

        if (getline(&line, &size, stdin) == -1) {
            printf("out!");
            break;
        }


        arglist = (char**) malloc(sizeof(char*));
        if (arglist == NULL) {
            printf("malloc failed: %s\n", strerror(errno));
            exit(-1);
        }
        arglist[0] = strtok(line, " \t\n");

        while (arglist[count] != NULL) {
            ++count;
            arglist = (char**) realloc(arglist, sizeof(char*) * (count + 1));
            if (arglist == NULL) {
                printf("realloc failed: %s\n", strerror(errno));
                exit(-1);
            }

            arglist[count] = strtok(NULL, " \t\n");
        }

        if (count != 0) {
            int result = process_arglist(count, arglist);
            printf("result = %d\n", result);
            if (!result) {
                free(line);
                free(arglist);
                printf("out\n");
                break;
            }
        }

        free(line);
        free(arglist);
    }
    pthread_exit(NULL);
    return 0;
}

同样,如果我去掉信号处理代码,它就可以工作了。
什么原因?

编辑 这是 strace 实用程序的输出(最后一行):

--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2818, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, NULL, WNOHANG, NULL)          = 2818
wait4(-1, NULL, WNOHANG, NULL)          = -1 ECHILD (No child processes)
rt_sigreturn()                          = -1 EINTR (Interrupted system call)
write(1, "out!", 4out!)                     = 4
exit_group(0)                           = ?
+++ exited with 0 +++

最佳答案

您的程序从 getline 函数(getline 被信号处理程序中断)以 EINTR(中断的系统调用)退出。

检查这个:How to handle EINTR (interrupted System Call)

关于c - 为什么我的程序在使用 sigaction 时会出现异常行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37093653/

相关文章:

c++ - Axis2 wsdl2c 未生成骨架中的所有调用

c - 我正在尝试使用fork编写C程序

c - 如何使用用 C 编写的 GTK 文本编辑器的运行实例从 CLI 打开文本文件?

linux - 通过 inode 获取文件详细信息

python - 如何在 Python 中处理多个相同的未阻塞信号?

C 语言 - 无法将 SIGINT 重置为默认值

c - 如何创建不重复的随机数?

php 的 set_time_limit(0) 挂起并抛出 HTTP 500

php - Linux 服务器上 symfony 中的无效请求错误

c - mmap 可执行文件可以多次使用吗?