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