1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
5 .\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
30 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
31 .\" Modified 2002-09-28, aeb
32 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
33 .\" details for dup2().
34 .\" 2008-10-09, mtk: add description of dup3()
36 .\" Japanese Version Copyright (c) 1996 Takeshi Ueno
37 .\" all rights reserved.
38 .\" Translated 1996-07-03, Takeshi Ueno <tueno@vio.co.jp>
39 .\" Modified 1997-12-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
40 .\" Modified 2003-01-16, Akihiro Motoki <amotoki@dd.iij4u.or.jp>
41 .\" Updated & Modified 2004-05-19, Yuichi SATO <ysato444@yahoo.co.jp>
42 .\" Updated & Modified 2005-09-07 Akihiro MOTOKI
43 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
44 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
46 .TH DUP 2 2010-09-10 "Linux" "Linux Programmer's Manual"
48 .\"O dup, dup2, dup3 \- duplicate a file descriptor
50 dup, dup2, dup3 \- ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊ£À½¤¹¤ë
54 .B #include <unistd.h>
56 .BI "int dup(int " oldfd );
57 .BI "int dup2(int " oldfd ", int " newfd );
59 .\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
60 .BR "#define _GNU_SOURCE" " /* feature_test_macros(7) »²¾È */"
61 .B #include <unistd.h>
63 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
67 .\"O These system calls create a copy of the file descriptor
69 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
74 .\"O uses the lowest-numbered unused descriptor for the new descriptor.
76 ¤ÏºÇ¤â¾®¤µ¤¤ÈÖ¹æ¤Î̤»ÈÍѤΥǥ£¥¹¥¯¥ê¥×¥¿¤ò
77 ¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æ»ÈÍѤ¹¤ë¡£
80 .\"O .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
81 .\"O first if necessary, but note the following:
87 ¤ÎÊ£À½¤È¤·¤ÆºîÀ®¤¹¤ë¡£
91 °Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¹¤ë¤³¤È¡£
95 .\"O is not a valid file descriptor, then the call fails, and
99 ¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¾ì¹ç¡¢¤½¤Î¸Æ¤Ó½Ð¤·¤Ï¼ºÇÔ¤·¡¢
105 .\"O is a valid file descriptor, and
107 .\"O has the same value as
111 .\"O does nothing, and returns
114 ¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
124 .\"O After a successful return from one of these system calls,
125 .\"O the old and new file descriptors may be used interchangeably.
126 .\"O They refer to the same open file description (see
128 .\"O and thus share file offset and file status flags;
129 .\"O for example, if the file offset is modified by using
131 .\"O on one of the descriptors, the offset is also changed for the other.
132 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Î¤¤¤º¤ì¤«¤¬À®¸ù¤òÊÖ¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
133 ¸Å¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï
134 ¸ß¤¤¤Ë²Ä´¹¤Ê¤â¤Î¤È¤·¤Æ»È¤¦¤³¤È¤¬¤Ç¤¤ë¡£
135 2¤Ä¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏƱ¤¸¥Õ¥¡¥¤¥ëµ½Ò (description)
137 »²¾È) ¤ò»²¾È¤·¤Æ¤ª¤ê¡¢¤·¤¿¤¬¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤ä¥Õ¥¡¥¤¥ë¾õÂ֥ե饰¤¬
138 ¶¦Í¤µ¤ì¤ë¡£Î㤨¤Ð¡¢°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
140 ¤ò»È¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤òÊѹ¹¤·¤¿¾ì¹ç¡¢¤â¤¦°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
141 ¥ª¥Õ¥»¥Ã¥È¤âÊѲ½¤¹¤ë¡£
143 .\"O The two descriptors do not share file descriptor flags
144 .\"O (the close-on-exec flag).
145 .\"O The close-on-exec flag
146 .\"O .RB ( FD_CLOEXEC ;
149 .\"O for the duplicate descriptor is off.
150 2¤Ä¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¦¥Õ¥é¥° (close-on-exec flag)
151 ¤ò¶¦Í¤·¤Ê¤¤¡£Ê£À½¤µ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
154 »²¾È) ¤Ï off ¤È¤Ê¤ë¡£
163 ¤ÈƱ¤¸¤À¤¬¡¢°Ê²¼¤ÎÅÀ¤¬°Û¤Ê¤ë¡£
165 .\"O The caller can force the close-on-exec flag to be set
166 .\"O for the new file descriptor by specifying
170 .\"O See the description of the same flag in
172 .\"O for reasons why this may be useful.
173 ¸Æ¤Ó½Ð¤·¸µ¤¬¡¢¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
174 close-on-exec ¥Õ¥é¥°¤ò¶¯À©Åª¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
180 ¤³¤Î¥Õ¥é¥°¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
184 ¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
186 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
193 .\"O fails with the error
203 .\"O .SH "RETURN VALUE"
205 .\"O On success, these system calls
206 .\"O return the new descriptor.
207 .\"O On error, \-1 is returned, and
209 .\"O is set appropriately.
210 À®¸ù¤¹¤ë¤È¡¢¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
211 ¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
219 .\"O isn't an open file descriptor, or
221 .\"O is out of the allowed range for file descriptors.
224 ¤¬¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¤«¡¢
226 ¤¬¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æµö¤µ¤ì¤ëÈϰϤ«¤é³°¤ì¤Æ¤¤¤ë¡£
229 .\"O (Linux only) This may be returned by
233 .\"O during a race condition with
241 ¤È¤Î¶¥¹ç¾õÂ֤ξì¹ç¤Ë¡¢
245 ¤Ï¤³¤Î¥¨¥é¡¼¤òÊÖ¤¹¤«¤â¤·¤ì¤Ê¤¤¡£
252 .\"O call was interrupted by a signal; see
257 ¤Î¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¡£
264 .\"O contain an invalid value.
267 ¤Ë̵¸ú¤ÊÃͤ¬Æþ¤Ã¤Æ¤¤¤ë¡£
268 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
280 .\"O The process already has the maximum number of file
281 .\"O descriptors open and tried to open a new one.
283 ¥×¥í¥»¥¹¤¬¤¹¤Ç¤Ë¥ª¡¼¥×¥ó¤Ç¤¤ëºÇÂç¿ô¤Þ¤Ç¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
284 ¤ò³«¤¤¤Æ¤¤¤Æ¡¢¤µ¤é¤Ë¿·¤·¤¤¤â¤Î¤ò³«¤³¤¦¤È¤·¤¿¡£
288 .\"O was added to Linux in version 2.6.27;
289 .\"O glibc support is available starting with
292 ¤Ï¥Ð¡¼¥¸¥ç¥ó 2.6.27 ¤Ç Linux ¤ËÄɲ䵤줿¡£
293 glibc ¤Ë¤è¤ë¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.9 °Ê¹ß¤ÇÍøÍѤǤ¤ë¡£
294 .\"O .SH "CONFORMING TO"
298 SVr4, 4.3BSD, POSIX.1-2001.
301 .\"O is Linux-specific.
303 ¤Ï Linux ¸ÇͤǤ¢¤ë¡£
304 .\"O .\" SVr4 documents additional
305 .\"O .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
306 .\"O .\" The EBUSY return is Linux-specific.
307 .\" SVr4 ¤Ë¤Ï¾¤Ë EINTR, ENOLINK ¥¨¥é¡¼¾õÂ֤ε½Ò¤¬¤¢¤ë¡£
308 .\" POSIX.1 ¤Ë¤Ï¾¤Ë EINTR ¤¬¤¢¤ë¡£
309 .\" EBUSY ¤¬ÊÖ¤µ¤ì¤ë¤Î¤Ï Linux Æȼ«¤Î¤â¤Î¤Ç¤¢¤ë¡£
312 .\"O The error returned by
314 .\"O is different from that returned by
315 .\"O .BR fcntl( "..., " F_DUPFD ", ..." )
318 .\"O is out of range.
321 .\"O also sometimes returns
326 ¤¬ÈϰϤòĶ¤¨¤¿»þ¤ËÊÖ¤µ¤ì¤ë¥¨¥é¡¼¤Ï¡¢
329 .BR fcntl( "..., " F_DUPFD ", ..." )
336 ¤òÊÖ¤¹¥·¥¹¥Æ¥à¤â¤¢¤ë¡£
340 .\"O was open, any errors that would have been reported at
343 .\"O A careful programmer will not use
351 ¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤È¡¢
353 ¤·¤¿»þ¤ËÊó¹ð¤µ¤ì¤ë¤Ï¤º¤Î¥¨¥é¡¼¤¬¼º¤ï¤ì¤Æ¤·¤Þ¤¦¡£
359 ¤ò¥¯¥í¡¼¥º¤¹¤ë¤è¤¦¤Ë¤·¤¿Êý¤¬¤¤¤¤¤À¤í¤¦¡£