.\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson. .\" and Copyright (C) 2005, 2008 Michael Kerrisk .\" and Copyright (C) 2014 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified 1993-07-21, Rik Faith .\" Modified 1994-08-21, Michael Chastain : .\" Fixed typoes. .\" Modified 1997-01-31, Eric S. Raymond .\" Modified 2002-09-28, aeb .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some .\" details for dup2(). .\" 2008-10-09, mtk: add description of dup3() .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1996 Takeshi Ueno .\" all rights reserved. .\" Translated 1996-07-03, Takeshi Ueno .\" Modified 1997-12-14, HANATAKA Shinya .\" Modified 2003-01-16, Akihiro Motoki .\" Updated & Modified 2004-05-19, Yuichi SATO .\" Updated & Modified 2005-09-07, Akihiro MOTOKI .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13 .\" .TH DUP 2 2015\-01\-22 Linux "Linux Programmer's Manual" .SH 名前 dup, dup2, dup3 \- ファイルディスクリプターを複製する .SH 書式 .nf \fB#include \fP .sp \fBint dup(int \fP\fIoldfd\fP\fB);\fP \fBint dup2(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB);\fP .sp \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP /* 定数 O_* の定義の取得 */ \fB#include \fP .sp \fBint dup3(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB, int \fP\fIflags\fP\fB);\fP .fi .SH 説明 \fBdup\fP() システムコールは、 ファイルディスクリプター \fIoldfd\fP のコピーを作成し、 最も小さい番号の未使用のディスクリプターを 新しいディスクリプターとして使用する。 成功が返された場合には、 古いファイルディスクリプターと新しいファイルディスクリプターは 互いに可換なものとして使うことができる。 2つのファイルディスクリプターは同じファイル記述 (description) (\fBopen\fP(2) 参照) を参照しており、したがってファイルオフセットやファイル状態フラグが 共有される。例えば、一方のディスクリプターに対して \fBlseek\fP(2) を使ってファイルオフセットを変更した場合、もう一方のディスクリプターの オフセットも変化する。 .\" 2つのディスクリプターはファイルディスクリプターフラグ (close\-on\-exec flag) を共有しない。複製されたディスクリプターの close\-on\-exec flag (\fBfcntl\fP(2) 参照) は off となる。 .SS dup2() \fBdup2\fP() システムコールは \fBdup\fP() と同じ処理を実行するが、 番号が最も小さい未使用のファイルディスクリプターを使用する代わりに、 \fInewfd\fP で指定されたディスクリプター番号を使用する。 ディスクリプター \fInewfd\fP が以前にオープンされていた場合には、 黙ってそのディスクリプターをクローズしてから再利用する。 ファイルディスクリプター \fInewfd\fP をクローズして再利用する処理は \fIアトミック(不可分)に\fP実行される。これは重要な点である。 なぜなら、 等価な機能を \fBclose\fP(2) と \fBdup\fP() を使って実装しようとすると、 2 つの処理の間に \fInewfd\fP が再利用されてしまうという、 競合状態にさらされることになるからだ。 このような再利用が起こるのは、 メインプログラムがファイルディスクリプターを割り当てる シグナルハンドラーにより割り込まれたり、並行動作するスレッドが ファイルディスクリプターを割り当てたりすることがあるからだ。 以下の点について注意すること: .IP * 3 \fIoldfd\fP が有効なファイルディスクリプターでない場合、その呼び出しは失敗し、 \fInewfd\fP はクローズされない。 .IP * .\" \fIoldfd\fP が有効なファイルディスクリプターで、 \fInewfd\fP が \fIoldfd\fP と同じ値の場合、 \fBdup2\fP() は何もせず、 \fInewfd\fP を返す。 .SS dup3() \fBdup3\fP() は \fBdup2\fP() と同じだが、以下の点が異なる。 .IP * 3 呼び出し元が、新しいファイルディスクリプターに対して close\-on\-exec フラグを強制的に設定することができる。 これを行うには、 \fIflags\fP に \fBO_CLOEXEC\fP を指定する。 このフラグが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの説明を参照のこと。 .IP * .\" FIXME . To confirm with Al Viro that this was intended, and its rationale \fIoldfd\fP が \fInewfd\fP と同じ場合、 \fBdup3\fP() は \fBEINVAL\fP エラーで失敗する。 .SH 返り値 成功すると、これらのシステムコールは新しいディスクリプターを返す。 エラーの場合、\-1 を返し、 \fIerrno\fP を適切に設定する。 .SH エラー .TP \fBEBADF\fP \fIoldfd\fP がオープンされたファイルディスクリプターではない。 .TP \fBEBADF\fP \fInewfd\fP がファイルディスクリプターとして許可されている範囲ではない (\fBgetrlimit\fP(2) の \fBRLIMIT_NOFILE\fP の議論を参照)。 .TP \fBEBUSY\fP (Linux のみ) \fBopen\fP(2) や \fBdup\fP() との競合状態の場合に、 \fBdup2\fP() や \fBdup3\fP() はこのエラーを返すかもしれない。 .TP \fBEINTR\fP \fBdup2\fP() や \fBdup3\fP() の呼び出しがシグナルにより割り込まれた。 \fBsignal\fP(7) 参照。 .TP \fBEINVAL\fP (\fBdup3\fP()) \fIflags\fP に無効な値が入っている。 .TP \fBEINVAL\fP .\" FIXME . To confirm with Al Viro that this was intended, and its rationale (\fBdup3\fP()) \fIoldfd\fP が \fInewfd\fP と同じであった。 .TP \fBEMFILE\fP プロセスがすでにオープンできる最大数までファイルディスクリプター を開いていて、さらに新しいものを開こうとした (\fBgetrlimit\fP(2) のリソース上限 \fBRLIMIT_NOFILE\fP を参照)。 .SH バージョン \fBdup3\fP() はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。 .SH 準拠 \fBdup\fP(), \fBdup2\fP(): SVr4, 4.3BSD, POSIX.1\-2001. .\" SVr4 documents additional .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR. .\" The EBUSY return is Linux-specific. \fBdup3\fP() は Linux 固有である。 .SH 注意 \fInewfd\fP が範囲を超えた時に返されるエラーは、 \fBdup2\fP() と \fBfcntl(\fP..., \fBF_DUPFD\fP, ...\fB)\fP では異っている。 \fBdup2\fP() が \fBF_DUPFD\fP と同じように \fBEINVAL\fP を返すシステムもある。 \fInewfd\fP がオープンされていた場合、 \fBclose\fP(2) 時に報告されることになるエラーはすべて失われる。 これが心配で、シングルスレッドかつシグナルハンドラーで ファイルディスクリプターを割り当てるようなプログラムでない場合には、 正しい方法は \fBdup2\fP() を呼び出す前に \fInewfd\fP をクローズ「しない」ことである。 なぜなら、上で説明した競合状況があるからである。 代わりに、以下のようなコードが使用できることだろう。 .nf /* あとで close() エラーをチェックするのに使用できる ように 'newfd' の複製を取得する。 EBADF エラーは 'newfd' がオープンされていないことを意味する。 */ tmpfd = dup(newfd); if (tmpfd == \-1 && errno != EBADF) { /* 予期しない dup() のエラーを処理する */ } /* アトミックに 'oldfd' を 'newfd' に複製する */ if (dup2(oldfd, newfd) == \-1) { /* dup2() のエラーを処理する */ } /* ここでもともと 'newfd' で参照されていたファイルの close() エラーをチェックする */ if (tmpfd != \-1) { if (close(tmpfd) == \-1) { /* close からのエラーを処理する */ } } .fi .SH 関連項目 \fBclose\fP(2), \fBfcntl\fP(2), \fBopen\fP(2) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。