OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / draft / man2 / dup.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
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>
6 .\"
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 .\"
27 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
29 .\"   Fixed typoes.
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()
35 .\"
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
45 .\"
46 .TH DUP 2 2008-10-09 "Linux" "Linux Programmer's Manual"
47 .\"O .SH NAME
48 .\"O dup, dup2, dup3 \- duplicate a file descriptor
49 .SH Ì¾Á°
50 dup, dup2, dup3 \- ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊ£À½¤¹¤ë
51 .\"O .SH SYNOPSIS
52 .SH ½ñ¼°
53 .nf
54 .B #include <unistd.h>
55 .sp
56 .BI "int dup(int " oldfd );
57 .BI "int dup2(int " oldfd ", int " newfd );
58 .sp
59 .B #define _GNU_SOURCE
60 .B #include <unistd.h>
61 .sp
62 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
63 .fi
64 .\"O .SH DESCRIPTION
65 .SH ÀâÌÀ
66 .\"O These system calls create a copy of the file descriptor
67 .\"O .IR oldfd .
68 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
69 .I oldfd
70 ¤ÎÊ£À½¤òºî¤ë¡£
71
72 .\"O .BR dup ()
73 .\"O uses the lowest-numbered unused descriptor for the new descriptor.
74 .BR dup ()
75 ¤ÏºÇ¤â¾®¤µ¤¤ÈÖ¹æ¤Î̤»ÈÍѤΥǥ£¥¹¥¯¥ê¥×¥¿¤ò
76 ¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æ»ÈÍѤ¹¤ë¡£
77
78 .\"O .BR dup2 ()
79 .\"O .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
80 .\"O first if necessary, but note the following:
81 .BR dup2 ()
82 ¤Ï
83 .I newfd
84 ¤ò
85 .I oldfd
86 ¤ÎÊ£À½¤È¤·¤ÆºîÀ®¤¹¤ë¡£
87 ɬÍפǤ¢¤ì¤ÐºÇ½é¤Ë
88 .I newfd
89 ¤ò¥¯¥í¡¼¥º¤¹¤ë¡£
90 °Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¹¤ë¤³¤È¡£
91 .IP * 3
92 .\"O If
93 .\"O .I oldfd
94 .\"O is not a valid file descriptor, then the call fails, and
95 .\"O .I newfd
96 .\"O is not closed.
97 .I oldfd
98 ¤¬Í­¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¾ì¹ç¡¢¤½¤Î¸Æ¤Ó½Ð¤·¤Ï¼ºÇÔ¤·¡¢
99 .I newfd
100 ¤Ï¥¯¥í¡¼¥º¤µ¤ì¤ë¡£
101 .IP *
102 .\"O If
103 .\"O .I oldfd
104 .\"O is a valid file descriptor, and
105 .\"O .I newfd
106 .\"O has the same value as
107 .\"O .IR oldfd ,
108 .\"O then
109 .\"O .BR dup2 ()
110 .\"O does nothing, and returns
111 .\"O .IR newfd .
112 .I oldfd
113 ¤¬Í­¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
114 .I newfd
115 ¤¬
116 .I oldfd
117 ¤ÈƱ¤¸Ãͤξì¹ç¡¢
118 .BR dup2 ()
119 ¤Ï²¿¤â¤»¤º¡¢
120 .I newfd
121 ¤òÊÖ¤¹¡£
122 .PP
123 .\"O After a successful return from one of these system calls,
124 .\"O the old and new file descriptors may be used interchangeably.
125 .\"O They refer to the same open file description (see
126 .\"O .BR open (2))
127 .\"O and thus share file offset and file status flags;
128 .\"O for example, if the file offset is modified by using
129 .\"O .BR lseek (2)
130 .\"O on one of the descriptors, the offset is also changed for the other.
131 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Î¤¤¤º¤ì¤«¤¬À®¸ù¤òÊÖ¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
132 ¸Å¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï
133 ¸ß¤¤¤Ë²Ä´¹¤Ê¤â¤Î¤È¤·¤Æ»È¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
134 2¤Ä¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏƱ¤¸¥Õ¥¡¥¤¥ëµ­½Ò (description)
135 .RB ( open (2)
136 »²¾È) ¤ò»²¾È¤·¤Æ¤ª¤ê¡¢¤·¤¿¤¬¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤ä¥Õ¥¡¥¤¥ë¾õÂ֥ե饰¤¬
137 ¶¦Í­¤µ¤ì¤ë¡£Î㤨¤Ð¡¢°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
138 .BR lseek (2)
139 ¤ò»È¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤òÊѹ¹¤·¤¿¾ì¹ç¡¢¤â¤¦°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
140 ¥ª¥Õ¥»¥Ã¥È¤âÊѲ½¤¹¤ë¡£
141
142 .\"O The two descriptors do not share file descriptor flags
143 .\"O (the close-on-exec flag).
144 .\"O The close-on-exec flag
145 .\"O .RB ( FD_CLOEXEC ;
146 .\"O see
147 .\"O .BR fcntl (2))
148 .\"O for the duplicate descriptor is off.
149 2¤Ä¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¦¥Õ¥é¥° (close-on-exec flag)
150 ¤ò¶¦Í­¤·¤Ê¤¤¡£Ê£À½¤µ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
151 close-on-exec flag
152 .RB ( fcntl (2)
153 »²¾È) ¤Ï off ¤È¤Ê¤ë¡£
154
155 .\"O .BR dup3 ()
156 .\"O is the same as
157 .\"O .BR dup2 (),
158 .\"O except that:
159 .BR dup3 ()
160 ¤Ï
161 .BR dup2 ()
162 ¤ÈƱ¤¸¤À¤¬¡¢°Ê²¼¤ÎÅÀ¤¬°Û¤Ê¤ë¡£
163 .IP * 3
164 .\"O The caller can force the close-on-exec flag to be set
165 .\"O for the new file descriptor by specifying
166 .\"O .BR O_CLOEXEC
167 .\"O in
168 .\"O .IR flags .
169 .\"O See the description of the same flag in
170 .\"O .BR open (2)
171 .\"O for reasons why this may be useful.
172 ¸Æ¤Ó½Ð¤·¸µ¤¬¡¢¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
173 close-on-exec ¥Õ¥é¥°¤ò¶¯À©Åª¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
174 ¤³¤ì¤ò¹Ô¤¦¤Ë¤Ï¡¢
175 .I flags
176 ¤Ë
177 .B O_CLOEXEC
178 ¤ò»ØÄꤹ¤ë¡£
179 ¤³¤Î¥Õ¥é¥°¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
180 .BR open (2)
181 ¤Î
182 .B O_CLOEXEC
183 ¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
184 .IP *
185 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
186 .\"O If
187 .\"O .IR oldfd
188 .\"O equals
189 .\"O .IR newfd ,
190 .\"O then
191 .\"O .BR dup3 ()
192 .\"O fails with the error
193 .\"O .BR EINVAL .
194 .I oldfd
195 ¤¬
196 .I newfd
197 ¤ÈƱ¤¸¾ì¹ç¡¢
198 .BR dup3 ()
199 ¤Ï
200 .B EINVAL
201 ¥¨¥é¡¼¤Ç¼ºÇÔ¤¹¤ë¡£
202 .\"O .SH "RETURN VALUE"
203 .SH ÊÖ¤êÃÍ
204 .\"O On success, these system calls
205 .\"O return the new descriptor.
206 .\"O On error, \-1 is returned, and
207 .\"O .I errno
208 .\"O is set appropriately.
209 À®¸ù¤¹¤ë¤È¡¢¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
210 ¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
211 .I errno
212 ¤òŬÀÚ¤ËÀßÄꤹ¤ë¡£
213 .\"O .SH ERRORS
214 .SH ¥¨¥é¡¼
215 .TP
216 .\"O .B EBADF
217 .\"O .I oldfd
218 .\"O isn't an open file descriptor, or
219 .\"O .I newfd
220 .\"O is out of the allowed range for file descriptors.
221 .B EBADF
222 .I oldfd
223 ¤¬¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¤«¡¢
224 .I newfd
225 ¤¬¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æµö¤µ¤ì¤ëÈϰϤ«¤é³°¤ì¤Æ¤¤¤ë¡£
226 .TP
227 .B EBUSY
228 .\"O (Linux only) This may be returned by
229 .\"O .BR dup2 ()
230 .\"O or
231 .\"O .BR dup3 ()
232 .\"O during a race condition with
233 .\"O .BR open (2)
234 .\"O and
235 .\"O .BR dup ().
236 (Linux ¤Î¤ß)
237 .BR open (2)
238 ¤ä
239 .BR dup ()
240 ¤È¤Î¶¥¹ç¾õÂ֤ξì¹ç¤Ë¡¢
241 .BR dup2 ()
242 ¤ä
243 .BR dup3 ()
244 ¤Ï¤³¤Î¥¨¥é¡¼¤òÊÖ¤¹¤«¤â¤·¤ì¤Ê¤¤¡£
245 .TP
246 .B EINTR
247 .\"O The
248 .\"O .BR dup2 ()
249 .\"O or
250 .\"O .BR dup3 ()
251 .\"O call was interrupted by a signal; see
252 .\"O .BR signal (7).
253 .BR dup2 ()
254 ¤ä
255 .BR dup3 ()
256 ¤Î¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¡£
257 .BR signal (7)
258 »²¾È¡£
259 .TP
260 .B EINVAL
261 .\"O .RB ( dup3 ())
262 .\"O .I flags
263 .\"O contain an invalid value.
264 .RB ( dup3 ())
265 .I flags
266 ¤Ë̵¸ú¤ÊÃͤ¬Æþ¤Ã¤Æ¤¤¤ë¡£
267 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
268 .\"O Or,
269 .\"O .I oldfd
270 .\"O was equal to
271 .\"O .IR newfd .
272 ¤â¤·¤¯¤Ï¡¢
273 .I oldfd
274 ¤¬
275 .I newfd
276 ¤ÈƱ¤¸¤Ç¤¢¤Ã¤¿¡£
277 .TP
278 .\"O .B EMFILE
279 .\"O The process already has the maximum number of file
280 .\"O descriptors open and tried to open a new one.
281 .B EMFILE
282 ¥×¥í¥»¥¹¤¬¤¹¤Ç¤Ë¥ª¡¼¥×¥ó¤Ç¤­¤ëºÇÂç¿ô¤Þ¤Ç¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
283 ¤ò³«¤¤¤Æ¤¤¤Æ¡¢¤µ¤é¤Ë¿·¤·¤¤¤â¤Î¤ò³«¤³¤¦¤È¤·¤¿¡£
284 .\"O .SH VERSIONS
285 .SH ¥Ð¡¼¥¸¥ç¥ó
286 .\"O .BR dup3 ()
287 .\"O was added to Linux in version 2.6.27;
288 .\"O glibc support is available starting with
289 .\"O version 2.9.
290 .BR dup3 ()
291 ¤Ï¥Ð¡¼¥¸¥ç¥ó 2.6.27 ¤Ç Linux ¤ËÄɲ䵤줿¡£
292 glibc ¤Ë¤è¤ë¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.9 °Ê¹ß¤ÇÍøÍѤǤ­¤ë¡£
293 .\"O .SH "CONFORMING TO"
294 .SH ½àµò
295 .BR dup (),
296 .BR dup2 ():
297 SVr4, 4.3BSD, POSIX.1-2001.
298
299 .\"O .BR dup3 ()
300 .\"O is Linux-specific.
301 .BR dup3 ()
302 ¤Ï Linux ¸ÇÍ­¤Ç¤¢¤ë¡£
303 .\"O .\" SVr4 documents additional
304 .\"O .\" EINTR and ENOLINK error conditions.  POSIX.1 adds EINTR.
305 .\"O .\" The EBUSY return is Linux-specific.
306 .\" SVr4 ¤Ë¤Ï¾¤Ë EINTR, ENOLINK ¥¨¥é¡¼¾õÂ֤ε­½Ò¤¬¤¢¤ë¡£
307 .\" POSIX.1 ¤Ë¤Ï¾¤Ë EINTR ¤¬¤¢¤ë¡£
308 .\" EBUSY ¤¬ÊÖ¤µ¤ì¤ë¤Î¤Ï Linux Æȼ«¤Î¤â¤Î¤Ç¤¢¤ë¡£
309 .\"O .SH NOTES
310 .SH Ãí°Õ
311 .\"O The error returned by
312 .\"O .BR dup2 ()
313 .\"O is different from that returned by
314 .\"O .BR fcntl( "..., " F_DUPFD ", ..." )
315 .\"O when
316 .\"O .I newfd
317 .\"O is out of range.
318 .\"O On some systems
319 .\"O .BR dup2 ()
320 .\"O also sometimes returns
321 .\"O .B EINVAL
322 .\"O like
323 .\"O .BR F_DUPFD .
324 .I newfd
325 ¤¬ÈϰϤòĶ¤¨¤¿»þ¤ËÊÖ¤µ¤ì¤ë¥¨¥é¡¼¤Ï¡¢
326 .BR dup2 ()
327 ¤È
328 .BR fcntl( "..., " F_DUPFD ", ..." )
329 ¤Ç¤Ï°Û¤Ã¤Æ¤¤¤ë¡£
330 .BR dup2 ()
331 ¤¬
332 .B F_DUPFD
333 ¤ÈƱ¤¸¤è¤¦¤Ë
334 .B EINVAL
335 ¤òÊÖ¤¹¥·¥¹¥Æ¥à¤â¤¢¤ë¡£
336
337 .\"O If
338 .\"O .I newfd
339 .\"O was open, any errors that would have been reported at
340 .\"O .BR close 2()
341 .\"O time are lost.
342 .\"O A careful programmer will not use
343 .\"O .BR dup2 ()
344 .\"O or
345 .\"O .BR dup3 ()
346 .\"O without closing
347 .\"O .I newfd
348 .\"O first.
349 .I newfd
350 ¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤È¡¢
351 .BR close (2)
352 ¤·¤¿»þ¤ËÊó¹ð¤µ¤ì¤ë¤Ï¤º¤Î¥¨¥é¡¼¤¬¼º¤ï¤ì¤Æ¤·¤Þ¤¦¡£
353 .BR dup2 ()
354 ¤ä
355 .BR dup3 ()
356 ¤ò»È¤¦Á°¤ËÀ褺
357 .I newfd
358 ¤ò¥¯¥í¡¼¥º¤¹¤ë¤è¤¦¤Ë¤·¤¿Êý¤¬¤¤¤¤¤À¤í¤¦¡£
359 .\"O .SH "SEE ALSO"
360 .SH ´ØÏ¢¹àÌÜ
361 .BR close (2),
362 .BR fcntl (2),
363 .BR open (2)