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