35.9. fcntl
— fcntl
および ioctl
システムコール¶
This module performs file control and I/O control on file descriptors. It is an
interface to the fcntl()
and ioctl()
Unix routines. For a
complete description of these calls, see fcntl(2) and
ioctl(2) Unix manual pages.
All functions in this module take a file descriptor fd as their first
argument. This can be an integer file descriptor, such as returned by
sys.stdin.fileno()
, or an io.IOBase
object, such as sys.stdin
itself, which provides a fileno()
that returns a genuine file
descriptor.
このモジュールでは以下の関数を定義しています:
-
fcntl.
fcntl
(fd, cmd, arg=0)¶ Perform the operation cmd on file descriptor fd (file objects providing a
fileno()
method are accepted as well). The values used for cmd are operating system dependent, and are available as constants in thefcntl
module, using the same names as used in the relevant C header files. The argument arg can either be an integer value, or abytes
object. With an integer value, the return value of this function is the integer return value of the Cfcntl()
call. When the argument is bytes it represents a binary structure, e.g. created bystruct.pack()
. The binary data is copied to a buffer whose address is passed to the Cfcntl()
call. The return value after a successful call is the contents of the buffer, converted to abytes
object. The length of the returned object will be the same as the length of the arg argument. This is limited to 1024 bytes. If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.fcntl()
が失敗した場合、OSError
が送出されます。
-
fcntl.
ioctl
(fd, request, arg=0, mutate_flag=True)¶ この関数は
fcntl()
関数と同じですが、引数の扱いがより複雑であるところが異なります。The request parameter is limited to values that can fit in 32-bits. Additional constants of interest for use as the request argument can be found in the
termios
module, under the same names as used in the relevant C header files.The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like
bytes
) or an object supporting the read-write buffer interface (likebytearray
).最後の型のオブジェクトを除き、動作は
fcntl()
関数と同じです。可変なバッファが渡された場合、動作は mutate_flag 引数の値で決定されます。
If it is false, the buffer’s mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided – so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.
mutate_flag が真 (デフォルト) の場合、バッファは (実際には) 根底にある
ioctl()
システムコールに渡され、後者の戻り値が呼び出し側の Python に引き渡され、バッファの新たな内容はioctl()
の動作を反映します。この説明はやや単純化されています。というのは、与えられたバッファが 1024 バイト長よりも短い場合、バッファはまず 1024 バイト長の静的なバッファにコピーされてからioctl()
に渡され、その後引数で与えたバッファに戻しコピーされるからです。If the
ioctl()
fails, anOSError
exception is raised.以下に例を示します:
>>> import array, fcntl, struct, termios, os >>> os.getpgrp() 13341 >>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0] 13341 >>> buf = array.array('h', [0]) >>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1) 0 >>> buf array('h', [13341])
-
fcntl.
flock
(fd, operation)¶ Perform the lock operation operation on file descriptor fd (file objects providing a
fileno()
method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated usingfcntl()
.)If the
flock()
fails, anOSError
exception is raised.
-
fcntl.
lockf
(fd, cmd, len=0, start=0, whence=0)¶ This is essentially a wrapper around the
fcntl()
locking calls. fd is the file descriptor of the file to lock or unlock, and cmd is one of the following values:LOCK_UN
– アンロックLOCK_SH
– 共有ロックを取得LOCK_EX
– 排他的ロックを取得
When cmd is
LOCK_SH
orLOCK_EX
, it can also be bitwise ORed withLOCK_NB
to avoid blocking on lock acquisition. IfLOCK_NB
is used and the lock cannot be acquired, anOSError
will be raised and the exception will have an errno attribute set toEACCES
orEAGAIN
(depending on the operating system; for portability, check for both values). On at least some systems,LOCK_EX
can only be used if the file descriptor refers to a file opened for writing.len is the number of bytes to lock, start is the byte offset at which the lock starts, relative to whence, and whence is as with
io.IOBase.seek()
, specifically:0
– ファイル先頭からの相対位置 (os.SEEK_SET
)1
– 現在のバッファ位置からの相対位置 (os.SEEK_CUR
)2
– ファイルの末尾からの相対位置 (os.SEEK_END
)
The default for start is 0, which means to start at the beginning of the file. The default for len is 0 which means to lock to the end of the file. The default for whence is also 0.
以下に (全ての SVR4 互換システムでの) 例を示します:
import struct, fcntl, os
f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Note that in the first example the return value variable rv will hold an
integer value; in the second example it will hold a bytes
object. The
structure lay-out for the lockdata variable is system dependent — therefore
using the flock()
call may be better.
参考
os
モジュールもし
os
モジュールにos.O_SHLOCK
とos.O_EXLOCK
が 存在する場合 (BSD のみ)、os.open()
関数はlockf()
やflock()
関数を代替できます。