blob: d02eaa5eb5e1b09b811290e8fefb9d635ecebc1e [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()}
Fred Draked0de57c2001-05-09 21:09:57 +000014\UNIX{} routines.
15
16All functions in this module take a file descriptor \var{fd} as their
17first argument. This can be an integer file descriptor, such as
18returned by \code{sys.stdin.fileno()}, or a file object, such as
19\code{sys.stdin} itself.
Guido van Rossum7f61b351994-05-19 09:09:50 +000020
21The module defines the following functions:
22
Guido van Rossum7f61b351994-05-19 09:09:50 +000023
Fred Drakec71585e1998-03-12 05:33:40 +000024\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
25 Perform the requested operation on file descriptor \var{fd}.
26 The operation is defined by \var{op} and is operating system
Fred Draked0de57c2001-05-09 21:09:57 +000027 dependent. These codes are also found in the \module{fcntl}
28 module. The argument \var{arg} is optional, and defaults to the
29 integer value \code{0}. When present, it can either be an integer
30 value, or a string. With the argument missing or an integer value,
31 the return value of this function is the integer return value of the
32 C \cfunction{fcntl()} call. When the argument is a string it
33 represents a binary structure, e.g.\ created by
34 \function{struct.pack()}. The binary data is copied to a buffer
35 whose address is passed to the C \cfunction{fcntl()} call. The
36 return value after a successful call is the contents of the buffer,
37 converted to a string object. The length of the returned string
38 will be the same as the length of the \var{arg} argument. This is
39 limited to 1024 bytes. If the information returned in the buffer by
40 the operating system is larger than 1024 bytes, this is most likely
41 to result in a segmentation violation or a more subtle data
42 corruption.
Fred Drake6c7a46a2000-08-02 20:53:51 +000043
44 If the \cfunction{fcntl()} fails, an \exception{IOError} is
Fred Drakec71585e1998-03-12 05:33:40 +000045 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000046\end{funcdesc}
47
Fred Drakec71585e1998-03-12 05:33:40 +000048\begin{funcdesc}{ioctl}{fd, op, arg}
49 This function is identical to the \function{fcntl()} function, except
Guido van Rossum7f61b351994-05-19 09:09:50 +000050 that the operations are typically defined in the library module
Fred Drakec71585e1998-03-12 05:33:40 +000051 \module{IOCTL}.
Guido van Rossum7f61b351994-05-19 09:09:50 +000052\end{funcdesc}
53
Fred Drakec71585e1998-03-12 05:33:40 +000054\begin{funcdesc}{flock}{fd, op}
Guido van Rossum50ec5c01996-06-26 19:20:33 +000055Perform the lock operation \var{op} on file descriptor \var{fd}.
Fred Drake55e3cbd1998-04-03 06:54:27 +000056See the \UNIX{} manual \manpage{flock}{3} for details. (On some
57systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +000058\end{funcdesc}
59
Barry Warsaw8ee1a4b2001-01-25 00:36:54 +000060\begin{funcdesc}{lockf}{fd, operation,
61 \optional{len, \optional{start, \optional{whence}}}}
62This is essentially a wrapper around the \function{fcntl()} locking
63calls. \var{fd} is the file descriptor of the file to lock or unlock,
64and \var{operation} is one of the following values:
65
66\begin{itemize}
67\item \constant{LOCK_UN} -- unlock
68\item \constant{LOCK_SH} -- acquire a shared lock
69\item \constant{LOCK_EX} -- acquire an exclusive lock
70\end{itemize}
71
72When \var{operation} is \constant{LOCK_SH} or \constant{LOCK_EX}, it
73can also be bit-wise OR'd with \constant{LOCK_NB} to avoid blocking on
74lock acquisition. If \constant{LOCK_NB} is used and the lock cannot
75be acquired, an \exception{IOError} will be raised and the exception
76will have an \var{errno} attribute set to \constant{EACCES} or
77\constant{EAGAIN} (depending on the operating system; for portability,
78check for both values).
79
80\var{length} is the number of bytes to lock, \var{start} is the byte
81offset at which the lock starts, relative to \var{whence}, and
82\var{whence} is as with \function{fileobj.seek()}, specifically:
83
84\begin{itemize}
85\item \constant{0} -- relative to the start of the file
86 (\constant{SEEK_SET})
87\item \constant{1} -- relative to the current buffer position
88 (\constant{SEEK_CUR})
89\item \constant{2} -- relative to the end of the file
90 (\constant{SEEK_END})
91\end{itemize}
92
93The default for \var{start} is 0, which means to start at the
94beginning of the file. The default for \var{length} is 0 which means
95to lock to the end of the file. The default for \var{whence} is also
960.
Guido van Rossum9b058111996-10-11 17:43:34 +000097\end{funcdesc}
98
Guido van Rossum7f61b351994-05-19 09:09:50 +000099Examples (all on a SVR4 compliant system):
100
Fred Drake19479911998-02-13 06:58:54 +0000101\begin{verbatim}
Fred Draked0de57c2001-05-09 21:09:57 +0000102import struct, fcntl
Guido van Rossum7f61b351994-05-19 09:09:50 +0000103
104file = open(...)
Fred Draked0de57c2001-05-09 21:09:57 +0000105rv = fcntl(file, fcntl.F_SETFL, os.O_NDELAY)
Guido van Rossum7f61b351994-05-19 09:09:50 +0000106
Fred Draked0de57c2001-05-09 21:09:57 +0000107lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
108rv = fcntl.fcntl(file, fcntl.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +0000109\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +0000110
Fred Drakeb942c2f2001-04-11 21:33:47 +0000111Note that in the first example the return value variable \var{rv} will
Guido van Rossum7f61b351994-05-19 09:09:50 +0000112hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +0000113value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +0000114system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +0000115better.