从SIGCHLD返回的信号似乎是错误的

时间:2017-02-26 15:24:17

标签: c unix signals child-process

我在C中编写了一个基本shell,我试图从子进程中捕获SIGTSTP信号。为此,我为SIGCHLD设置了一个处理程序,但返回的信号是20,应该是24

我有SIGCHLD处理程序:

signal(SIGCHLD, trapChld);

void trapChld(int signo) {
    printf("%d", signo);
}

运行20时会打印信号kill -SIGTSTP child_pid。为什么会发生这种情况?

这是我的完整代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>

int statusCode;
int foregroundMode = 0;
int bg = 0;
int bgPsArray[20];
int bgPsCount = 0;
int i;
char line[256];

pid_t popBgProcess() {
    int size = sizeof(bgPsArray)/sizeof(bgPsArray[0]);
    if (size > 0) {
      return bgPsArray[size+1];
    } else {
        return 0;
    }
}

void trapInterrupt(int _) {
    int childStatus;
    pid_t child;
    while ((child = popBgProcess())) {
        if(child != getpid()) {
            kill(child, SIGKILL);
            waitpid(child, &childStatus, 0);
        }
    }
}

void trapChld(int signo) {
    printf("%d", signo);
    if(signo == 24) {
        if(foregroundMode == 0) {
            write(1, "Entering foreground-only mode (& is now ignored)\n", 49);
            write(1, ": ", 2);
            fflush(stdout);
            foregroundMode = 1;
        } else {
            write(1, "Exiting foreground-only mode\n", 29);
            write(1, ": ", 2);
            fflush(stdout);
            foregroundMode = 0;
        }
    }
}

int getCommand() {
    printf(": ");
    fflush(stdout);
    if(fgets(line, sizeof(line), stdin) != NULL) {
        char *position = strchr(line, '\n');
    *position = '\0'; // Replace '\n' with '\0'
        if(foregroundMode == 1) { // Foreground mode on
            if((position = strchr(line, '&')) != NULL) {
                *position = '\0'; // Replace '&' with '\0'
            }
            bg = 0; // Ignore '&' so do not create background process
        } else { // Foreground mode off
            if((position = strchr(line, '&')) != NULL) {
                *position = '\0'; // Replace '&' with '\0'
                bg = 1; // Is a background process
            } else {
                bg = 0;
            }
        }
  } else { // If input is null
        return 0;
  }
    return 1;
}

void checkProcessCompletion() {
    int status;
    for(i=0; i<bgPsCount; i++) {
        if(waitpid(bgPsArray[i], &status, WNOHANG) > 0) {
            if(WIFEXITED(status)) { // If exit
                printf("Background PID %d is done: exit value %d\n", bgPsArray[i], WEXITSTATUS(status));
                fflush(stdout);
            } else if(WIFSIGNALED(status)) { // If signal
                printf("Background PID %d is done: terminated by signal %d\n", bgPsArray[i], WTERMSIG(status));
                fflush(stdout);
            }
        }
    }
}

int runCommand(int cmd) {
    if(cmd == 0) { // Return if there was no command
        return 0;
    } else if(strcmp(line, "exit") == 0) {
        exit(0);
    } else if(strstr(line, "#")) { // Comment input (do nothing)
    } else if(strcmp(line, "status") == 0) {
        printf("exit value %d\n", statusCode);
        fflush(stdout);
    }
    else if(strncmp("cd", line, strlen("cd")) == 0) {
        if(line[2] == ' ') { // If space after 'cd' expect directory
            char cwd[1024];
            getcwd(cwd, sizeof(cwd));
            char *path = strstr(line, " ");
            if(path) {
                path += 1;
                char *value;
                value = malloc(strlen(path));
                memcpy(value, path, strlen(path));
                *(value + strlen(path)) = 0;
                sprintf(cwd, "%s/%s", cwd, value); // Directory to change to
                free(value);
            }
            chdir(cwd); // cd to new directory
        } else { // cd with no argument
            char *home = getenv("HOME");
            chdir(home); // cd to HOME directory
        }
    }
    else { // System commands
        pid_t pid, ppid;
        int status;
        char *command;
        char *args[256];
        int argCount;
        command = strtok(line, " ");

        // Create args array for execvp
        args[0] = command;
        argCount = 1;
        args[argCount] = strtok(NULL, " ");
        while(args[argCount] != NULL) { // Add arguments to array
            argCount++;
            args[argCount] = strtok(NULL, " ");
        }
        if((pid = fork()) < 0) { // Fork fails
            perror("fork");
            fflush(stdout);
            exit(1);
        }
        if(pid == 0) { // Child process
            for(i=0; i<argCount; i++) {
                if(strcmp(args[i], "<") == 0) { // Redirecting input
                    if(access(args[i+1], R_OK) == -1) { // File is unreadable
                        perror("access");
                        fflush(stdout);
                    } else { // File is readable
                        int file = open(args[i+1], O_RDONLY, 0);
                        dup2(file, STDIN_FILENO);
                        close(file);
                        execvp(command, &command);
                    }
                }
                else if(strcmp(args[i], ">") == 0) { // Redirecting output
                    int file = creat(args[i+1], 7777);
                    dup2(file, STDOUT_FILENO);
                    close(file);
                    execvp(command, args);
                } else { // No redirection
                    execvp(command, args);
                }
            }
            perror("execvp"); // Error for execvp
            exit(1);
        } else { // Parent process
            if (bg == 1) { // Background process
                int status;
                int process;
                printf("Background PID: %d\n", pid);
                fflush(stdout);
                bgPsArray[bgPsCount] = pid; // Add process to background process array
                bgPsCount++;
                process = waitpid(pid, &status, WNOHANG);
            } else { // Foreground process
                int status;
                waitpid(pid, &status, 0); // Wait on the process
                if(WIFEXITED(status)) {
                    statusCode = WEXITSTATUS(status);
                }
            }
        }
    }
    return 1;
}

int main(int argc, char *argv[], char *envp[]) {
    // Creating 'junk' manually is necessary because output redirection is broken,
    // and a large portion of the grading script is depedent upon it's existence.
    FILE *fp = fopen("junk", "ab+");
    const char *text;
    fprintf(fp, "Junk in junkfile\n");
    fclose(fp);
    signal(SIGINT, trapInterrupt);
    signal(SIGCHLD, trapChld);
    while(1) {
        checkProcessCompletion(); //Check the processes
        int cmd = getCommand(); // Get command from user
        int result = runCommand(cmd);
        if (result == 0) {
            break;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你还没有告诉我们你正在运行什么平台,所以这只是猜测,但也许是因为该平台将SIGTSTP定义为20?

例如,

Linux;

$ grep SIGTSTP /usr/include/asm/signal.h
#define SIGTSTP         20

更好的问题是为什么你认为它应该是24?在AIX上它是18.在HP-UX上它是25.各种Cygwin标头将它定义为8,18或24(因为Cygwin标头来自glib并且充满了特定于平台的条件编译恶作剧); 18是运行时使用的实际值。

在Solaris上,现在恰好是24.我相信Solaris 2从SVR4继承了它,后续的Solaris版本保留了它。但信号编号并未按任何适用规范(SUS及其祖先,如POSIX和XPG3)标准化。

不要假设信号值是固定的。这就是为什么你有signal.h。

哦,sigaction(2)比支持它的平台上的信号(2)更可取,大多数都支持它。