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