Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 1 | % Manual text by Jaap Vermeulen |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 2 | \section{Built-in Module \sectcode{fcntl}} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 3 | \bimodindex{fcntl} |
Fred Drake | 19b97b1 | 1998-02-09 20:16:18 +0000 | [diff] [blame^] | 4 | \indexii{UNIX@\UNIX{}}{file control} |
| 5 | \indexii{UNIX@\UNIX{}}{I/O control} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 7 | This module performs file control and I/O control on file descriptors. |
| 8 | It is an interface to the \dfn{fcntl()} and \dfn{ioctl()} \UNIX{} routines. |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 9 | File descriptors can be obtained with the \dfn{fileno()} method of a |
| 10 | file or socket object. |
| 11 | |
| 12 | The module defines the following functions: |
| 13 | |
| 14 | \renewcommand{\indexsubitem}{(in module struct)} |
| 15 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 16 | \begin{funcdesc}{fcntl}{fd\, op\optional{\, arg}} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 17 | Perform the requested operation on file descriptor \code{\var{fd}}. |
| 18 | The operation is defined by \code{\var{op}} and is operating system |
| 19 | dependent. Typically these codes can be retrieved from the library |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 20 | module \code{FCNTL}. The argument \code{\var{arg}} is optional, and |
| 21 | defaults to the integer value \code{0}. When |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 22 | it is present, it can either be an integer value, or a string. With |
| 23 | the argument missing or an integer value, the return value of this |
| 24 | function is the integer return value of the real \code{fcntl()} |
| 25 | call. When the argument is a string it represents a binary |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 26 | structure, e.g.\ created by \code{struct.pack()}. The binary data is |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 27 | copied to a buffer whose address is passed to the real \code{fcntl()} |
| 28 | call. The return value after a successful call is the contents of |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 29 | the buffer, converted to a string object. In case the |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 30 | \code{fcntl()} fails, an \code{IOError} will be raised. |
| 31 | \end{funcdesc} |
| 32 | |
| 33 | \begin{funcdesc}{ioctl}{fd\, op\, arg} |
| 34 | This function is identical to the \code{fcntl()} function, except |
| 35 | that the operations are typically defined in the library module |
| 36 | \code{IOCTL}. |
| 37 | \end{funcdesc} |
| 38 | |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 39 | \begin{funcdesc}{flock}{fd\, op} |
| 40 | Perform the lock operation \var{op} on file descriptor \var{fd}. |
Fred Drake | efc1e0f | 1998-01-13 19:00:33 +0000 | [diff] [blame] | 41 | See the \UNIX{} manual for details. (On some systems, this function is |
| 42 | emulated using \code{fcntl()}.) |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Guido van Rossum | 9b05811 | 1996-10-11 17:43:34 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{lockf}{fd\, code\, \optional{len\, \optional{start\, \optional{whence}}}} |
| 46 | This is a wrapper around the \code{F_SETLK} and \code{F_SETLKW} |
Fred Drake | efc1e0f | 1998-01-13 19:00:33 +0000 | [diff] [blame] | 47 | \code{fcntl()} calls. See the \UNIX{} manual for details. |
Guido van Rossum | 9b05811 | 1996-10-11 17:43:34 +0000 | [diff] [blame] | 48 | \end{funcdesc} |
| 49 | |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 50 | If the library modules \code{FCNTL} or \code{IOCTL} are missing, you |
Fred Drake | 19d5827 | 1997-12-15 22:23:52 +0000 | [diff] [blame] | 51 | can find the opcodes in the C include files \file{sys/fcntl.h} and |
| 52 | \file{sys/ioctl.h}. You can create the modules yourself with the h2py |
| 53 | script, found in the \file{Tools/scripts} directory. |
| 54 | \refstmodindex{FCNTL} |
| 55 | \refstmodindex{IOCTL} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 56 | |
| 57 | Examples (all on a SVR4 compliant system): |
| 58 | |
| 59 | \bcode\begin{verbatim} |
| 60 | import struct, FCNTL |
| 61 | |
| 62 | file = open(...) |
| 63 | rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1) |
| 64 | |
| 65 | lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0) |
| 66 | rv = fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata) |
| 67 | \end{verbatim}\ecode |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 68 | % |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 69 | Note that in the first example the return value variable \code{rv} will |
| 70 | hold an integer value; in the second example it will hold a string |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 71 | value. The structure lay-out for the \var{lockadata} variable is |
| 72 | system dependent -- therefore using the \code{flock()} call may be |
| 73 | better. |