OSDN Git Service

5c2813b141c25711f5133db62ef78ebcbf4248d7
[linuxjm/LDP_man-pages.git] / draft / man2 / fcntl.2
1 .\" t
2 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
3 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
4 .\" and Copyright (C) 1998 Jamie Lokier;
5 .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk;
6 .\" and Copyright (C) 2014 Jeff Layton
7 .\"
8 .\" %%%LICENSE_START(VERBATIM)
9 .\" Permission is granted to make and distribute verbatim copies of this
10 .\" manual provided the copyright notice and this permission notice are
11 .\" preserved on all copies.
12 .\"
13 .\" Permission is granted to copy and distribute modified versions of this
14 .\" manual under the conditions for verbatim copying, provided that the
15 .\" entire resulting derived work is distributed under the terms of a
16 .\" permission notice identical to this one.
17 .\"
18 .\" Since the Linux kernel and libraries are constantly changing, this
19 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
20 .\" responsibility for errors or omissions, or for damages resulting from
21 .\" the use of the information contained herein.  The author(s) may not
22 .\" have taken the same level of care in the production of this manual,
23 .\" which is licensed free of charge, as they might when working
24 .\" professionally.
25 .\"
26 .\" Formatted or processed versions of this manual, if unaccompanied by
27 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" %%%LICENSE_END
29 .\"
30 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
31 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
32 .\" and again on 960413 and 980804 and 981223.
33 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
34 .\" Applied correction by Christian Ehrhardt - aeb, 990712
35 .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\"     Added note on F_SETFL and O_DIRECT
37 .\"     Complete rewrite + expansion of material on file locking
38 .\"     Incorporated description of F_NOTIFY, drawing on
39 .\"             Stephen Rothwell's notes in Documentation/dnotify.txt.
40 .\"     Added description of F_SETLEASE and F_GETLEASE
41 .\" Corrected and polished, aeb, 020527.
42 .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com>
43 .\"     Modified description of file leases: fixed some errors of detail
44 .\"     Replaced the term "lease contestant" by "lease breaker"
45 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
46 .\"     Added notes on capability requirements
47 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
48 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
49 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
50 .\"     Described behavior of F_SETOWN/F_SETSIG in
51 .\"     multithreaded processes, and generally cleaned
52 .\"     up the discussion of F_SETOWN.
53 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
54 .\"     mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
55 .\"     and earlier.  Added text on permissions required to send signal.
56 .\" 2009-09-30, Michael Kerrisk
57 .\"     Note obsolete F_SETOWN behavior with threads.
58 .\"     Document F_SETOWN_EX and F_GETOWN_EX
59 .\" 2010-06-17, Michael Kerrisk
60 .\"     Document F_SETPIPE_SZ and F_GETPIPE_SZ.
61 .\"
62 .\"*******************************************************************
63 .\"
64 .\" This file was generated with po4a. Translate the source file.
65 .\"
66 .\"*******************************************************************
67 .\"
68 .\" Japanese Version Copyright (c) 1996 Takeshi Ueno
69 .\" and Copyright (c) 2005, 2006, 2008 Akihiro MOTOKI
70 .\" Translated 1996-07-03, Takeshi Ueno <tueno@vio.co.jp>
71 .\" Modified 1998-09-10, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
72 .\" Modified 1999-08-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
73 .\" Updated & Modified 2001-04-03, Yuichi SATO <ysato@h4.dion.ne.jp>
74 .\" Updated & Modified 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
75 .\" Updated & Modified 2005-04-22, Akihiro MOTOKI
76 .\" Updated & Modified 2005-10-14, Akihiro MOTOKI
77 .\" Updated & Modified 2005-11-19, Akihiro MOTOKI, LDP v2.14
78 .\" Updated 2006-04-16, Akihiro MOTOKI, LDP v2.29
79 .\" Updated 2008-02-11, Akihiro MOTOKI, LDP v2.77
80 .\" Updated 2008-09-19, Akihiro MOTOKI, LDP v3.09
81 .\" Updated 2010-04-23, Akihiro MOTOKI, LDP v3.24
82 .\" Updated 2012-05-08, Akihiro MOTOKI <amotoki@gmail.com>
83 .\" Updated 2013-03-26, Akihiro MOTOKI <amotoki@gmail.com>
84 .\"
85 .TH FCNTL 2 2014\-09\-06 Linux "Linux Programmer's Manual"
86 .SH 名前
87 fcntl \- ファイルディスクリプタの操作を行う
88 .SH 書式
89 .nf
90 \fB#include <unistd.h>\fP
91 \fB#include <fcntl.h>\fP
92 .sp
93 \fBint fcntl(int \fP\fIfd\fP\fB, int \fP\fIcmd\fP\fB, ... /* \fP\fIarg\fP\fB */ );\fP
94 .fi
95 .SH 説明
96 \fBfcntl\fP()  は、オープンされたファイルディスクリプタ \fIfd\fP に関して下記の操作を行う。操作は \fIcmd\fP によって決まる:
97
98 \fBfcntl\fP() はオプションとして第三引き数をとることができる。 第三引き数が必要
99 かどうかは \fIcmd\fP により決まる。必要な引き数の型は \fIcmd\fP 名の後ろの括弧内で
100 指定されている (ほとんどの場合、必要な型は \fIint\fP であり、この引き数を表すの
101 に \fIarg\fP という名前を使っている)。引き数が必要ない場合には \fIvoid\fP が指定さ
102 れている。
103
104 Certain of the operations below are supported only since a particular Linux
105 kernel version.  The preferred method of checking whether the host kernel
106 supports a particular operation is to invoke \fBfcntl\fP()  with the desired
107 \fIcmd\fP value and then test whether the call failed with \fBEINVAL\fP,
108 indicating that the kernel does not recognize this value.
109 .SS ファイルディスクリプタの複製
110 .TP 
111 \fBF_DUPFD\fP (\fIint\fP)
112 利用可能なファイルディスクリプタのうち、 \fIarg\fP 以上で最小のものを探し、 \fIfd\fP のコピーとする。これは別の形の \fBdup2\fP(2)
113 である。 \fBdup2\fP(2)  では指定されたディスクリプタが使われる点が違う。
114 .IP
115 成功すると、新しいディスクリプタが返される。
116 .IP
117 詳細は \fBdup\fP(2)  を参照のこと。
118 .TP 
119 \fBF_DUPFD_CLOEXEC\fP (\fIint\fP; Linux 2.6.24 以降)
120 \fBF_DUPFD\fP と同様だが、それに加えて複製されたディスクリプタに対して close\-on\-exec フラグをセットする。
121 このフラグを指定することで、プログラムは \fBFD_CLOEXEC\fP フラグをセットするために \fBfcntl\fP()  の \fBF_SETFD\fP
122 操作を追加で行う必要がなくなる。 このフラグがなぜ有用かについては、 \fBopen\fP(2)  の \fBO_CLOEXEC\fP の説明を参照のこと。
123 .SS ファイルディスクリプタフラグ
124 以下のコマンドを使って、ファイルディスクリプタに関連するフラグ を操作することができる。 現在のところ、定義されているフラグは一つだけである:
125 \fBFD_CLOEXEC\fP (close\-on\-exec フラグ)。 \fBFD_CLOEXEC\fP ビットが 0 なら、ファイルディスクリプタは
126 \fBexecve\fP(2)  を行ってもオープンされたままだが、そうでない場合はクローズされる。
127 .TP 
128 \fBF_GETFD\fP (\fIvoid\fP)
129 ファイルディスクリプタフラグを読み出す。 \fIarg\fP は無視される。
130 .TP 
131 \fBF_SETFD\fP (\fIint\fP)
132 ファイルディスクリプタフラグに \fIarg\fP で指定した値を設定する。
133 .PP
134 マルチスレッドプログラムでは、 \fBfcntl\fP() の \fBF_SETFD\fP を使って close\-on\-exec フラグを設定するのと同時に、
135 別のスレッドで \fBexecve\fP(2) と \fBfork\fP(2) を実行することは、競合条件次第では、
136 そのファイルディスクリプタが子プロセスで実行されるプログラムに意図せず見えてしまうという危険性がある。 詳細とこの問題への対処法については
137 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの議論を参照のこと。
138 .SS ファイル状態フラグ
139 .\" or
140 .\" .BR creat (2),
141 オープンファイル記述 (open file description) には、 ファイル記述毎に設定される状態フラグがいくつかある。これらのフラグは
142 \fBopen\fP(2)  によって初期化され、 \fBfcntl\fP(2)  により変更することもできる。これらは、 (\fBdup\fP(2),
143 \fBfcntl\fP(F_DUPFD), \fBfork\fP(2)  などで) 複製されたファイルディスクリプタ同士は 同じオープンファイル記述を参照する。
144 そのため、 同じファイル状態フラグが共有される。
145
146 ファイル状態フラグとその意味は \fBopen\fP(2)  で説明されている。
147 .TP 
148 \fBF_GETFL\fP (\fIvoid\fP)
149 ファイルのアクセスモードとファイル状態フラグを取得する。
150 \fIarg\fP は無視される。
151 .TP 
152 \fBF_SETFL\fP (\fIint\fP)
153 ファイル状態フラグに \fIarg\fP で指定された値を設定する。 \fIarg\fP のうち、ファイルのアクセスモード (\fBO_RDONLY\fP,
154 \fBO_WRONLY\fP, \fBO_RDWR\fP)  とファイル作成フラグ (すなわち \fBO_CREAT\fP, \fBO_EXCL\fP,
155 \fBO_NOCTTY\fP, \fBO_TRUNC\fP)  に関するビットは無視される。 Linux では、このコマンドで変更できるのは
156 \fBO_APPEND\fP, \fBO_ASYNC\fP, \fBO_DIRECT\fP, \fBO_NOATIME\fP, \fBO_NONBLOCK\fP
157 フラグだけである。フラグ \fBO_DSYNC\fP, \fBO_SYNC\fP を変更することはできない。下記の「バグ」を参照。
158 .SS "Advisory record locking"
159 Linux implements traditional ("process\-associated") UNIX record locks, as
160 standardized by POSIX.  For a Linux\-specific alternative with better
161 semantics, see the discussion of open file description locks below.
162
163 \fBF_SETLK\fP, \fBF_SETLKW\fP, \fBF_GETLK\fP は、レコードロックの獲得/解放/テストのために使用する
164 (レコードロックは、バイト範囲ロック、ファイルセグメントロック、ファイル領域ロックとも呼ばれる)。 三番目の引き数 \fIlock\fP
165 は、以下に示すフィールドを含む構造体へのポインタである (フィールドの順序は関係なく、構造体に他のフィールドがあってもよい)。
166 .in +4n
167 .nf
168 .sp
169 struct flock {
170     ...
171     short l_type;    /* Type of lock: F_RDLCK,
172                         F_WRLCK, F_UNLCK */
173     short l_whence;  /* How to interpret l_start:
174                         SEEK_SET, SEEK_CUR, SEEK_END */
175     off_t l_start;   /* Starting offset for lock */
176     off_t l_len;     /* Number of bytes to lock */
177     pid_t l_pid;     /* PID of process blocking our lock
178                         (set by F_GETLK and F_OFD_GETLK) */
179     ...
180 };
181 .fi
182 .in
183 .P
184 この構造体の \fIl_whence\fP, \fIl_start\fP, \fIl_len\fP フィールドで、ロックを行いたいバイト範囲を指定する。
185 ファイルの末尾より後ろのバイトをロックすることはできるが、 ファイルの先頭より前のバイトをロックすることはできない。
186
187 \fIl_start\fP はロックを行う領域の開始オフセットである。 その意味は \fIl_whence\fP により異なる: \fIl_whence\fP が
188 \fBSEEK_SET\fP の場合はファイルの先頭からのオフセット、 \fIl_whence\fP が \fBSEEK_CUR\fP
189 の場合は現在のファイルオフセットからのオフセット、 \fIl_whence\fP が \fBSEEK_END\fP
190 の場合はファイルの末尾からのオフセットと解釈される。 後ろの2つの場合には、 ファイルの先頭より前にならない範囲で、 \fIl_start\fP
191 に負の値を指定することができる。
192
193 \fIl_len\fP はロックしたいバイト数を示す。 \fIl_len\fP が正の場合、ロックされるバイト範囲は \fIl_start\fP 以上
194 \fIl_start\fP+\fIl_len\fP\-1 以下となる。 \fIl_len\fP に 0 を指定した場合は特別な意味を持つ: \fIl_whence\fP and
195 \fIl_start\fP で指定される位置からファイルの末尾までの全てのバイトをロックする
196 (ファイルがどんなに大きくなったとしてもファイルの末尾までロックする)。
197
198 POSIX.1\-2001 では、負の値の \fIl_len\fP をサポートする実装を認めている (必須ではない)。 \fIl_len\fP
199 が負の場合、ロックされるバイト範囲は \fIl_start\fP+\fIl_len\fP 以上 \fIl_start\fP\-1 以下となる。 この動作はカーネル
200 2.4.21 以降および 2.5.49 以降の Linux で サポートされている。
201
202 \fIl_type\fP フィールドは、ファイルに対して読み出しロック (\fBF_RDLCK\fP)  と書き込みロック (\fBF_WRLCK\fP)  のどちらを
203 設定するかを指定する。 ファイルのある領域に対して、読み出しロック (共有ロック) を保持できる プロセス数に制限はないが、書き込みロック
204 (排他ロック) を保持できる のは一つのプロセスだけである。排他ロックを設定すると、(共有ロックか 排他ロックにかかわらず)
205 他のロックは何も設定できない。 一つのプロセスは、ファイルのある領域に対して一種類のロックしか保持できない。
206 新規のロックがロックが設定されている領域に対して適用されると、既存のロック は新規のロックの種別に変換される
207 (新規のロックで指定されたバイト範囲が既存ロックの範囲と一致する場合以外では、 変換の過程で既存のロックの分割、縮小、結合が行われることがある)。
208 .TP 
209 \fBF_SETLK\fP (\fIstruct flock *\fP)
210 Acquire a lock (when \fIl_type\fP is \fBF_RDLCK\fP or \fBF_WRLCK\fP)  or release a
211 lock (when \fIl_type\fP is \fBF_UNLCK\fP)  on the bytes specified by the
212 \fIl_whence\fP, \fIl_start\fP, and \fIl_len\fP fields of \fIlock\fP.  If a conflicting
213 lock is held by another process, this call returns \-1 and sets \fIerrno\fP to
214 \fBEACCES\fP or \fBEAGAIN\fP.  (The error returned in this case differs across
215 implementations, so POSIX requires a portable application to check for both
216 errors.)
217 .TP 
218 \fBF_SETLKW\fP (\fIstruct flock *\fP)
219 \fBF_SETLK\fP と同様だが、こちらではそのファイルに対して衝突するロックが 適用されていた場合に、そのロックが解放されるのを待つ点が異なる。
220 待っている間にシグナルを受けた場合は、システムコールは中断され、 (シグナルハンドラが戻った直後に) 返り値 \-1 を返す (また \fIerrno\fP に
221 \fBEINTR\fP が設定される; \fBsignal\fP(7)  参照)。
222 .TP 
223 \fBF_GETLK\fP (\fIstruct flock *\fP)
224 On input to this call, \fIlock\fP describes a lock we would like to place on
225 the file.  If the lock could be placed, \fBfcntl\fP()  does not actually place
226 it, but returns \fBF_UNLCK\fP in the \fIl_type\fP field of \fIlock\fP and leaves the
227 other fields of the structure unchanged.
228
229 If one or more incompatible locks would prevent this lock being placed, then
230 \fBfcntl\fP()  returns details about one of those locks in the \fIl_type\fP,
231 \fIl_whence\fP, \fIl_start\fP, and \fIl_len\fP fields of \fIlock\fP.  If the conflicting
232 lock is a traditional (process\-associated) record lock, then the \fIl_pid\fP
233 field is set to the PID of the process holding that lock.  If the
234 conflicting lock is an open file description lock, then \fIl_pid\fP is set to
235 \-1.  Note that the returned information may already be out of date by the
236 time the caller inspects it.
237 .P
238 読み出しロックを適用するには、 \fIfd\fP は読み出し用にオープンされていなければならない。 書き込みロックを適用するには、 \fIfd\fP
239 は書き込み用にオープンされていなければならない。 読み書き両方のロックを適用するには、読み書き両用で ファイルをオープンしなければならない。
240
241 When placing locks with \fBF_SETLKW\fP, the kernel detects \fIdeadlocks\fP,
242 whereby two or more processes have their lock requests mutually blocked by
243 locks held by the other processes.  For example, suppose process A holds a
244 write lock on byte 100 of a file, and process B holds a write lock on byte
245 200.  If each process then attempts to lock the byte already locked by the
246 other process using \fBF_SETLKW\fP, then, without deadlock detection, both
247 processes would remain blocked indefinitely.  When the kernel detects such
248 deadlocks, it causes one of the blocking lock requests to immediately fail
249 with the error \fBEDEADLK\fP; an application that encounters such an error
250 should release some of its locks to allow other applications to proceed
251 before attempting regain the locks that it requires.  Circular deadlocks
252 involving more than two processes are also detected.  Note, however, that
253 there are limitations to the kernel's deadlock\-detection algorithm; see
254 BUGS.
255
256 As well as being removed by an explicit \fBF_UNLCK\fP, record locks are
257 automatically released when the process terminates.
258 .P
259 レコードのロックは \fBfork\fP(2)  で作成された子プロセスには継承されないが、 \fBexecve\fP(2)  の前後では保存される。
260 .P
261 \fBstdio\fP(3)  ではバッファリングが行われるので、 stdio 関連の関数ではレコードのロックの使用は回避される; 代わりに
262 \fBread\fP(2)  や \fBwrite\fP(2)  を使用すること。
263
264 The record locks described above are associated with the process (unlike the
265 open file description locks described below).  This has some unfortunate
266 consequences:
267 .IP * 3
268 .\" (Additional file descriptors referring to the same file
269 .\" may have been obtained by calls to
270 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
271 If a process closes \fIany\fP file descriptor referring to a file, then all of
272 the process's locks on that file are released, regardless of the file
273 descriptor(s) on which the locks were obtained.  This is bad: it means that
274 a process can lose its locks on a file such as \fI/etc/passwd\fP or
275 \fI/etc/mtab\fP when for some reason a library function decides to open, read,
276 and close the same file.
277 .IP *
278 The threads in a process share locks.  In other words, a multithreaded
279 program can't use record locking to ensure that threads don't simultaneously
280 access the same region of a file.
281 .PP
282 Open file description locks solve both of these problems.
283 .SS "Open file description locks (non\-POSIX)"
284 Open file description locks are advisory byte\-range locks whose operation is
285 in most respects identical to the traditional record locks described above.
286 This lock type is Linux\-specific, and available since Linux 3.15.  For an
287 explanation of open file descriptions, see \fBopen\fP(2).
288
289 The principal difference between the two lock types is that whereas
290 traditional record locks are associated with a process, open file
291 description locks are associated with the open file description on which
292 they are acquired, much like locks acquired with \fBflock\fP(2).  Consequently
293 (and unlike traditional advisory record locks), open file description locks
294 are inherited across \fBfork\fP(2)  (and \fBclone\fP(2)  with \fBCLONE_FILES\fP), and
295 are only automatically released on the last close of the open file
296 description, instead of being released on any close of the file.
297 .PP
298 Open file description locks always conflict with traditional record locks,
299 even when they are acquired by the same process on the same file descriptor.
300
301 Open file description locks placed via the same open file description (i.e.,
302 via the same file descriptor, or via a duplicate of the file descriptor
303 created by \fBfork\fP(2), \fBdup\fP(2), \fBfcntl\fP(2)  \fBF_DUPFD\fP, and so on) are
304 always compatible: if a new lock is placed on an already locked region, then
305 the existing lock is converted to the new lock type.  (Such conversions may
306 result in splitting, shrinking, or coalescing with an existing lock as
307 discussed above.)
308
309 On the other hand, open file description locks may conflict with each other
310 when they are acquired via different open file descriptions.  Thus, the
311 threads in a multithreaded program can use open file description locks to
312 synchronize access to a file region by having each thread perform its own
313 \fBopen\fP(2)  on the file and applying locks via the resulting file
314 descriptor.
315 .PP
316 As with traditional advisory locks, the third argument to \fBfcntl\fP(),
317 \fIlock\fP, is a pointer to an \fIflock\fP structure.  By contrast with
318 traditional record locks, the \fIl_pid\fP field of that structure must be set
319 to zero when using the commands described below.
320
321 The commands for working with open file description locks are analogous to
322 those used with traditional locks:
323 .TP 
324 \fBF_OFD_SETLK\fP (\fIstruct flock *\fP)
325 Acquire an open file description lock (when \fIl_type\fP is \fBF_RDLCK\fP or
326 \fBF_WRLCK\fP)  or release an open file description lock (when \fIl_type\fP is
327 \fBF_UNLCK\fP)  on the bytes specified by the \fIl_whence\fP, \fIl_start\fP, and
328 \fIl_len\fP fields of \fIlock\fP.  If a conflicting lock is held by another
329 process, this call returns \-1 and sets \fIerrno\fP to \fBEAGAIN\fP.
330 .TP 
331 \fBF_OFD_SETLKW\fP (\fIstruct flock *\fP)
332 \fBF_OFD_SETLK\fP と同様だが、こちらではそのファイルに対して衝突するロックが
333 適用されていた場合に、そのロックが解放されるのを待つ点が異なる。 待っている間にシグナルを受けた場合は、システムコールは中断され、
334 (シグナルハンドラが戻った直後に) 返り値 \-1 を返す (また \fIerrno\fP に \fBEINTR\fP が設定される; \fBsignal\fP(7)
335 参照)。
336 .TP 
337 \fBF_OFD_GETLK\fP (\fIstruct flock *\fP)
338 On input to this call, \fIlock\fP describes an open file description lock we
339 would like to place on the file.  If the lock could be placed, \fBfcntl\fP()
340 does not actually place it, but returns \fBF_UNLCK\fP in the \fIl_type\fP field of
341 \fIlock\fP and leaves the other fields of the structure unchanged.  If one or
342 more incompatible locks would prevent this lock being placed, then details
343 about one of these locks are returned via \fIlock\fP, as described above for
344 \fBF_GETLK\fP.
345 .PP
346 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
347 .\"
348 In the current implementation, no deadlock detection is performed for open
349 file description locks.  (This contrasts with process\-associated record
350 locks, for which the kernel does perform deadlock detection.)
351 .SS "強制ロック (mandatory locking)"
352 \fI警告\fP: Linux の強制ロックの実装は信頼性に欠けるものである。 下記の「バグ」の節を参照のこと。
353
354 By default, both traditional (process\-associated) and open file description
355 record locks are advisory.  Advisory locks are not enforced and are useful
356 only between cooperating processes.
357
358 Both lock types can also be mandatory.  Mandatory locks are enforced for all
359 processes.  If a process tries to perform an incompatible access (e.g.,
360 \fBread\fP(2)  or \fBwrite\fP(2))  on a file region that has an incompatible
361 mandatory lock, then the result depends upon whether the \fBO_NONBLOCK\fP flag
362 is enabled for its open file description.  If the \fBO_NONBLOCK\fP flag is not
363 enabled, then the system call is blocked until the lock is removed or
364 converted to a mode that is compatible with the access.  If the
365 \fBO_NONBLOCK\fP flag is enabled, then the system call fails with the error
366 \fBEAGAIN\fP.
367
368 強制ロックを使用するためには、ロック対象のファイルが含まれるファイルシステム
369 と、ロック対象のファイル自身の両方について、強制ロックが有効になっていなけれ ばならない。ファイルシステムについて強制ロックを有効にするには、
370 \fBmount\fP(8)  に "\-o mand" オプションを渡すか、 \fBmount\fP(2)  に \fBMS_MANDLOCK\fP
371 フラグを指定する。ファイルについて強制ロックを有効にするには、 そのファイルのグループ実行許可 (group execute permission)
372 を無効とし、 かつ set\-group\-ID 許可ビットを有効にする (\fBchmod\fP(1)  と \fBchmod\fP(2)  を参照)。
373
374 Mandatory locking is not specified by POSIX.  Some other systems also
375 support mandatory locking, although the details of how to enable it vary
376 across systems.
377 .SS シグナルの管理
378 \fBF_GETOWN\fP, \fBF_SETOWN\fP, \fBF_GETOWN_EX\fP, \fBF_SETOWN_EX\fP, \fBF_GETSIG\fP,
379 \fBF_SETSIG\fP は、I/O が利用可能になったことを示すシグナルを管理するために使用される。
380 .TP 
381 \fBF_GETOWN\fP (\fIvoid\fP)
382 ファイルディスクリプタ \fIfd\fP のイベントに対するシグナル \fBSIGIO\fP および \fBSIGURG\fP を受けているプロセスのプロセスID
383 かプロセスグループを (関数の結果として) 返す。 プロセスID は正の値として返される。 プロセスグループID は負の値として返される
384 (下記のバグの章を参照)。 \fIarg\fP は無視される。
385 .TP 
386 \fBF_SETOWN\fP (\fIint\fP)
387 ファイルディスクリプタ \fIfd\fP のイベント発生を知らせるシグナル \fBSIGIO\fP や \fBSIGURG\fP を受けるプロセスの プロセス ID
388 またはプロセスグループID を \fIarg\fP で指定された ID に設定する。 プロセスID は正の値として指定し、 プロセスグループID
389 は負の値として指定する。 ほとんどの場合、呼び出し元プロセスは所有者として自分自身を指定する (つまり \fIarg\fP に \fBgetpid\fP(2)
390 を指定する)。
391
392 .\" From glibc.info:
393 \fBfcntl\fP()  の \fBF_SETFL\fP コマンドを使用してファイルディスクリプタに \fBO_ASYNC\fP
394 状態フラグを設定した場合には、そのファイルディスクリプタへの 入出力が可能になる度に \fBSIGIO\fP シグナルが送られる。 \fBF_SETSIG\fP は
395 \fBSIGIO\fP 以外の別のシグナルの配送を受けられるように するのにも使うことができる。 許可 (permission)
396 のチェックで失敗した場合には、 シグナルは黙って捨てられる。
397
398 \fBF_SETOWN\fP により指定された所有者のプロセス (またはプロセスグループ) に シグナルを送る際には、 \fBkill\fP(2)
399 に書かれているのと同じ許可のチェックが行われる。 このとき、シグナルを送信するプロセスは \fBF_SETOWN\fP を使ったプロセスである
400 (但し、下記の「バグ」の章を参照のこと)。
401
402 .\" The following appears to be rubbish.  It doesn't seem to
403 .\" be true according to the kernel source, and I can write
404 .\" a program that gets a terminal-generated SIGIO even though
405 .\" it is not the foreground process group of the terminal.
406 .\" -- MTK, 8 Apr 05
407 .\"
408 .\" If the file descriptor
409 .\" .I fd
410 .\" refers to a terminal device, then SIGIO
411 .\" signals are sent to the foreground process group of the terminal.
412 ファイルディスクリプタがソケットを参照している場合は、 \fBF_SETOWN\fP を使用して、ソケットに帯域外 (out\-of\-band)
413 データが届いた時に \fBSIGURG\fP シグナルを配送する相手を選択することもできる (\fBSIGURG\fP が送られた場合には \fBselect\fP(2)
414 がソケットが「特別な状態」にあると報告することだろう)。
415
416 バージョン 2.6.11 以前の 2.6.x カーネルでは、以下に示す動作であった。
417 .RS
418 .IP
419 .\" The relevant place in the (2.6) kernel source is the
420 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
421 .\" send_sigurg()/send_sigurg_to_task() bypasses
422 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
423 .\" to directly call send_group_sig_info()
424 .\"     -- MTK, Apr 2005 (kernel 2.6.11)
425 スレッドグループをサポートしているスレッドライブラリ (例えば NPTL) を 使って動作しているマルチスレッドプロセスで \fBF_SETSIG\fP に
426 0 以外の値を指定した場合、 \fBF_SETOWN\fP に正の値を渡すと、その意味が違ってくる: プロセス全体を示すプロセスID
427 ではなく、プロセス内の特定の スレッドを示すスレッドID と解釈される。 したがって、 \fBF_SETSIG\fP
428 を使う場合には、きちんと結果を受け取るには、 \fBF_SETOWN\fP に渡す値を \fBgetpid\fP(2)  ではなく \fBgettid\fP(2)
429 の返り値にする必要があるだろう。 (現状の Linux スレッド実装では、メインスレッドのスレッドID は そのスレッドのプロセスID
430 と同じである。つまり、 シグナルスレッドのプログラムではこの場合 \fBgettid\fP(2)  と \fBgetpid\fP(2)
431 は全く同じように使うことができる。)  ただし、注意すべき点として、この段落で述べたことは、 ソケットの帯域外データが届いたときに生成される
432 \fBSIGURG\fP シグナルにはあてはまらない。 このシグナルは常にプロセスかプロセスグループに送られ、 送信先は \fBF_SETOWN\fP
433 に渡された値にしたがって決められる。
434 .RE
435 .IP
436 上記の動作は、Linux 2.6.12 で図らずも削除され、 元に戻されない予定である。 Linux 2.6.32 以降で、特定のスレッド宛にシグナル
437 \fBSIGIO\fP と \fBSIGURG\fP を送るには \fBF_SETOWN_EX\fP を使うこと。
438 .TP 
439 \fBF_GETOWN_EX\fP (struct f_owner_ex *) (Linux 2.6.32 以降)
440 直前の \fBF_SETOWN_EX\fP 操作で定義された現在のファイルディスクリプタの所有者設定 を返す。情報は \fIarg\fP
441 が指す構造体に格納されて返される。構造体は以下の通りである。
442 .nf
443 .in +4n
444
445 struct f_owner_ex {
446     int   type;
447     pid_t pid;
448 };
449
450 .in
451 .fi
452 \fItype\fP フィールドは、 \fBF_OWNER_TID ,\fP \fBF_OWNER_PID ,\fP \fBF_OWNER_PGRP\fP
453 のいずれか一つの値となる。 \fIpid\fP フィールドは、スレッド ID、プロセス ID、プロセスグループ ID を 表す正の整数である。詳細は
454 \fBF_SETOWN_EX\fP を参照。
455 .TP 
456 \fBF_SETOWN_EX\fP (struct f_owner_ex *) (Linux 2.6.32 以降)
457 この操作は \fBF_SETOWN\fP と同様の処理を行う。 この操作を使うと、I/O が利用可能になったことを示すシグナルを、
458 特定のスレッド、プロセス、プロセスグループに送ることができる ようになる。 呼び出し元は、 \fIarg\fP 経由でシグナルの配送先を指定する。
459 \fIarg\fP は \fIf_owner_ex\fP 構造体へのポインタである。 \fItype\fP フィールドは以下のいずれかの値を取り、 この値により
460 \fIpid\fP がどのように解釈されるかが規定される。
461 .RS
462 .TP 
463 \fBF_OWNER_TID\fP
464 スレッド ID が \fIpid\fP で指定された値のスレッドにそのシグナルを送る (スレッド ID は \fBclone\fP(2)  や
465 \fBgettid\fP(2)  の呼び出しで返される値である)。
466 .TP 
467 \fBF_OWNER_PID\fP
468 ID が \fIpid\fP で指定された値のプロセスにそのシグナルを送る。
469 .TP 
470 \fBF_OWNER_PGRP\fP
471 ID が \fIpid\fP で指定された値のプロセスグループにそのシグナルを送る。 (\fBF_SETOWN\fP と異なり、プロセスグループ ID
472 には正の値を指定する点に注意すること。)
473 .RE
474 .TP 
475 \fBF_GETSIG\fP (\fIvoid\fP)
476 入力や出力が可能になった場合に送るシグナルを (関数の結果として) 返す。 値ゼロは \fBSIGIO\fP を送ることを意味する。 (\fBSIGIO\fP
477 を含む) 他の値はいずれも、 \fBSIGIO\fP の代わりに送るシグナル番号を表す。 後者の場合、シグナルハンドラを \fBSA_SIGINFO\fP
478 フラグ付きで設定すれば、ハンドラで追加の情報を得ることができる。 \fIarg\fP は無視される。
479 .TP 
480 \fBF_SETSIG\fP (\fIint\fP)
481 .\"
482 .\" The following was true only up until 2.6.11:
483 .\"
484 .\" Additionally, passing a nonzero value to
485 .\" .B F_SETSIG
486 .\" changes the signal recipient from a whole process to a specific thread
487 .\" within a process.
488 .\" See the description of
489 .\" .B F_SETOWN
490 .\" for more details.
491 入力や出力が可能になった場合に送るシグナルを \fIarg\fP に指定された値に設定する。 値ゼロは \fBSIGIO\fP を送ることを意味する。
492 (\fBSIGIO\fP を含む) 他の値はいずれも、 \fBSIGIO\fP の代わりに送るシグナル番号を表す。 後者の場合、シグナルハンドラを
493 \fBSA_SIGINFO\fP フラグ付きで設定すれば、 ハンドラで追加の情報を得ることができる。
494
495 \fBF_SETSIG\fP にゼロ以外の値を設定し、シグナルハンドラに \fBSA_SIGINFO\fP フラグを設定すると、 (\fBsigaction\fP(2)
496 を参照) I/O イベントに関する追加の情報が \fIsiginfo_t\fP 構造体でシグナルハンドラへ渡される。 \fIsi_code\fP
497 フィールドが示すシグナルの原因が \fBSI_SIGIO\fP である場合、 \fIsi_fd\fP
498 フィールドにはイベントに対応するファイルディスクリプタが入っている。 それ以外の場合は、どのファイルディスクリプタが利用可能かを示す情報は
499 ないので、どのファイルディスクリプタで I/O が可能かを判断するためには 通常の機構 (\fBselect\fP(2), \fBpoll\fP(2),
500 \fBO_NONBLOCK\fP を設定した \fBread\fP(2)  など) を使用しなければならない。
501
502 リアルタイムシグナル (値が \fBSIGRTMIN\fP 以上) を選択している場合は、 同じシグナル番号を持つ複数の I/O
503 イベントがキューに入ることがある (キューに入れるかどうかは利用可能なメモリに依存している)。 上記と同様、 \fBSA_SIGINFO\fP
504 が設定されている場合、シグナルハンドラのための追加の情報が得られる。
505
506 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
507 以下の点に注意すること。 Linux では一つのプロセスに対してキューに入れられるリアルタイム シグナルの数に上限が設けられており
508 (\fBgetrlimit\fP(2)  と \fBsignal\fP(7)  を参照)、この上限に達するとカーネルは \fBSIGIO\fP シグナルを配送する。この
509 \fBSIGIO\fP シグナルは、指定されたスレッドではなくプロセス全体に送られる。
510 .PP
511 これらの機構を使用することで、ほとんどの場合で \fBselect\fP(2)  や \fBpoll\fP(2)  を使用せずに完全な非同期 I/O
512 を実装することができる。
513 .PP
514 \fBO_ASYNC\fP の使用方法は BSD と Linux に特有である。 POSIX.1 で規定されている \fBF_GETOWN\fP と
515 \fBF_SETOWN\fP の使用方法は、ソケットに対する \fBSIGURG\fP シグナルとの組み合わせだけである (POSIX は \fBSIGIO\fP
516 シグナルは規定していない)。 \fBF_GETOWN_EX\fP, \fBF_SETOWN_EX\fP, \fBF_GETSIG\fP, \fBF_SETSIG\fP は
517 Linux 固有である。POSIX には、同様のことを行うために、非同期 I/O と \fIaio_sigevent\fP 構造体がある。Linux
518 では、GNU C ライブラリ (Glibc) の一部として これらも利用可能である。
519 .SS "リース (leases)"
520 (Linix 2.4 以降で利用可能)  \fBF_SETLEASE\fP は、 \fIfd\fP
521 が参照するオープンファイル記述に対して新しいリースを設定するのに使用される。 \fBF_GETLEASE\fP は、 \fIfd\fP
522 が参照するオープンファイル記述に対して設定されている 現在のリースを取得するのに使用される。 ファイルのリースにより、 あるプロセス ("lease
523 breaker") がそのファイルディスクリプタが参照 しているファイルに対して \fBopen\fP(2)  や \fBtruncate\fP(2)
524 を行おうとした際に、リースを保持しているプロセス ("lease holder") へ (シグナルの配送による) 通知が行われるという機構が提供される。
525 .TP 
526 \fBF_SETLEASE\fP (\fIint\fP)
527 \fIarg\fP の内容に基いてファイルのリースの設定、削除を行う。整数 \fIarg\fP には以下の値が指定できる:
528 .RS
529 .TP 
530 \fBF_RDLCK\fP
531 .\" The following became true in kernel 2.6.10:
532 .\" See the man-pages-2.09 Changelog for further info.
533 読み出しリースを取得する。これにより、 そのファイルが書き込み用にオープンされたり、ファイルが切り詰められた場合に、
534 呼び出し元のプロセスに通知が行われるようになる。 読み出しリースを設定できるのは、読み出し専用でオープンされている
535 ファイルディスクリプタに対してのみである。
536 .TP 
537 \fBF_WRLCK\fP
538 書き込みリースを取得する。これにより、 (読み出し用か書き込み用にかかわらず) そのファイルがオープンされたり、
539 ファイルが切り詰められた場合に、呼び出し元のプロセスに通知が行われるようになる。
540 書き込みリースは、そのファイルに対するオープンされたファイルディスクリプタが 他にない場合にのみ設定できる。
541 .TP 
542 \fBF_UNLCK\fP
543 そのファイルからリースを削除する。
544 .RE
545 .P
546 リースはオープンファイル記述に対して関連付けられる (\fBopen\fP(2)  参照)。 つまり、 (\fBfork\fP(2)  や \fBdup\fP(2)
547 などにより作成された) ファイルディスクリプタの複製は同じリースを参照し、 複製も含めたどのファイルディスクリプタを使ってもこのリースを変更したり
548 解放したりできる。 また、これらのファイルディスクリプタのいずれかに対して \fBF_UNLCK\fP
549 操作が明示的に実行された場合や、すべてのファイルディスクリプタが 閉じられた場合にも、リースは解放される。
550 .P
551 リースの取得は通常のファイル (regular file) に対してのみ可能である。 非特権プロセスがリースを取得できるのは、UID (所有者)
552 がプロセスの ファイルシステム UID と一致するファイルに対してだけである。 \fBCAP_LEASE\fP
553 ケーパビリティを持つプロセスは任意のファイルに対してリースを取得できる。
554 .TP 
555 \fBF_GETLEASE\fP (\fIvoid\fP)
556 ファイルディスクリプタ \fIfd\fP に対して設定されているリースの種別を取得する。 \fBF_RDLCK\fP, \fBF_WRLCK\fP, \fBF_UNLCK\fP
557 のいずれかが返される。 \fBF_RDLCK\fP, \fBF_WRLCK\fP はそれぞれ、読み出しリース、書き込みリースが設定されていることを示し、
558 \fBF_UNLCK\fP はリースが何も設定されていないことを示す。 \fIarg\fP は無視される。
559 .PP
560 あるプロセス ("lease breaker") が \fBF_SETLEASE\fP で設定されたリースと矛
561 盾するような \fBopen\fP(2) や \fBtruncate\fP(2) を実行した場合、 そのシステム
562 コールはカーネルによって停止され、 カーネルは lease holder にシグナル
563 (デフォルトでは \fBSIGIO\fP) を送って通知を行う。 lease holder はこのシグ
564 ナルを受信したときにはきちんと対応すべきである。 具体的には、別のプロセ
565 スがそのファイルにアクセスするための準備として 必要な後片付け (例えば、
566 キャッシュされたバッファのフラッシュ) を すべて行ってから、そのファイル
567 のリースの削除または格下げを行う。リースを削除をするには、 \fIarg\fP に
568 \fBF_UNLCK\fP を指定して \fBF_SETLEASE\fP を実行する。lease holder がファイル
569 に書き込みリースを保持していて、 lease breaker が読み出し用にそのファイ
570 ルをオープンしている場合、 lease holder が保持しているリースを読み出し
571 リースに格下げすれば 十分である。これをするには、 \fIarg\fP に \fBF_RDLCK\fP
572 を指定して \fBF_SETLEASE\fP を実行する。
573
574 If the lease holder fails to downgrade or remove the lease within the number
575 of seconds specified in \fI/proc/sys/fs/lease\-break\-time\fP, then the kernel
576 forcibly removes or downgrades the lease holder's lease.
577
578 いったん lease break が開始されると、 lease holder が自発的にそのリース
579 の格下げか削除を行うか、lease break timer の満了後にカーネルが強制的に
580 リースの格下げか削除を行うまで、 \fBF_GETLEASE\fP は対象となるリースの型を
581 返す (リースの型は \fBF_RDLCK\fP か \fBF_UNLCK\fP のどちらであり、lease
582 breaker と互換性のある型となる)。
583
584 一度リースの削除か格下げが自発的もしくは強制的に行われると、 lease breaker がまだシステムコールを再開していない場合には、 カーネルが
585 lease breaker のシステムコールの続行を許可する。
586
587 lease breaker が実行した \fBopen\fP(2)  や \fBtruncate\fP(2)  が停止中にシグナルハンドラにより中断された場合、
588 そのシステムコールは \fBEINTR\fP エラーで失敗するが、上で述べた他の処理は そのまま行われる。 \fBopen\fP(2)  や
589 \fBtruncate\fP(2)  が停止中に lease breaker がシグナルにより kill された場合、 上で述べた他の処理はそのまま行われる。
590 lease breaker が \fBopen\fP(2)  を呼ぶ際に \fBO_NONBLOCK\fP フラグを指定した場合、そのシステムコールは
591 \fBEWOULDBLOCK\fP エラーで直ちに失敗するが、上で述べた他の処理はそのまま行われる。
592
593 lease holder への通知に使われるデフォルトのシグナルは \fBSIGIO\fP だが、 \fBfcntl\fP()  の \fBF_SETSIG\fP
594 コマンドで変更することができる。 \fBF_SETSIG\fP コマンドが実行され (\fBSIGIO\fP を指定された場合も含む)、 \fBSA_SIGINFO\fP
595 フラグ付きでシグナルハンドラが設定されている場合には、 ハンドラの第二引き数として \fIsiginfo_t\fP 構造体が渡され、この引き数の
596 \fIsi_fd\fP フィールドには別のプロセスがアクセスしたリース設定済みファイルの ディスクリプタが入っている
597 (この機能は複数のファイルに対してリースを設定する場合に有用である)。
598 .SS "ファイルやディレクトリの変更の通知 (dnotify)"
599 .TP 
600 \fBF_NOTIFY\fP (\fIint\fP)
601 (Linux 2.4 以降)  \fIfd\fP で参照されるディレクトリか、その中にあるファイルに変更があった場合に 通知を行う。どのイベントを通知するかは
602 \fIarg\fP で指定する。 \fIarg\fP はビットマスクで、以下のビットの 0個以上の論理和をとったものを指定する。
603 .RS
604 .sp
605 .PD 0
606 .TP  12
607 \fBDN_ACCESS\fP
608 A file was accessed (\fBread\fP(2), \fBpread\fP(2), \fBreadv\fP(2), and similar)
609 .TP 
610 \fBDN_MODIFY\fP
611 A file was modified (\fBwrite\fP(2), \fBpwrite\fP(2), \fBwritev\fP(2),
612 \fBtruncate\fP(2), \fBftruncate\fP(2), and similar).
613 .TP 
614 \fBDN_CREATE\fP
615 A file was created (\fBopen\fP(2), \fBcreat\fP(2), \fBmknod\fP(2), \fBmkdir\fP(2),
616 \fBlink\fP(2), \fBsymlink\fP(2), \fBrename\fP(2)  into this directory).
617 .TP 
618 \fBDN_DELETE\fP
619 A file was unlinked (\fBunlink\fP(2), \fBrename\fP(2)  to another directory,
620 \fBrmdir\fP(2)).
621 .TP 
622 \fBDN_RENAME\fP
623 A file was renamed within this directory (\fBrename\fP(2)).
624 .TP 
625 \fBDN_ATTRIB\fP
626 The attributes of a file were changed (\fBchown\fP(2), \fBchmod\fP(2),
627 \fButime\fP(2), \fButimensat\fP(2), and similar).
628 .PD
629 .RE
630 .IP
631 (上記の定義を利用するには、\fIどの\fP ヘッダファイルをインクルードするより前に、
632 \fB_GNU_SOURCE\fP 機能検査マクロを定義しなければならない。)
633
634 ディレクトリの変更通知は通常「一回限り (one\-shot)」であり、 アプリケーション側でその後さらに通知を受信したい場合は
635 再登録しなければならない。 \fIarg\fP に \fBDN_MULTISHOT\fP が含まれていた場合には、
636 変更通知は明示的に解除されるまで有効状態が継続する。
637
638 .\" The following does seem a poor API-design choice...
639 \fBF_NOTIFY\fP 要求は積算されていく。つまり、 \fIarg\fP で指定されたイベントがすでにモニタされている イベント集合に加算される形になる。
640 すべてのイベントの通知を無効にするには、 \fIarg\fP に 0 を指定して \fBF_NOTIFY\fP を呼び出す必要がある。
641
642 Notification occurs via delivery of a signal.  The default signal is
643 \fBSIGIO\fP, but this can be changed using the \fBF_SETSIG\fP command to
644 \fBfcntl\fP().  (Note that \fBSIGIO\fP is one of the nonqueuing standard signals;
645 switching to the use of a real\-time signal means that multiple notifications
646 can be queued to the process.)  In the latter case, the signal handler
647 receives a \fIsiginfo_t\fP structure as its second argument (if the handler was
648 established using \fBSA_SIGINFO\fP)  and the \fIsi_fd\fP field of this structure
649 contains the file descriptor which generated the notification (useful when
650 establishing notification on multiple directories).
651
652 特に \fBDN_MULTISHOT\fP を使う場合は、通知にはリアルタイムシグナルを使うべきである。
653 それは、リアルタイムシグナルを使うことで、複数の通知をキューに入れる ことができるからである。
654
655 \fB注意:\fP 新しくアプリケーションを書く際には、(カーネル 2.6.13 以降で利用可能となった)  \fIinotify\fP
656 インタフェースを使用すべきである。 \fIinotify\fP はファイルシステムイベントの通知を取得するための ずっと優れたインタフェースである。
657 \fBinotify\fP(7)  を参照。
658 .SS パイプの容量の変更
659 .TP 
660 \fBF_SETPIPE_SZ\fP (\fIint\fP; Linux 2.6.35 以降)
661 Change the capacity of the pipe referred to by \fIfd\fP to be at least \fIarg\fP
662 bytes.  An unprivileged process can adjust the pipe capacity to any value
663 between the system page size and the limit defined in
664 \fI/proc/sys/fs/pipe\-max\-size\fP (see \fBproc\fP(5)).  Attempts to set the pipe
665 capacity below the page size are silently rounded up to the page size.
666 Attempts by an unprivileged process to set the pipe capacity above the limit
667 in \fI/proc/sys/fs/pipe\-max\-size\fP yield the error \fBEPERM\fP; a privileged
668 process (\fBCAP_SYS_RESOURCE\fP)  can override the limit.  When allocating the
669 buffer for the pipe, the kernel may use a capacity larger than \fIarg\fP, if
670 that is convenient for the implementation.  The actual capacity that is set
671 is returned as the function result.  Attempting to set the pipe capacity
672 smaller than the amount of buffer space currently used to store data
673 produces the error \fBEBUSY\fP.
674 .TP 
675 \fBF_GETPIPE_SZ\fP (\fIvoid\fP; Linux 2.6.35 以降)
676 \fIfd\fP が参照するパイプの容量を (関数の結果として) 返す。
677 .SH 返り値
678 成功した場合の返り値は操作の種類により違う:
679 .TP  0.9i
680 \fBF_DUPFD\fP
681 新しいディスクリプタを返す。
682 .TP 
683 \fBF_GETFD\fP
684 ファイルディスクリプタフラグの値
685 .TP 
686 \fBF_GETFL\fP
687 ファイル状態フラグの値
688 .TP 
689 \fBF_GETLEASE\fP
690 ファイルディスクリプタに対して保持されているリースの種別を返す。
691 .TP 
692 \fBF_GETOWN\fP
693 ディスクリプタの所有者を返す。
694 .TP 
695 \fBF_GETSIG\fP
696 読み込みや書き出しが可能になった時に送られるシグナルの値、もしくは 伝統的な \fBSIGIO\fP 動作の場合にはゼロを返す。
697 .TP 
698 \fBF_GETPIPE_SZ\fP, \fBF_SETPIPE_SZ\fP
699 パイプの容量。
700 .TP 
701 他の全てのコマンド
702 0 を返す。
703 .PP
704 エラーの時は \-1 が返され、 \fIerrno\fP に適切な値が設定される。
705 .SH エラー
706 .TP 
707 \fBEACCES\fP か \fBEAGAIN\fP
708 他のプロセスが保持しているロックによって操作が禁止されている。
709 .TP 
710 \fBEAGAIN\fP
711 そのファイルは他のプロセスによってメモリマップされているため、 操作が禁止されている。
712 .TP 
713 \fBEBADF\fP
714 \fIfd\fP がオープンされたファイルディスクリプタでない。 あるいはコマンドが \fBF_SETLK\fP または \fBF_SETLKW\fP
715 だったが、対象のファイルディスクリプタのオープンモードが 必要となるロックの型にマッチしていない。
716 .TP 
717 \fBEDEADLK\fP
718 指定された \fBF_SETLKW\fP コマンドを実行した場合にはデッドロックになることが検出された。
719 .TP 
720 \fBEFAULT\fP
721 \fIlock\fP が利用可能なアドレス空間の外部にある。
722 .TP 
723 \fBEINTR\fP
724 \fBF_SETLKW\fP コマンドがシグナルにより割り込まれた (\fBsignal\fP(7)  参照)。 \fBF_GETLK\fP と \fBF_SETLK\fP
725 の場合、ロックを確認したり取得したりする前にシグナルによって割り込まれた。 これはたいていリモートのファイルをロックする場合 (例えば NFS
726 上でロックする場合) に起こる。 しかしローカルでも起こる場合がある。
727 .TP 
728 \fBEINVAL\fP
729 The value specified in \fIcmd\fP is not recognized by this kernel.
730 .TP 
731 \fBEINVAL\fP
732 \fBF_DUPFD\fPで、 \fIarg\fP が負か、もしくは許される最大値よりも大きい。 \fBF_SETSIG\fP の場合、 \fIarg\fP
733 が利用可能なシグナル番号ではない。
734 .TP 
735 \fBEINVAL\fP
736 \fIcmd\fP is \fBF_OFD_SETLK\fP, \fBF_OFD_SETLKW\fP, or \fBF_OFD_GETLK\fP, and \fIl_pid\fP
737 was not specified as zero.
738 .TP 
739 \fBEMFILE\fP
740 \fBF_DUPFD\fPで、 プロセスがすでに最大数までファイルディスクリプタをオープンしている。
741 .TP 
742 \fBENOLCK\fP
743 オープンされているロックの数が多過ぎて、ロックテーブルがいっぱいである。 または remote locking protocol (例えば NFS
744 上のロック) が失敗した。
745 .TP 
746 \fBENOTDIR\fP
747 \fBF_NOTIFY\fP was specified in \fIcmd\fP, but \fIfd\fP does not refer to a
748 directory.
749 .TP 
750 \fBEPERM\fP
751 追加専用属性が設定されたファイルの \fBO_APPEND\fP フラグをクリアしようと試みた。
752 .SH 準拠
753 SVr4, 4.3BSD, POSIX.1\-2001.  POSIX.1\-2001 で規定されている操作は、
754 \fBF_DUPFD\fP, \fBF_GETFD\fP, \fBF_SETFD\fP, \fBF_GETFL\fP, \fBF_SETFL\fP,
755 \fBF_GETLK\fP, \fBF_SETLK\fP, \fBF_SETLKW\fP だけである。
756
757 \fBF_GETOWN\fP and \fBF_SETOWN\fP are specified in POSIX.1\-2001.  (To get their
758 definitions, define either \fB_BSD_SOURCE\fP, or \fB_XOPEN_SOURCE\fP with the
759 value 500 or greater, or \fB_POSIX_C_SOURCE\fP with the value 200809L or
760 greater.)
761
762 \fBF_DUPFD_CLOEXEC\fP は POSIX.1\-2008 で規定されている。
763 (これら定義するには、
764 \fB_POSIX_C_SOURCE\fP を 200809L 以上の値で定義するか、
765 \fB_XOPEN_SOURCE\fP を 700 以上の値で定義すること。)
766
767 .\" .PP
768 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
769 \fBF_GETOWN_EX\fP, \fBF_SETOWN_EX\fP, \fBF_SETPIPE_SZ\fP, \fBF_GETPIPE_SZ\fP,
770 \fBF_GETSIG\fP,
771 \fBF_SETSIG\fP, \fBF_NOTIFY\fP, \fBF_GETLEASE\fP, \fBF_SETLEASE\fP は Linux 固有である
772 (これらの定義を有効にするには \fB_GNU_SOURCE\fP マクロを定義すること)。
773
774 \fBF_OFD_SETLK\fP, \fBF_OFD_SETLKW\fP, and \fBF_OFD_GETLK\fP are Linux\-specific (and
775 one must define \fB_GNU_SOURCE\fP to obtain their definitions), but work is
776 being done to have them included in the next version of POSIX.1.
777 .SH 注意
778 .\"
779 エラーの際の返り値が \fBdup2\fP(2)  と \fBF_DUPFD\fP では異なっている。
780 .SS ファイルロック
781 元々の Linux の \fBfcntl\fP() システムコールは (\fIflock\fP 構造体で) 大きな
782 ファイルオフセットを扱えるように設計されていなかった。
783 その結果、Linux 2.4 で \fBfcntl64\fP() システムコールが追加された。
784 この新しいシステムコールは、ファイルのロックに \fIflock64\fP という別の
785 構造体を利用し、これに対応するコマンドとして \fBF_GETLK64\fP,
786 \fBF_SETLK64\fP, \fBF_SETLKW64\fP を使用する。
787 しかし、 glibc を使うアプリケーションではこれらの詳細を無視することが
788 できる。 glibc の \fBfcntl\fP のラッパー関数は新しいシステムコールが
789 利用できる場合はそれを利用するようになっているからである。
790
791 エラーの際の返り値が \fBdup2\fP(2)  と \fBF_DUPFD\fP では異なっている。
792 .SS "Record locks"
793 カーネル 2.0 以降では、 \fBflock\fP(2)  と \fBfcntl\fP()  が設定するロック種別の間に相互作用はない。
794
795 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
796 .\" documents it in fcntl(5).  mtk, May 2007
797 .\" Also, FreeBSD documents it (Apr 2014).
798 システムによっては、 \fIstruct flock\fP に上記以外のフィールドがあるものもある (例えば \fIl_sysid\fP)。
799 はっきりと言えることは、ロックを保持しているプロセスが別のマシンに存在 する場合には、 \fIl_pid\fP
800 だけはあまり役にたたないだろうということである。
801
802 元々の Linux の \fBfcntl\fP() システムコールは (\fIflock\fP 構造体で) 大きな
803 ファイルオフセットを扱えるように設計されていなかった。
804 その結果、Linux 2.4 で \fBfcntl64\fP() システムコールが追加された。
805 この新しいシステムコールは、ファイルのロックに \fIflock64\fP という別の
806 構造体を利用し、これに対応するコマンドとして \fBF_GETLK64\fP,
807 \fBF_SETLK64\fP, \fBF_SETLKW64\fP を使用する。
808 しかし、 glibc を使うアプリケーションではこれらの詳細を無視することが
809 できる。 glibc の \fBfcntl\fP のラッパー関数は新しいシステムコールが
810 利用できる場合はそれを利用するようになっているからである。
811 .SS "Record locking and NFS"
812 .\"
813 .\" Neil Brown: With NFSv3 the failure mode is the reverse.  If
814 .\"     the server loses contact with a client then any lock stays in place
815 .\"     indefinitely ("why can't I read my mail"... I remember it well).
816 .\"
817 .\"
818 .\" Jeff Layton:
819 .\"     Note that this is not a firm timeout. The server runs a job
820 .\"     periodically to clean out expired stateful objects, and it's likely
821 .\"     that there is some time (maybe even up to another whole lease period)
822 .\"     between when the timeout expires and the job actually runs. If the
823 .\"     client gets a RENEW in there within that window, its lease will be
824 .\"     renewed and its state preserved.
825 .\"
826 Before Linux 3.12, if an NFSv4 client loses contact with the server for a
827 period of time (defined as more than 90 seconds with no communication), it
828 might lose and regain a lock without ever being aware of the fact.  (The
829 period of time after which contact is assumed lost is known as the NFSv4
830 leasetime.  On a Linux NFS server, this can be determined by looking at
831 \fI/proc/fs/nfsd/nfsv4leasetime\fP, which expresses the period in seconds.  The
832 default value for this file is 90.)  This scenario potentially risks data
833 corruption, since another process might acquire a lock in the intervening
834 period and perform file I/O.
835
836 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
837 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
838 Since Linux 3.12, if an NFSv4 client loses contact with the server, any I/O
839 to the file by a process which "thinks" it holds a lock will fail until that
840 process closes and reopens the file.  A kernel parameter,
841 \fInfs.recover_lost_locks\fP, can be set to 1 to obtain the pre\-3.12 behavior,
842 whereby the client will attempt to recover lost locks when contact is
843 reestablished with the server.  Because of the attendant risk of data
844 corruption, this parameter defaults to 0 (disabled).
845 .SH バグ
846 .SS F_SETFL
847 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
848 .\" via fcntl(2), but currently Linux does not permit this
849 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
850 \fBF_SETFL\fP を使って、 フラグ \fBO_DSYNC\fP と \fBO_SYNC\fP
851 の状態を変更することはできない。これらのフラグの状態を変更しようとした場合には、黙って無視される。
852 .SS F_GETOWN
853 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
854 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
855 .\" indicate that ANY negative PGID value will cause F_GETOWN
856 .\" to misinterpret the return as an error. Some other architectures
857 .\" seem to have the same range check as i386.
858 いくつかのアーキテクチャ (特に i386) における Linux システムコールの慣習
859 のため以下の制限が存在する。
860 \fBF_GETOWN\fP が返す (負の) プロセスグループID が \-1 から \-4095 の範囲に入った場合、
861 glibc はこの返り値をシステムコールでエラーが起こったと間違って解釈してしまう。
862 つまり、 \fBfcntl\fP() の返り値は \-1 となり、 \fIerrno\fP には (正の) プロセスグループID
863 が設定されることになる。Linux 固有の \fBF_GETOWN_EX\fP ではこの問題を回避できる。
864 glibc バージョン 2.11 以降では、glibc では \fBF_GETOWN_EX\fP を使って
865 \fBF_GETOWN\fP を実装することで、カーネルの \fBF_GETOWN\fP の問題を見えないようにしている。
866 .SS F_SETOWN
867 .\"
868 Linux 2.4 以前では、非特権プロセスが \fBF_SETOWN\fP を使って、ソケットのファイルディスクリプタの所有者に 呼び出し元以外のプロセス
869 (やプロセスグループ) を指定すると 発生するバグがある。この場合、 呼び出し元が所有者として指定したプロセス (やプロセスグループ) に
870 シグナルを送る許可を持っていたとしても、 \fBfcntl\fP()  が \-1 を返し \fIerrno\fP に \fBEPERM\fP を設定することがある。
871 このエラーが返ったにもかかわらず、ファイルディスクリプタの所有者 は設定され、シグナルはその所有者に送られる。
872 .SS "Deadlock detection"
873 .\"
874 The deadlock\-detection algorithm employed by the kernel when dealing with
875 \fBF_SETLKW\fP requests can yield both false negatives (failures to detect
876 deadlocks, leaving a set of deadlocked processes blocked indefinitely)  and
877 false positives (\fBEDEADLK\fP errors when there is no deadlock).  For example,
878 the kernel limits the lock depth of its dependency search to 10 steps,
879 meaning that circular deadlock chains that exceed that size will not be
880 detected.  In addition, the kernel may falsely indicate a deadlock when two
881 or more processes created using the \fBclone\fP(2)  \fBCLONE_FILES\fP flag place
882 locks that appear (to the kernel) to conflict.
883 .SS "強制ロック (mandatory locking)"
884 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
885 .\"
886 .\" Reconfirmed by Jeff Layton
887 .\"     From: Jeff Layton <jlayton <at> redhat.com>
888 .\"     Subject: Re: Status of fcntl() mandatory locking
889 .\"     Newsgroups: gmane.linux.file-systems
890 .\"     Date: 2014-04-28 10:07:57 GMT
891 .\"     http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
892 The Linux implementation of mandatory locking is subject to race conditions
893 which render it unreliable: a \fBwrite\fP(2)  call that overlaps with a lock
894 may modify data after the mandatory lock is acquired; a \fBread\fP(2)  call
895 that overlaps with a lock may detect changes to data that were made only
896 after a write lock was acquired.  Similar races exist between mandatory
897 locks and \fBmmap\fP(2).  It is therefore inadvisable to rely on mandatory
898 locking.
899 .SH 関連項目
900 \fBdup2\fP(2), \fBflock\fP(2), \fBopen\fP(2), \fBsocket\fP(2), \fBlockf\fP(3),
901 \fBcapabilities\fP(7), \fBfeature_test_macros\fP(7)
902
903 Linux カーネルソースの \fIDocumentation/filesystems/\fP ディレクトリ内の \fIlocks.txt\fP,
904 \fImandatory\-locking.txt\fP, \fIdnotify.txt\fP (以前のカーネルでは、これらのファイルは
905 \fIDocumentation/\fP ディレクトリ直下にあり、 \fImandatory\-locking.txt\fP は \fImandatory.txt\fP
906 という名前であった)
907 .SH この文書について
908 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.77 の一部
909 である。プロジェクトの説明とバグ報告に関する情報は
910 http://www.kernel.org/doc/man\-pages/ に書かれている。