OSDN Git Service

05ee3c88aebeb6d1a64f16531ecae278d91639d2
[linuxjm/LDP_man-pages.git] / draft / 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 .\"*******************************************************************
38 .\"
39 .\" This file was generated with po4a. Translate the source file.
40 .\"
41 .\"*******************************************************************
42 .\"
43 .\" Japanese Version Copyright (c) 1996 Takeshi Ueno
44 .\"         all rights reserved.
45 .\" Translated 1996-07-03, Takeshi Ueno <tueno@vio.co.jp>
46 .\" Modified 1997-12-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
47 .\" Modified 2003-01-16, Akihiro Motoki <amotoki@dd.iij4u.or.jp>
48 .\" Updated & Modified 2004-05-19, Yuichi SATO <ysato444@yahoo.co.jp>
49 .\" Updated & Modified 2005-09-07, Akihiro MOTOKI
50 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
51 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
52 .\"
53 .TH DUP 2 2015\-01\-22 Linux "Linux Programmer's Manual"
54 .SH 名前
55 dup, dup2, dup3 \- ファイルディスクリプタを複製する
56 .SH 書式
57 .nf
58 \fB#include <unistd.h>\fP
59 .sp
60 \fBint dup(int \fP\fIoldfd\fP\fB);\fP
61 \fBint dup2(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB);\fP
62 .sp
63 \fB#define _GNU_SOURCE\fP             /* feature_test_macros(7) 参照 */
64 \fB#include <fcntl.h>\fP              /* 定数 O_* の定義の取得 */
65 \fB#include <unistd.h>\fP
66 .sp
67 \fBint dup3(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB, int \fP\fIflags\fP\fB);\fP
68 .fi
69 .SH 説明
70 \fBdup\fP() システムコールは、 ファイルディスクリプタ \fIoldfd\fP のコピーを作成し、 最も小さい番号の未使用のディスクリプタを
71 新しいディスクリプタとして使用する。
72
73 成功が返された場合には、 古いファイルディスクリプタと新しいファイルディスクリプタは 互いに可換なものとして使うことができる。
74 2つのファイルディスクリプタは同じファイル記述 (description)  (\fBopen\fP(2)  参照)
75 を参照しており、したがってファイルオフセットやファイル状態フラグが 共有される。例えば、一方のディスクリプタに対して \fBlseek\fP(2)
76 を使ってファイルオフセットを変更した場合、もう一方のディスクリプタの オフセットも変化する。
77
78 .\"
79 2つのディスクリプタはファイルディスクリプタフラグ (close\-on\-exec flag)  を共有しない。複製されたディスクリプタの
80 close\-on\-exec flag (\fBfcntl\fP(2)  参照) は off となる。
81 .SS dup2()
82 \fBdup2\fP() システムコールは \fBdup\fP() と同じ処理を実行するが、
83 番号が最も小さい未使用のファイルディスクリプタを使用する代わりに、
84 \fInewfd\fP で指定されたディスクリプタ番号を使用する。
85 ディスクリプタ \fInewfd\fP が以前にオープンされていた場合には、
86 黙ってそのディスクリプタをクローズしてから再利用する。
87
88 ファイルディスクリプタ \fInewfd\fP をクローズして再利用する処理は
89 \fIアトミック(不可分)に\fP実行される。これは重要な点である。 なぜなら、
90 等価な機能を \fBclose\fP(2) と \fBdup\fP() を使って実装しようとすると、
91 2 つの処理の間に \fInewfd\fP が再利用されてしまうという、
92 競合状態にさらされることになるからだ。
93 このような再利用が起こるのは、
94 メインプログラムがファイルディスクリプタを割り当てる
95 シグナルハンドラーにより割り込まれたり、並行動作するスレッドが
96 ファイルディスクリプタを割り当てたりすることがあるからだ。
97
98 以下の点について注意すること:
99 .IP * 3
100 \fIoldfd\fP が有効なファイルディスクリプタでない場合、その呼び出しは失敗し、 \fInewfd\fP はクローズされない。
101 .IP *
102 .\"
103 \fIoldfd\fP が有効なファイルディスクリプタで、 \fInewfd\fP が \fIoldfd\fP と同じ値の場合、 \fBdup2\fP()  は何もせず、
104 \fInewfd\fP を返す。
105 .SS dup3()
106 \fBdup3\fP()  は \fBdup2\fP()  と同じだが、以下の点が異なる。
107 .IP * 3
108 呼び出し元が、新しいファイルディスクリプタに対して close\-on\-exec フラグを強制的に設定することができる。 これを行うには、
109 \fIflags\fP に \fBO_CLOEXEC\fP を指定する。 このフラグが役に立つ理由については、 \fBopen\fP(2)  の \fBO_CLOEXEC\fP
110 フラグの説明を参照のこと。
111 .IP *
112 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
113 \fIoldfd\fP が \fInewfd\fP と同じ場合、 \fBdup3\fP()  は \fBEINVAL\fP エラーで失敗する。
114 .SH 返り値
115 成功すると、これらのシステムコールは新しいディスクリプタを返す。 エラーの場合、\-1 を返し、 \fIerrno\fP を適切に設定する。
116 .SH エラー
117 .TP 
118 \fBEBADF\fP
119 \fIoldfd\fP がオープンされたファイルディスクリプタではない。
120 .TP 
121 \fBEBADF\fP
122 \fInewfd\fP がファイルディスクリプターとして許可されている範囲ではない (\fBgetrlimit\fP(2) の \fBRLIMIT_NOFILE\fP
123 の議論を参照)。
124 .TP 
125 \fBEBUSY\fP
126 (Linux のみ)  \fBopen\fP(2)  や \fBdup\fP()  との競合状態の場合に、 \fBdup2\fP()  や \fBdup3\fP()
127 はこのエラーを返すかもしれない。
128 .TP 
129 \fBEINTR\fP
130 \fBdup2\fP()  や \fBdup3\fP()  の呼び出しがシグナルにより割り込まれた。 \fBsignal\fP(7)  参照。
131 .TP 
132 \fBEINVAL\fP
133 (\fBdup3\fP())  \fIflags\fP に無効な値が入っている。
134 .TP 
135 \fBEINVAL\fP
136 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
137 (\fBdup3\fP()) \fIoldfd\fP が \fInewfd\fP と同じであった。
138 .TP 
139 \fBEMFILE\fP
140 プロセスがすでにオープンできる最大数までファイルディスクリプタ を開いていて、さらに新しいものを開こうとした (\fBgetrlimit\fP(2)
141 のリソース上限 \fBRLIMIT_NOFILE\fP を参照)。
142 .SH バージョン
143 \fBdup3\fP()  はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。
144 .SH 準拠
145 \fBdup\fP(), \fBdup2\fP(): SVr4, 4.3BSD, POSIX.1\-2001.
146
147 .\" SVr4 documents additional
148 .\" EINTR and ENOLINK error conditions.  POSIX.1 adds EINTR.
149 .\" The EBUSY return is Linux-specific.
150 \fBdup3\fP()  は Linux 固有である。
151 .SH 注意
152 \fInewfd\fP が範囲を超えた時に返されるエラーは、 \fBdup2\fP()  と \fBfcntl(\fP..., \fBF_DUPFD\fP, ...\fB)\fP
153 では異っている。 \fBdup2\fP()  が \fBF_DUPFD\fP と同じように \fBEINVAL\fP を返すシステムもある。
154
155 \fInewfd\fP がオープンされていた場合、
156 \fBclose\fP(2) 時に報告されることになるエラーはすべて失われる。
157 これが心配で、シングルスレッドかつシグナルハンドラーで
158 ファイルディスクリプタを割り当てるようなプログラムでない場合には、
159 正しい方法は \fBdup2\fP() を呼び出す前に
160 \fInewfd\fP をクローズ「しない」ことである。
161 なぜなら、上で説明した競合状況があるからである。
162 代わりに、以下のようなコードが使用できることだろう。
163
164 .nf
165     /* あとで close() エラーをチェックするのに使用できる
166        ように 'newfd' の複製を取得する。 EBADF エラーは
167        'newfd' がオープンされていないことを意味する。 */
168
169     tmpfd = dup(newfd);
170     if (tmpfd == \-1 && errno != EBADF) {
171         /* 予期しない dup() のエラーを処理する */
172     }
173
174     /* アトミックに 'oldfd' を 'newfd' に複製する */
175
176     if (dup2(oldfd, newfd) == \-1) {
177         /* dup2() のエラーを処理する */
178     }
179
180     /* ここでもともと 'newfd' で参照されていたファイルの
181        close() エラーをチェックする */
182
183     if (tmpfd != \-1) {
184         if (close(tmpfd) == \-1) {
185             /* close からのエラーを処理する */
186         }
187     }
188 .fi
189 .SH 関連項目
190 \fBclose\fP(2), \fBfcntl\fP(2), \fBopen\fP(2)
191 .SH この文書について
192 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.78 の一部
193 である。プロジェクトの説明とバグ報告に関する情報は
194 http://www.kernel.org/doc/man\-pages/ に書かれている。