]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/tee.2
mmap.2: Don't mark MAP_ANON as deprecated
[thirdparty/man-pages.git] / man2 / tee.2
CommitLineData
2bc4bb77 1.\" This manpage is Copyright (C) 2006 Jens Axboe
c11b1abf 2.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2bc4bb77 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
2bc4bb77
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
2bc4bb77
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
2bc4bb77
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
2bc4bb77 25.\"
9ba01802 26.TH TEE 2 2019-03-06 "Linux" "Linux Programmer's Manual"
2bc4bb77
MK
27.SH NAME
28tee \- duplicating pipe content
29.SH SYNOPSIS
30.nf
b80f966b 31.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
2bc4bb77 32.B #include <fcntl.h>
dbfe9c70 33.PP
52520fb5 34.BI "ssize_t tee(int " fd_in ", int " fd_out ", size_t " len \
2bc4bb77
MK
35", unsigned int " flags );
36.fi
52520fb5 37.\" Return type was long before glibc 2.7
2bc4bb77
MK
38.SH DESCRIPTION
39.\" Example programs http://brick.kernel.dk/snaps
40.\"
41.\"
c13182ef
MK
42.\" add a "tee(in, out1, out2)" system call that duplicates the pages
43.\" (again, incrementing their reference count, not copying the data) from
2bc4bb77
MK
44.\" one pipe to two other pipes.
45.BR tee ()
46duplicates up to
47.I len
48bytes of data from the pipe referred to by the file descriptor
49.I fd_in
50to the pipe referred to by the file descriptor
51.IR fd_out .
52It does not consume the data that is duplicated from
c13182ef
MK
53.IR fd_in ;
54therefore, that data can be copied by a subsequent
2bc4bb77 55.BR splice (2).
efeece04 56.PP
2bc4bb77 57.I flags
3111b6a6 58is a bit mask that is composed by ORing together
0bd79129 59zero or more of the following values:
2bc4bb77
MK
60.TP 1.9i
61.B SPLICE_F_MOVE
c13182ef 62Currently has no effect for
2bc4bb77
MK
63.BR tee ();
64see
65.BR splice (2).
66.TP
67.B SPLICE_F_NONBLOCK
c13182ef
MK
68Do not block on I/O; see
69.BR splice (2)
2bc4bb77
MK
70for further details.
71.TP
72.B SPLICE_F_MORE
c13182ef 73Currently has no effect for
2bc4bb77
MK
74.BR tee (),
75but may be implemented in the future; see
76.BR splice (2).
77.TP
78.B SPLICE_F_GIFT
79Unused for
80.BR tee ();
81see
82.BR vmsplice (2).
83.SH RETURN VALUE
84Upon successful completion,
85.BR tee ()
86returns the number of bytes that were duplicated between the input
c13182ef
MK
87and output.
88A return value of 0 means that there was no data to transfer,
89and it would not make sense to block, because there are no
90writers connected to the write end of the pipe referred to by
2bc4bb77 91.IR fd_in .
efeece04 92.PP
c13182ef 93On error,
2bc4bb77
MK
94.BR tee ()
95returns \-1 and
96.I errno
97is set to indicate the error.
98.SH ERRORS
c13182ef 99.TP
b7224af9
MK
100.B EAGAIN
101.B SPLICE_F_NONBLOCK
102was specified in
5ca397fa
SK
103.IR flags
104or one of the file descriptors had been marked as nonblocking
105.RB ( O_NONBLOCK ) ,
b7224af9
MK
106and the operation would block.
107.TP
2bc4bb77
MK
108.B EINVAL
109.I fd_in
110or
111.I fd_out
112does not refer to a pipe; or
113.I fd_in
114and
115.I fd_out
116refer to the same pipe.
117.TP
118.B ENOMEM
119Out of memory.
2dd578fd
MK
120.SH VERSIONS
121The
2777b1ca 122.BR tee ()
c95b6ae1
MK
123system call first appeared in Linux 2.6.17;
124library support was added to glibc in version 2.5.
47297adb 125.SH CONFORMING TO
8382f16d 126This system call is Linux-specific.
2bc4bb77
MK
127.SH NOTES
128Conceptually,
129.BR tee ()
130copies the data between the two pipes.
131In reality no real data copying takes place though:
132under the covers,
133.BR tee ()
5dec7b73 134assigns data to the output by merely grabbing
2bc4bb77
MK
135a reference to the input.
136.SH EXAMPLE
f7d11fda 137The example below implements a basic
2bc4bb77
MK
138.BR tee (1)
139program using the
2777b1ca 140.BR tee ()
c13182ef 141system call.
f7d11fda 142Here is an example of its use:
efeece04 143.PP
f7d11fda 144.in +4n
b8302363 145.EX
f7d11fda
MK
146$ \fBdate |./a.out out.log | cat\fP
147Tue Oct 28 10:06:00 CET 2014
148$ \fBcat out.log\fP
149Tue Oct 28 10:06:00 CET 2014
b8302363 150.EE
f7d11fda
MK
151.in
152.SS Program source
c7885256 153\&
e7d0bb47 154.EX
2bc4bb77
MK
155#define _GNU_SOURCE
156#include <fcntl.h>
157#include <stdio.h>
158#include <stdlib.h>
159#include <unistd.h>
2bc4bb77
MK
160#include <errno.h>
161#include <limits.h>
162
163int
164main(int argc, char *argv[])
165{
166 int fd;
167 int len, slen;
168
6b34fb3f 169 if (argc != 2) {
d1a71985 170 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
99c1a999 171 exit(EXIT_FAILURE);
6b34fb3f 172 }
2bc4bb77
MK
173
174 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
29059a65 175 if (fd == \-1) {
2bc4bb77
MK
176 perror("open");
177 exit(EXIT_FAILURE);
178 }
179
180 do {
181 /*
182 * tee stdin to stdout.
183 */
184 len = tee(STDIN_FILENO, STDOUT_FILENO,
185 INT_MAX, SPLICE_F_NONBLOCK);
186
187 if (len < 0) {
188 if (errno == EAGAIN)
189 continue;
190 perror("tee");
191 exit(EXIT_FAILURE);
192 } else
193 if (len == 0)
194 break;
195
196 /*
197 * Consume stdin by splicing it to a file.
198 */
199 while (len > 0) {
200 slen = splice(STDIN_FILENO, NULL, fd, NULL,
201 len, SPLICE_F_MOVE);
202 if (slen < 0) {
203 perror("splice");
204 break;
205 }
29059a65 206 len \-= slen;
2bc4bb77
MK
207 }
208 } while (1);
209
210 close(fd);
211 exit(EXIT_SUCCESS);
212}
e7d0bb47 213.EE
2bc4bb77
MK
214.SH SEE ALSO
215.BR splice (2),
6c97eb40
MK
216.BR vmsplice (2),
217.BR pipe (7)