blob: 6eccb4a5b641b903405b9722bbb2484196ea5d56 [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 Drakec37b65e2001-11-28 07:26:15 +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
Fred Drake6a6bb182001-11-28 07:48:32 +000019\code{sys.stdin} itself, which provides a \method{fileno()} which
20returns a genuine file descriptor.
Guido van Rossum7f61b351994-05-19 09:09:50 +000021
22The module defines the following functions:
23
Guido van Rossum7f61b351994-05-19 09:09:50 +000024
Fred Drakec71585e1998-03-12 05:33:40 +000025\begin{funcdesc}{fcntl}{fd, op\optional{, arg}}
Fred Drake6a6bb182001-11-28 07:48:32 +000026 Perform the requested operation on file descriptor \var{fd} (file
27 objects providing a \method{fileno()} method are accepted as well).
Fred Drakec71585e1998-03-12 05:33:40 +000028 The operation is defined by \var{op} and is operating system
Fred Draked0de57c2001-05-09 21:09:57 +000029 dependent. These codes are also found in the \module{fcntl}
30 module. The argument \var{arg} is optional, and defaults to the
31 integer value \code{0}. When present, it can either be an integer
32 value, or a string. With the argument missing or an integer value,
33 the return value of this function is the integer return value of the
34 C \cfunction{fcntl()} call. When the argument is a string it
35 represents a binary structure, e.g.\ created by
36 \function{struct.pack()}. The binary data is copied to a buffer
37 whose address is passed to the C \cfunction{fcntl()} call. The
38 return value after a successful call is the contents of the buffer,
39 converted to a string object. The length of the returned string
40 will be the same as the length of the \var{arg} argument. This is
41 limited to 1024 bytes. If the information returned in the buffer by
42 the operating system is larger than 1024 bytes, this is most likely
43 to result in a segmentation violation or a more subtle data
44 corruption.
Fred Drake6c7a46a2000-08-02 20:53:51 +000045
46 If the \cfunction{fcntl()} fails, an \exception{IOError} is
Fred Drakec71585e1998-03-12 05:33:40 +000047 raised.
Guido van Rossum7f61b351994-05-19 09:09:50 +000048\end{funcdesc}
49
Michael W. Hudsonf0089982003-03-03 12:29:42 +000050\begin{funcdesc}{ioctl}{fd, op\optional{, arg\optional{, mutate_flag}}}
51 This function is identical to the \function{fcntl()} function,
52 except that the operations are typically defined in the library
53 module \refmodule{termios} and the argument handling is even more
54 complicated.
55
56 The parameter \var{arg} can be one of an integer, absent (treated
57 identically to the integer \code{0}), an object supporting the
58 read-only buffer interface (most likely a plain Python string) or an
59 object supporting the read-write buffer interface.
60
61 In all but the last case, behaviour is as for the \function{fcntl()}
62 function.
63
64 If a mutable buffer is passed, then the behaviour is determined by
65 the value of the \var{mutate_flag} parameter.
66
67 If it is false, the buffer's mutability is ignored and behaviour is
68 as for a read-only buffer, except that the 1024 byte limit mentioned
69 above is avoided -- so long as the buffer you pass is longer than
70 what the operating system wants to put there, things should work.
71
72 If \var{mutate_flag} is true, then the buffer is (in effect) passed
73 to the underlying \function{ioctl()} system call, the latter's
74 return code is passed back to the calling Python, and the buffer's
75 new contents reflect the action of the \function{ioctl}. This is a
76 slight simplification, because if the supplied buffer is less than
77 1024 bytes long it is first copied into a static buffer 1024 bytes
78 long which is then passed to \function{ioctl} and copied back into
79 the supplied buffer.
80
81 If \var{mutate_flag} is not supplied, then in 2.3 it defaults to
82 false. This is planned to change over the next few Python versions:
83 in 2.4 failing to supply \var{mutate_flag} will get a warning but
84 the same behavior and in versions later than 2.5 it will default to
85 true.
86
87 An example:
88
89\begin{verbatim}
90>>> import array, fnctl, struct, termios, os
91>>> os.getpgrp()
9213341
93>>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0]
9413341
95>>> buf = array.array('h', [0])
96>>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
970
98>>> buf
99array('h', [13341])
100\end{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000101\end{funcdesc}
102
Fred Drakec71585e1998-03-12 05:33:40 +0000103\begin{funcdesc}{flock}{fd, op}
Fred Drake6a6bb182001-11-28 07:48:32 +0000104Perform the lock operation \var{op} on file descriptor \var{fd} (file
105 objects providing a \method{fileno()} method are accepted as well).
Fred Drake55e3cbd1998-04-03 06:54:27 +0000106See the \UNIX{} manual \manpage{flock}{3} for details. (On some
107systems, this function is emulated using \cfunction{fcntl()}.)
Guido van Rossum50ec5c01996-06-26 19:20:33 +0000108\end{funcdesc}
109
Barry Warsaw8ee1a4b2001-01-25 00:36:54 +0000110\begin{funcdesc}{lockf}{fd, operation,
111 \optional{len, \optional{start, \optional{whence}}}}
112This is essentially a wrapper around the \function{fcntl()} locking
113calls. \var{fd} is the file descriptor of the file to lock or unlock,
114and \var{operation} is one of the following values:
115
116\begin{itemize}
117\item \constant{LOCK_UN} -- unlock
118\item \constant{LOCK_SH} -- acquire a shared lock
119\item \constant{LOCK_EX} -- acquire an exclusive lock
120\end{itemize}
121
122When \var{operation} is \constant{LOCK_SH} or \constant{LOCK_EX}, it
123can also be bit-wise OR'd with \constant{LOCK_NB} to avoid blocking on
124lock acquisition. If \constant{LOCK_NB} is used and the lock cannot
125be acquired, an \exception{IOError} will be raised and the exception
126will have an \var{errno} attribute set to \constant{EACCES} or
127\constant{EAGAIN} (depending on the operating system; for portability,
Fred Drake6a6bb182001-11-28 07:48:32 +0000128check for both values). On at least some systems, \constant{LOCK_EX}
129can only be used if the file descriptor refers to a file opened for
130writing.
Barry Warsaw8ee1a4b2001-01-25 00:36:54 +0000131
132\var{length} is the number of bytes to lock, \var{start} is the byte
133offset at which the lock starts, relative to \var{whence}, and
134\var{whence} is as with \function{fileobj.seek()}, specifically:
135
136\begin{itemize}
137\item \constant{0} -- relative to the start of the file
138 (\constant{SEEK_SET})
139\item \constant{1} -- relative to the current buffer position
140 (\constant{SEEK_CUR})
141\item \constant{2} -- relative to the end of the file
142 (\constant{SEEK_END})
143\end{itemize}
144
145The default for \var{start} is 0, which means to start at the
146beginning of the file. The default for \var{length} is 0 which means
147to lock to the end of the file. The default for \var{whence} is also
1480.
Guido van Rossum9b058111996-10-11 17:43:34 +0000149\end{funcdesc}
150
Guido van Rossum7f61b351994-05-19 09:09:50 +0000151Examples (all on a SVR4 compliant system):
152
Fred Drake19479911998-02-13 06:58:54 +0000153\begin{verbatim}
Fred Draked0de57c2001-05-09 21:09:57 +0000154import struct, fcntl
Guido van Rossum7f61b351994-05-19 09:09:50 +0000155
156file = open(...)
Fred Draked0de57c2001-05-09 21:09:57 +0000157rv = fcntl(file, fcntl.F_SETFL, os.O_NDELAY)
Guido van Rossum7f61b351994-05-19 09:09:50 +0000158
Fred Draked0de57c2001-05-09 21:09:57 +0000159lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
160rv = fcntl.fcntl(file, fcntl.F_SETLKW, lockdata)
Fred Drake19479911998-02-13 06:58:54 +0000161\end{verbatim}
Fred Drakec71585e1998-03-12 05:33:40 +0000162
Fred Drakeb942c2f2001-04-11 21:33:47 +0000163Note that in the first example the return value variable \var{rv} will
Guido van Rossum7f61b351994-05-19 09:09:50 +0000164hold an integer value; in the second example it will hold a string
Fred Drake55e3cbd1998-04-03 06:54:27 +0000165value. The structure lay-out for the \var{lockdata} variable is
Fred Drakec71585e1998-03-12 05:33:40 +0000166system dependent --- therefore using the \function{flock()} call may be
Guido van Rossum50ec5c01996-06-26 19:20:33 +0000167better.
Skip Montanaroa5caa6f2003-01-15 21:08:19 +0000168
169\begin{seealso}
170 \seemodule{os}{The \function{os.open} function supports locking flags
171 and is available on a wider variety of platforms than
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000172 the \function{fcntl.lockf} and \function{fcntl.flock}
173 functions, providing a more platform-independent file
174 locking facility.}
Skip Montanaroa5caa6f2003-01-15 21:08:19 +0000175\end{seealso}