blob: c64cc83827a4d2f86632c4fef3c609009a9219dd [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text by Jaap Vermeulen
Fred Drake295da241998-08-10 19:42:37 +00002\section{\module{fcntl} ---
3 The \function{fcntl()} and \function{ioctl()} system calls.}
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{fcntl}
5
6\modulesynopsis{The \function{fcntl()} and \function{ioctl()} system calls.}
7
Fred Drake19b97b11998-02-09 20:16:18 +00008\indexii{UNIX@\UNIX{}}{file control}
9\indexii{UNIX@\UNIX{}}{I/O control}
Guido van Rossum7f61b351994-05-19 09:09:50 +000010
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000011This module performs file control and I/O control on file descriptors.
Fred Drakec71585e1998-03-12 05:33:40 +000012It is an interface to the \cfunction{fcntl()} and \cfunction{ioctl()}
13\UNIX{} routines. File descriptors can be obtained with the
14\method{fileno()} method of a file or socket object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000015
16The module defines the following functions:
17
Guido van Rossum7f61b351994-05-19 09:09:50 +000018
Fred Drakec71585e1998-03-12 05:33:40 +000019\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
20 Perform the requested operation on file descriptor \var{fd}.
21 The operation is defined by \var{op} and is operating system
Guido van Rossum7f61b351994-05-19 09:09:50 +000022 dependent. Typically these codes can be retrieved from the library
Fred Drake55e3cbd1998-04-03 06:54:27 +000023 module \module{FCNTL}\refstmodindex{FCNTL}. The argument \var{arg}
24 is optional, and defaults to the integer value \code{0}. When
25 present, it can either be an integer value, or a string. With
Guido van Rossum7f61b351994-05-19 09:09:50 +000026 the argument missing or an integer value, the return value of this
Fred Drakec71585e1998-03-12 05:33:40 +000027 function is the integer return value of the \C{} \cfunction{fcntl()}
Guido van Rossum7f61b351994-05-19 09:09:50 +000028 call. When the argument is a string it represents a binary
Fred Drakec71585e1998-03-12 05:33:40 +000029 structure, e.g.\ created by \function{struct.pack()}. The binary
30 data is copied to a buffer whose address is passed to the \C{}
31 \cfunction{fcntl()} call. The return value after a successful call
32 is the contents of the buffer, converted to a string object. In
33 case the \cfunction{fcntl()} fails, an \exception{IOError} is
34 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000035\end{funcdesc}
36
Fred Drakec71585e1998-03-12 05:33:40 +000037\begin{funcdesc}{ioctl}{fd, op, arg}
38 This function is identical to the \function{fcntl()} function, except
Guido van Rossum7f61b351994-05-19 09:09:50 +000039 that the operations are typically defined in the library module
Fred Drakec71585e1998-03-12 05:33:40 +000040 \module{IOCTL}.
Guido van Rossum7f61b351994-05-19 09:09:50 +000041\end{funcdesc}
42
Fred Drakec71585e1998-03-12 05:33:40 +000043\begin{funcdesc}{flock}{fd, op}
Guido van Rossum50ec5c01996-06-26 19:20:33 +000044Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drake55e3cbd1998-04-03 06:54:27 +000045See the \UNIX{} manual \manpage{flock}{3} for details. (On some
46systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000047\end{funcdesc}
48
Fred Drakec71585e1998-03-12 05:33:40 +000049\begin{funcdesc}{lockf}{fd, code, \optional{len, \optional{start, \optional{whence}}}}
50This is a wrapper around the \constant{FCNTL.F_SETLK} and
51\constant{FCNTL.F_SETLKW} \function{fcntl()} calls. See the \UNIX{}
52manual for details.
Guido van Rossum9b058111996-10-11 17:43:34 +000053\end{funcdesc}
54
Fred Drakec71585e1998-03-12 05:33:40 +000055If the library modules \module{FCNTL}\refstmodindex{FCNTL} or
56\module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the
57opcodes in the \C{} include files \code{<sys/fcntl.h>} and
58\code{<sys/ioctl.h>}. You can create the modules yourself with the
59\program{h2py} script, found in the \file{Tools/scripts/} directory.
60
Guido van Rossum7f61b351994-05-19 09:09:50 +000061
62Examples (all on a SVR4 compliant system):
63
Fred Drake19479911998-02-13 06:58:54 +000064\begin{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +000065import struct, FCNTL
66
67file = open(...)
68rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
69
70lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
71rv = fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +000072\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +000073
Guido van Rossum7f61b351994-05-19 09:09:50 +000074Note that in the first example the return value variable \code{rv} will
75hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +000076value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +000077system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +000078better.