c - 产生了一个 bash shell。 shell 死了但 pipe 没坏?

标签 c linux bash shell pipe

问题

我正在尝试将内容从主例程传输到 execvp 的 bash shell。我遇到了一个问题,当我在子 shell 中写入“exit”时,它并没有告诉我管道真的坏了。应该是——对吧?进程终止,因此管道 fd 也应该返回 EOF 或 SIGPIPE。但是,它不会像往常一样继续读/写。

代码

代码附在这里:

/************************************************************
 * Includes:
 * ioctl - useless(?)
 * termios, tcsetattr, tcgetattr - are for setting the
 *      noncanonical, character-at-a-time terminal.
 * fork, exec - creating the child process for part 2.
 * pthread, pipe - creating the pipe process to communicate
 *      with the child shell.
 * kill - to exit the process
 * atexit - does some cleanups. Used in termios, tcsetattr,
 *      tcgetattr.
 ************************************************************/
#include <sys/ioctl.h> // ioctl
#include <termios.h> // termios, tcsetattr, tcgetattr
#include <unistd.h> // fork, exec, pipe
#include <sys/wait.h> // waitpid
#include <pthread.h> // pthread
#include <signal.h> // kill
#include <stdlib.h> // atexit
#include <stdio.h> // fprintf and other utility functions
#include <getopt.h> // getopt
/**********************
 * GLOBALS
 **********************/
pid_t pid;

/**********************
 * CONSTANTS
 **********************/
static const int BUFFER_SIZE = 16;
static const int STDIN_FD = 0;
static const int STDOUT_FD = 1;
static const int STDERR_FD = 2;

// these attributes are reverted to later
struct termios saved_attributes;
// to revert the saved attributes
void
reset_input_mode (void) {
    tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}

// to set the input mode to correct non-canonical mode.
void
set_input_mode (void) {
    struct termios tattr;

    /* Make sure stdin is a terminal. */
    if (!isatty (STDIN_FILENO))
    {
        fprintf (stderr, "Not a terminal.\n");
        exit (EXIT_FAILURE);
    }

    /* Save the terminal attributes so we can restore them later. */
    tcgetattr (STDIN_FILENO, &saved_attributes);
    atexit (reset_input_mode);

    /* Set the funny terminal modes. */
    tcgetattr (STDIN_FILENO, &tattr);
    tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
    tattr.c_cc[VMIN] = 1;
    tattr.c_cc[VTIME] = 0;
    tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}

// pthread 1 will read from pipe_fd[0], which
// is really the child's pipe_fd[1](stdout). 
// It then prints out the contents.
void* thread_read(void* arg){
    int* pipe_fd = ((int *) arg);
    int read_fd = pipe_fd[0];
    int write_fd = pipe_fd[1];
    char c;
    while(1){
       int bytes_read = read(read_fd, &c, 1);
       if(bytes_read > 0){
            putchar(c);
       }
       else{
            close(read_fd);
            close(write_fd);
            fprintf(stdout, "The read broke.");
            fflush(stdout);
            break;
       }
    }
}

// pthread 2 will write to child_pipe_fd[1], which
// is really the child's stdin.
// but in addition to writing to child_pipe_fd[1],
// we must also print to stdout what our 
// argument was into the terminal. (so pthread 2
// does extra).
void* thread_write(void* arg){
    set_input_mode();
    int* pipe_args = ((int *) arg);
    int child_read_fd = pipe_args[0];
    int child_write_fd = pipe_args[1];
    int parent_read_fd = pipe_args[2];
    int parent_write_fd = pipe_args[3];
    char c;
    while(1) {
        int bytes_read = read(STDIN_FD, &c, 1);
        write(child_write_fd, &c, bytes_read);
        putchar(c);
        if(c == 0x04){
            // If an EOF has been detected, then 
            // we need to close the pipes.
            close(child_write_fd);
            close(child_read_fd);
            close(parent_write_fd);
            close(parent_read_fd);
            kill(pid, SIGHUP);
            break;
        }
    }
}

int main(int argc, char* argv[]) {
    /***************************
     * Getopt process here for --shell
     **************************/
    int child_pipe_fd[2];
    int parent_pipe_fd[2];
    pipe(child_pipe_fd);
    pipe(parent_pipe_fd);

    // We need to spawn a subshell.
    pid = fork();
    if(pid < 0){
        perror("Forking was unsuccessful. Exiting");
        exit(EXIT_FAILURE);
    }
    else if(pid == 0){ // is the child. 
        // We dup the fd and close the pipe.

        close(0); // close stdin. child's pipe should read.
        dup(child_pipe_fd[0]); // pipe_fd[0] is the read. Make read the stdin.
        close(child_pipe_fd[0]);

        close(1); // close stdout
        dup(parent_pipe_fd[1]); // pipe_fd[1] is the write. Make write the stdout.
        close(parent_pipe_fd[1]);

        char* BASH[] = {"/bin/bash", NULL};
        execvp(BASH[0], BASH);
    }
    else{ // is the parent
        // We dup the fd and close the pipe.
        //
        // create 2 pthreads.
        // pthread 1 will read from pipe_fd[0], which
        // is really the child's pipe_fd[1](stdout). 
        // It then prints out the contents.
        //
        // pthread 2 will write to pipe_fd[1], which
        // is really the child's pipe_fd[0](stdin)
        // but in addition to writing to pipe_fd[1],
        // we must also print to stdout what our 
        // argument was into the terminal. (so pthread 2
        // does extra).
        //
        // We also need to take care of signal handling:
        signal(SIGINT, sigint_handler);
        /*signal(SIGPIPE, sigpipe_handler);*/
        int write_args[] = {child_pipe_fd[0], child_pipe_fd[1],
                             parent_pipe_fd[0], parent_pipe_fd[1]};

        pthread_t t[2];
        pthread_create(t, NULL, thread_read, parent_pipe_fd);
        pthread_create(t+1, NULL, thread_write, write_args);

        pthread_join(t[0], NULL);
        pthread_join(t[1], NULL);

        int status;
        if (waitpid(pid, &status, 0) == -1) {
            perror("Waiting for child failed.");
            exit(EXIT_FAILURE);
        }

        printf("Subshell exited with the error code %d", status);
        exit(0);
    }    

    return 0;
}

该程序基本上将来自终端的输入通过管道传输到子 shell 中,并尝试执行它们并返回输出。为了写入管道,我有一个 pthread 将 stdin 输入写入子 shell。要读取到管​​道,我有一个 pthread 将管道读取到父级。为了通过子 shell 死亡(调用退出)检测破损的管道,我从读取线程中检测到 EOF 字符。

我的尝试

我添加了对 0x04 字符 (EOF) 的检查,我检查了 read_bytes == 0read_bytes < 0 .除非我在写入端明确关闭管道,否则它似乎永远不会收到备忘录。如果我发送字符 ^D(在我的代码中,它通过关闭子进程和父进程的所有管道来处理),它只会满足 EOF 字符。

如有任何意见,我们将不胜感激!谢谢。

最佳答案

您的父进程持有子进程的文件描述符的副本。因此,即使在子进程退出后,这些 FD 仍处于打开状态——因此这些管道的另一端也保持打开状态,从而阻止任何 SIGPIPE。

修改你的代码如下:

else {
  // pid >0; this is the parent
  close(child_pipe_fd[0]);  // ADD THIS LINE
  close(parent_pipe_fd[1]); // ADD THIS LINE

关于c - 产生了一个 bash shell。 shell 死了但 pipe 没坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39857463/

相关文章:

为宏 __FUNCTION__ 和 __func__ 定义#define 的跨平台

c - 为什么我的字数不正确?

c - 使用 regex.h 进行字符之间的匹配

代码返回地址

linux - Bash While 循环新参数

linux - svn 导出更改并保存在本地

bash - HP-UX - 如何在不解压缩的情况下从 tar 存档中读取文本文件?

linux - 如何为匿名上传服务 file.io 创建 bashrc 脚本?

bash - 使用 for 循环在 bash 中启动多个进程

linux - 目录检查/proc/<thread id> 返回 true,虽然没有这样的目录