OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / dup.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" and Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
30 .\"   Fixed typoes.
31 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 2002-09-28, aeb
33 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
34 .\"     details for dup2().
35 .\" 2008-10-09, mtk: add description of dup3()
36 .\"
37 .TH DUP 2 2015-01-22 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 dup, dup2, dup3 \- duplicate a file descriptor
40 .SH SYNOPSIS
41 .nf
42 .B #include <unistd.h>
43 .sp
44 .BI "int dup(int " oldfd );
45 .BI "int dup2(int " oldfd ", int " newfd );
46 .sp
47 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
48 .BR "#include <fcntl.h>" "              /* Obtain O_* constant definitions */
49 .B #include <unistd.h>
50 .sp
51 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
52 .fi
53 .SH DESCRIPTION
54 The
55 .BR dup ()
56 system call creates a copy of the file descriptor
57 .IR oldfd ,
58 using the lowest-numbered unused descriptor for the new descriptor.
59
60 After a successful return,
61 the old and new file descriptors may be used interchangeably.
62 They refer to the same open file description (see
63 .BR open (2))
64 and thus share file offset and file status flags;
65 for example, if the file offset is modified by using
66 .BR lseek (2)
67 on one of the descriptors, the offset is also changed for the other.
68
69 The two descriptors do not share file descriptor flags
70 (the close-on-exec flag).
71 The close-on-exec flag
72 .RB ( FD_CLOEXEC ;
73 see
74 .BR fcntl (2))
75 for the duplicate descriptor is off.
76 .\"
77 .SS dup2()
78 The
79 .BR dup2 ()
80 system call performs the same task as
81 .BR dup (),
82 but instead of using the lowest-numbered unused file descriptor,
83 it uses the descriptor number specified in
84 .IR newfd .
85 If the descriptor
86 .IR newfd
87 was previously open, it is silently closed before being reused.
88
89 The steps of closing and reusing the file descriptor
90 .IR newfd
91 are performed
92 .IR atomically .
93 This is important, because trying to implement equivalent functionality using
94 .BR close (2)
95 and
96 .BR dup ()
97 would be
98 subject to race conditions, whereby
99 .I newfd
100 might be reused between the two steps.
101 Such reuse could happen because the main program is interrupted
102 by a signal handler that allocates a file descriptor,
103 or because a parallel thread allocates a file descriptor.
104
105 Note the following points:
106 .IP * 3
107 If
108 .I oldfd
109 is not a valid file descriptor, then the call fails, and
110 .I newfd
111 is not closed.
112 .IP *
113 If
114 .I oldfd
115 is a valid file descriptor, and
116 .I newfd
117 has the same value as
118 .IR oldfd ,
119 then
120 .BR dup2 ()
121 does nothing, and returns
122 .IR newfd .
123 .\"
124 .SS dup3()
125 .BR dup3 ()
126 is the same as
127 .BR dup2 (),
128 except that:
129 .IP * 3
130 The caller can force the close-on-exec flag to be set
131 for the new file descriptor by specifying
132 .BR O_CLOEXEC
133 in
134 .IR flags .
135 See the description of the same flag in
136 .BR open (2)
137 for reasons why this may be useful.
138 .IP *
139 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
140 If
141 .IR oldfd
142 equals
143 .IR newfd ,
144 then
145 .BR dup3 ()
146 fails with the error
147 .BR EINVAL .
148 .SH RETURN VALUE
149 On success, these system calls
150 return the new descriptor.
151 On error, \-1 is returned, and
152 .I errno
153 is set appropriately.
154 .SH ERRORS
155 .TP
156 .B EBADF
157 .I oldfd
158 isn't an open file descriptor.
159 .TP
160 .B EBADF
161 .I newfd
162 is out of the allowed range for file descriptors (see the discussion of
163 .BR RLIMIT_NOFILE
164 in
165 .BR getrlimit (2)).
166 .TP
167 .B EBUSY
168 (Linux only) This may be returned by
169 .BR dup2 ()
170 or
171 .BR dup3 ()
172 during a race condition with
173 .BR open (2)
174 and
175 .BR dup ().
176 .TP
177 .B EINTR
178 The
179 .BR dup2 ()
180 or
181 .BR dup3 ()
182 call was interrupted by a signal; see
183 .BR signal (7).
184 .TP
185 .B EINVAL
186 .RB ( dup3 ())
187 .I flags
188 contain an invalid value.
189 .TP
190 .B EINVAL
191 .RB ( dup3 ())
192 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
193 .I oldfd
194 was equal to
195 .IR newfd .
196 .TP
197 .B EMFILE
198 The process already has the maximum number of file
199 descriptors open and tried to open a new one (see the discussion of
200 .BR RLIMIT_NOFILE
201 in
202 .BR getrlimit (2)).
203 .SH VERSIONS
204 .BR dup3 ()
205 was added to Linux in version 2.6.27;
206 glibc support is available starting with
207 version 2.9.
208 .SH CONFORMING TO
209 .BR dup (),
210 .BR dup2 ():
211 SVr4, 4.3BSD, POSIX.1-2001.
212
213 .BR dup3 ()
214 is Linux-specific.
215 .\" SVr4 documents additional
216 .\" EINTR and ENOLINK error conditions.  POSIX.1 adds EINTR.
217 .\" The EBUSY return is Linux-specific.
218 .SH NOTES
219 The error returned by
220 .BR dup2 ()
221 is different from that returned by
222 .BR fcntl( "..., " F_DUPFD ", ..." )
223 when
224 .I newfd
225 is out of range.
226 On some systems,
227 .BR dup2 ()
228 also sometimes returns
229 .B EINVAL
230 like
231 .BR F_DUPFD .
232
233 If
234 .I newfd
235 was open, any errors that would have been reported at
236 .BR close (2)
237 time are lost.
238 If this is of concern,
239 then\(emunless the program is single-threaded and does not allocate
240 file descriptors in signal handlers\(emthe correct approach is
241 .I not
242 to close
243 .I newfd
244 before calling
245 .BR dup2 (),
246 because of the race condition described above.
247 Instead, code something like the following could be used:
248
249 .nf
250     /* Obtain a duplicate of 'newfd' that can subsequently
251        be used to check for close() errors; an EBADF error
252        means that 'newfd' was not open. */
253
254     tmpfd = dup(newfd);
255     if (tmpfd == \-1 && errno != EBADF) {
256         /* Handle unexpected dup() error */
257     }
258
259     /* Atomically duplicate 'oldfd' on 'newfd' */
260
261     if (dup2(oldfd, newfd) == \-1) {
262         /* Handle dup2() error */
263     }
264
265     /* Now check for close() errors on the file originally
266        referred to by 'newfd' */
267
268     if (tmpfd != \-1) {
269         if (close(tmpfd) == \-1) {
270             /* Handle errors from close */
271         }
272     }
273 .fi
274 .SH SEE ALSO
275 .BR close (2),
276 .BR fcntl (2),
277 .BR open (2)
278 .SH COLOPHON
279 This page is part of release 3.79 of the Linux
280 .I man-pages
281 project.
282 A description of the project,
283 information about reporting bugs,
284 and the latest version of this page,
285 can be found at
286 \%http://www.kernel.org/doc/man\-pages/.