blob: 7c357e6650f699588d4d893b2ac841e778a6fe4a [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{fcntl} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 The \function{fcntl()} and \function{ioctl()} system calls}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakef6863c11999-03-02 16:37:17 +00004\declaremodule{builtin}{fcntl}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drakeb91e9341998-07-23 17:59:49 +00006\modulesynopsis{The \function{fcntl()} and \function{ioctl()} system calls.}
Fred Drake38e5d272000-04-03 20:13:55 +00007\sectionauthor{Jaap Vermeulen}{}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Fred Drake19b97b11998-02-09 20:16:18 +00009\indexii{UNIX@\UNIX{}}{file control}
10\indexii{UNIX@\UNIX{}}{I/O control}
Guido van Rossum7f61b351994-05-19 09:09:50 +000011
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000012This module performs file control and I/O control on file descriptors.
Fred Drakec71585e1998-03-12 05:33:40 +000013It is an interface to the \cfunction{fcntl()} and \cfunction{ioctl()}
14\UNIX{} routines. File descriptors can be obtained with the
15\method{fileno()} method of a file or socket object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000016
17The module defines the following functions:
18
Guido van Rossum7f61b351994-05-19 09:09:50 +000019
Fred Drakec71585e1998-03-12 05:33:40 +000020\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
21 Perform the requested operation on file descriptor \var{fd}.
22 The operation is defined by \var{op} and is operating system
Guido van Rossum7f61b351994-05-19 09:09:50 +000023 dependent. Typically these codes can be retrieved from the library
Fred Drake55e3cbd1998-04-03 06:54:27 +000024 module \module{FCNTL}\refstmodindex{FCNTL}. The argument \var{arg}
25 is optional, and defaults to the integer value \code{0}. When
26 present, it can either be an integer value, or a string. With
Guido van Rossum7f61b351994-05-19 09:09:50 +000027 the argument missing or an integer value, the return value of this
Fred Drakef6863c11999-03-02 16:37:17 +000028 function is the integer return value of the C \cfunction{fcntl()}
Guido van Rossum7f61b351994-05-19 09:09:50 +000029 call. When the argument is a string it represents a binary
Fred Drakec71585e1998-03-12 05:33:40 +000030 structure, e.g.\ created by \function{struct.pack()}. The binary
Fred Drakef6863c11999-03-02 16:37:17 +000031 data is copied to a buffer whose address is passed to the C
Fred Drakec71585e1998-03-12 05:33:40 +000032 \cfunction{fcntl()} call. The return value after a successful call
Fred Drake6c7a46a2000-08-02 20:53:51 +000033 is the contents of the buffer, converted to a string object. The length
34 of the returned string will be the same as the length of the \var{arg}
35 argument. This is limited to 1024 bytes. If the information returned
36 in the buffer by the operating system is larger than 1024 bytes,
37 this is most likely to result in a segmentation violation or a more
38 subtle data corruption.
39
40 If the \cfunction{fcntl()} fails, an \exception{IOError} is
Fred Drakec71585e1998-03-12 05:33:40 +000041 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000042\end{funcdesc}
43
Fred Drakec71585e1998-03-12 05:33:40 +000044\begin{funcdesc}{ioctl}{fd, op, arg}
45 This function is identical to the \function{fcntl()} function, except
Guido van Rossum7f61b351994-05-19 09:09:50 +000046 that the operations are typically defined in the library module
Fred Drakec71585e1998-03-12 05:33:40 +000047 \module{IOCTL}.
Guido van Rossum7f61b351994-05-19 09:09:50 +000048\end{funcdesc}
49
Fred Drakec71585e1998-03-12 05:33:40 +000050\begin{funcdesc}{flock}{fd, op}
Guido van Rossum50ec5c01996-06-26 19:20:33 +000051Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drake55e3cbd1998-04-03 06:54:27 +000052See the \UNIX{} manual \manpage{flock}{3} for details. (On some
53systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000054\end{funcdesc}
55
Barry Warsaw8ee1a4b2001-01-25 00:36:54 +000056\begin{funcdesc}{lockf}{fd, operation,
57 \optional{len, \optional{start, \optional{whence}}}}
58This is essentially a wrapper around the \function{fcntl()} locking
59calls. \var{fd} is the file descriptor of the file to lock or unlock,
60and \var{operation} is one of the following values:
61
62\begin{itemize}
63\item \constant{LOCK_UN} -- unlock
64\item \constant{LOCK_SH} -- acquire a shared lock
65\item \constant{LOCK_EX} -- acquire an exclusive lock
66\end{itemize}
67
68When \var{operation} is \constant{LOCK_SH} or \constant{LOCK_EX}, it
69can also be bit-wise OR'd with \constant{LOCK_NB} to avoid blocking on
70lock acquisition. If \constant{LOCK_NB} is used and the lock cannot
71be acquired, an \exception{IOError} will be raised and the exception
72will have an \var{errno} attribute set to \constant{EACCES} or
73\constant{EAGAIN} (depending on the operating system; for portability,
74check for both values).
75
76\var{length} is the number of bytes to lock, \var{start} is the byte
77offset at which the lock starts, relative to \var{whence}, and
78\var{whence} is as with \function{fileobj.seek()}, specifically:
79
80\begin{itemize}
81\item \constant{0} -- relative to the start of the file
82 (\constant{SEEK_SET})
83\item \constant{1} -- relative to the current buffer position
84 (\constant{SEEK_CUR})
85\item \constant{2} -- relative to the end of the file
86 (\constant{SEEK_END})
87\end{itemize}
88
89The default for \var{start} is 0, which means to start at the
90beginning of the file. The default for \var{length} is 0 which means
91to lock to the end of the file. The default for \var{whence} is also
920.
93
Guido van Rossum9b058111996-10-11 17:43:34 +000094\end{funcdesc}
95
Fred Drakec71585e1998-03-12 05:33:40 +000096If the library modules \module{FCNTL}\refstmodindex{FCNTL} or
97\module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the
Fred Drakef6863c11999-03-02 16:37:17 +000098opcodes in the C include files \code{<sys/fcntl.h>} and
Fred Drakec71585e1998-03-12 05:33:40 +000099\code{<sys/ioctl.h>}. You can create the modules yourself with the
100\program{h2py} script, found in the \file{Tools/scripts/} directory.
101
Guido van Rossum7f61b351994-05-19 09:09:50 +0000102
103Examples (all on a SVR4 compliant system):
104
Fred Drake19479911998-02-13 06:58:54 +0000105\begin{verbatim}
Guido van Rossumcf6905f1999-06-24 17:58:44 +0000106import struct, fcntl, FCNTL
Guido van Rossum7f61b351994-05-19 09:09:50 +0000107
108file = open(...)
Fred Drakeb942c2f2001-04-11 21:33:47 +0000109rv = fcntl(file.fileno(), FCNTL.F_SETFL, FCNTL.O_NDELAY)
Guido van Rossum7f61b351994-05-19 09:09:50 +0000110
111lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
Guido van Rossumcf6905f1999-06-24 17:58:44 +0000112rv = fcntl.fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +0000113\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +0000114
Fred Drakeb942c2f2001-04-11 21:33:47 +0000115Note that in the first example the return value variable \var{rv} will
Guido van Rossum7f61b351994-05-19 09:09:50 +0000116hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +0000117value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +0000118system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +0000119better.