blob: e3a4ae0c274776b42113506751763e99f75f42b7 [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text by Jaap Vermeulen
Guido van Rossum470be141995-03-17 16:07:09 +00002\section{Built-in Module \sectcode{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.
9It is an interface to the \dfn{fcntl()} and \dfn{ioctl()} \UNIX{} routines.
Guido van Rossum7f61b351994-05-19 09:09:50 +000010File descriptors can be obtained with the \dfn{fileno()} method of a
11file or socket object.
12
13The module defines the following functions:
14
Fred Drake19479911998-02-13 06:58:54 +000015\setindexsubitem{(in module struct)}
Guido van Rossum7f61b351994-05-19 09:09:50 +000016
Guido van Rossum16d6e711994-08-08 12:30:22 +000017\begin{funcdesc}{fcntl}{fd\, op\optional{\, arg}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000018 Perform the requested operation on file descriptor \code{\var{fd}}.
19 The operation is defined by \code{\var{op}} and is operating system
20 dependent. Typically these codes can be retrieved from the library
Guido van Rossum16d6e711994-08-08 12:30:22 +000021 module \code{FCNTL}. The argument \code{\var{arg}} is optional, and
22 defaults to the integer value \code{0}. When
Guido van Rossum7f61b351994-05-19 09:09:50 +000023 it is present, it can either be an integer value, or a string. With
24 the argument missing or an integer value, the return value of this
25 function is the integer return value of the real \code{fcntl()}
26 call. When the argument is a string it represents a binary
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000027 structure, e.g.\ created by \code{struct.pack()}. The binary data is
Guido van Rossum7f61b351994-05-19 09:09:50 +000028 copied to a buffer whose address is passed to the real \code{fcntl()}
29 call. The return value after a successful call is the contents of
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030 the buffer, converted to a string object. In case the
Guido van Rossum7f61b351994-05-19 09:09:50 +000031 \code{fcntl()} fails, an \code{IOError} will be raised.
32\end{funcdesc}
33
34\begin{funcdesc}{ioctl}{fd\, op\, arg}
35 This function is identical to the \code{fcntl()} function, except
36 that the operations are typically defined in the library module
37 \code{IOCTL}.
38\end{funcdesc}
39
Guido van Rossum50ec5c01996-06-26 19:20:33 +000040\begin{funcdesc}{flock}{fd\, op}
41Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drakeefc1e0f1998-01-13 19:00:33 +000042See the \UNIX{} manual for details. (On some systems, this function is
43emulated using \code{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000044\end{funcdesc}
45
Guido van Rossum9b058111996-10-11 17:43:34 +000046\begin{funcdesc}{lockf}{fd\, code\, \optional{len\, \optional{start\, \optional{whence}}}}
47This is a wrapper around the \code{F_SETLK} and \code{F_SETLKW}
Fred Drakeefc1e0f1998-01-13 19:00:33 +000048\code{fcntl()} calls. See the \UNIX{} manual for details.
Guido van Rossum9b058111996-10-11 17:43:34 +000049\end{funcdesc}
50
Guido van Rossum7f61b351994-05-19 09:09:50 +000051If the library modules \code{FCNTL} or \code{IOCTL} are missing, you
Fred Drake19d58271997-12-15 22:23:52 +000052can find the opcodes in the C include files \file{sys/fcntl.h} and
53\file{sys/ioctl.h}. You can create the modules yourself with the h2py
54script, found in the \file{Tools/scripts} directory.
55\refstmodindex{FCNTL}
56\refstmodindex{IOCTL}
Guido van Rossum7f61b351994-05-19 09:09:50 +000057
58Examples (all on a SVR4 compliant system):
59
Fred Drake19479911998-02-13 06:58:54 +000060\begin{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +000061import struct, FCNTL
62
63file = open(...)
64rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
65
66lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
67rv = fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +000068\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000069%
Guido van Rossum7f61b351994-05-19 09:09:50 +000070Note that in the first example the return value variable \code{rv} will
71hold an integer value; in the second example it will hold a string
Guido van Rossum50ec5c01996-06-26 19:20:33 +000072value. The structure lay-out for the \var{lockadata} variable is
73system dependent -- therefore using the \code{flock()} call may be
74better.