fcntl
---fcntl
和ioctl
系統呼叫¶
This module performs file and I/O control on file descriptors. It is aninterface to thefcntl()
andioctl()
Unix routines.See thefcntl(2) andioctl(2) Unix manual pagesfor full details.
適用: Unix, not WASI.
All functions in this module take a file descriptorfd as their firstargument. This can be an integer file descriptor, such as returned bysys.stdin.fileno()
, or anio.IOBase
object, such assys.stdin
itself, which provides afileno()
that returns a genuine filedescriptor.
在 3.8 版的變更:Thefcntl
module now containsF_ADD_SEALS
,F_GET_SEALS
, andF_SEAL_*
constants for sealing ofos.memfd_create()
filedescriptors.
在 3.9 版的變更:On macOS, thefcntl
module exposes theF_GETPATH
constant,which obtains the path of a file from a file descriptor.On Linux(>=3.15), thefcntl
module exposes theF_OFD_GETLK
,F_OFD_SETLK
andF_OFD_SETLKW
constants, which are used when workingwith open file description locks.
在 3.10 版的變更:On Linux >= 2.6.11, thefcntl
module exposes theF_GETPIPE_SZ
andF_SETPIPE_SZ
constants, which allow to check and modify a pipe's sizerespectively.
在 3.11 版的變更:On FreeBSD, thefcntl
module exposes theF_DUP2FD
andF_DUP2FD_CLOEXEC
constants, which allow to duplicate a file descriptor,the latter settingFD_CLOEXEC
flag in addition.
在 3.12 版的變更:On Linux >= 4.5, thefcntl
module exposes theFICLONE
andFICLONERANGE
constants, which allow to share some data of one file withanother file by reflinking on some filesystems (e.g., btrfs, OCFS2, andXFS). This behavior is commonly referred to as "copy-on-write".
在 3.13 版的變更:On Linux >= 2.6.32, thefcntl
module exposes theF_GETOWN_EX
,F_SETOWN_EX
,F_OWNER_TID
,F_OWNER_PID
,F_OWNER_PGRP
constants, which allow to direct I/O availability signalsto a specific thread, process, or process group.On Linux >= 4.13, thefcntl
module exposes theF_GET_RW_HINT
,F_SET_RW_HINT
,F_GET_FILE_RW_HINT
,F_SET_FILE_RW_HINT
, andRWH_WRITE_LIFE_*
constants, which allowto inform the kernel about the relative expected lifetime of writes ona given inode or via a particular open file description.On Linux >= 5.1 and NetBSD, thefcntl
module exposes theF_SEAL_FUTURE_WRITE
constant for use withF_ADD_SEALS
andF_GET_SEALS
operations.On FreeBSD, thefcntl
module exposes theF_READAHEAD
,F_ISUNIONSTACK
, andF_KINFO
constants.On macOS and FreeBSD, thefcntl
module exposes theF_RDAHEAD
constant.On NetBSD and AIX, thefcntl
module exposes theF_CLOSEM
constant.On NetBSD, thefcntl
module exposes theF_MAXFD
constant.On macOS and NetBSD, thefcntl
module exposes theF_GETNOSIGPIPE
andF_SETNOSIGPIPE
constant.
The module defines the following functions:
- fcntl.fcntl(fd,cmd,arg=0,/)¶
Perform the operationcmd on file descriptorfd (file objects providinga
fileno()
method are accepted as well). The values usedforcmd are operating system dependent, and are available as constantsin thefcntl
module, using the same names as used in the relevant Cheader files. The argumentarg can either be an integer value, abytes
object, or a string.The type and size ofarg must match the type and size ofthe argument of the operation as specified in the relevant C documentation.Whenarg is an integer, the function returns the integerreturn value of the C
fcntl()
call.When the argument is bytes, it represents a binary structure,for example, created by
struct.pack()
.A string value is encoded to binary using the UTF-8 encoding.The binary data is copied to a buffer whose address ispassed to the Cfcntl()
call. The return value after a successfulcall is the contents of the buffer, converted to abytes
object.The length of the returned object will be the same as the length of thearg argument. This is limited to 1024 bytes.If the
fcntl()
call fails, anOSError
is raised.備註
If the type or the size ofarg does not match the type or sizeof the argument of the operation (for example, if an integer ispassed when a pointer is expected, or the information returned inthe buffer by the operating system is larger than 1024 bytes),this is most likely to result in a segmentation violation ora more subtle data corruption.
引發一個附帶引數
fd
、cmd
、arg
的稽核事件fcntl.fcntl
。
- fcntl.ioctl(fd,request,arg=0,mutate_flag=True,/)¶
This function is identical to the
fcntl()
function, exceptthat the argument handling is even more complicated.Therequest parameter is limited to values that can fit in 32-bitsor 64-bits, depending on the platform.Additional constants of interest for use as therequest argument can befound in the
termios
module, under the same names as used inthe relevant C header files.The parameterarg can be an integer, abytes-like object,or a string.The type and size ofarg must match the type and size ofthe argument of the operation as specified in the relevant C documentation.
Ifarg does not support the read-write buffer interface orthemutate_flag is false, behavior is as for the
fcntl()
function.Ifarg supports the read-write buffer interface (like
bytearray
)andmutate_flag is true (the default), then the buffer is (in effect) passedto the underlyingioctl()
system call, the latter's return code ispassed back to the calling Python, and the buffer's new contents reflect theaction of theioctl()
. This is a slight simplification, because if thesupplied buffer is less than 1024 bytes long it is first copied into a staticbuffer 1024 bytes long which is then passed toioctl()
and copied backinto the supplied buffer.If the
ioctl()
call fails, anOSError
exception is raised.備註
If the type or size ofarg does not match the type or sizeof the operation's argument (for example, if an integer ispassed when a pointer is expected, or the information returned inthe buffer by the operating system is larger than 1024 bytes,or the size of the mutable bytes-like object is too small),this is most likely to result in a segmentation violation ora more subtle data corruption.
範例:
>>>importarray,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>>>bufarray('h', [13341])
引發一個附帶引數
fd
、request
、arg
的稽核事件fcntl.ioctl
。
- fcntl.flock(fd,operation,/)¶
Perform the lock operationoperation on file descriptorfd (file objects providinga
fileno()
method are accepted as well). See the Unix manualflock(2) for details. (On some systems, this function is emulatedusingfcntl()
.)If the
flock()
call fails, anOSError
exception is raised.引發一個附帶引數
fd
、operation
的稽核事件fcntl.flock
。
- 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 (file objects providing afileno()
method are accepted as well) of the file to lock or unlock, andcmdis one of the following values:- fcntl.LOCK_UN¶
Release an existing lock.
- fcntl.LOCK_SH¶
Acquire a shared lock.
- fcntl.LOCK_EX¶
Acquire an exclusive lock.
- fcntl.LOCK_NB¶
Bitwise OR with any of the other three
LOCK_*
constants to makethe request non-blocking.
If
LOCK_NB
is used and the lock cannot be acquired, anOSError
will be raised and the exception will have anerrnoattribute set toEACCES
orEAGAIN
(depending on theoperating system; for portability, check for both values). On at least somesystems,LOCK_EX
can only be used if the file descriptor refers to afile opened for writing.len is the number of bytes to lock,start is the byte offset atwhich the lock starts, relative towhence, andwhence is as with
io.IOBase.seek()
, specifically:0
-- relative to the start of the file (os.SEEK_SET
)1
-- relative to the current buffer position (os.SEEK_CUR
)2
-- relative to the end of the file (os.SEEK_END
)
The default forstart is 0, which means to start at the beginning of the file.The default forlen is 0 which means to lock to the end of the file. Thedefault forwhence is also 0.
引發一個附帶引數
fd
、cmd
、len
、start
、whence
的稽核事件fcntl.lockf
。
Examples (all on a SVR4 compliant system):
importstruct,fcntl,osf=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 variablerv will hold aninteger value; in the second example it will hold abytes
object. Thestructure lay-out for thelockdata variable is system dependent --- thereforeusing theflock()
call may be better.