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