OSDN Git Service

(split) LDP: Update release pages based on LDP 3.54 release
[linuxjm/LDP_man-pages.git] / release / man2 / open.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\"             and Copyright (C) 2008 Greg Banks
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Haardt
29 .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified 1996-05-13 by Thomas Koenig
31 .\" Modified 1996-12-20 by Michael Haardt
32 .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
33 .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
34 .\" Modified 1999-06-03 by Michael Haardt
35 .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2004-12-08, mtk, reordered flags list alphabetically
38 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
39 .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
40 .\" 2008-01-03, mtk, with input from Trond Myklebust
41 .\"     <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
42 .\"     Rewrite description of O_EXCL.
43 .\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
44 .\"     on O_DIRECT.
45 .\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
46 .\"
47 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
48 .\" O_TTYINIT.  Eventually these may need to be documented.  --mtk
49 .\" FIXME Linux 2.6.33 has O_DSYNC, and a hidden __O_SYNC.
50 .\"
51 .\"*******************************************************************
52 .\"
53 .\" This file was generated with po4a. Translate the source file.
54 .\"
55 .\"*******************************************************************
56 .\"
57 .\" Japanese Version Copyright (c) 1997-1999 HANATAKA Shinya
58 .\"         all rights reserved.
59 .\" Translated 1999-08-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
60 .\" Updated 2001-05-25, Yuichi SATO <ysato444@yahoo.co.jp>
61 .\" Updated & Modified 2002-01-02, Yuichi SATO
62 .\" Updated & Modified 2002-07-07, Yuichi SATO
63 .\" Updated & Modified 2002-09-19, Yuichi SATO
64 .\" Updated & Modified 2003-07-30, Yuichi SATO
65 .\" Updated & Modified 2003-11-27, Yuichi SATO
66 .\" Updated & Modified 2005-01-01, Yuichi SATO
67 .\" Updated & Modified 2005-09-14, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
68 .\" Updated & Modified 2005-10-14, Akihiro MOTOKI
69 .\" Updated & Modified 2006-01-18, Akihiro MOTOKI
70 .\" Updated & Modified 2006-04-15, Akihiro MOTOKI, LDP v2.29
71 .\" Updated 2007-01-07, Akihiro MOTOKI, LDP v2.43
72 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
73 .\" Updated 2007-10-12, Akihiro MOTOKI, LDP v2.66
74 .\" Updated 2008-02-12, Akihiro MOTOKI, LDP v2.77
75 .\" Updated 2008-04-04, Akihiro MOTOKI, LDP v2.79
76 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05
77 .\" Updated 2010-04-23, Akihiro MOTOKI, LDP v3.24
78 .\" Updated 2012-05-08, Akihiro MOTOKI <amotoki@gmail.com>
79 .\" Updated 2012-05-30, Akihiro MOTOKI <amotoki@gmail.com>
80 .\" Updated 2013-05-06, Akihiro MOTOKI <amotoki@gmail.com>
81 .\" Updated 2013-08-16, Akihiro MOTOKI <amotoki@gmail.com>
82 .\"
83 .TH OPEN 2 2013\-08\-09 Linux "Linux Programmer's Manual"
84 .SH 名前
85 open, creat \- ファイルやデバイスのオープン、作成を行う
86 .SH 書式
87 .nf
88 \fB#include <sys/types.h>\fP
89 \fB#include <sys/stat.h>\fP
90 \fB#include <fcntl.h>\fP
91 .sp
92 \fBint open(const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB);\fP
93 \fBint open(const char *\fP\fIpathname\fP\fB, int \fP\fIflags\fP\fB, mode_t \fP\fImode\fP\fB);\fP
94
95 \fBint creat(const char *\fP\fIpathname\fP\fB, mode_t \fP\fImode\fP\fB);\fP
96 .fi
97 .SH 説明
98 ファイルの \fIpathname\fP を与えると、 \fBopen\fP()  はファイルディスクリプタを返す。
99 ファイルディスクリプタは、この後に続くシステムコール (\fBread\fP(2), \fBwrite\fP(2), \fBlseek\fP(2),
100 \fBfcntl\fP(2) など)  で使用される小さな非負の整数である。 このシステムコールが成功した場合に返されるファイルディスクリプタは
101 そのプロセスがその時点でオープンしていないファイルディスクリプタの うち最小の数字のものとなる。
102 .PP
103 デフォルトでは、新しいファイルディスクリプタは \fBexecve\fP(2) を実行した後も
104 オープンされたままとなる (つまり、 \fBfcntl\fP(2) に説明がある \fBFD_CLOEXEC\fP
105 ファイルディスクリプタフラグは最初は無効である; 後述の \fBO_CLOEXEC\fP フラグ
106 を使うとこのデフォルトを変更することができる)。 ファイルオフセット
107 (file offset) はファイルの先頭に設定される (\fBlseek\fP(2) 参照)。
108 .PP
109 \fBopen\fP()  を呼び出すと、「オープンファイル記述」 \fI(open file description)\fP
110 が作成される。ファイル記述とは、システム全体の オープン中のファイルのテーブルのエントリである。 このエントリは、ファイルオフセットとファイル状態フラグ
111 (\fBfcntl\fP(2)  \fBF_SETFL\fP 操作により変更可能) が保持する。 ファイルディスクリプタはこれらのエントリの一つへの参照である。
112 この後で \fIpathname\fP が削除されたり、他のファイルを参照するように変更されたりしても、 この参照は影響を受けない。
113 新しいオープンファイル記述は最初は他のどのプロセスとも 共有されていないが、 \fBfork\fP(2)  で共有が起こる場合がある。
114 .PP
115 引き数 \fIflags\fP には、アクセスモード \fBO_RDONLY\fP, \fBO_WRONLY\fP, \fBO_RDWR\fP
116 のどれかひとつが入っていなければならない。 これらはそれぞれ読み込み専用、書き込み専用、読み書き用に ファイルをオープンすることを要求するものである。
117
118 .\" SUSv4 divides the flags into:
119 .\" * Access mode
120 .\" * File creation
121 .\" * File status
122 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
123 .\" though it's not clear what the difference between "other" and
124 .\" "File creation" flags is.  I raised an Aardvark to see if this
125 .\" can be clarified in SUSv4; 10 Oct 2008.
126 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
127 .\" TC1 (balloted in 2013), resolved this, so that those three constants
128 .\" are also categorized" as file status flags.
129 .\"
130 さらに、 \fIflags\fP には、ファイル作成フラグ (file creation flag) とファイル状態フラグ (file status
131 flag) を 0 個以上「ビット単位の OR (bitwise\-or)」で 指定することができる。 \fIファイル作成フラグ\fP は
132 \fBO_CLOEXEC\fP, \fBO_CREAT\fP, \fBO_DIRECTORY\fP, \fBO_EXCL\fP, \fBO_NOCTTY\fP,
133 \fBO_NOFOLLOW\fP, \fBO_TRUNC\fP, \fBO_TTY_INIT\fP である。 \fIファイル状態フラグ\fP
134 は以下のリストのうち上記以外の残りのものである。 二種類のフラグの違いは、ファイル状態フラグの方は \fBfcntl\fP(2)
135 を使ってその内容を取得したり (場合によっては) 変更したりできる点にある。 ファイル作成フラグとファイル状態フラグの全リストを以下に示す:
136 .TP 
137 \fBO_APPEND\fP
138 .\" For more background, see
139 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
140 .\" http://nfs.sourceforge.net/
141 The file is opened in append mode.  Before each \fBwrite\fP(2), the file offset
142 is positioned at the end of the file, as if with \fBlseek\fP(2).  \fBO_APPEND\fP
143 may lead to corrupted files on NFS filesystems if more than one process
144 appends data to a file at once.  This is because NFS does not support
145 appending to a file, so the client kernel has to simulate it, which can't be
146 done without a race condition.
147 .TP 
148 \fBO_ASYNC\fP
149 シグナル駆動 I/O (signal\-driven I/O) を有効にする: このファイルディスクリプタへの
150 入力または出力が可能になった場合に、シグナルを生成する (デフォルトは \fBSIGIO\fP であるが、 \fBfcntl\fP(2)
151 によって変更可能である)。 この機能が使用可能なのは端末、疑似端末、ソケットのみであり、 (Linux 2.6 以降では) パイプと FIFO
152 に対しても使用できる。 さらに詳しい説明は \fBfcntl\fP(2)  を参照すること。
153 .TP 
154 \fBO_CLOEXEC\fP (Linux 2.6.23 以降)
155 .\" This flag fixes only one form of the race condition;
156 .\" The race can also occur with, for example, descriptors
157 .\" returned by accept(), pipe(), etc.
158 新しいファイルディスクリプタに対して close\-on\-exec フラグを有効にする。 このフラグを指定することで、プログラムは
159 \fBFD_CLOEXEC\fP フラグをセットするための \fBfcntl\fP(2)  \fBF_SETFD\fP 操作を別途呼び出す必要がなくなる。
160 また、ある種のマルチスレッドのプログラムはこのフラグの使用は 不可欠である。なぜなら、個別に \fBFD_CLOEXEC\fP フラグを設定する
161 \fBfcntl\fP(2)  \fBF_SETFD\fP 操作を呼び出したとしても、あるスレッドがファイルディスクリプタを オープンするのと同時に別のスレッドが
162 \fBfork\fP(2)  と \fBexecve\fP(2)  を実行するという競合条件を避けるのには十分ではないからである。
163 .TP 
164 \fBO_CREAT\fP
165 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
166 .\" XFS (since 2.6.14).
167 If the file does not exist it will be created.  The owner (user ID) of the
168 file is set to the effective user ID of the process.  The group ownership
169 (group ID) is set either to the effective group ID of the process or to the
170 group ID of the parent directory (depending on filesystem type and mount
171 options, and the mode of the parent directory, see the mount options
172 \fIbsdgroups\fP and \fIsysvgroups\fP described in \fBmount\fP(8)).
173 .RS
174 .PP
175 \fImode\fP は新しいファイルを作成する場合に使用するアクセス許可 (permission) を指定する。 \fIflags\fP に \fBO_CREAT\fP
176 が指定されている場合、 \fImode\fP を指定しなければならない。 \fBO_CREAT\fP が指定されていない場合、 \fImode\fP は無視される。
177 有効なアクセス許可は、普段と同じようにプロセスの \fIumask\fP によって修正され、作成されたファイルの許可は \fI(mode\ &\ ~umask)\fP となる。 このモードは、新しく作成されたファイルに対するそれ以降のアクセス にのみ適用される点に注意すること。
178 読み取り専用のファイルを作成する \fBopen\fP()  コールであっても、 読み書き可能なファイルディスクリプタを返すことがありうる。
179 .PP
180 \fImode\fP のために以下のシンボル定数が提供されている :
181 .TP  9
182 \fBS_IRWXU\fP
183 00700 ユーザー (ファイルの所有者) に読み込み、書き込み、 実行の許可がある。
184 .TP 
185 \fBS_IRUSR\fP
186 00400 ユーザーに読み込みの許可がある。
187 .TP 
188 \fBS_IWUSR\fP
189 00200 ユーザーに書き込みの許可がある。
190 .TP 
191 \fBS_IXUSR\fP
192 00100 ユーザーに実行の許可がある。
193 .TP 
194 \fBS_IRWXG\fP
195 00070 グループに読み込み、書き込み、実行の許可がある。
196 .TP 
197 \fBS_IRGRP\fP
198 00040 グループに読み込みの許可がある。
199 .TP 
200 \fBS_IWGRP\fP
201 00020 グループに書き込みの許可がある。
202 .TP 
203 \fBS_IXGRP\fP
204 00010 グループに実行の許可がある。
205 .TP 
206 \fBS_IRWXO\fP
207 00007 他人 (others) に読み込み、書き込み、実行の許可がある。
208 .TP 
209 \fBS_IROTH\fP
210 00004 他人に読み込みの許可がある。
211 .TP 
212 \fBS_IWOTH\fP
213 00002 他人に書き込みの許可がある。
214 .TP 
215 \fBS_IXOTH\fP
216 00001 他人に実行の許可がある。
217 .RE
218 .TP 
219 \fBO_DIRECT\fP (Linux 2.4.10 以降)
220 このファイルに対する I/O のキャッシュの効果を最小化しようとする。このフラグを使うと、一般的に性能が低下する。
221 しかしアプリケーションが独自にキャッシングを行っているような 特別な場合には役に立つ。 ファイルの I/O はユーザー空間バッファに対して直接行われる。
222 \fBO_DIRECT\fP フラグ自身はデータを同期で転送しようとはするが、 \fBO_SYNC\fP
223 フラグのようにデータと必要なメタデータの転送が保証されるわけではない。同期 I/O を保証するためには、 \fBO_DIRECT\fP に加えて
224 \fBO_SYNC\fP を使用しなければならない。下記の「注意」の節の議論も参照。
225 .sp
226 ブロックデバイスに対する似通った意味のインターフェースが \fBraw\fP(8)  で説明されている (但し、このインタフェースは非推奨である)。
227 .TP 
228 \fBO_DIRECTORY\fP
229 .\" But see the following and its replies:
230 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
231 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
232 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
233 \fIpathname\fP がディレクトリでなければオープンは失敗する。 このフラグは Linux 特有であり、 \fBopendir\fP(3)  が FIFO
234 やテープデバイスに対してコールされた場合の サービス不能 (denial\-of\-service) 攻撃を避けるために カーネル 2.1.126
235 で追加された。
236 .TP 
237 \fBO_EXCL\fP
238 この呼び出しでファイルが作成されることを保証する。このフラグが \fBO_CREAT\fP と
239 一緒に指定され、 \fIpathname\fP のファイルが既に存在した場合、 \fBopen\fP() は失敗
240 する。
241
242 .\" POSIX.1-2001 explicitly requires this behavior.
243 これら二つのフラグが指定された際、シンボリックリンクは辿られない。 \fIpathname\fP がシンボリックリンクの場合、
244 シンボリックリンクがどこを指しているかに関わらず \fBopen\fP()  は失敗する。
245
246 一般的には、 \fBO_CREAT\fP を指定せずに \fBO_EXCL\fP を使用した場合の
247 \fBO_EXCL\fP の動作は規定されていない。
248 これには一つ例外があり、Linux 2.6 以降では、
249 \fIpathname\fP がブロックデバイスを参照している場合、
250 \fBO_CREAT\fP なしで \fBO_EXCL\fP を使用することができる。
251 システムがそのブロックデバイスを使用中の場合 (例えば、
252 マウントされているなど)、 \fBopen\fP() はエラー \fBEBUSY\fP で失敗する。
253
254 On NFS, \fBO_EXCL\fP is supported only when using NFSv3 or later on kernel 2.6
255 or later.  In NFS environments where \fBO_EXCL\fP support is not provided,
256 programs that rely on it for performing locking tasks will contain a race
257 condition.  Portable programs that want to perform atomic file locking using
258 a lockfile, and need to avoid reliance on NFS support for \fBO_EXCL\fP, can
259 create a unique file on the same filesystem (e.g., incorporating hostname
260 and PID), and use \fBlink\fP(2)  to make a link to the lockfile.  If \fBlink\fP(2)
261 returns 0, the lock is successful.  Otherwise, use \fBstat\fP(2)  on the unique
262 file to check if its link count has increased to 2, in which case the lock
263 is also successful.
264 .TP 
265 \fBO_LARGEFILE\fP
266 (LFS) \fIoff_t\fP ではサイズを表せない (だだし \fIoff64_t\fP ではサイズを表せる)ファ
267 イルをオープン可能にする。この定義を有効にするためには、(\fIどの\fPヘッダファイ
268 ルをインクルードするよりも前に) \fB_LARGEFILE64_SOURCE\fP マクロを定義しなければ
269 ならない。
270 32 ビットシステムにおいて大きなファイルにアクセスしたい場合、
271 (\fBO_LARGEFILE\fP を使うよりも) \fB_FILE_OFFSET_BITS\fP 機能検査マクロを 64 に
272 セットする方が望ましい方法である (\fBfeature_test_macros\fP(7) を参照)。
273 .TP 
274 \fBO_NOATIME\fP (Linux 2.6.8 以降)
275 .\" The O_NOATIME flag also affects the treatment of st_atime
276 .\" by mmap() and readdir(2), MTK, Dec 04.
277 Do not update the file last access time (\fIst_atime\fP in the inode)  when the
278 file is \fBread\fP(2).  This flag is intended for use by indexing or backup
279 programs, where its use can significantly reduce the amount of disk
280 activity.  This flag may not be effective on all filesystems.  One example
281 is NFS, where the server maintains the access time.
282 .TP 
283 \fBO_NOCTTY\fP
284 \fIpathname\fP が端末 (terminal) デバイス \(em \fBtty\fP(4) 参照 \(em を指している
285 場合に、たとえそのプロセスが制御端末を持っていなくても、オープンしたファイル
286 は制御端末にはならない。
287 .TP 
288 \fBO_NOFOLLOW\fP
289 .\" The headers from glibc 2.0.100 and later include a
290 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
291 .\" used\fP.
292 \fIpathname\fP がシンボリックリンクだった場合、オープンは失敗する。 これは FreeBSD の拡張で、Linux には バージョン
293 2.1.126 で追加された。 このフラグが指定された場合でも \fIpathname\fP の前の方の要素 (最後のディレクトリセパレータより前の部分)
294 にあるシンボリックリンクについてはリンクが辿られる。 下記の \fBO_PATH\fP も参照のこと。
295 .TP 
296 \fBO_NONBLOCK\fP または \fBO_NDELAY\fP
297 可能ならば、ファイルは非停止 (nonblocking) モードでオープンされる。
298 \fBopen\fP() も、返したファイルディスクリプタに対する以後のすべての操作も呼び出
299 したプロセスを待たせることはない。 FIFO (名前付きパイプ) を扱う場合には
300 \fBfifo\fP(7) も参照すること。 強制ファイルロック (mandatory file lock) やファイ
301 ルリース (file lease) と組み合わせた場合の、 \fBO_NONBLOCK\fP の効果についての
302 議論は、 \fBfcntl\fP(2) を参照すること。
303 .TP 
304 \fBO_PATH\fP (Linux 2.6.39 以降)
305 .\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
306 .\" commit 326be7b484843988afe57566b627fb7a70beac56
307 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
308 .\"
309 .\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
310 .\"     Subject: Re: [PATCH] open(2): document O_PATH
311 .\"     Newsgroups: gmane.linux.man, gmane.linux.kernel
312 .\"
313 Obtain a file descriptor that can be used for two purposes: to indicate a
314 location in the filesystem tree and to perform operations that act purely at
315 the file descriptor level.  The file itself is not opened, and other file
316 operations (e.g., \fBread\fP(2), \fBwrite\fP(2), \fBfchmod\fP(2), \fBfchown\fP(2),
317 \fBfgetxattr\fP(2), \fBmmap\fP(2))  fail with the error \fBEBADF\fP.
318
319 The following operations \fIcan\fP be performed on the resulting file
320 descriptor:
321 .RS
322 .IP * 3
323 .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
324 .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
325 \fBclose\fP(2); \fBfchdir\fP(2)  (since Linux 3.5); \fBfstat\fP(2)  (since Linux
326 3.6).
327 .IP *
328 Duplicating the file descriptor (\fBdup\fP(2), \fBfcntl\fP(2)  \fBF_DUPFD\fP, etc.).
329 .IP *
330 Getting and setting file descriptor flags (\fBfcntl\fP(2)  \fBF_GETFD\fP and
331 \fBF_SETFD\fP).
332 .IP *
333 Retrieving open file status flags using the \fBfcntl\fP(2)  \fBF_GETFL\fP
334 operation: the returned flags will include the bit \fBO_PATH\fP.
335
336 .IP *
337 Passing the file descriptor as the \fIdirfd\fP argument of \fBopenat\fP(2)  and
338 the other "*at()" system calls.
339 .IP *
340 Passing the file descriptor to another process via a UNIX domain socket (see
341 \fBSCM_RIGHTS\fP in \fBunix\fP(7)).
342 .RE
343 .IP
344 When \fBO_PATH\fP is specified in \fIflags\fP, flag bits other than \fBO_DIRECTORY\fP
345 and \fBO_NOFOLLOW\fP are ignored.
346
347 If the \fBO_NOFOLLOW\fP flag is also specified, then the call returns a file
348 descriptor referring to the symbolic link.  This file descriptor can be used
349 as the \fIdirfd\fP argument in calls to \fBfchownat\fP(2), \fBfstatat\fP(2),
350 \fBlinkat\fP(2), and \fBreadlinkat\fP(2)  with an empty pathname to have the calls
351 operate on the symbolic link.
352 .TP 
353 \fBO_SYNC\fP
354 ファイルは同期 (synchronous) I/O モードでオープンされる。 \fBopen\fP()  が返したファイルディスクリプタに対して
355 \fBwrite\fP(2)  を行うと、必ず呼び出したプロセスをブロックし、 該当ハードウェアに物理的に書き込まれるまで返らない。
356 \fI以下の「注意」の章も参照。\fP
357 .TP 
358 \fBO_TRUNC\fP
359 ファイルが既に存在し、通常ファイルであり、 書き込み可モードでオープンされている (つまり、 \fBO_RDWR\fPまたは\fBO_WRONLY\fP の)
360 場合、長さ 0 に切り詰め (truncate) られる。 ファイルが FIFO または端末デバイスファイルの場合、 \fBO_TRUNC\fP
361 フラグは無視される。 それ以外の場合、 \fBO_TRUNC\fP の効果は未定義である。
362 .PP
363 これらの選択フラグのいくつかはファイルをオープンした後でも \fBfcntl\fP(2)  を使用して変更することができる。
364
365 \fBcreat\fP()  は \fIflags\fP に \fBO_CREAT|O_WRONLY|O_TRUNC\fP を指定して \fBopen\fP()
366 を行うのと等価である。
367 .SH 返り値
368 \fBopen\fP()  と \fBcreat\fP()  は新しいファイルディスクリプタを返す。 エラーが発生した場合は \-1 を返す (その場合は
369 \fIerrno\fP が適切に設定される)。
370 .SH エラー
371 .TP 
372 \fBEACCES\fP
373 ファイルに対する要求されたアクセスが許されていないか、 \fIpathname\fP のディレクトリ部分の何れかのディレクトリに検索許可がなかった。
374 またはファイルが存在せず、親ディレクトリへの書き込み許可がなかった。 (\fBpath_resolution\fP(7)  も参照すること。)
375 .TP 
376 \fBEDQUOT\fP
377 Where \fBO_CREAT\fP is specified, the file does not exist, and the user's quota
378 of disk blocks or inodes on the filesystem has been exhausted.
379 .TP 
380 \fBEEXIST\fP
381 \fIpathname\fP は既に存在し、 \fBO_CREAT\fP と \fBO_EXCL\fP が使用された。
382 .TP 
383 \fBEFAULT\fP
384 \fIpathname\fP がアクセス可能なアドレス空間の外を指している。
385 .TP 
386 \fBEFBIG\fP
387 \fBEOVERFLOW\fP 参照。
388 .TP 
389 \fBEINTR\fP
390 遅いデバイス (例えば FIFO、 \fBfifo\fP(7)  参照) のオープンが完了するのを待って停止している間に
391 システムコールがシグナルハンドラにより割り込まれた。 \fBsignal\fP(7)  参照。
392 .TP 
393 \fBEINVAL\fP
394 The filesystem does not support the \fBO_DIRECT\fP flag. See \fBNOTES\fP for more
395 information.
396 .TP 
397 \fBEISDIR\fP
398 \fIpathname\fP はディレクトリを参照しており、書き込み要求が含まれていた (つまり \fBO_WRONLY\fP または \fBO_RDWR\fP
399 が設定されている)。
400 .TP 
401 \fBELOOP\fP
402 \fIpathname\fP を解決する際に遭遇したシンボリックリンクが多過ぎる。 または \fBO_NOFOLLOW\fP が指定されており、
403 \fIpathname\fP がシンボリックリンクだった。
404 .TP 
405 \fBEMFILE\fP
406 プロセスがオープンしているファイル数がすでに最大数に達している。
407 .TP 
408 \fBENAMETOOLONG\fP
409 \fIpathname\fP が長過ぎる。
410 .TP 
411 \fBENFILE\fP
412 オープンされているファイルの総数がシステムの制限に達している。
413 .TP 
414 \fBENODEV\fP
415 \fIpathname\fP がデバイススペシャルファイルを参照しており、対応するデバイスが存在しない。 (これは Linux
416 カーネルのバグであり、この場合には \fBENXIO\fP が返されるべきである)
417 .TP 
418 \fBENOENT\fP
419 \fBO_CREAT\fP が設定されておらず、かつ指定されたファイルが存在しない。 または、 \fIpathname\fP のディレクトリ部分が存在しないか壊れた
420 (dangling) シンボリックリンクである。
421 .TP 
422 \fBENOMEM\fP
423 十分なカーネルメモリーがない。
424 .TP 
425 \fBENOSPC\fP
426 \fIpathname\fP を作成する必要があるが、 \fIpathname\fP を含んでいるデバイスに新しいファイルのための空き容量がない。
427 .TP 
428 \fBENOTDIR\fP
429 \fIpathname\fP に含まれるディレクトリ部分のどれかが実際にはディレクトリでない。 または \fBO_DIRECTORY\fP が指定されており、
430 \fIpathname\fP がディレクトリでない。
431 .TP 
432 \fBENXIO\fP
433 \fBO_NONBLOCK\fP | \fBO_WRONLY\fP が設定されており、指定したファイルが FIFO で
434 そのファイルを読み込みのためにオープンしているプロセスが存在しない。 または、ファイルがデバイススペシャルファイルで 対応するデバイスが存在しない。
435 .TP 
436 \fBEOVERFLOW\fP
437 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
438 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
439 .\" Reported 2006-10-03
440 \fIpathname\fP が参照しているのが、大き過ぎてオープンできない通常のファイルである。 通常、このエラーが発生するは、32
441 ビットプラットフォーム上で \fI\-D_FILE_OFFSET_BITS=64\fP を指定せずにコンパイルされたアプリケーションが、ファイルサイズが
442 \fI(2<31)\-1\fP ビットを超えるファイルを開こうとした場合である。 上記の \fBO_LARGEFILE\fP も参照。 これは
443 POSIX.1\-2001 で規定されているエラーである。 2.6.24 より前のカーネルでは、Linux はこの場合にエラー \fBEFBIG\fP
444 を返していた。
445 .TP 
446 \fBEPERM\fP
447 .\" Strictly speaking, it's the filesystem UID... (MTK)
448 \fBO_NOATIME\fP フラグが指定されたが、呼び出し元の実効ユーザー ID が ファイルの所有者と一致せず、かつ呼び出し元に特権
449 (\fBCAP_FOWNER\fP)  がない。
450 .TP 
451 \fBEROFS\fP
452 \fIpathname\fP refers to a file on a read\-only filesystem and write access was
453 requested.
454 .TP 
455 \fBETXTBSY\fP
456 \fIpathname\fP が現在実行中の実行イメージを参照しており、書き込みが要求された。
457 .TP 
458 \fBEWOULDBLOCK\fP
459 \fBO_NONBLOCK\fP フラグが指定されたが、そのファイルには矛盾するリースが設定されていた (\fBfcntl\fP(2)  参照)。
460 .SH 準拠
461 SVr4, 4.3BSD, POSIX.1\-2001.  フラグ \fBO_DIRECTORY\fP, \fBO_NOATIME\fP,
462 \fBO_NOFOLLOW\fP, \fBO_PATH\fP は Linux 特有のものであり、 これらのフラグの定義を得るためには、
463 (「どの」ヘッダファイルをインクルードするよりも前に)  \fB_GNU_SOURCE\fP を定義する必要があるかもしれない。
464
465 \fBO_CLOEXEC\fP フラグは POSIX.1\-2001 では規定されていないが、 POSIX.1\-2008 で規定されている。
466
467 \fBO_DIRECT\fP は POSIX では規定されていない。 \fBO_DIRECT\fP の定義を得るには
468 (「どの」ヘッダファイルをインクルードするよりも前に)  \fB_GNU_SOURCE\fP を定義しなければならない。
469 .SH 注意
470 Linux では、 \fBO_NONBLOCK\fP フラグは、 open を実行したいが read または write を実行する意図は
471 必ずしもないことを意味する。 これは \fBioctl\fP(2)  のためのファイルディスクリプタを取得するために、
472 デバイスをオープンするときによく用いられる。
473
474 .\" See for example util-linux's disk-utils/setfdprm.c
475 .\" For some background on access mode 3, see
476 .\" http://thread.gmane.org/gmane.linux.kernel/653123
477 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
478 .\" LKML, 12 Mar 2008
479 「アクセスモード」の値 \fBO_RDONLY\fP, \fBO_WRONLY\fP, \fBO_RDWR\fP は、 \fIflags\fP
480 に指定できる他の値と違い、個々のビットを指定するものではなく、 これらの値は \fIflags\fP の下位 2 ビットを定義する。 \fBO_RDONLY\fP,
481 \fBO_WRONLY\fP, \fBO_RDWR\fP はそれぞれ 0, 1, 2 に定義されている。 言い換えると、 \fBO_RDONLY |
482 O_WRONLY\fP の組み合わせは論理的に間違いであり、確かに \fBO_RDWR\fP と同じ意味ではない。 Linux
483 では、特別な、非標準なアクセスモードとして 3 (バイナリでは 11) が 予約されており \fIflags\fP に指定できる。
484 このアクセスモードを指定すると、ファイルの読み出し/書き込み許可をチェックし、 読み出しにも書き込みにも使用できないディスクリプタを返す。
485 この非標準のアクセスモードはいくつかの Linux ドライバで、デバイス固有の \fBioctl\fP(2)
486 操作にのみ使用されるディスクリプタを返すために使われている。
487 .LP
488 .\" Linux 2.0, 2.5: truncate
489 .\" Solaris 5.7, 5.8: truncate
490 .\" Irix 6.5: truncate
491 .\" Tru64 5.1B: truncate
492 .\" HP-UX 11.22: truncate
493 .\" FreeBSD 4.7: truncate
494 \fBO_RDONLY | O_TRUNC\fP の影響は未定義であり、その動作は実装によって異なる。 多くのシステムではファイルは実際に切り詰められる。
495 .PP
496 NFS を実現しているプロトコルには多くの不備があり、特に \fBO_SYNC\fP と \fBO_NDELAY\fP に影響する。
497
498 POSIX provides for three different variants of synchronized I/O,
499 corresponding to the flags \fBO_SYNC\fP, \fBO_DSYNC\fP, and \fBO_RSYNC\fP.  Currently
500 (2.6.31), Linux implements only \fBO_SYNC\fP, but glibc maps \fBO_DSYNC\fP and
501 \fBO_RSYNC\fP to the same numerical value as \fBO_SYNC\fP.  Most Linux filesystems
502 don't actually implement the POSIX \fBO_SYNC\fP semantics, which require all
503 metadata updates of a write to be on disk on returning to user space, but
504 only the \fBO_DSYNC\fP semantics, which require only actual file data and
505 metadata necessary to retrieve it to be on disk by the time the system call
506 returns.
507
508 \fBopen\fP()  はスペシャルファイルをオープンすることができるが、 \fBcreat\fP()  でスペシャルファイルを作成できない点に注意すること。
509 代わりに \fBmknod\fP(2)  を使用する。
510 .LP
511 On NFS filesystems with UID mapping enabled, \fBopen\fP()  may return a file
512 descriptor but, for example, \fBread\fP(2)  requests are denied with
513 \fBEACCES\fP.  This is because the client performs \fBopen\fP()  by checking the
514 permissions, but UID mapping is performed by the server upon read and write
515 requests.
516
517 ファイルが新しく作成されると、 ファイルの \fIst_atime\fP, \fIst_ctime\fP, \fIst_mtime\fP フィールド
518 (それぞれ最終アクセス時刻、最終状態変更時刻、最終修正時刻である。 \fBstat\fP(2)  参照) が現在時刻に設定される。 さらに親ディレクトリの
519 \fIst_ctime\fP と \fIst_mtime\fP も現在時刻に設定される。 それ以外の場合で、O_TRUNC フラグでファイルが修正されたときは、
520 ファイルの \fIst_ctime\fP と \fIst_mtime\fP フィールドが現在時刻に設定される。
521 .SS O_DIRECT
522 .LP
523 The \fBO_DIRECT\fP flag may impose alignment restrictions on the length and
524 address of user\-space buffers and the file offset of I/Os.  In Linux
525 alignment restrictions vary by filesystem and kernel version and might be
526 absent entirely.  However there is currently no filesystem\-independent
527 interface for an application to discover these restrictions for a given file
528 or filesystem.  Some filesystems provide their own interfaces for doing so,
529 for example the \fBXFS_IOC_DIOINFO\fP operation in \fBxfsctl\fP(3).
530 .LP
531 Under Linux 2.4, transfer sizes, and the alignment of the user buffer and
532 the file offset must all be multiples of the logical block size of the
533 filesystem.  Under Linux 2.6, alignment to 512\-byte boundaries suffices.
534 .LP
535 メモリバッファがプライベートマッピング (\fBmmap\fP(2) の \fBMAP_PRIVATE\fP
536 フラグで作成されたマッピング) の場合には、\fBO_DIRECT\fP I/O は
537 \fBfork\fP(2) システムコールと同時に決して実行すべきではない
538 (プライベートマッピングには、ヒープ領域に割り当てられたメモリや静的に
539 割り当てたバッファも含まれる)。非同期 I/O インターフェース (AIO) 経由
540 やプロセス内の他のスレッドから発行された、このような I/O は、
541 \fBfork\fP(2) が呼び出される前に完了されるべきである。
542 そうしなかった場合、データ破壊や、親プロセスや子プロセスでの予期しない
543 動作が起こる可能性がある。
544 \fBO_DIRECT\fP I/O 用のメモリバッファが \fBshmat\fP(2) や\fBMAP_SHARED\fP フラグ
545 付きの \fBmmap\fP(2) で作成された場合には、この制限はあてはまらない。
546 \fBmadvise\fP(2) でメモリバッファにアドバイス \fBMADV_DONTFORK\fP が設定され
547 ている場合にも、この制限はあてはまらない(\fBMADV_DONTFORK\fP はそのメモリ
548 バッファが \fBfork\fP(2) 後に子プロセスからは利用できないことを保証するも
549 のである)。
550 .LP
551 \fBO_DIRECT\fP フラグは SGI IRIX で導入された。SGI IRIX にも Linux 2.4 と同様の (ユーザーバッファの)
552 アラインメントの制限がある。 また、IRIX には適切な配置とサイズを取得するための \fBfcntl\fP(2)  コールがある。 FreeBSD 4.x
553 も同じ名前のフラグを導入したが、アラインメントの制限はない。
554 .LP
555 \fBO_DIRECT\fP support was added under Linux in kernel version 2.4.10.  Older
556 Linux kernels simply ignore this flag.  Some filesystems may not implement
557 the flag and \fBopen\fP()  will fail with \fBEINVAL\fP if it is used.
558 .LP
559 Applications should avoid mixing \fBO_DIRECT\fP and normal I/O to the same
560 file, and especially to overlapping byte regions in the same file.  Even
561 when the filesystem correctly handles the coherency issues in this
562 situation, overall I/O throughput is likely to be slower than using either
563 mode alone.  Likewise, applications should avoid mixing \fBmmap\fP(2)  of files
564 with direct I/O to the same files.
565 .LP
566 The behaviour of \fBO_DIRECT\fP with NFS will differ from local filesystems.
567 Older kernels, or kernels configured in certain ways, may not support this
568 combination.  The NFS protocol does not support passing the flag to the
569 server, so \fBO_DIRECT\fP I/O will bypass the page cache only on the client;
570 the server may still cache the I/O.  The client asks the server to make the
571 I/O synchronous to preserve the synchronous semantics of \fBO_DIRECT\fP.  Some
572 servers will perform poorly under these circumstances, especially if the I/O
573 size is small.  Some servers may also be configured to lie to clients about
574 the I/O having reached stable storage; this will avoid the performance
575 penalty at some risk to data integrity in the event of server power
576 failure.  The Linux NFS client places no alignment restrictions on
577 \fBO_DIRECT\fP I/O.
578 .PP
579 まとめると、 \fBO_DIRECT\fP は、注意して使うべきであるが、強力なツールとなる可能性を持っている。 アプリケーションは \fBO_DIRECT\fP
580 をデフォルトでは無効になっている性能向上のためのオプションと 考えておくのがよいであろう。
581 .PP
582 .RS
583 「O_DIRECT でいつも困るのは、インタフェース全部が本当にお馬鹿な点だ。 たぶん危ないマインドコントロール剤で
584 頭がおかしくなったサルが設計したんじゃないかな」 \(em Linus
585 .RE
586 .SH バグ
587 .\" FIXME . Check bugzilla report on open(O_ASYNC)
588 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
589 現在のところ、 \fBopen\fP()  の呼び出し時に \fBO_ASYNC\fP を指定してシグナル駆動 I/O を有効にすることはできない。
590 このフラグを有効にするには \fBfcntl\fP(2)  を使用すること。
591 .SH 関連項目
592 \fBchmod\fP(2), \fBchown\fP(2), \fBclose\fP(2), \fBdup\fP(2), \fBfcntl\fP(2), \fBlink\fP(2),
593 \fBlseek\fP(2), \fBmknod\fP(2), \fBmmap\fP(2), \fBmount\fP(2), \fBopenat\fP(2), \fBread\fP(2),
594 \fBsocket\fP(2), \fBstat\fP(2), \fBumask\fP(2), \fBunlink\fP(2), \fBwrite\fP(2),
595 \fBfopen\fP(3), \fBfifo\fP(7), \fBpath_resolution\fP(7), \fBsymlink\fP(7)
596 .SH この文書について
597 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.54 の一部
598 である。プロジェクトの説明とバグ報告に関する情報は
599 http://www.kernel.org/doc/man\-pages/ に書かれている。