xref: /freebsd/lib/libsys/pipe.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd December 1, 2017
29*8269e767SBrooks Davis.Dt PIPE 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm pipe ,
33*8269e767SBrooks Davis.Nm pipe2
34*8269e767SBrooks Davis.Nd create descriptor pair for interprocess communication
35*8269e767SBrooks Davis.Sh LIBRARY
36*8269e767SBrooks Davis.Lb libc
37*8269e767SBrooks Davis.Sh SYNOPSIS
38*8269e767SBrooks Davis.In unistd.h
39*8269e767SBrooks Davis.Ft int
40*8269e767SBrooks Davis.Fn pipe "int fildes[2]"
41*8269e767SBrooks Davis.Ft int
42*8269e767SBrooks Davis.Fn pipe2 "int fildes[2]" "int flags"
43*8269e767SBrooks Davis.Sh DESCRIPTION
44*8269e767SBrooks DavisThe
45*8269e767SBrooks Davis.Fn pipe
46*8269e767SBrooks Davisfunction
47*8269e767SBrooks Daviscreates a
48*8269e767SBrooks Davis.Em pipe ,
49*8269e767SBrooks Daviswhich is an object allowing
50*8269e767SBrooks Davisbidirectional data flow,
51*8269e767SBrooks Davisand allocates a pair of file descriptors.
52*8269e767SBrooks Davis.Pp
53*8269e767SBrooks DavisThe
54*8269e767SBrooks Davis.Fn pipe2
55*8269e767SBrooks Davissystem call allows control over the attributes of the file descriptors
56*8269e767SBrooks Davisvia the
57*8269e767SBrooks Davis.Fa flags
58*8269e767SBrooks Davisargument.
59*8269e767SBrooks DavisValues for
60*8269e767SBrooks Davis.Fa flags
61*8269e767SBrooks Davisare constructed by a bitwise-inclusive OR of flags from the following
62*8269e767SBrooks Davislist, defined in
63*8269e767SBrooks Davis.In fcntl.h :
64*8269e767SBrooks Davis.Bl -tag -width ".Dv O_NONBLOCK"
65*8269e767SBrooks Davis.It Dv O_CLOEXEC
66*8269e767SBrooks DavisSet the close-on-exec flag for the new file descriptors.
67*8269e767SBrooks Davis.It Dv O_NONBLOCK
68*8269e767SBrooks DavisSet the non-blocking flag for the ends of the pipe.
69*8269e767SBrooks Davis.El
70*8269e767SBrooks Davis.Pp
71*8269e767SBrooks DavisIf the
72*8269e767SBrooks Davis.Fa flags
73*8269e767SBrooks Davisargument is 0, the behavior is identical to a call to
74*8269e767SBrooks Davis.Fn pipe .
75*8269e767SBrooks Davis.Pp
76*8269e767SBrooks DavisBy convention, the first descriptor is normally used as the
77*8269e767SBrooks Davis.Em read end
78*8269e767SBrooks Davisof the pipe,
79*8269e767SBrooks Davisand the second is normally the
80*8269e767SBrooks Davis.Em write end ,
81*8269e767SBrooks Davisso that data written to
82*8269e767SBrooks Davis.Fa fildes[1]
83*8269e767SBrooks Davisappears on (i.e., can be read from)
84*8269e767SBrooks Davis.Fa fildes[0] .
85*8269e767SBrooks DavisThis allows the output of one program to be
86*8269e767SBrooks Davissent
87*8269e767SBrooks Davisto another program:
88*8269e767SBrooks Davisthe source's standard output is set up to be
89*8269e767SBrooks Davisthe write end of the pipe,
90*8269e767SBrooks Davisand the sink's standard input is set up to be
91*8269e767SBrooks Davisthe read end of the pipe.
92*8269e767SBrooks DavisThe pipe itself persists until all its associated descriptors are
93*8269e767SBrooks Davisclosed.
94*8269e767SBrooks Davis.Pp
95*8269e767SBrooks DavisA pipe that has had an end closed is considered
96*8269e767SBrooks Davis.Em widowed .
97*8269e767SBrooks DavisWriting on such a pipe causes the writing process to receive
98*8269e767SBrooks Davisa
99*8269e767SBrooks Davis.Dv SIGPIPE
100*8269e767SBrooks Davissignal.
101*8269e767SBrooks DavisWidowing a pipe is the only way to deliver end-of-file to a reader:
102*8269e767SBrooks Davisafter the reader consumes any buffered data, reading a widowed pipe
103*8269e767SBrooks Davisreturns a zero count.
104*8269e767SBrooks Davis.Pp
105*8269e767SBrooks DavisThe bidirectional nature of this implementation of pipes is not
106*8269e767SBrooks Davisportable to older systems, so it is recommended to use the convention
107*8269e767SBrooks Davisfor using the endpoints in the traditional manner when using a
108*8269e767SBrooks Davispipe in one direction.
109*8269e767SBrooks Davis.Sh IMPLEMENTATION NOTES
110*8269e767SBrooks DavisThe
111*8269e767SBrooks Davis.Fn pipe
112*8269e767SBrooks Davisfunction calls the
113*8269e767SBrooks Davis.Fn pipe2
114*8269e767SBrooks Davissystem call.
115*8269e767SBrooks DavisAs a result, system call traces such as those captured by
116*8269e767SBrooks Davis.Xr dtrace 1
117*8269e767SBrooks Davisor
118*8269e767SBrooks Davis.Xr ktrace 1
119*8269e767SBrooks Daviswill show calls to
120*8269e767SBrooks Davis.Fn pipe2 .
121*8269e767SBrooks Davis.Sh RETURN VALUES
122*8269e767SBrooks Davis.Rv -std pipe
123*8269e767SBrooks Davis.Sh ERRORS
124*8269e767SBrooks DavisThe
125*8269e767SBrooks Davis.Fn pipe
126*8269e767SBrooks Davisand
127*8269e767SBrooks Davis.Fn pipe2
128*8269e767SBrooks Davissystem calls will fail if:
129*8269e767SBrooks Davis.Bl -tag -width Er
130*8269e767SBrooks Davis.It Bq Er EFAULT
131*8269e767SBrooks Davis.Ar fildes
132*8269e767SBrooks Davisargument points to an invalid memory location.
133*8269e767SBrooks Davis.It Bq Er EMFILE
134*8269e767SBrooks DavisToo many descriptors are active.
135*8269e767SBrooks Davis.It Bq Er ENFILE
136*8269e767SBrooks DavisThe system file table is full.
137*8269e767SBrooks Davis.It Bq Er ENOMEM
138*8269e767SBrooks DavisNot enough kernel memory to establish a pipe.
139*8269e767SBrooks Davis.El
140*8269e767SBrooks Davis.Pp
141*8269e767SBrooks DavisThe
142*8269e767SBrooks Davis.Fn pipe2
143*8269e767SBrooks Davissystem call will also fail if:
144*8269e767SBrooks Davis.Bl -tag -width Er
145*8269e767SBrooks Davis.It Bq Er EINVAL
146*8269e767SBrooks DavisThe
147*8269e767SBrooks Davis.Fa flags
148*8269e767SBrooks Davisargument is invalid.
149*8269e767SBrooks Davis.El
150*8269e767SBrooks Davis.Sh SEE ALSO
151*8269e767SBrooks Davis.Xr sh 1 ,
152*8269e767SBrooks Davis.Xr fork 2 ,
153*8269e767SBrooks Davis.Xr read 2 ,
154*8269e767SBrooks Davis.Xr socketpair 2 ,
155*8269e767SBrooks Davis.Xr write 2
156*8269e767SBrooks Davis.Sh HISTORY
157*8269e767SBrooks DavisThe
158*8269e767SBrooks Davis.Fn pipe
159*8269e767SBrooks Davisfunction appeared in
160*8269e767SBrooks Davis.At v3 .
161*8269e767SBrooks Davis.Pp
162*8269e767SBrooks DavisBidirectional pipes were first used on
163*8269e767SBrooks Davis.At V.4 .
164*8269e767SBrooks Davis.Pp
165*8269e767SBrooks DavisThe
166*8269e767SBrooks Davis.Fn pipe2
167*8269e767SBrooks Davisfunction appeared in
168*8269e767SBrooks Davis.Fx 10.0 .
169*8269e767SBrooks Davis.Pp
170*8269e767SBrooks DavisThe
171*8269e767SBrooks Davis.Fn pipe
172*8269e767SBrooks Davisfunction became a wrapper around
173*8269e767SBrooks Davis.Fn pipe2
174*8269e767SBrooks Davisin
175*8269e767SBrooks Davis.Fx 11.0 .
176