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