简体   繁体   中英

tee always returns EINVAL

I am trying to figure out how to correctly use tee . In my application, tee always returns EINVAL for some reason. I was getting desperate, and tried to run the example application listed in the man page of tee (for example: https://linux.die.net/man/2/tee ), only to find out that even that example code always fails with: tee: Invalid argument , for example when using it as follows: cat tee.c | ./tee tee.log cat tee.c | ./tee tee.log . Any idea why this might happen?

The example code from die.net:

#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

int
main(int argc, char *argv[])
{
    int fd;
    int len, slen;
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }
    do {
        /*
         * tee stdin to stdout.
         */
        len = tee(STDIN_FILENO, STDOUT_FILENO,
                  INT_MAX, SPLICE_F_NONBLOCK);
        if (len < 0) {
            if (errno == EAGAIN)
                continue;
            perror("tee");
            exit(EXIT_FAILURE);
        } else
            if (len == 0)
                break;
        /*
         * Consume stdin by splicing it to a file.
         */
        while (len > 0) {
            slen = splice(STDIN_FILENO, NULL, fd, NULL,
                          len, SPLICE_F_MOVE);
            if (slen < 0) {
                perror("splice");
                break;
            }
            len -= slen;
        }
    } while (1);
    close(fd);
    exit(EXIT_SUCCESS);
}

tee requires a pipe for both file descriptors, fd_in and fd_out .

Your invocation does not supply a pipe for the second file descriptor, but a file descriptor referring to a TTY. Note also that the example in the manpage specifically uses a trailing | cat | cat :

The example below implements a basic tee(1) program using the tee() system call.
Here is an example of its use:

    $ date |./a.out out.log | cat
    Tue Oct 28 10:06:00 CET 2014
    $ cat out.log
    Tue Oct 28 10:06:00 CET 2014

Not using a pipe file descriptor for the second (or first, for that matter) argument would qualify for EINVAL:

EINVAL fd_in  or  fd_out  does not refer to a pipe; or fd_in and fd_out refer to
       the same pipe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM