Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* fcntl module */ |
| 3 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 6 | #ifdef HAVE_SYS_FILE_H |
| 7 | #include <sys/file.h> |
| 8 | #endif |
| 9 | |
Guido van Rossum | 3d65fa3 | 1996-12-09 18:49:14 +0000 | [diff] [blame] | 10 | #include <sys/ioctl.h> |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 11 | #include <fcntl.h> |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 12 | #ifdef HAVE_STROPTS_H |
| 13 | #include <stropts.h> |
| 14 | #endif |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 15 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 16 | static int |
| 17 | conv_descriptor(PyObject *object, int *target) |
| 18 | { |
| 19 | int fd = PyObject_AsFileDescriptor(object); |
| 20 | |
| 21 | if (fd < 0) |
| 22 | return 0; |
| 23 | *target = fd; |
| 24 | return 1; |
| 25 | } |
| 26 | |
| 27 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 28 | /* fcntl(fd, opt, [arg]) */ |
| 29 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 30 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 31 | fcntl_fcntl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 32 | { |
| 33 | int fd; |
| 34 | int code; |
| 35 | int arg; |
| 36 | int ret; |
| 37 | char *str; |
| 38 | int len; |
| 39 | char buf[1024]; |
| 40 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 41 | if (PyArg_ParseTuple(args, "O&is#:fcntl", |
| 42 | conv_descriptor, &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 43 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 44 | PyErr_SetString(PyExc_ValueError, |
| 45 | "fcntl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
| 48 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 49 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 50 | ret = fcntl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 51 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 52 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 53 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 56 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 59 | PyErr_Clear(); |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 60 | arg = 0; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 61 | if (!PyArg_ParseTuple(args, |
| 62 | "O&i|i;fcntl requires a file or file descriptor," |
| 63 | " an integer and optionally a third integer or a string", |
| 64 | conv_descriptor, &fd, &code, &arg)) { |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 65 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 66 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 67 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 68 | ret = fcntl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 69 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 70 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 71 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 74 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 77 | PyDoc_STRVAR(fcntl_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 78 | "fcntl(fd, opt, [arg])\n\ |
| 79 | \n\ |
| 80 | Perform the requested operation on file descriptor fd. The operation\n\ |
Fred Drake | 1d53199 | 2001-05-10 15:54:32 +0000 | [diff] [blame] | 81 | is defined by op and is operating system dependent. These constants are\n\ |
| 82 | available from the fcntl module. The argument arg is optional, and\n\ |
| 83 | defaults to 0; it may be an int or a string. If arg is given as a string,\n\ |
| 84 | the return value of fcntl is a string of that length, containing the\n\ |
| 85 | resulting value put in the arg buffer by the operating system.The length\n\ |
| 86 | of the arg string is not allowed to exceed 1024 bytes. If the arg given\n\ |
| 87 | is an integer or if none is specified, the result value is an integer\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 88 | corresponding to the return value of the fcntl call in the C code."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* ioctl(fd, opt, [arg]) */ |
| 92 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 93 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 94 | fcntl_ioctl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 95 | { |
| 96 | int fd; |
| 97 | int code; |
| 98 | int arg; |
| 99 | int ret; |
| 100 | char *str; |
| 101 | int len; |
| 102 | char buf[1024]; |
| 103 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 104 | if (PyArg_ParseTuple(args, "O&is#:ioctl", |
| 105 | conv_descriptor, &fd, &code, &str, &len)) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 106 | if (len > sizeof buf) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 107 | PyErr_SetString(PyExc_ValueError, |
| 108 | "ioctl string arg too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | memcpy(buf, str, len); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 112 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 113 | ret = ioctl(fd, code, buf); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 114 | Py_END_ALLOW_THREADS |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 115 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 116 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 119 | return PyString_FromStringAndSize(buf, len); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 122 | PyErr_Clear(); |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 123 | arg = 0; |
Fred Drake | 460f069 | 2001-05-14 21:02:36 +0000 | [diff] [blame] | 124 | if (!PyArg_ParseTuple(args, |
| 125 | "O&i|i;ioctl requires a file or file descriptor," |
| 126 | " an integer and optionally a third integer or a string", |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 127 | conv_descriptor, &fd, &code, &arg)) { |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 128 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 129 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 130 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 131 | ret = ioctl(fd, code, arg); |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 133 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 134 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 137 | return PyInt_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 140 | PyDoc_STRVAR(ioctl_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 141 | "ioctl(fd, opt, [arg])\n\ |
| 142 | \n\ |
| 143 | Perform the requested operation on file descriptor fd. The operation\n\ |
| 144 | is defined by op and is operating system dependent. Typically these\n\ |
| 145 | codes can be retrieved from the library module IOCTL. The argument arg\n\ |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 146 | is optional, and defaults to 0; it may be an int or a string. If arg is\n\ |
| 147 | given as a string, the return value of ioctl is a string of that length,\n\ |
| 148 | containing the resulting value put in the arg buffer by the operating system.\n\ |
| 149 | The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\ |
| 150 | given is an integer or if none is specified, the result value is an integer\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 151 | corresponding to the return value of the ioctl call in the C code."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 154 | /* flock(fd, operation) */ |
| 155 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 156 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 157 | fcntl_flock(PyObject *self, PyObject *args) |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 158 | { |
| 159 | int fd; |
| 160 | int code; |
| 161 | int ret; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 162 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 163 | if (!PyArg_ParseTuple(args, "O&i:flock", |
| 164 | conv_descriptor, &fd, &code)) |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 167 | #ifdef HAVE_FLOCK |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 168 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 169 | ret = flock(fd, code); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 170 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 171 | #else |
| 172 | |
| 173 | #ifndef LOCK_SH |
| 174 | #define LOCK_SH 1 /* shared lock */ |
| 175 | #define LOCK_EX 2 /* exclusive lock */ |
| 176 | #define LOCK_NB 4 /* don't block when locking */ |
| 177 | #define LOCK_UN 8 /* unlock */ |
| 178 | #endif |
| 179 | { |
| 180 | struct flock l; |
| 181 | if (code == LOCK_UN) |
| 182 | l.l_type = F_UNLCK; |
| 183 | else if (code & LOCK_SH) |
| 184 | l.l_type = F_RDLCK; |
| 185 | else if (code & LOCK_EX) |
| 186 | l.l_type = F_WRLCK; |
| 187 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 188 | PyErr_SetString(PyExc_ValueError, |
| 189 | "unrecognized flock argument"); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | l.l_whence = l.l_start = l.l_len = 0; |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 193 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 194 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 195 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 196 | } |
| 197 | #endif /* HAVE_FLOCK */ |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 198 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 199 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 200 | return NULL; |
| 201 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 202 | Py_INCREF(Py_None); |
| 203 | return Py_None; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 206 | PyDoc_STRVAR(flock_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 207 | "flock(fd, operation)\n\ |
| 208 | \n\ |
| 209 | Perform the lock operation op on file descriptor fd. See the Unix \n\ |
| 210 | manual flock(3) for details. (On some systems, this function is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 211 | emulated using fcntl().)"); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 212 | |
| 213 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 214 | /* lockf(fd, operation) */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 215 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 216 | fcntl_lockf(PyObject *self, PyObject *args) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 217 | { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 218 | int fd, code, ret, whence = 0; |
| 219 | PyObject *lenobj = NULL, *startobj = NULL; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 220 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 221 | if (!PyArg_ParseTuple(args, "O&i|OOi:lockf", |
| 222 | conv_descriptor, &fd, &code, |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 223 | &lenobj, &startobj, &whence)) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 224 | return NULL; |
| 225 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 226 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 227 | PyErr_SetString(PyExc_NotImplementedError, |
| 228 | "lockf not supported on OS/2 (EMX)"); |
| 229 | return NULL; |
| 230 | #else |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 231 | #ifndef LOCK_SH |
| 232 | #define LOCK_SH 1 /* shared lock */ |
| 233 | #define LOCK_EX 2 /* exclusive lock */ |
| 234 | #define LOCK_NB 4 /* don't block when locking */ |
| 235 | #define LOCK_UN 8 /* unlock */ |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 236 | #endif /* LOCK_SH */ |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 237 | { |
| 238 | struct flock l; |
| 239 | if (code == LOCK_UN) |
| 240 | l.l_type = F_UNLCK; |
| 241 | else if (code & LOCK_SH) |
| 242 | l.l_type = F_RDLCK; |
| 243 | else if (code & LOCK_EX) |
| 244 | l.l_type = F_WRLCK; |
| 245 | else { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 246 | PyErr_SetString(PyExc_ValueError, |
| 247 | "unrecognized flock argument"); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 250 | l.l_start = l.l_len = 0; |
| 251 | if (startobj != NULL) { |
| 252 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 253 | l.l_start = PyInt_AsLong(startobj); |
| 254 | #else |
| 255 | l.l_start = PyLong_Check(startobj) ? |
| 256 | PyLong_AsLongLong(startobj) : |
| 257 | PyInt_AsLong(startobj); |
| 258 | #endif |
| 259 | if (PyErr_Occurred()) |
| 260 | return NULL; |
| 261 | } |
| 262 | if (lenobj != NULL) { |
| 263 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 264 | l.l_len = PyInt_AsLong(lenobj); |
| 265 | #else |
| 266 | l.l_len = PyLong_Check(lenobj) ? |
| 267 | PyLong_AsLongLong(lenobj) : |
| 268 | PyInt_AsLong(lenobj); |
| 269 | #endif |
| 270 | if (PyErr_Occurred()) |
| 271 | return NULL; |
| 272 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 273 | l.l_whence = whence; |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 274 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 275 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 276 | Py_END_ALLOW_THREADS |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 277 | } |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 278 | if (ret < 0) { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 279 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 282 | Py_INCREF(Py_None); |
| 283 | return Py_None; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 284 | #endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */ |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 285 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 287 | PyDoc_STRVAR(lockf_doc, |
Sjoerd Mullender | 82e00d6 | 2001-01-25 10:10:39 +0000 | [diff] [blame] | 288 | "lockf (fd, operation, length=0, start=0, whence=0)\n\ |
| 289 | \n\ |
| 290 | This is essentially a wrapper around the fcntl() locking calls. fd is the\n\ |
| 291 | file descriptor of the file to lock or unlock, and operation is one of the\n\ |
| 292 | following values:\n\ |
| 293 | \n\ |
| 294 | LOCK_UN - unlock\n\ |
| 295 | LOCK_SH - acquire a shared lock\n\ |
| 296 | LOCK_EX - acquire an exclusive lock\n\ |
| 297 | \n\ |
| 298 | When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\ |
| 299 | LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\ |
| 300 | lock cannot be acquired, an IOError will be raised and the exception will\n\ |
| 301 | have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\ |
| 302 | system -- for portability, check for either value).\n\ |
| 303 | \n\ |
| 304 | length is the number of bytes to lock, with the default meaning to lock to\n\ |
| 305 | EOF. start is the byte offset, relative to whence, to that the lock\n\ |
| 306 | starts. whence is as with fileobj.seek(), specifically:\n\ |
| 307 | \n\ |
| 308 | 0 - relative to the start of the file (SEEK_SET)\n\ |
| 309 | 1 - relative to the current buffer position (SEEK_CUR)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 310 | 2 - relative to the end of the file (SEEK_END)"); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 312 | /* List of functions */ |
| 313 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 314 | static PyMethodDef fcntl_methods[] = { |
Guido van Rossum | a2214c3 | 2000-08-02 20:46:51 +0000 | [diff] [blame] | 315 | {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc}, |
| 316 | {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc}, |
| 317 | {"flock", fcntl_flock, METH_VARARGS, flock_doc}, |
| 318 | {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 319 | {NULL, NULL} /* sentinel */ |
| 320 | }; |
| 321 | |
| 322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 323 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 324 | "This module performs file control and I/O control on file \n\ |
| 325 | descriptors. It is an interface to the fcntl() and ioctl() Unix\n\ |
| 326 | routines. File descriptors can be obtained with the fileno() method of\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 327 | a file or socket object."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 329 | /* Module initialisation */ |
| 330 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 331 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 332 | ins(PyObject* d, char* symbol, long value) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 333 | { |
| 334 | PyObject* v = PyInt_FromLong(value); |
| 335 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 336 | return -1; |
| 337 | |
| 338 | Py_DECREF(v); |
| 339 | return 0; |
| 340 | } |
| 341 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 342 | #define INS(x) if (ins(d, #x, (long)x)) return -1 |
| 343 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 344 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 345 | all_ins(PyObject* d) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 346 | { |
| 347 | if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1; |
| 348 | if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1; |
| 349 | if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1; |
| 350 | if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 351 | /* GNU extensions, as of glibc 2.2.4 */ |
| 352 | #ifdef LOCK_MAND |
| 353 | if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1; |
| 354 | #endif |
| 355 | #ifdef LOCK_READ |
| 356 | if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1; |
| 357 | #endif |
| 358 | #ifdef LOCK_WRITE |
| 359 | if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1; |
| 360 | #endif |
| 361 | #ifdef LOCK_RW |
| 362 | if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1; |
| 363 | #endif |
| 364 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 365 | #ifdef F_DUPFD |
| 366 | if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1; |
| 367 | #endif |
| 368 | #ifdef F_GETFD |
| 369 | if (ins(d, "F_GETFD", (long)F_GETFD)) return -1; |
| 370 | #endif |
| 371 | #ifdef F_SETFD |
| 372 | if (ins(d, "F_SETFD", (long)F_SETFD)) return -1; |
| 373 | #endif |
| 374 | #ifdef F_GETFL |
| 375 | if (ins(d, "F_GETFL", (long)F_GETFL)) return -1; |
| 376 | #endif |
| 377 | #ifdef F_SETFL |
| 378 | if (ins(d, "F_SETFL", (long)F_SETFL)) return -1; |
| 379 | #endif |
| 380 | #ifdef F_GETLK |
| 381 | if (ins(d, "F_GETLK", (long)F_GETLK)) return -1; |
| 382 | #endif |
| 383 | #ifdef F_SETLK |
| 384 | if (ins(d, "F_SETLK", (long)F_SETLK)) return -1; |
| 385 | #endif |
| 386 | #ifdef F_SETLKW |
| 387 | if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1; |
| 388 | #endif |
| 389 | #ifdef F_GETOWN |
| 390 | if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1; |
| 391 | #endif |
| 392 | #ifdef F_SETOWN |
| 393 | if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1; |
| 394 | #endif |
| 395 | #ifdef F_GETSIG |
| 396 | if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1; |
| 397 | #endif |
| 398 | #ifdef F_SETSIG |
| 399 | if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1; |
| 400 | #endif |
| 401 | #ifdef F_RDLCK |
| 402 | if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1; |
| 403 | #endif |
| 404 | #ifdef F_WRLCK |
| 405 | if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1; |
| 406 | #endif |
| 407 | #ifdef F_UNLCK |
| 408 | if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1; |
| 409 | #endif |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 410 | /* LFS constants */ |
| 411 | #ifdef F_GETLK64 |
| 412 | if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1; |
| 413 | #endif |
| 414 | #ifdef F_SETLK64 |
| 415 | if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1; |
| 416 | #endif |
| 417 | #ifdef F_SETLKW64 |
| 418 | if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1; |
| 419 | #endif |
| 420 | /* GNU extensions, as of glibc 2.2.4. */ |
| 421 | #ifdef F_SETLEASE |
| 422 | if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1; |
| 423 | #endif |
| 424 | #ifdef F_GETLEASE |
| 425 | if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1; |
| 426 | #endif |
| 427 | #ifdef F_NOTIFY |
| 428 | if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1; |
| 429 | #endif |
| 430 | /* Old BSD flock(). */ |
| 431 | #ifdef F_EXLCK |
| 432 | if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1; |
| 433 | #endif |
| 434 | #ifdef F_SHLCK |
| 435 | if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1; |
| 436 | #endif |
| 437 | |
| 438 | /* For F_{GET|SET}FL */ |
| 439 | #ifdef FD_CLOEXEC |
| 440 | if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1; |
| 441 | #endif |
| 442 | |
| 443 | /* For F_NOTIFY */ |
| 444 | #ifdef DN_ACCESS |
| 445 | if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1; |
| 446 | #endif |
| 447 | #ifdef DN_MODIFY |
| 448 | if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1; |
| 449 | #endif |
| 450 | #ifdef DN_CREATE |
| 451 | if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1; |
| 452 | #endif |
| 453 | #ifdef DN_DELETE |
| 454 | if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1; |
| 455 | #endif |
| 456 | #ifdef DN_RENAME |
| 457 | if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1; |
| 458 | #endif |
| 459 | #ifdef DN_ATTRIB |
| 460 | if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1; |
| 461 | #endif |
| 462 | #ifdef DN_MULTISHOT |
| 463 | if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1; |
| 464 | #endif |
| 465 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 466 | #ifdef HAVE_STROPTS_H |
| 467 | /* Unix 98 guarantees that these are in stropts.h. */ |
| 468 | INS(I_PUSH); |
| 469 | INS(I_POP); |
| 470 | INS(I_LOOK); |
| 471 | INS(I_FLUSH); |
| 472 | INS(I_FLUSHBAND); |
| 473 | INS(I_SETSIG); |
| 474 | INS(I_GETSIG); |
| 475 | INS(I_FIND); |
| 476 | INS(I_PEEK); |
| 477 | INS(I_SRDOPT); |
| 478 | INS(I_GRDOPT); |
| 479 | INS(I_NREAD); |
| 480 | INS(I_FDINSERT); |
| 481 | INS(I_STR); |
| 482 | INS(I_SWROPT); |
| 483 | INS(I_GWROPT); |
| 484 | INS(I_SENDFD); |
| 485 | INS(I_RECVFD); |
| 486 | INS(I_LIST); |
| 487 | INS(I_ATMARK); |
| 488 | INS(I_CKBAND); |
| 489 | INS(I_GETBAND); |
| 490 | INS(I_CANPUT); |
| 491 | INS(I_SETCLTIME); |
| 492 | INS(I_GETCLTIME); |
| 493 | INS(I_LINK); |
| 494 | INS(I_UNLINK); |
| 495 | INS(I_PLINK); |
| 496 | INS(I_PUNLINK); |
| 497 | #endif |
| 498 | |
Guido van Rossum | 7c14103 | 1997-08-15 02:52:08 +0000 | [diff] [blame] | 499 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 502 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 503 | initfcntl(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 504 | { |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 505 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 507 | /* Create the module and add the functions and documentation */ |
| 508 | m = Py_InitModule3("fcntl", fcntl_methods, module_doc); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 509 | |
| 510 | /* Add some symbolic constants to the module */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 511 | d = PyModule_GetDict(m); |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 512 | all_ins(d); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 513 | } |