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