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 |
Fred Drake | 6c7a46a | 2000-08-02 20:53:51 +0000 | [diff] [blame] | 33 | is the contents of the buffer, converted to a string object. The length |
| 34 | of the returned string will be the same as the length of the \var{arg} |
| 35 | argument. This is limited to 1024 bytes. If the information returned |
| 36 | in the buffer by the operating system is larger than 1024 bytes, |
| 37 | this is most likely to result in a segmentation violation or a more |
| 38 | subtle data corruption. |
| 39 | |
| 40 | If the \cfunction{fcntl()} fails, an \exception{IOError} is |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 41 | raised. |
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}{ioctl}{fd, op, arg} |
| 45 | This function is identical to the \function{fcntl()} function, except |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 46 | that the operations are typically defined in the library module |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 47 | \module{IOCTL}. |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 48 | \end{funcdesc} |
| 49 | |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 50 | \begin{funcdesc}{flock}{fd, op} |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 51 | Perform the lock operation \var{op} on file descriptor \var{fd}. |
Fred Drake | 55e3cbd | 1998-04-03 06:54:27 +0000 | [diff] [blame] | 52 | See the \UNIX{} manual \manpage{flock}{3} for details. (On some |
| 53 | systems, this function is emulated using \cfunction{fcntl()}.) |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 54 | \end{funcdesc} |
| 55 | |
Barry Warsaw | 8ee1a4b | 2001-01-25 00:36:54 +0000 | [diff] [blame^] | 56 | \begin{funcdesc}{lockf}{fd, operation, |
| 57 | \optional{len, \optional{start, \optional{whence}}}} |
| 58 | This is essentially a wrapper around the \function{fcntl()} locking |
| 59 | calls. \var{fd} is the file descriptor of the file to lock or unlock, |
| 60 | and \var{operation} is one of the following values: |
| 61 | |
| 62 | \begin{itemize} |
| 63 | \item \constant{LOCK_UN} -- unlock |
| 64 | \item \constant{LOCK_SH} -- acquire a shared lock |
| 65 | \item \constant{LOCK_EX} -- acquire an exclusive lock |
| 66 | \end{itemize} |
| 67 | |
| 68 | When \var{operation} is \constant{LOCK_SH} or \constant{LOCK_EX}, it |
| 69 | can also be bit-wise OR'd with \constant{LOCK_NB} to avoid blocking on |
| 70 | lock acquisition. If \constant{LOCK_NB} is used and the lock cannot |
| 71 | be acquired, an \exception{IOError} will be raised and the exception |
| 72 | will have an \var{errno} attribute set to \constant{EACCES} or |
| 73 | \constant{EAGAIN} (depending on the operating system; for portability, |
| 74 | check for both values). |
| 75 | |
| 76 | \var{length} is the number of bytes to lock, \var{start} is the byte |
| 77 | offset at which the lock starts, relative to \var{whence}, and |
| 78 | \var{whence} is as with \function{fileobj.seek()}, specifically: |
| 79 | |
| 80 | \begin{itemize} |
| 81 | \item \constant{0} -- relative to the start of the file |
| 82 | (\constant{SEEK_SET}) |
| 83 | \item \constant{1} -- relative to the current buffer position |
| 84 | (\constant{SEEK_CUR}) |
| 85 | \item \constant{2} -- relative to the end of the file |
| 86 | (\constant{SEEK_END}) |
| 87 | \end{itemize} |
| 88 | |
| 89 | The default for \var{start} is 0, which means to start at the |
| 90 | beginning of the file. The default for \var{length} is 0 which means |
| 91 | to lock to the end of the file. The default for \var{whence} is also |
| 92 | 0. |
| 93 | |
Guido van Rossum | 9b05811 | 1996-10-11 17:43:34 +0000 | [diff] [blame] | 94 | \end{funcdesc} |
| 95 | |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 96 | If the library modules \module{FCNTL}\refstmodindex{FCNTL} or |
| 97 | \module{IOCTL}\refstmodindex{IOCTL} are missing, you can find the |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 98 | opcodes in the C include files \code{<sys/fcntl.h>} and |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 99 | \code{<sys/ioctl.h>}. You can create the modules yourself with the |
| 100 | \program{h2py} script, found in the \file{Tools/scripts/} directory. |
| 101 | |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 102 | |
| 103 | Examples (all on a SVR4 compliant system): |
| 104 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 105 | \begin{verbatim} |
Guido van Rossum | cf6905f | 1999-06-24 17:58:44 +0000 | [diff] [blame] | 106 | import struct, fcntl, FCNTL |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 107 | |
| 108 | file = open(...) |
| 109 | rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1) |
| 110 | |
| 111 | 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] | 112 | rv = fcntl.fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 113 | \end{verbatim} |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 115 | Note that in the first example the return value variable \code{rv} will |
| 116 | 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] | 117 | value. The structure lay-out for the \var{lockdata} variable is |
Fred Drake | c71585e | 1998-03-12 05:33:40 +0000 | [diff] [blame] | 118 | system dependent --- therefore using the \function{flock()} call may be |
Guido van Rossum | 50ec5c0 | 1996-06-26 19:20:33 +0000 | [diff] [blame] | 119 | better. |