dup2坏文件描述符

时间:2015-11-30 23:15:57

标签: c shell pipe

我正在尝试将管道实现到我的shell。现在它可以处理正常的命令,如ls,who,date等。

通过阅读很多内容并进入其他堆栈溢出帖子来检查管道应该如何工作,我已经想出了这个代码,我将为你们演示。该函数基本上接受一个应该处理的命令。

void
runCommand(Command *cmd){

    char **pl = cmd->pgm->pgmlist;
    int status;
    Pgm *p = cmd->pgm;

    //Count the number of pipes and create the right number of filedescriptors
    int numPipes = countPipes(cmd->pgm);
    int pipefds[2*numPipes];

    //Pipe the file descriptors here and check if there was an error.
    for(int i = 0; i < (numPipes); i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("couldn't pipe");
            exit(EXIT_FAILURE);
        }
    }

    pid_t pid, wpid;


    int fdr;
    int fd;

    int j = 0;
    while(p != NULL) {

        pid = fork();

        if (pid == 0) {
            // Child process
            if (cmd->bakground == 1) // Check if it should be running in the background, if so, assign a new PID
            {
                setpgid(pid, 0);
            }

            // Check if RSTDOUT is on or not
            if(cmd->rstdout != NULL){

                fd = open(cmd->rstdout, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
                //fclose(fopen(cmd->rstdout, "w"));
                printf("in first\n");
                dup2(fd,1);
                close(fd);
            }

            // Check if RSTIN is on or not
            if(cmd->rstdin != NULL) {

                fdr = open(cmd->rstdin, O_RDONLY);
                printf("in second\n");
                dup2(fdr, 0);
                close(fdr);
            }

            //if not last command
            if(p->next){
                printf("in third\n");
                if(dup2(pipefds[j + 1], 1) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            //if not first command&& j!= 2*numPipes
            if(j != 0 ){

                printf("in fourth: %d\n", j);
                if(dup2(pipefds[j-2], 0) < 0){
                    perror(" dup2");///j-2 0 j+1 1
                    exit(EXIT_FAILURE);

                }

            }

            for(int i = 0; i < 2*numPipes; i++){
                    printf("in fifth\n");
                    close(pipefds[i]);
            }

            j += 2;
            printf("%s\n",pl[0] );
            if (execvp(pl[0], pl) == -1) {
                perror("lsh");
            }

            exit(EXIT_FAILURE);
        }

        else if (pid < 0) {
            // Error forking
            perror("lsh");
        } 
        else if(cmd->bakground != 1){ // check in the parent process if it was running in background, if so dont call for wait()
            // Parent process
            j+=2;

            pl = p->pgmlist;    
            p = p->next;
            for(int i = 0; i < 2 * numPipes; i++){
                printf("in sixth\n");
                close(pipefds[i]);
            }

            int returnStatus;    
            waitpid(pid, &returnStatus, 0);  // Parent process waits here for child to terminate.

            if (returnStatus == 0)  // Verify child process terminated without error.  
            {
               printf("The child process terminated normally.");    
            }

            if (returnStatus == 1)      
            {
               printf("The child process terminated with an error!.");    
            }

        } 
        else
            printf("Child process with ID %d is running in background\n",pid );
    }

}

正常命令仍然正常但是当我尝试这样做时,我得到以下结果。

> ls | wc
>  dup2: Bad file descriptor

当我读到它时,我认为这意味着我只是提前接近或者我正在严格处理我的文件描述符。我试图解决它,但没有成功,我转向你们可能会看到我无法看到的东西。如果您需要任何进一步的信息,请发表评论,我将尽我所能提供帮助。

******编辑1 ******

我将printf添加到我使用的每个dup2或close中,将它们命名为first,second,third等,以确切地知道我在哪个dup或close中得到了错误。这是我运行命令时的结果:

> ls | wc
in sixth
in sixth
in third
in sixth
in sixth
in fourth: 2
 dup2: Bad file descriptor

所以知道我们至少知道失败的地方。

****** EDIT2 ******

我还添加了我们正在编写或正在阅读或关闭的文件描述符:

In parent closing fd: 0
In parent closing fd: 1
in child third writing to fd: 1
In parent closing fd: 0
In parent closing fd: 1
in child fourth reading from fd: 0
 dup2: Bad file descriptor

1 个答案:

答案 0 :(得分:0)

我认为你的dup2问题在这里:

if(cmd->rstdin != NULL) {

                fdr = open(cmd->rstdin, O_RDONLY);
                dup2(fd, 0);
                close(fdr);
            }

你正在打开“fdr”但是你在dup2中使用“fd”。 “fd”未在前一个if {}中打开或已经关闭。此外,在这两个条件中,您不检查dup2的返回值。