1

我的目标是通过 FIFO 在孩子和父母之间建立 IPC。孩子应该跑

execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);

将其输出重定向到父母的输入,父母应该运行这个命令:

cut -l : -f 1

并将其输出到命令行。

现在,我已经成功链接了我的 FIFO 并将我的子进程的输出重定向到父进程的输入。我已经进行了多次测试,并且该连接工作正常。问题在于剪切的 execl,它应该看起来像这样:

execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);

但我很确定不是。

int cut(){

    //
    int myfifo;     
    char buf[MAX_BUF];


    printf("\nCut opening FIFO");
    if((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC))<0){
        perror("open FIFO at cut");
        quit(EXIT_FAILURE);}
    else{printf("\nCut has FIFO opened and is reading\n");}

    //read(myfifo, buf, MAX_BUF); outputting buf goes as supposed to

    if( dup2(myfifo, 0) < 0 ){
        perror("dup2 at cut");
        quit(EXIT_FAILURE);}

    //read(STDIN_FILENO, buf, MAX_BUF);

    close(myfifo);

    execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);

    //this is for testing buf, but i guess the program shouldn't even get here
    printf("\nCut has received: %s\nAnd is closing FIFO", buf);

    return -1;

}


int cat(){

    int myfifo;
    //OPEN FIFO
    printf("\nCat opening FIFO");
    if( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) )<0){
        perror("open FIFO at cat");
        quit(EXIT_FAILURE);
    }
    else{
        printf("\nCat has opened FIFO");
    //WRITE OUTPUT OF "cat \etc\passwd" TO FIFO
        dup2(myfifo, 1);
        execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
    }
    close(myfifo);


    return 0;
}

main 目前只创建fifo(mkfifo)、forks()并调用函数。我的问题可能与父级的标准输入(运行剪切)有关,但我不这么认为,或者我假设 execl() 直接从标准输入读取而它没有。我认为这真的是因为我没有正确地通过 execl() 编写“剪切”。

对代码的任何更正,甚至我表达某些想法的方式都可能表明我没有正确理解某些内容,我们将不胜感激。感谢您的帮助

4

1 回答 1

0

如评论中所述cut,GNU 和 BSD(和 POSIX)下的命令不支持-l选项;您需要的选项是-d分隔符。

这段代码对我有用。在我的机器上,/bin/cat是正确的,但是/usr/bin/cut是正确的(不是/bin/cut),所以我不得不编译:

$ rmk cutcat UFLAGS=-DCUT_CMD=/usr/bin/cut && ./cutcat
    gcc -O3 -g -std=c11 -Wall -Wextra -Werror -DCUT_CMD=/usr/bin/cut cutcat.c -o cutcat
$

它使用make(称为rmk) 和 custom的自定义变体makefile,但命令的位置是通过(user flags) 宏cut在命令行上指定的。UFLAGSC 代码中的STRINGandEXPAND宏很麻烦,但不如尝试在调用make(or rmk) 的 shell 和make调用以运行编译器的 shell 之后获取双引号那么麻烦。

编码:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>

#ifndef CUT_CMD
#define CUT_CMD /bin/cut
#endif
#ifndef CAT_CMD
#define CAT_CMD /bin/cat
#endif
#define EXPAND(x)   #x
#define STRING(x)   EXPAND(x)

static const char cut_cmd[] = STRING(CUT_CMD);
static const char cat_cmd[] = STRING(CAT_CMD);

static inline void quit(int status) { exit(status); }

enum { MAX_BUF = 4096 };

static void cut(void)
{
    int myfifo;

    printf("\nCut opening FIFO");
    if ((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC)) < 0)
    {
        perror("open FIFO at cut");
        quit(EXIT_FAILURE);
    }
    printf("\nCut has FIFO opened and is reading\n");

    if (dup2(myfifo, 0) < 0)
    {
        perror("dup2 at cut");
        quit(EXIT_FAILURE);
    }

    close(myfifo);

    execlp(cut_cmd, "cut", "-d:", "-f", "1", NULL);
    fprintf(stderr, "Failed to execute %s\n", cut_cmd);
    exit(1);
}

static
void cat(void)
{
    int myfifo;

    printf("\nCat opening FIFO");
    if ( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) ) < 0)
    {
        perror("open FIFO at cat");
        quit(EXIT_FAILURE);
    }
    printf("\nCat has opened FIFO");
    dup2(myfifo, 1);
    close(myfifo);
    execl(cat_cmd, "cat", "/etc/passwd", (char *)0);
    fprintf(stderr, "Failed to execute %s\n", cat_cmd);
    exit(1);
}

int main(void)
{
    mkfifo("/tmp/myfifo", 0600);
    if (fork() == 0)
        cat();
    else
        cut();
    /*NOTREACHED*/
    fprintf(stderr, "You should not see this message\n");
    return 0;
}

样本输出:

截断很多

…
nobody
root
daemon
_uucp
_taskgated
_networkd
…

该代码实际上与您所拥有的代码并没有太大不同。我删除了一些杂物。我确实确保这是由;close(myfifo)执行的。cat()它不在原版中。这可能很重要。

于 2015-05-24T03:32:48.270 回答