xref: /third_party/python/Doc/library/fcntl.rst (revision 7db96d56)
17db96d56Sopenharmony_ci:mod:`fcntl` --- The ``fcntl`` and ``ioctl`` system calls
27db96d56Sopenharmony_ci=========================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: fcntl
57db96d56Sopenharmony_ci   :platform: Unix
67db96d56Sopenharmony_ci   :synopsis: The fcntl() and ioctl() system calls.
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. sectionauthor:: Jaap Vermeulen
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci.. index::
117db96d56Sopenharmony_ci   pair: UNIX; file control
127db96d56Sopenharmony_ci   pair: UNIX; I/O control
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci----------------
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ciThis module performs file control and I/O control on file descriptors. It is an
177db96d56Sopenharmony_ciinterface to the :c:func:`fcntl` and :c:func:`ioctl` Unix routines.  For a
187db96d56Sopenharmony_cicomplete description of these calls, see :manpage:`fcntl(2)` and
197db96d56Sopenharmony_ci:manpage:`ioctl(2)` Unix manual pages.
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ciAll functions in this module take a file descriptor *fd* as their first
247db96d56Sopenharmony_ciargument.  This can be an integer file descriptor, such as returned by
257db96d56Sopenharmony_ci``sys.stdin.fileno()``, or an :class:`io.IOBase` object, such as ``sys.stdin``
267db96d56Sopenharmony_ciitself, which provides a :meth:`~io.IOBase.fileno` that returns a genuine file
277db96d56Sopenharmony_cidescriptor.
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci.. versionchanged:: 3.3
307db96d56Sopenharmony_ci   Operations in this module used to raise an :exc:`IOError` where they now
317db96d56Sopenharmony_ci   raise an :exc:`OSError`.
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci.. versionchanged:: 3.8
347db96d56Sopenharmony_ci   The fcntl module now contains ``F_ADD_SEALS``, ``F_GET_SEALS``, and
357db96d56Sopenharmony_ci   ``F_SEAL_*`` constants for sealing of :func:`os.memfd_create` file
367db96d56Sopenharmony_ci   descriptors.
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci.. versionchanged:: 3.9
397db96d56Sopenharmony_ci   On macOS, the fcntl module exposes the ``F_GETPATH`` constant, which obtains
407db96d56Sopenharmony_ci   the path of a file from a file descriptor.
417db96d56Sopenharmony_ci   On Linux(>=3.15), the fcntl module exposes the ``F_OFD_GETLK``, ``F_OFD_SETLK``
427db96d56Sopenharmony_ci   and ``F_OFD_SETLKW`` constants, which are used when working with open file
437db96d56Sopenharmony_ci   description locks.
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci.. versionchanged:: 3.10
467db96d56Sopenharmony_ci   On Linux >= 2.6.11, the fcntl module exposes the ``F_GETPIPE_SZ`` and
477db96d56Sopenharmony_ci   ``F_SETPIPE_SZ`` constants, which allow to check and modify a pipe's size
487db96d56Sopenharmony_ci   respectively.
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ci.. versionchanged:: 3.11
517db96d56Sopenharmony_ci   On FreeBSD, the fcntl module exposes the ``F_DUP2FD`` and ``F_DUP2FD_CLOEXEC``
527db96d56Sopenharmony_ci   constants, which allow to duplicate a file descriptor, the latter setting
537db96d56Sopenharmony_ci   ``FD_CLOEXEC`` flag in addition.
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ciThe module defines the following functions:
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci.. function:: fcntl(fd, cmd, arg=0)
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci   Perform the operation *cmd* on file descriptor *fd* (file objects providing
617db96d56Sopenharmony_ci   a :meth:`~io.IOBase.fileno` method are accepted as well).  The values used
627db96d56Sopenharmony_ci   for *cmd* are operating system dependent, and are available as constants
637db96d56Sopenharmony_ci   in the :mod:`fcntl` module, using the same names as used in the relevant C
647db96d56Sopenharmony_ci   header files. The argument *arg* can either be an integer value, or a
657db96d56Sopenharmony_ci   :class:`bytes` object. With an integer value, the return value of this
667db96d56Sopenharmony_ci   function is the integer return value of the C :c:func:`fcntl` call.  When
677db96d56Sopenharmony_ci   the argument is bytes it represents a binary structure, e.g. created by
687db96d56Sopenharmony_ci   :func:`struct.pack`. The binary data is copied to a buffer whose address is
697db96d56Sopenharmony_ci   passed to the C :c:func:`fcntl` call.  The return value after a successful
707db96d56Sopenharmony_ci   call is the contents of the buffer, converted to a :class:`bytes` object.
717db96d56Sopenharmony_ci   The length of the returned object will be the same as the length of the
727db96d56Sopenharmony_ci   *arg* argument. This is limited to 1024 bytes. If the information returned
737db96d56Sopenharmony_ci   in the buffer by the operating system is larger than 1024 bytes, this is
747db96d56Sopenharmony_ci   most likely to result in a segmentation violation or a more subtle data
757db96d56Sopenharmony_ci   corruption.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci   If the :c:func:`fcntl` fails, an :exc:`OSError` is raised.
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci   .. audit-event:: fcntl.fcntl fd,cmd,arg fcntl.fcntl
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci.. function:: ioctl(fd, request, arg=0, mutate_flag=True)
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   This function is identical to the :func:`~fcntl.fcntl` function, except
857db96d56Sopenharmony_ci   that the argument handling is even more complicated.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci   The *request* parameter is limited to values that can fit in 32-bits.
887db96d56Sopenharmony_ci   Additional constants of interest for use as the *request* argument can be
897db96d56Sopenharmony_ci   found in the :mod:`termios` module, under the same names as used in
907db96d56Sopenharmony_ci   the relevant C header files.
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci   The parameter *arg* can be one of an integer, an object supporting the
937db96d56Sopenharmony_ci   read-only buffer interface (like :class:`bytes`) or an object supporting
947db96d56Sopenharmony_ci   the read-write buffer interface (like :class:`bytearray`).
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci   In all but the last case, behaviour is as for the :func:`~fcntl.fcntl`
977db96d56Sopenharmony_ci   function.
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci   If a mutable buffer is passed, then the behaviour is determined by the value of
1007db96d56Sopenharmony_ci   the *mutate_flag* parameter.
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ci   If it is false, the buffer's mutability is ignored and behaviour is as for a
1037db96d56Sopenharmony_ci   read-only buffer, except that the 1024 byte limit mentioned above is avoided --
1047db96d56Sopenharmony_ci   so long as the buffer you pass is at least as long as what the operating system
1057db96d56Sopenharmony_ci   wants to put there, things should work.
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci   If *mutate_flag* is true (the default), then the buffer is (in effect) passed
1087db96d56Sopenharmony_ci   to the underlying :func:`ioctl` system call, the latter's return code is
1097db96d56Sopenharmony_ci   passed back to the calling Python, and the buffer's new contents reflect the
1107db96d56Sopenharmony_ci   action of the :func:`ioctl`.  This is a slight simplification, because if the
1117db96d56Sopenharmony_ci   supplied buffer is less than 1024 bytes long it is first copied into a static
1127db96d56Sopenharmony_ci   buffer 1024 bytes long which is then passed to :func:`ioctl` and copied back
1137db96d56Sopenharmony_ci   into the supplied buffer.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci   If the :c:func:`ioctl` fails, an :exc:`OSError` exception is raised.
1167db96d56Sopenharmony_ci
1177db96d56Sopenharmony_ci   An example::
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci      >>> import array, fcntl, struct, termios, os
1207db96d56Sopenharmony_ci      >>> os.getpgrp()
1217db96d56Sopenharmony_ci      13341
1227db96d56Sopenharmony_ci      >>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, "  "))[0]
1237db96d56Sopenharmony_ci      13341
1247db96d56Sopenharmony_ci      >>> buf = array.array('h', [0])
1257db96d56Sopenharmony_ci      >>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
1267db96d56Sopenharmony_ci      0
1277db96d56Sopenharmony_ci      >>> buf
1287db96d56Sopenharmony_ci      array('h', [13341])
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci   .. audit-event:: fcntl.ioctl fd,request,arg fcntl.ioctl
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci.. function:: flock(fd, operation)
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci   Perform the lock operation *operation* on file descriptor *fd* (file objects providing
1367db96d56Sopenharmony_ci   a :meth:`~io.IOBase.fileno` method are accepted as well). See the Unix manual
1377db96d56Sopenharmony_ci   :manpage:`flock(2)` for details.  (On some systems, this function is emulated
1387db96d56Sopenharmony_ci   using :c:func:`fcntl`.)
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ci   If the :c:func:`flock` fails, an :exc:`OSError` exception is raised.
1417db96d56Sopenharmony_ci
1427db96d56Sopenharmony_ci   .. audit-event:: fcntl.flock fd,operation fcntl.flock
1437db96d56Sopenharmony_ci
1447db96d56Sopenharmony_ci
1457db96d56Sopenharmony_ci.. function:: lockf(fd, cmd, len=0, start=0, whence=0)
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ci   This is essentially a wrapper around the :func:`~fcntl.fcntl` locking calls.
1487db96d56Sopenharmony_ci   *fd* is the file descriptor (file objects providing a :meth:`~io.IOBase.fileno`
1497db96d56Sopenharmony_ci   method are accepted as well) of the file to lock or unlock, and *cmd*
1507db96d56Sopenharmony_ci   is one of the following values:
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ci   * :const:`LOCK_UN` -- unlock
1537db96d56Sopenharmony_ci   * :const:`LOCK_SH` -- acquire a shared lock
1547db96d56Sopenharmony_ci   * :const:`LOCK_EX` -- acquire an exclusive lock
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ci   When *cmd* is :const:`LOCK_SH` or :const:`LOCK_EX`, it can also be
1577db96d56Sopenharmony_ci   bitwise ORed with :const:`LOCK_NB` to avoid blocking on lock acquisition.
1587db96d56Sopenharmony_ci   If :const:`LOCK_NB` is used and the lock cannot be acquired, an
1597db96d56Sopenharmony_ci   :exc:`OSError` will be raised and the exception will have an *errno*
1607db96d56Sopenharmony_ci   attribute set to :const:`EACCES` or :const:`EAGAIN` (depending on the
1617db96d56Sopenharmony_ci   operating system; for portability, check for both values).  On at least some
1627db96d56Sopenharmony_ci   systems, :const:`LOCK_EX` can only be used if the file descriptor refers to a
1637db96d56Sopenharmony_ci   file opened for writing.
1647db96d56Sopenharmony_ci
1657db96d56Sopenharmony_ci   *len* is the number of bytes to lock, *start* is the byte offset at
1667db96d56Sopenharmony_ci   which the lock starts, relative to *whence*, and *whence* is as with
1677db96d56Sopenharmony_ci   :func:`io.IOBase.seek`, specifically:
1687db96d56Sopenharmony_ci
1697db96d56Sopenharmony_ci   * :const:`0` -- relative to the start of the file (:data:`os.SEEK_SET`)
1707db96d56Sopenharmony_ci   * :const:`1` -- relative to the current buffer position (:data:`os.SEEK_CUR`)
1717db96d56Sopenharmony_ci   * :const:`2` -- relative to the end of the file (:data:`os.SEEK_END`)
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_ci   The default for *start* is 0, which means to start at the beginning of the file.
1747db96d56Sopenharmony_ci   The default for *len* is 0 which means to lock to the end of the file.  The
1757db96d56Sopenharmony_ci   default for *whence* is also 0.
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_ci   .. audit-event:: fcntl.lockf fd,cmd,len,start,whence fcntl.lockf
1787db96d56Sopenharmony_ci
1797db96d56Sopenharmony_ciExamples (all on a SVR4 compliant system)::
1807db96d56Sopenharmony_ci
1817db96d56Sopenharmony_ci   import struct, fcntl, os
1827db96d56Sopenharmony_ci
1837db96d56Sopenharmony_ci   f = open(...)
1847db96d56Sopenharmony_ci   rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)
1857db96d56Sopenharmony_ci
1867db96d56Sopenharmony_ci   lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
1877db96d56Sopenharmony_ci   rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
1887db96d56Sopenharmony_ci
1897db96d56Sopenharmony_ciNote that in the first example the return value variable *rv* will hold an
1907db96d56Sopenharmony_ciinteger value; in the second example it will hold a :class:`bytes` object.  The
1917db96d56Sopenharmony_cistructure lay-out for the *lockdata* variable is system dependent --- therefore
1927db96d56Sopenharmony_ciusing the :func:`flock` call may be better.
1937db96d56Sopenharmony_ci
1947db96d56Sopenharmony_ci
1957db96d56Sopenharmony_ci.. seealso::
1967db96d56Sopenharmony_ci
1977db96d56Sopenharmony_ci   Module :mod:`os`
1987db96d56Sopenharmony_ci      If the locking flags :data:`~os.O_SHLOCK` and :data:`~os.O_EXLOCK` are
1997db96d56Sopenharmony_ci      present in the :mod:`os` module (on BSD only), the :func:`os.open`
2007db96d56Sopenharmony_ci      function provides an alternative to the :func:`lockf` and :func:`flock`
2017db96d56Sopenharmony_ci      functions.
202