blob: e693067e0495fca166c717550c9906f0d0032ada [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 Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{fcntl}
4
5\modulesynopsis{The \function{fcntl()} and \function{ioctl()} system calls.}
6
Fred Drake19b97b11998-02-09 20:16:18 +00007\indexii{UNIX@\UNIX{}}{file control}
8\indexii{UNIX@\UNIX{}}{I/O control}
Guido van Rossum7f61b351994-05-19 09:09:50 +00009
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000010This module performs file control and I/O control on file descriptors.
Fred Drakec71585e1998-03-12 05:33:40 +000011It is an interface to the \cfunction{fcntl()} and \cfunction{ioctl()}
12\UNIX{} routines. File descriptors can be obtained with the
13\method{fileno()} method of a file or socket object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000014
15The module defines the following functions:
16
Guido van Rossum7f61b351994-05-19 09:09:50 +000017
Fred Drakec71585e1998-03-12 05:33:40 +000018\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
19 Perform the requested operation on file descriptor \var{fd}.
20 The operation is defined by \var{op} and is operating system
Guido van Rossum7f61b351994-05-19 09:09:50 +000021 dependent. Typically these codes can be retrieved from the library
Fred Drake55e3cbd1998-04-03 06:54:27 +000022 module \module{FCNTL}\refstmodindex{FCNTL}. The argument \var{arg}
23 is optional, and defaults to the integer value \code{0}. When
24 present, it can either be an integer value, or a string. With
Guido van Rossum7f61b351994-05-19 09:09:50 +000025 the argument missing or an integer value, the return value of this
Fred Drakec71585e1998-03-12 05:33:40 +000026 function is the integer return value of the \C{} \cfunction{fcntl()}
Guido van Rossum7f61b351994-05-19 09:09:50 +000027 call. When the argument is a string it represents a binary
Fred Drakec71585e1998-03-12 05:33:40 +000028 structure, e.g.\ created by \function{struct.pack()}. The binary
29 data is copied to a buffer whose address is passed to the \C{}
30 \cfunction{fcntl()} call. The return value after a successful call
31 is the contents of the buffer, converted to a string object. In
32 case the \cfunction{fcntl()} fails, an \exception{IOError} is
33 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000034\end{funcdesc}
35
Fred Drakec71585e1998-03-12 05:33:40 +000036\begin{funcdesc}{ioctl}{fd, op, arg}
37 This function is identical to the \function{fcntl()} function, except
Guido van Rossum7f61b351994-05-19 09:09:50 +000038 that the operations are typically defined in the library module
Fred Drakec71585e1998-03-12 05:33:40 +000039 \module{IOCTL}.
Guido van Rossum7f61b351994-05-19 09:09:50 +000040\end{funcdesc}
41
Fred Drakec71585e1998-03-12 05:33:40 +000042\begin{funcdesc}{flock}{fd, op}
Guido van Rossum50ec5c01996-06-26 19:20:33 +000043Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drake55e3cbd1998-04-03 06:54:27 +000044See the \UNIX{} manual \manpage{flock}{3} for details. (On some
45systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000046\end{funcdesc}
47
Fred Drakec71585e1998-03-12 05:33:40 +000048\begin{funcdesc}{lockf}{fd, code, \optional{len, \optional{start, \optional{whence}}}}
49This is a wrapper around the \constant{FCNTL.F_SETLK} and
50\constant{FCNTL.F_SETLKW} \function{fcntl()} calls. See the \UNIX{}
51manual for details.
Guido van Rossum9b058111996-10-11 17:43:34 +000052\end{funcdesc}
53
Fred Drakec71585e1998-03-12 05:33:40 +000054If the library modules \module{FCNTL}\refstmodindex{FCNTL} or
55\module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the
56opcodes in the \C{} include files \code{<sys/fcntl.h>} and
57\code{<sys/ioctl.h>}. You can create the modules yourself with the
58\program{h2py} script, found in the \file{Tools/scripts/} directory.
59
Guido van Rossum7f61b351994-05-19 09:09:50 +000060
61Examples (all on a SVR4 compliant system):
62
Fred Drake19479911998-02-13 06:58:54 +000063\begin{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +000064import struct, FCNTL
65
66file = open(...)
67rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
68
69lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
70rv = fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +000071\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +000072
Guido van Rossum7f61b351994-05-19 09:09:50 +000073Note that in the first example the return value variable \code{rv} will
74hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +000075value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +000076system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +000077better.