1 .\" $NetBSD: dup.2,v 1.30 2013/12/25 02:49:52 wiz Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)dup.2 8.1 (Berkeley) 6/4/93
39 .Nd duplicate an existing file descriptor
47 .Fn dup2 "int oldfd" "int newfd"
49 .Fn dup3 "int oldfd" "int newfd" "int flags"
53 family of calls duplicates an existing file descriptor
55 A new file descriptor is produced; it is a new reference to the same
56 underlying system object.
57 The object in question does not distinguish between the descriptors
58 referencing it in any way.
64 calls all move a single shared seek position.
65 Similarly, all object modes, settings, properties, and behavior other
66 than the close-on-exec flag are shared between references.
67 This includes the setting of append mode, non-blocking I/O actions,
68 asynchronous I/O operations in progress, socket options, and so forth.
69 The close-on-exec flag, however, is a property of the descriptor
70 rather than the object and can be set independently for each
73 To get an independent handle with its own seek position and settings,
77 (This is not generally possible for pipes and sockets.)
81 call chooses the new descriptor: it is the lowest-numbered descriptor
87 calls allow the caller to choose the new descriptor by passing
89 which must be within the range of valid descriptors.
94 the call has no effect.
97 is already in use, it is closed as if
101 File descriptors are small non-negative integers that index into the
102 per-process file table.
103 Values 0, 1, and 2 have the special property that they are treated as
104 standard input, standard output, and standard error respectively.
110 are provided as symbolic forms for these values.)
111 The maximum value for a file descriptor is one less than the file
113 The file table size can be interrogated with
115 and can to some extent be adjusted with
120 call includs an additional
122 argument supporting a subset of the
125 .Bl -tag -width O_NOSIGPIPE -offset indent
127 Set the close-on-exec flag on
130 Sets non-blocking I/O.
132 For pipes and sockets, do not raise
134 when a write is made to a broken pipe.
135 Instead, the write will fail with
138 As described above, only the close-on-exec flag is
139 per-file-descriptor, so passing any of the other
146 These settings are, however, applied atomically along with the rest of
155 the close-on-exec flag on the new file descriptor is always left
156 unset and all the modes and settings of the underlying object are left
159 Functionality similar to
161 with slightly different semantics is also available via
164 These calls return the new file descriptor value.
169 this is always the same as
171 If an error occurs, the value \-1 is returned and
173 is set to indicate what happened.
175 A common use for these functions is to set up a pipe as the standard
176 input or standard output of a subprocess.
177 That is done approximately as follows (error handling omitted for
179 .Bd -literal -offset indent
180 #include \*[Lt]unistd.h\*[Gt]
188 /* child; use read end of pipe to stdin */
189 dup2(fds[0], STDIN_FILENO);
192 execv("/some/program", args);
194 /* parent process; return write end of pipe */
199 These functions fail if:
203 is not a valid active descriptor, or for
208 is not in the range of valid file descriptors.
211 contained an invalid value.
214 can generate this error.
216 Too many descriptors are active.
219 can generate this error.
242 function originated in Linux and appeared in