Debian Codegraph @

Doc/library/fcntl.rst

raw
1 :mod:`!fcntl` --- The ``fcntl`` and ``ioctl`` system calls
2 ==========================================================
3
4 .. module:: fcntl
5 :platform: Unix
6 :synopsis: The fcntl() and ioctl() system calls.
7
8 .. sectionauthor:: Jaap Vermeulen
9
10 .. index::
11 pair: UNIX; file control
12 pair: UNIX; I/O control
13
14 ----------------
15
16 This module performs file and I/O control on file descriptors. It is an
17 interface to the :c:func:`fcntl` and :c:func:`ioctl` Unix routines.
18 See the :manpage:`fcntl(2)` and :manpage:`ioctl(2)` Unix manual pages
19 for full details.
20
21 .. availability:: Unix, not WASI.
22
23 All functions in this module take a file descriptor *fd* as their first
24 argument. This can be an integer file descriptor, such as returned by
25 ``sys.stdin.fileno()``, or an :class:`io.IOBase` object, such as ``sys.stdin``
26 itself, which provides a :meth:`~io.IOBase.fileno` that returns a genuine file
27 descriptor.
28
29 .. versionchanged:: 3.3
30 Operations in this module used to raise an :exc:`IOError` where they now
31 raise an :exc:`OSError`.
32
33 .. versionchanged:: 3.8
34 The :mod:`!fcntl` module now contains ``F_ADD_SEALS``, ``F_GET_SEALS``, and
35 ``F_SEAL_*`` constants for sealing of :func:`os.memfd_create` file
36 descriptors.
37
38 .. versionchanged:: 3.9
39 On macOS, the :mod:`!fcntl` module exposes the ``F_GETPATH`` constant,
40 which obtains the path of a file from a file descriptor.
41 On Linux(>=3.15), the :mod:`!fcntl` module exposes the ``F_OFD_GETLK``,
42 ``F_OFD_SETLK`` and ``F_OFD_SETLKW`` constants, which are used when working
43 with open file description locks.
44
45 .. versionchanged:: 3.10
46 On Linux >= 2.6.11, the :mod:`!fcntl` module exposes the ``F_GETPIPE_SZ`` and
47 ``F_SETPIPE_SZ`` constants, which allow to check and modify a pipe's size
48 respectively.
49
50 .. versionchanged:: 3.11
51 On FreeBSD, the :mod:`!fcntl` module exposes the ``F_DUP2FD`` and
52 ``F_DUP2FD_CLOEXEC`` constants, which allow to duplicate a file descriptor,
53 the latter setting ``FD_CLOEXEC`` flag in addition.
54
55 .. versionchanged:: 3.12
56 On Linux >= 4.5, the :mod:`fcntl` module exposes the ``FICLONE`` and
57 ``FICLONERANGE`` constants, which allow to share some data of one file with
58 another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and
59 XFS). This behavior is commonly referred to as "copy-on-write".
60
61 .. versionchanged:: 3.13
62 On Linux >= 2.6.32, the :mod:`!fcntl` module exposes the
63 ``F_GETOWN_EX``, ``F_SETOWN_EX``, ``F_OWNER_TID``, ``F_OWNER_PID``, ``F_OWNER_PGRP`` constants, which allow to direct I/O availability signals
64 to a specific thread, process, or process group.
65 On Linux >= 4.13, the :mod:`!fcntl` module exposes the
66 ``F_GET_RW_HINT``, ``F_SET_RW_HINT``, ``F_GET_FILE_RW_HINT``,
67 ``F_SET_FILE_RW_HINT``, and ``RWH_WRITE_LIFE_*`` constants, which allow
68 to inform the kernel about the relative expected lifetime of writes on
69 a given inode or via a particular open file description.
70 On Linux >= 5.1 and NetBSD, the :mod:`!fcntl` module exposes the
71 ``F_SEAL_FUTURE_WRITE`` constant for use with ``F_ADD_SEALS`` and
72 ``F_GET_SEALS`` operations.
73 On FreeBSD, the :mod:`!fcntl` module exposes the ``F_READAHEAD``, ``F_ISUNIONSTACK``, and ``F_KINFO`` constants.
74 On macOS and FreeBSD, the :mod:`!fcntl` module exposes the ``F_RDAHEAD``
75 constant.
76 On NetBSD and AIX, the :mod:`!fcntl` module exposes the ``F_CLOSEM``
77 constant.
78 On NetBSD, the :mod:`!fcntl` module exposes the ``F_MAXFD`` constant.
79 On macOS and NetBSD, the :mod:`!fcntl` module exposes the ``F_GETNOSIGPIPE``
80 and ``F_SETNOSIGPIPE`` constant.
81
82 The module defines the following functions:
83
84
85 .. function:: fcntl(fd, cmd, arg=0, /)
86
87 Perform the operation *cmd* on file descriptor *fd* (file objects providing
88 a :meth:`~io.IOBase.fileno` method are accepted as well). The values used
89 for *cmd* are operating system dependent, and are available as constants
90 in the :mod:`fcntl` module, using the same names as used in the relevant C
91 header files. The argument *arg* can either be an integer value, a
92 :class:`bytes` object, or a string.
93 The type and size of *arg* must match the type and size of
94 the argument of the operation as specified in the relevant C documentation.
95
96 When *arg* is an integer, the function returns the integer
97 return value of the C :c:func:`fcntl` call.
98
99 When the argument is bytes, it represents a binary structure,
100 for example, created by :func:`struct.pack`.
101 A string value is encoded to binary using the UTF-8 encoding.
102 The binary data is copied to a buffer whose address is
103 passed to the C :c:func:`fcntl` call. The return value after a successful
104 call is the contents of the buffer, converted to a :class:`bytes` object.
105 The length of the returned object will be the same as the length of the
106 *arg* argument. This is limited to 1024 bytes.
107
108 If the :c:func:`fcntl` call fails, an :exc:`OSError` is raised.
109
110 .. note::
111 If the type or the size of *arg* does not match the type or size
112 of the argument of the operation (for example, if an integer is
113 passed when a pointer is expected, or the information returned in
114 the buffer by the operating system is larger than 1024 bytes),
115 this is most likely to result in a segmentation violation or
116 a more subtle data corruption.
117
118 .. audit-event:: fcntl.fcntl fd,cmd,arg fcntl.fcntl
119
120
121 .. function:: ioctl(fd, request, arg=0, mutate_flag=True, /)
122
123 This function is identical to the :func:`~fcntl.fcntl` function, except
124 that the argument handling is even more complicated.
125
126 The *request* parameter is limited to values that can fit in 32-bits
127 or 64-bits, depending on the platform.
128 Additional constants of interest for use as the *request* argument can be
129 found in the :mod:`termios` module, under the same names as used in
130 the relevant C header files.
131
132 The parameter *arg* can be an integer, a :term:`bytes-like object`,
133 or a string.
134 The type and size of *arg* must match the type and size of
135 the argument of the operation as specified in the relevant C documentation.
136
137 If *arg* does not support the read-write buffer interface or
138 the *mutate_flag* is false, behavior is as for the :func:`~fcntl.fcntl`
139 function.
140
141 If *arg* supports the read-write buffer interface (like :class:`bytearray`)
142 and *mutate_flag* is true (the default), then the buffer is (in effect) passed
143 to the underlying :c:func:`!ioctl` system call, the latter's return code is
144 passed back to the calling Python, and the buffer's new contents reflect the
145 action of the :c:func:`ioctl`. This is a slight simplification, because if the
146 supplied buffer is less than 1024 bytes long it is first copied into a static
147 buffer 1024 bytes long which is then passed to :func:`ioctl` and copied back
148 into the supplied buffer.
149
150 If the :c:func:`ioctl` call fails, an :exc:`OSError` exception is raised.
151
152 .. note::
153 If the type or size of *arg* does not match the type or size
154 of the operation's argument (for example, if an integer is
155 passed when a pointer is expected, or the information returned in
156 the buffer by the operating system is larger than 1024 bytes,
157 or the size of the mutable bytes-like object is too small),
158 this is most likely to result in a segmentation violation or
159 a more subtle data corruption.
160
161 An example::
162
163 >>> import array, fcntl, struct, termios, os
164 >>> os.getpgrp()
165 13341
166 >>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0]
167 13341
168 >>> buf = array.array('h', [0])
169 >>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
170 0
171 >>> buf
172 array('h', [13341])
173
174 .. audit-event:: fcntl.ioctl fd,request,arg fcntl.ioctl
175
176
177 .. function:: flock(fd, operation, /)
178
179 Perform the lock operation *operation* on file descriptor *fd* (file objects providing
180 a :meth:`~io.IOBase.fileno` method are accepted as well). See the Unix manual
181 :manpage:`flock(2)` for details. (On some systems, this function is emulated
182 using :c:func:`fcntl`.)
183
184 If the :c:func:`flock` call fails, an :exc:`OSError` exception is raised.
185
186 .. audit-event:: fcntl.flock fd,operation fcntl.flock
187
188
189 .. function:: lockf(fd, cmd, len=0, start=0, whence=0, /)
190
191 This is essentially a wrapper around the :func:`~fcntl.fcntl` locking calls.
192 *fd* is the file descriptor (file objects providing a :meth:`~io.IOBase.fileno`
193 method are accepted as well) of the file to lock or unlock, and *cmd*
194 is one of the following values:
195
196 .. data:: LOCK_UN
197
198 Release an existing lock.
199
200 .. data:: LOCK_SH
201
202 Acquire a shared lock.
203
204 .. data:: LOCK_EX
205
206 Acquire an exclusive lock.
207
208 .. data:: LOCK_NB
209
210 Bitwise OR with any of the other three ``LOCK_*`` constants to make
211 the request non-blocking.
212
213 If :const:`!LOCK_NB` is used and the lock cannot be acquired, an
214 :exc:`OSError` will be raised and the exception will have an *errno*
215 attribute set to :const:`~errno.EACCES` or :const:`~errno.EAGAIN` (depending on the
216 operating system; for portability, check for both values). On at least some
217 systems, :const:`!LOCK_EX` can only be used if the file descriptor refers to a
218 file opened for writing.
219
220 *len* is the number of bytes to lock, *start* is the byte offset at
221 which the lock starts, relative to *whence*, and *whence* is as with
222 :func:`io.IOBase.seek`, specifically:
223
224 * ``0`` -- relative to the start of the file (:const:`os.SEEK_SET`)
225 * ``1`` -- relative to the current buffer position (:const:`os.SEEK_CUR`)
226 * ``2`` -- relative to the end of the file (:const:`os.SEEK_END`)
227
228 The default for *start* is 0, which means to start at the beginning of the file.
229 The default for *len* is 0 which means to lock to the end of the file. The
230 default for *whence* is also 0.
231
232 .. audit-event:: fcntl.lockf fd,cmd,len,start,whence fcntl.lockf
233
234 Examples (all on a SVR4 compliant system)::
235
236 import struct, fcntl, os
237
238 f = open(...)
239 rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)
240
241 lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
242 rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
243
244 Note that in the first example the return value variable *rv* will hold an
245 integer value; in the second example it will hold a :class:`bytes` object. The
246 structure lay-out for the *lockdata* variable is system dependent --- therefore
247 using the :func:`flock` call may be better.
248
249
250 .. seealso::
251
252 Module :mod:`os`
253 If the locking flags :const:`~os.O_SHLOCK` and :const:`~os.O_EXLOCK` are
254 present in the :mod:`os` module (on BSD only), the :func:`os.open`
255 function provides an alternative to the :func:`lockf` and :func:`flock`
256 functions.