Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* fcntl module */ |
| 3 | |
Thomas Wouters | 26cc63f | 2006-03-02 00:21:10 +0000 | [diff] [blame] | 4 | #define PY_SSIZE_T_CLEAN |
| 5 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 8 | #ifdef HAVE_SYS_FILE_H |
| 9 | #include <sys/file.h> |
| 10 | #endif |
| 11 | |
Guido van Rossum | 3d65fa3 | 1996-12-09 18:49:14 +0000 | [diff] [blame] | 12 | #include <sys/ioctl.h> |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 13 | #include <fcntl.h> |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 14 | #ifdef HAVE_STROPTS_H |
| 15 | #include <stropts.h> |
| 16 | #endif |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 17 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 18 | static int |
| 19 | conv_descriptor(PyObject *object, int *target) |
| 20 | { |
| 21 | int fd = PyObject_AsFileDescriptor(object); |
| 22 | |
| 23 | if (fd < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | return 0; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 25 | *target = fd; |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 30 | /* fcntl(fd, opt, [arg]) */ |
| 31 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 32 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 33 | fcntl_fcntl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 34 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | int fd; |
| 36 | int code; |
| 37 | long arg; |
| 38 | int ret; |
| 39 | char *str; |
| 40 | Py_ssize_t len; |
| 41 | char buf[1024]; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | if (PyArg_ParseTuple(args, "O&is#:fcntl", |
| 44 | conv_descriptor, &fd, &code, &str, &len)) { |
| 45 | if (len > sizeof buf) { |
| 46 | PyErr_SetString(PyExc_ValueError, |
| 47 | "fcntl string arg too long"); |
| 48 | return NULL; |
| 49 | } |
| 50 | memcpy(buf, str, len); |
| 51 | Py_BEGIN_ALLOW_THREADS |
| 52 | ret = fcntl(fd, code, buf); |
| 53 | Py_END_ALLOW_THREADS |
| 54 | if (ret < 0) { |
| 55 | PyErr_SetFromErrno(PyExc_IOError); |
| 56 | return NULL; |
| 57 | } |
| 58 | return PyBytes_FromStringAndSize(buf, len); |
| 59 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | PyErr_Clear(); |
| 62 | arg = 0; |
| 63 | if (!PyArg_ParseTuple(args, |
| 64 | "O&i|l;fcntl requires a file or file descriptor," |
| 65 | " an integer and optionally a third integer or a string", |
| 66 | conv_descriptor, &fd, &code, &arg)) { |
| 67 | return NULL; |
| 68 | } |
| 69 | Py_BEGIN_ALLOW_THREADS |
| 70 | ret = fcntl(fd, code, arg); |
| 71 | Py_END_ALLOW_THREADS |
| 72 | if (ret < 0) { |
| 73 | PyErr_SetFromErrno(PyExc_IOError); |
| 74 | return NULL; |
| 75 | } |
| 76 | return PyLong_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(fcntl_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 80 | "fcntl(fd, opt, [arg])\n\ |
| 81 | \n\ |
| 82 | Perform the requested operation on file descriptor fd. The operation\n\ |
Fred Drake | 1d53199 | 2001-05-10 15:54:32 +0000 | [diff] [blame] | 83 | is defined by op and is operating system dependent. These constants are\n\ |
| 84 | available from the fcntl module. The argument arg is optional, and\n\ |
Georg Brandl | 1f94cd0 | 2010-09-05 17:09:18 +0000 | [diff] [blame] | 85 | defaults to 0; it may be an int or a string. If arg is given as a string,\n\ |
Fred Drake | 1d53199 | 2001-05-10 15:54:32 +0000 | [diff] [blame] | 86 | the return value of fcntl is a string of that length, containing the\n\ |
Georg Brandl | 1f94cd0 | 2010-09-05 17:09:18 +0000 | [diff] [blame] | 87 | resulting value put in the arg buffer by the operating system. The length\n\ |
| 88 | of the arg string is not allowed to exceed 1024 bytes. If the arg given\n\ |
Fred Drake | 1d53199 | 2001-05-10 15:54:32 +0000 | [diff] [blame] | 89 | 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] | 90 | 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] | 91 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 92 | |
| 93 | /* ioctl(fd, opt, [arg]) */ |
| 94 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 95 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 96 | fcntl_ioctl(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 97 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 98 | #define IOCTL_BUFSZ 1024 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | int fd; |
| 100 | /* In PyArg_ParseTuple below, we use the unsigned non-checked 'I' |
| 101 | format for the 'code' parameter because Python turns 0x8000000 |
| 102 | into either a large positive number (PyLong or PyInt on 64-bit |
| 103 | platforms) or a negative number on others (32-bit PyInt) |
| 104 | whereas the system expects it to be a 32bit bit field value |
| 105 | regardless of it being passed as an int or unsigned long on |
| 106 | various platforms. See the termios.TIOCSWINSZ constant across |
| 107 | platforms for an example of thise. |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | If any of the 64bit platforms ever decide to use more than 32bits |
| 110 | in their unsigned long ioctl codes this will break and need |
| 111 | special casing based on the platform being built on. |
| 112 | */ |
| 113 | unsigned int code; |
| 114 | int arg; |
| 115 | int ret; |
| 116 | Py_buffer pstr; |
| 117 | char *str; |
| 118 | Py_ssize_t len; |
| 119 | int mutate_arg = 1; |
| 120 | char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | if (PyArg_ParseTuple(args, "O&Iw*|i:ioctl", |
| 123 | conv_descriptor, &fd, &code, |
| 124 | &pstr, &mutate_arg)) { |
| 125 | char *arg; |
| 126 | str = pstr.buf; |
| 127 | len = pstr.len; |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | if (mutate_arg) { |
| 130 | if (len <= IOCTL_BUFSZ) { |
| 131 | memcpy(buf, str, len); |
| 132 | buf[len] = '\0'; |
| 133 | arg = buf; |
| 134 | } |
| 135 | else { |
| 136 | arg = str; |
| 137 | } |
| 138 | } |
| 139 | else { |
| 140 | if (len > IOCTL_BUFSZ) { |
| 141 | PyBuffer_Release(&pstr); |
| 142 | PyErr_SetString(PyExc_ValueError, |
| 143 | "ioctl string arg too long"); |
| 144 | return NULL; |
| 145 | } |
| 146 | else { |
| 147 | memcpy(buf, str, len); |
| 148 | buf[len] = '\0'; |
| 149 | arg = buf; |
| 150 | } |
| 151 | } |
| 152 | if (buf == arg) { |
| 153 | Py_BEGIN_ALLOW_THREADS /* think array.resize() */ |
| 154 | ret = ioctl(fd, code, arg); |
| 155 | Py_END_ALLOW_THREADS |
| 156 | } |
| 157 | else { |
| 158 | ret = ioctl(fd, code, arg); |
| 159 | } |
Antoine Pitrou | 5e38aae | 2010-09-07 16:30:09 +0000 | [diff] [blame] | 160 | if (mutate_arg && (len <= IOCTL_BUFSZ)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | memcpy(str, buf, len); |
| 162 | } |
| 163 | PyBuffer_Release(&pstr); /* No further access to str below this point */ |
| 164 | if (ret < 0) { |
| 165 | PyErr_SetFromErrno(PyExc_IOError); |
| 166 | return NULL; |
| 167 | } |
| 168 | if (mutate_arg) { |
| 169 | return PyLong_FromLong(ret); |
| 170 | } |
| 171 | else { |
| 172 | return PyBytes_FromStringAndSize(buf, len); |
| 173 | } |
| 174 | } |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | PyErr_Clear(); |
| 177 | if (PyArg_ParseTuple(args, "O&Is*:ioctl", |
| 178 | conv_descriptor, &fd, &code, &pstr)) { |
| 179 | str = pstr.buf; |
| 180 | len = pstr.len; |
| 181 | if (len > IOCTL_BUFSZ) { |
| 182 | PyBuffer_Release(&pstr); |
| 183 | PyErr_SetString(PyExc_ValueError, |
| 184 | "ioctl string arg too long"); |
| 185 | return NULL; |
| 186 | } |
| 187 | memcpy(buf, str, len); |
| 188 | buf[len] = '\0'; |
| 189 | Py_BEGIN_ALLOW_THREADS |
| 190 | ret = ioctl(fd, code, buf); |
| 191 | Py_END_ALLOW_THREADS |
| 192 | if (ret < 0) { |
| 193 | PyBuffer_Release(&pstr); |
| 194 | PyErr_SetFromErrno(PyExc_IOError); |
| 195 | return NULL; |
| 196 | } |
| 197 | PyBuffer_Release(&pstr); |
| 198 | return PyBytes_FromStringAndSize(buf, len); |
| 199 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | PyErr_Clear(); |
| 202 | arg = 0; |
| 203 | if (!PyArg_ParseTuple(args, |
| 204 | "O&I|i;ioctl requires a file or file descriptor," |
| 205 | " an integer and optionally an integer or buffer argument", |
| 206 | conv_descriptor, &fd, &code, &arg)) { |
| 207 | return NULL; |
| 208 | } |
| 209 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 210 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | ret = ioctl(fd, code, (void *)arg); |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 212 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | ret = ioctl(fd, code, arg); |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 214 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_END_ALLOW_THREADS |
| 216 | if (ret < 0) { |
| 217 | PyErr_SetFromErrno(PyExc_IOError); |
| 218 | return NULL; |
| 219 | } |
| 220 | return PyLong_FromLong((long)ret); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 221 | #undef IOCTL_BUFSZ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 224 | PyDoc_STRVAR(ioctl_doc, |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 225 | "ioctl(fd, opt[, arg[, mutate_flag]])\n\ |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 226 | \n\ |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 227 | Perform the requested operation on file descriptor fd. The operation is\n\ |
Neal Norwitz | 4730880 | 2003-06-30 01:54:04 +0000 | [diff] [blame] | 228 | defined by opt and is operating system dependent. Typically these codes are\n\ |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 229 | retrieved from the fcntl or termios library modules.\n\ |
| 230 | \n\ |
| 231 | The argument arg is optional, and defaults to 0; it may be an int or a\n\ |
| 232 | buffer containing character data (most likely a string or an array). \n\ |
| 233 | \n\ |
| 234 | If the argument is a mutable buffer (such as an array) and if the\n\ |
| 235 | mutate_flag argument (which is only allowed in this case) is true then the\n\ |
| 236 | buffer is (in effect) passed to the operating system and changes made by\n\ |
| 237 | the OS will be reflected in the contents of the buffer after the call has\n\ |
| 238 | returned. The return value is the integer returned by the ioctl system\n\ |
| 239 | call.\n\ |
| 240 | \n\ |
| 241 | If the argument is a mutable buffer and the mutable_flag argument is not\n\ |
| 242 | passed or is false, the behavior is as if a string had been passed. This\n\ |
| 243 | behavior will change in future releases of Python.\n\ |
| 244 | \n\ |
| 245 | If the argument is an immutable buffer (most likely a string) then a copy\n\ |
| 246 | of the buffer is passed to the operating system and the return value is a\n\ |
| 247 | string of the same length containing whatever the operating system put in\n\ |
| 248 | the buffer. The length of the arg buffer in this case is not allowed to\n\ |
| 249 | exceed 1024 bytes.\n\ |
| 250 | \n\ |
| 251 | If the arg given is an integer or if none is specified, the result value is\n\ |
| 252 | an integer corresponding to the return value of the ioctl call in the C\n\ |
| 253 | code."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 256 | /* flock(fd, operation) */ |
| 257 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 258 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 259 | fcntl_flock(PyObject *self, PyObject *args) |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | int fd; |
| 262 | int code; |
| 263 | int ret; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | if (!PyArg_ParseTuple(args, "O&i:flock", |
| 266 | conv_descriptor, &fd, &code)) |
| 267 | return NULL; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 269 | #ifdef HAVE_FLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | Py_BEGIN_ALLOW_THREADS |
| 271 | ret = flock(fd, code); |
| 272 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 273 | #else |
| 274 | |
| 275 | #ifndef LOCK_SH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | #define LOCK_SH 1 /* shared lock */ |
| 277 | #define LOCK_EX 2 /* exclusive lock */ |
| 278 | #define LOCK_NB 4 /* don't block when locking */ |
| 279 | #define LOCK_UN 8 /* unlock */ |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 280 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | { |
| 282 | struct flock l; |
| 283 | if (code == LOCK_UN) |
| 284 | l.l_type = F_UNLCK; |
| 285 | else if (code & LOCK_SH) |
| 286 | l.l_type = F_RDLCK; |
| 287 | else if (code & LOCK_EX) |
| 288 | l.l_type = F_WRLCK; |
| 289 | else { |
| 290 | PyErr_SetString(PyExc_ValueError, |
| 291 | "unrecognized flock argument"); |
| 292 | return NULL; |
| 293 | } |
| 294 | l.l_whence = l.l_start = l.l_len = 0; |
| 295 | Py_BEGIN_ALLOW_THREADS |
| 296 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 297 | Py_END_ALLOW_THREADS |
| 298 | } |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 299 | #endif /* HAVE_FLOCK */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | if (ret < 0) { |
| 301 | PyErr_SetFromErrno(PyExc_IOError); |
| 302 | return NULL; |
| 303 | } |
| 304 | Py_INCREF(Py_None); |
| 305 | return Py_None; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 308 | PyDoc_STRVAR(flock_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 309 | "flock(fd, operation)\n\ |
| 310 | \n\ |
| 311 | Perform the lock operation op on file descriptor fd. See the Unix \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 312 | manual page for 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] | 313 | emulated using fcntl().)"); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 314 | |
| 315 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 316 | /* lockf(fd, operation) */ |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 317 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 318 | fcntl_lockf(PyObject *self, PyObject *args) |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | int fd, code, ret, whence = 0; |
| 321 | PyObject *lenobj = NULL, *startobj = NULL; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | if (!PyArg_ParseTuple(args, "O&i|OOi:lockf", |
| 324 | conv_descriptor, &fd, &code, |
| 325 | &lenobj, &startobj, &whence)) |
| 326 | return NULL; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 328 | #ifndef LOCK_SH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | #define LOCK_SH 1 /* shared lock */ |
| 330 | #define LOCK_EX 2 /* exclusive lock */ |
| 331 | #define LOCK_NB 4 /* don't block when locking */ |
| 332 | #define LOCK_UN 8 /* unlock */ |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 333 | #endif /* LOCK_SH */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | { |
| 335 | struct flock l; |
| 336 | if (code == LOCK_UN) |
| 337 | l.l_type = F_UNLCK; |
| 338 | else if (code & LOCK_SH) |
| 339 | l.l_type = F_RDLCK; |
| 340 | else if (code & LOCK_EX) |
| 341 | l.l_type = F_WRLCK; |
| 342 | else { |
| 343 | PyErr_SetString(PyExc_ValueError, |
| 344 | "unrecognized lockf argument"); |
| 345 | return NULL; |
| 346 | } |
| 347 | l.l_start = l.l_len = 0; |
| 348 | if (startobj != NULL) { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 349 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | l.l_start = PyLong_AsLong(startobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 351 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | l.l_start = PyLong_Check(startobj) ? |
| 353 | PyLong_AsLongLong(startobj) : |
| 354 | PyLong_AsLong(startobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 355 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (PyErr_Occurred()) |
| 357 | return NULL; |
| 358 | } |
| 359 | if (lenobj != NULL) { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 360 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | l.l_len = PyLong_AsLong(lenobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 362 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | l.l_len = PyLong_Check(lenobj) ? |
| 364 | PyLong_AsLongLong(lenobj) : |
| 365 | PyLong_AsLong(lenobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 366 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | if (PyErr_Occurred()) |
| 368 | return NULL; |
| 369 | } |
| 370 | l.l_whence = whence; |
| 371 | Py_BEGIN_ALLOW_THREADS |
| 372 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 373 | Py_END_ALLOW_THREADS |
| 374 | } |
| 375 | if (ret < 0) { |
| 376 | PyErr_SetFromErrno(PyExc_IOError); |
| 377 | return NULL; |
| 378 | } |
| 379 | Py_INCREF(Py_None); |
| 380 | return Py_None; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 381 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 383 | PyDoc_STRVAR(lockf_doc, |
Sjoerd Mullender | 82e00d6 | 2001-01-25 10:10:39 +0000 | [diff] [blame] | 384 | "lockf (fd, operation, length=0, start=0, whence=0)\n\ |
| 385 | \n\ |
| 386 | This is essentially a wrapper around the fcntl() locking calls. fd is the\n\ |
| 387 | file descriptor of the file to lock or unlock, and operation is one of the\n\ |
| 388 | following values:\n\ |
| 389 | \n\ |
| 390 | LOCK_UN - unlock\n\ |
| 391 | LOCK_SH - acquire a shared lock\n\ |
| 392 | LOCK_EX - acquire an exclusive lock\n\ |
| 393 | \n\ |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 394 | When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n\ |
Sjoerd Mullender | 82e00d6 | 2001-01-25 10:10:39 +0000 | [diff] [blame] | 395 | LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\ |
| 396 | lock cannot be acquired, an IOError will be raised and the exception will\n\ |
| 397 | have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\ |
| 398 | system -- for portability, check for either value).\n\ |
| 399 | \n\ |
| 400 | length is the number of bytes to lock, with the default meaning to lock to\n\ |
| 401 | EOF. start is the byte offset, relative to whence, to that the lock\n\ |
| 402 | starts. whence is as with fileobj.seek(), specifically:\n\ |
| 403 | \n\ |
| 404 | 0 - relative to the start of the file (SEEK_SET)\n\ |
| 405 | 1 - relative to the current buffer position (SEEK_CUR)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 406 | 2 - relative to the end of the file (SEEK_END)"); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 408 | /* List of functions */ |
| 409 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 410 | static PyMethodDef fcntl_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc}, |
| 412 | {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc}, |
| 413 | {"flock", fcntl_flock, METH_VARARGS, flock_doc}, |
| 414 | {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc}, |
| 415 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 419 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 420 | "This module performs file control and I/O control on file \n\ |
| 421 | descriptors. It is an interface to the fcntl() and ioctl() Unix\n\ |
| 422 | 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] | 423 | a file or socket object."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 425 | /* Module initialisation */ |
| 426 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 427 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 428 | ins(PyObject* d, char* symbol, long value) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | PyObject* v = PyLong_FromLong(value); |
| 431 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 432 | return -1; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | Py_DECREF(v); |
| 435 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 438 | #define INS(x) if (ins(d, #x, (long)x)) return -1 |
| 439 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 440 | static int |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 441 | all_ins(PyObject* d) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1; |
| 444 | if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1; |
| 445 | if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1; |
| 446 | if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 447 | /* GNU extensions, as of glibc 2.2.4 */ |
| 448 | #ifdef LOCK_MAND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 450 | #endif |
| 451 | #ifdef LOCK_READ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 453 | #endif |
| 454 | #ifdef LOCK_WRITE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 456 | #endif |
| 457 | #ifdef LOCK_RW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 459 | #endif |
| 460 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 461 | #ifdef F_DUPFD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 463 | #endif |
| 464 | #ifdef F_GETFD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (ins(d, "F_GETFD", (long)F_GETFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 466 | #endif |
| 467 | #ifdef F_SETFD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | if (ins(d, "F_SETFD", (long)F_SETFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 469 | #endif |
| 470 | #ifdef F_GETFL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | if (ins(d, "F_GETFL", (long)F_GETFL)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 472 | #endif |
| 473 | #ifdef F_SETFL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | if (ins(d, "F_SETFL", (long)F_SETFL)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 475 | #endif |
| 476 | #ifdef F_GETLK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | if (ins(d, "F_GETLK", (long)F_GETLK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 478 | #endif |
| 479 | #ifdef F_SETLK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | if (ins(d, "F_SETLK", (long)F_SETLK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 481 | #endif |
| 482 | #ifdef F_SETLKW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 484 | #endif |
| 485 | #ifdef F_GETOWN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 487 | #endif |
| 488 | #ifdef F_SETOWN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 490 | #endif |
| 491 | #ifdef F_GETSIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 493 | #endif |
| 494 | #ifdef F_SETSIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 496 | #endif |
| 497 | #ifdef F_RDLCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 499 | #endif |
| 500 | #ifdef F_WRLCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 502 | #endif |
| 503 | #ifdef F_UNLCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 505 | #endif |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 506 | /* LFS constants */ |
| 507 | #ifdef F_GETLK64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 509 | #endif |
| 510 | #ifdef F_SETLK64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 512 | #endif |
| 513 | #ifdef F_SETLKW64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 515 | #endif |
| 516 | /* GNU extensions, as of glibc 2.2.4. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 517 | #ifdef FASYNC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if (ins(d, "FASYNC", (long)FASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 519 | #endif |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 520 | #ifdef F_SETLEASE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 522 | #endif |
| 523 | #ifdef F_GETLEASE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 525 | #endif |
| 526 | #ifdef F_NOTIFY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 528 | #endif |
| 529 | /* Old BSD flock(). */ |
| 530 | #ifdef F_EXLCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 532 | #endif |
| 533 | #ifdef F_SHLCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 535 | #endif |
| 536 | |
Charles-François Natali | 23e1ecb | 2011-11-02 18:58:25 +0100 | [diff] [blame] | 537 | /* OS X specifics */ |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 538 | #ifdef F_FULLFSYNC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | if (ins(d, "F_FULLFSYNC", (long)F_FULLFSYNC)) return -1; |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 540 | #endif |
Charles-François Natali | 23e1ecb | 2011-11-02 18:58:25 +0100 | [diff] [blame] | 541 | #ifdef F_NOCACHE |
| 542 | if (ins(d, "F_NOCACHE", (long)F_NOCACHE)) return -1; |
| 543 | #endif |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 544 | |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 545 | /* For F_{GET|SET}FL */ |
| 546 | #ifdef FD_CLOEXEC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | /* For F_NOTIFY */ |
| 551 | #ifdef DN_ACCESS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 553 | #endif |
| 554 | #ifdef DN_MODIFY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 556 | #endif |
| 557 | #ifdef DN_CREATE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 559 | #endif |
| 560 | #ifdef DN_DELETE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 562 | #endif |
| 563 | #ifdef DN_RENAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 565 | #endif |
| 566 | #ifdef DN_ATTRIB |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 568 | #endif |
| 569 | #ifdef DN_MULTISHOT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 571 | #endif |
| 572 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 573 | #ifdef HAVE_STROPTS_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | /* Unix 98 guarantees that these are in stropts.h. */ |
| 575 | INS(I_PUSH); |
| 576 | INS(I_POP); |
| 577 | INS(I_LOOK); |
| 578 | INS(I_FLUSH); |
| 579 | INS(I_FLUSHBAND); |
| 580 | INS(I_SETSIG); |
| 581 | INS(I_GETSIG); |
| 582 | INS(I_FIND); |
| 583 | INS(I_PEEK); |
| 584 | INS(I_SRDOPT); |
| 585 | INS(I_GRDOPT); |
| 586 | INS(I_NREAD); |
| 587 | INS(I_FDINSERT); |
| 588 | INS(I_STR); |
| 589 | INS(I_SWROPT); |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 590 | #ifdef I_GWROPT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | /* despite the comment above, old-ish glibcs miss a couple... */ |
| 592 | INS(I_GWROPT); |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 593 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | INS(I_SENDFD); |
| 595 | INS(I_RECVFD); |
| 596 | INS(I_LIST); |
| 597 | INS(I_ATMARK); |
| 598 | INS(I_CKBAND); |
| 599 | INS(I_GETBAND); |
| 600 | INS(I_CANPUT); |
| 601 | INS(I_SETCLTIME); |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 602 | #ifdef I_GETCLTIME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | INS(I_GETCLTIME); |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 604 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | INS(I_LINK); |
| 606 | INS(I_UNLINK); |
| 607 | INS(I_PLINK); |
| 608 | INS(I_PUNLINK); |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 609 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | |
| 611 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 614 | |
| 615 | static struct PyModuleDef fcntlmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | PyModuleDef_HEAD_INIT, |
| 617 | "fcntl", |
| 618 | module_doc, |
| 619 | -1, |
| 620 | fcntl_methods, |
| 621 | NULL, |
| 622 | NULL, |
| 623 | NULL, |
| 624 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 627 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 628 | PyInit_fcntl(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | /* Create the module and add the functions and documentation */ |
| 633 | m = PyModule_Create(&fcntlmodule); |
| 634 | if (m == NULL) |
| 635 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | /* Add some symbolic constants to the module */ |
| 638 | d = PyModule_GetDict(m); |
| 639 | all_ins(d); |
| 640 | return m; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 641 | } |