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