blob: 1c64af928a5f9e5b047aa62719563a5ed09f68db [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text by Jaap Vermeulen
Fred Drake3a0351c1998-04-04 07:23:21 +00002\section{Built-in Module \module{fcntl}}
Fred Drake12918af1998-02-18 15:10:24 +00003\label{module-fcntl}
Guido van Rossum7f61b351994-05-19 09:09:50 +00004\bimodindex{fcntl}
Fred Drake19b97b11998-02-09 20:16:18 +00005\indexii{UNIX@\UNIX{}}{file control}
6\indexii{UNIX@\UNIX{}}{I/O control}
Guido van Rossum7f61b351994-05-19 09:09:50 +00007
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00008This module performs file control and I/O control on file descriptors.
Fred Drakec71585e1998-03-12 05:33:40 +00009It is an interface to the \cfunction{fcntl()} and \cfunction{ioctl()}
10\UNIX{} routines. File descriptors can be obtained with the
11\method{fileno()} method of a file or socket object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000012
13The module defines the following functions:
14
Guido van Rossum7f61b351994-05-19 09:09:50 +000015
Fred Drakec71585e1998-03-12 05:33:40 +000016\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
17 Perform the requested operation on file descriptor \var{fd}.
18 The operation is defined by \var{op} and is operating system
Guido van Rossum7f61b351994-05-19 09:09:50 +000019 dependent. Typically these codes can be retrieved from the library
Fred Drake55e3cbd1998-04-03 06:54:27 +000020 module \module{FCNTL}\refstmodindex{FCNTL}. The argument \var{arg}
21 is optional, and defaults to the integer value \code{0}. When
22 present, it can either be an integer value, or a string. With
Guido van Rossum7f61b351994-05-19 09:09:50 +000023 the argument missing or an integer value, the return value of this
Fred Drakec71585e1998-03-12 05:33:40 +000024 function is the integer return value of the \C{} \cfunction{fcntl()}
Guido van Rossum7f61b351994-05-19 09:09:50 +000025 call. When the argument is a string it represents a binary
Fred Drakec71585e1998-03-12 05:33:40 +000026 structure, e.g.\ created by \function{struct.pack()}. The binary
27 data is copied to a buffer whose address is passed to the \C{}
28 \cfunction{fcntl()} call. The return value after a successful call
29 is the contents of the buffer, converted to a string object. In
30 case the \cfunction{fcntl()} fails, an \exception{IOError} is
31 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000032\end{funcdesc}
33
Fred Drakec71585e1998-03-12 05:33:40 +000034\begin{funcdesc}{ioctl}{fd, op, arg}
35 This function is identical to the \function{fcntl()} function, except
Guido van Rossum7f61b351994-05-19 09:09:50 +000036 that the operations are typically defined in the library module
Fred Drakec71585e1998-03-12 05:33:40 +000037 \module{IOCTL}.
Guido van Rossum7f61b351994-05-19 09:09:50 +000038\end{funcdesc}
39
Fred Drakec71585e1998-03-12 05:33:40 +000040\begin{funcdesc}{flock}{fd, op}
Guido van Rossum50ec5c01996-06-26 19:20:33 +000041Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drake55e3cbd1998-04-03 06:54:27 +000042See the \UNIX{} manual \manpage{flock}{3} for details. (On some
43systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000044\end{funcdesc}
45
Fred Drakec71585e1998-03-12 05:33:40 +000046\begin{funcdesc}{lockf}{fd, code, \optional{len, \optional{start, \optional{whence}}}}
47This is a wrapper around the \constant{FCNTL.F_SETLK} and
48\constant{FCNTL.F_SETLKW} \function{fcntl()} calls. See the \UNIX{}
49manual for details.
Guido van Rossum9b058111996-10-11 17:43:34 +000050\end{funcdesc}
51
Fred Drakec71585e1998-03-12 05:33:40 +000052If the library modules \module{FCNTL}\refstmodindex{FCNTL} or
53\module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the
54opcodes in the \C{} include files \code{<sys/fcntl.h>} and
55\code{<sys/ioctl.h>}. You can create the modules yourself with the
56\program{h2py} script, found in the \file{Tools/scripts/} directory.
57
Guido van Rossum7f61b351994-05-19 09:09:50 +000058
59Examples (all on a SVR4 compliant system):
60
Fred Drake19479911998-02-13 06:58:54 +000061\begin{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +000062import struct, FCNTL
63
64file = open(...)
65rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
66
67lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
68rv = fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +000069\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +000070
Guido van Rossum7f61b351994-05-19 09:09:50 +000071Note that in the first example the return value variable \code{rv} will
72hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +000073value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +000074system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +000075better.