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 | |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 23 | #include "clinic/fcntlmodule.c.h" |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 24 | |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 25 | /*[clinic input] |
| 26 | fcntl.fcntl |
| 27 | |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 28 | fd: fildes |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 29 | cmd as code: int |
| 30 | arg: object(c_default='NULL') = 0 |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 31 | / |
| 32 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 33 | Perform the operation `cmd` on file descriptor fd. |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 34 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 35 | The values used for `cmd` are operating system dependent, and are available |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 36 | as constants in the fcntl module, using the same names as used in |
| 37 | the relevant C header files. The argument arg is optional, and |
| 38 | defaults to 0; it may be an int or a string. If arg is given as a string, |
| 39 | the return value of fcntl is a string of that length, containing the |
| 40 | resulting value put in the arg buffer by the operating system. The length |
| 41 | of the arg string is not allowed to exceed 1024 bytes. If the arg given |
| 42 | is an integer or if none is specified, the result value is an integer |
| 43 | corresponding to the return value of the fcntl call in the C code. |
| 44 | [clinic start generated code]*/ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 45 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 46 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 47 | fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg) |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 48 | /*[clinic end generated code: output=888fc93b51c295bd input=7955340198e5f334]*/ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 49 | { |
Serhiy Storchaka | 5a8dacf | 2014-11-10 11:25:50 +0200 | [diff] [blame] | 50 | unsigned int int_arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | int ret; |
| 52 | char *str; |
| 53 | Py_ssize_t len; |
| 54 | char buf[1024]; |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 55 | int async_err = 0; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 56 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 57 | if (PySys_Audit("fcntl.fcntl", "iiO", fd, code, arg ? arg : Py_None) < 0) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 61 | if (arg != NULL) { |
| 62 | int parse_result; |
| 63 | |
| 64 | if (PyArg_Parse(arg, "s#", &str, &len)) { |
| 65 | if ((size_t)len > sizeof buf) { |
| 66 | PyErr_SetString(PyExc_ValueError, |
| 67 | "fcntl string arg too long"); |
| 68 | return NULL; |
| 69 | } |
| 70 | memcpy(buf, str, len); |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 71 | do { |
| 72 | Py_BEGIN_ALLOW_THREADS |
| 73 | ret = fcntl(fd, code, buf); |
| 74 | Py_END_ALLOW_THREADS |
| 75 | } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 76 | if (ret < 0) { |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 77 | return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL; |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 78 | } |
| 79 | return PyBytes_FromStringAndSize(buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | } |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 81 | |
| 82 | PyErr_Clear(); |
| 83 | parse_result = PyArg_Parse(arg, |
Serhiy Storchaka | 5a8dacf | 2014-11-10 11:25:50 +0200 | [diff] [blame] | 84 | "I;fcntl requires a file or file descriptor," |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 85 | " an integer and optionally a third integer or a string", |
| 86 | &int_arg); |
| 87 | if (!parse_result) { |
| 88 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 91 | |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 92 | do { |
| 93 | Py_BEGIN_ALLOW_THREADS |
| 94 | ret = fcntl(fd, code, (int)int_arg); |
| 95 | Py_END_ALLOW_THREADS |
| 96 | } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (ret < 0) { |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 98 | return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | } |
| 100 | return PyLong_FromLong((long)ret); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 103 | |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 104 | /*[clinic input] |
| 105 | fcntl.ioctl |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 106 | |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 107 | fd: fildes |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 108 | request as code: unsigned_int(bitwise=True) |
| 109 | arg as ob_arg: object(c_default='NULL') = 0 |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 110 | mutate_flag as mutate_arg: bool = True |
| 111 | / |
| 112 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 113 | Perform the operation `request` on file descriptor `fd`. |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 114 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 115 | The values used for `request` are operating system dependent, and are available |
| 116 | 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] | 117 | used in the relevant C header files. |
| 118 | |
| 119 | The argument `arg` is optional, and defaults to 0; it may be an int or a |
| 120 | buffer containing character data (most likely a string or an array). |
| 121 | |
| 122 | If the argument is a mutable buffer (such as an array) and if the |
| 123 | mutate_flag argument (which is only allowed in this case) is true then the |
| 124 | buffer is (in effect) passed to the operating system and changes made by |
| 125 | the OS will be reflected in the contents of the buffer after the call has |
| 126 | returned. The return value is the integer returned by the ioctl system |
| 127 | call. |
| 128 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 129 | If the argument is a mutable buffer and the mutable_flag argument is false, |
| 130 | the behavior is as if a string had been passed. |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 131 | |
| 132 | If the argument is an immutable buffer (most likely a string) then a copy |
| 133 | of the buffer is passed to the operating system and the return value is a |
| 134 | string of the same length containing whatever the operating system put in |
| 135 | the buffer. The length of the arg buffer in this case is not allowed to |
| 136 | exceed 1024 bytes. |
| 137 | |
| 138 | If the arg given is an integer or if none is specified, the result value is |
| 139 | an integer corresponding to the return value of the ioctl call in the C |
| 140 | code. |
| 141 | [clinic start generated code]*/ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 142 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 143 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 144 | fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 145 | PyObject *ob_arg, int mutate_arg) |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 146 | /*[clinic end generated code: output=7f7f5840c65991be input=967b4a4cbeceb0a8]*/ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 147 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 148 | #define IOCTL_BUFSZ 1024 |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 149 | /* We use the unsigned non-checked 'I' format for the 'code' parameter |
| 150 | because the system expects it to be a 32bit bit field value |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | regardless of it being passed as an int or unsigned long on |
| 152 | various platforms. See the termios.TIOCSWINSZ constant across |
R David Murray | d5a2f0b | 2013-11-07 10:51:07 -0500 | [diff] [blame] | 153 | platforms for an example of this. |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | If any of the 64bit platforms ever decide to use more than 32bits |
| 156 | in their unsigned long ioctl codes this will break and need |
| 157 | special casing based on the platform being built on. |
| 158 | */ |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 159 | int arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | int ret; |
| 161 | Py_buffer pstr; |
| 162 | char *str; |
| 163 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 165 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 166 | if (PySys_Audit("fcntl.ioctl", "iIO", fd, code, |
| 167 | ob_arg ? ob_arg : Py_None) < 0) { |
| 168 | return NULL; |
| 169 | } |
| 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) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 213 | PyErr_SetFromErrno(PyExc_OSError); |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 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); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 241 | PyErr_SetFromErrno(PyExc_OSError); |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 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) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 261 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 271 | fd: fildes |
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) |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 283 | /*[clinic end generated code: output=84059e2b37d2fc64 input=0bfc00f795953452]*/ |
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; |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 286 | int async_err = 0; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 287 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 288 | if (PySys_Audit("fcntl.flock", "ii", fd, code) < 0) { |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 292 | #ifdef HAVE_FLOCK |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 293 | do { |
| 294 | Py_BEGIN_ALLOW_THREADS |
| 295 | ret = flock(fd, code); |
| 296 | Py_END_ALLOW_THREADS |
| 297 | } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 298 | #else |
| 299 | |
| 300 | #ifndef LOCK_SH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | #define LOCK_SH 1 /* shared lock */ |
| 302 | #define LOCK_EX 2 /* exclusive lock */ |
| 303 | #define LOCK_NB 4 /* don't block when locking */ |
| 304 | #define LOCK_UN 8 /* unlock */ |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 305 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | { |
| 307 | struct flock l; |
| 308 | if (code == LOCK_UN) |
| 309 | l.l_type = F_UNLCK; |
| 310 | else if (code & LOCK_SH) |
| 311 | l.l_type = F_RDLCK; |
| 312 | else if (code & LOCK_EX) |
| 313 | l.l_type = F_WRLCK; |
| 314 | else { |
| 315 | PyErr_SetString(PyExc_ValueError, |
| 316 | "unrecognized flock argument"); |
| 317 | return NULL; |
| 318 | } |
| 319 | l.l_whence = l.l_start = l.l_len = 0; |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 320 | do { |
| 321 | Py_BEGIN_ALLOW_THREADS |
| 322 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 323 | Py_END_ALLOW_THREADS |
| 324 | } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | } |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 326 | #endif /* HAVE_FLOCK */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | if (ret < 0) { |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 328 | return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | } |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 330 | Py_RETURN_NONE; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 333 | |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 334 | /*[clinic input] |
| 335 | fcntl.lockf |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 336 | |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 337 | fd: fildes |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 338 | cmd as code: int |
| 339 | len as lenobj: object(c_default='NULL') = 0 |
| 340 | start as startobj: object(c_default='NULL') = 0 |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 341 | whence: int = 0 |
| 342 | / |
| 343 | |
| 344 | A wrapper around the fcntl() locking calls. |
| 345 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 346 | `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] | 347 | of the following values: |
| 348 | |
| 349 | LOCK_UN - unlock |
| 350 | LOCK_SH - acquire a shared lock |
| 351 | LOCK_EX - acquire an exclusive lock |
| 352 | |
| 353 | When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with |
| 354 | 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] | 355 | 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] | 356 | have an errno attribute set to EACCES or EAGAIN (depending on the operating |
| 357 | system -- for portability, check for either value). |
| 358 | |
Serhiy Storchaka | 17d3a58 | 2015-03-20 20:04:21 +0200 | [diff] [blame] | 359 | `len` is the number of bytes to lock, with the default meaning to lock to |
| 360 | EOF. `start` is the byte offset, relative to `whence`, to that the lock |
| 361 | starts. `whence` is as with fileobj.seek(), specifically: |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 362 | |
| 363 | 0 - relative to the start of the file (SEEK_SET) |
| 364 | 1 - relative to the current buffer position (SEEK_CUR) |
| 365 | 2 - relative to the end of the file (SEEK_END) |
| 366 | [clinic start generated code]*/ |
| 367 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 368 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 369 | fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 370 | PyObject *startobj, int whence) |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 371 | /*[clinic end generated code: output=4985e7a172e7461a input=5480479fc63a04b8]*/ |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 372 | { |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 373 | int ret; |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 374 | int async_err = 0; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 375 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 376 | if (PySys_Audit("fcntl.lockf", "iiOOi", fd, code, lenobj ? lenobj : Py_None, |
| 377 | startobj ? startobj : Py_None, whence) < 0) { |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 381 | #ifndef LOCK_SH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | #define LOCK_SH 1 /* shared lock */ |
| 383 | #define LOCK_EX 2 /* exclusive lock */ |
| 384 | #define LOCK_NB 4 /* don't block when locking */ |
| 385 | #define LOCK_UN 8 /* unlock */ |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 386 | #endif /* LOCK_SH */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | { |
| 388 | struct flock l; |
| 389 | if (code == LOCK_UN) |
| 390 | l.l_type = F_UNLCK; |
| 391 | else if (code & LOCK_SH) |
| 392 | l.l_type = F_RDLCK; |
| 393 | else if (code & LOCK_EX) |
| 394 | l.l_type = F_WRLCK; |
| 395 | else { |
| 396 | PyErr_SetString(PyExc_ValueError, |
| 397 | "unrecognized lockf argument"); |
| 398 | return NULL; |
| 399 | } |
| 400 | l.l_start = l.l_len = 0; |
| 401 | if (startobj != 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_start = PyLong_AsLong(startobj); |
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_start = PyLong_Check(startobj) ? |
| 406 | PyLong_AsLongLong(startobj) : |
| 407 | PyLong_AsLong(startobj); |
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 | if (lenobj != NULL) { |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 413 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | l.l_len = PyLong_AsLong(lenobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 415 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | l.l_len = PyLong_Check(lenobj) ? |
| 417 | PyLong_AsLongLong(lenobj) : |
| 418 | PyLong_AsLong(lenobj); |
Guido van Rossum | 056bad9 | 1999-01-06 18:44:23 +0000 | [diff] [blame] | 419 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | if (PyErr_Occurred()) |
| 421 | return NULL; |
| 422 | } |
| 423 | l.l_whence = whence; |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 424 | do { |
| 425 | Py_BEGIN_ALLOW_THREADS |
| 426 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 427 | Py_END_ALLOW_THREADS |
| 428 | } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | } |
| 430 | if (ret < 0) { |
nierob | b409ffa | 2018-11-23 16:46:12 +0100 | [diff] [blame] | 431 | return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | } |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 433 | Py_RETURN_NONE; |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 434 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 436 | /* List of functions */ |
| 437 | |
Roger E. Masse | 919213a | 1996-12-17 17:42:22 +0000 | [diff] [blame] | 438 | static PyMethodDef fcntl_methods[] = { |
Brett Cannon | b7299dd | 2014-11-09 20:22:01 -0500 | [diff] [blame] | 439 | FCNTL_FCNTL_METHODDEF |
| 440 | FCNTL_IOCTL_METHODDEF |
| 441 | FCNTL_FLOCK_METHODDEF |
| 442 | FCNTL_LOCKF_METHODDEF |
| 443 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 447 | PyDoc_STRVAR(module_doc, |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 448 | "This module performs file control and I/O control on file\n\ |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 449 | descriptors. It is an interface to the fcntl() and ioctl() Unix\n\ |
| 450 | 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] | 451 | a file or socket object."); |
Guido van Rossum | 185ead6 | 1998-11-23 15:32:55 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 453 | /* Module initialisation */ |
| 454 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 455 | |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 456 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 457 | all_ins(PyObject* m) |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 458 | { |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 459 | if (PyModule_AddIntMacro(m, LOCK_SH)) return -1; |
| 460 | if (PyModule_AddIntMacro(m, LOCK_EX)) return -1; |
| 461 | if (PyModule_AddIntMacro(m, LOCK_NB)) return -1; |
| 462 | if (PyModule_AddIntMacro(m, LOCK_UN)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 463 | /* GNU extensions, as of glibc 2.2.4 */ |
| 464 | #ifdef LOCK_MAND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 465 | if (PyModule_AddIntMacro(m, LOCK_MAND)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 466 | #endif |
| 467 | #ifdef LOCK_READ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 468 | if (PyModule_AddIntMacro(m, LOCK_READ)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 469 | #endif |
| 470 | #ifdef LOCK_WRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 471 | if (PyModule_AddIntMacro(m, LOCK_WRITE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 472 | #endif |
| 473 | #ifdef LOCK_RW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 474 | if (PyModule_AddIntMacro(m, LOCK_RW)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 475 | #endif |
| 476 | |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 477 | #ifdef F_DUPFD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 478 | if (PyModule_AddIntMacro(m, F_DUPFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 479 | #endif |
Victor Stinner | 2716d53 | 2013-01-08 00:52:40 +0100 | [diff] [blame] | 480 | #ifdef F_DUPFD_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 481 | if (PyModule_AddIntMacro(m, F_DUPFD_CLOEXEC)) return -1; |
Victor Stinner | 2716d53 | 2013-01-08 00:52:40 +0100 | [diff] [blame] | 482 | #endif |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 483 | #ifdef F_GETFD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 484 | if (PyModule_AddIntMacro(m, F_GETFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 485 | #endif |
| 486 | #ifdef F_SETFD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 487 | if (PyModule_AddIntMacro(m, F_SETFD)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 488 | #endif |
| 489 | #ifdef F_GETFL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 490 | if (PyModule_AddIntMacro(m, F_GETFL)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 491 | #endif |
| 492 | #ifdef F_SETFL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 493 | if (PyModule_AddIntMacro(m, F_SETFL)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 494 | #endif |
| 495 | #ifdef F_GETLK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 496 | if (PyModule_AddIntMacro(m, F_GETLK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 497 | #endif |
| 498 | #ifdef F_SETLK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 499 | if (PyModule_AddIntMacro(m, F_SETLK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 500 | #endif |
| 501 | #ifdef F_SETLKW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 502 | if (PyModule_AddIntMacro(m, F_SETLKW)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 503 | #endif |
Dong-hee Na | 3bfc8e0 | 2019-10-28 16:31:15 +0900 | [diff] [blame] | 504 | #ifdef F_OFD_GETLK |
| 505 | if (PyModule_AddIntMacro(m, F_OFD_GETLK)) return -1; |
| 506 | #endif |
| 507 | #ifdef F_OFD_SETLK |
| 508 | if (PyModule_AddIntMacro(m, F_OFD_SETLK)) return -1; |
| 509 | #endif |
| 510 | #ifdef F_OFD_SETLKW |
| 511 | if (PyModule_AddIntMacro(m, F_OFD_SETLKW)) return -1; |
| 512 | #endif |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 513 | #ifdef F_GETOWN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 514 | if (PyModule_AddIntMacro(m, F_GETOWN)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 515 | #endif |
| 516 | #ifdef F_SETOWN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 517 | if (PyModule_AddIntMacro(m, F_SETOWN)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 518 | #endif |
Vinay Sharma | 13f37f2 | 2019-08-29 07:26:17 +0530 | [diff] [blame] | 519 | #ifdef F_GETPATH |
| 520 | if (PyModule_AddIntMacro(m, F_GETPATH)) return -1; |
| 521 | #endif |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 522 | #ifdef F_GETSIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 523 | if (PyModule_AddIntMacro(m, F_GETSIG)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 524 | #endif |
| 525 | #ifdef F_SETSIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 526 | if (PyModule_AddIntMacro(m, F_SETSIG)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 527 | #endif |
| 528 | #ifdef F_RDLCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 529 | if (PyModule_AddIntMacro(m, F_RDLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 530 | #endif |
| 531 | #ifdef F_WRLCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 532 | if (PyModule_AddIntMacro(m, F_WRLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 533 | #endif |
| 534 | #ifdef F_UNLCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 535 | if (PyModule_AddIntMacro(m, F_UNLCK)) return -1; |
Fred Drake | 152a25e | 2001-05-09 21:02:02 +0000 | [diff] [blame] | 536 | #endif |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 537 | /* LFS constants */ |
| 538 | #ifdef F_GETLK64 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 539 | if (PyModule_AddIntMacro(m, F_GETLK64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 540 | #endif |
| 541 | #ifdef F_SETLK64 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 542 | if (PyModule_AddIntMacro(m, F_SETLK64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 543 | #endif |
| 544 | #ifdef F_SETLKW64 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 545 | if (PyModule_AddIntMacro(m, F_SETLKW64)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 546 | #endif |
| 547 | /* GNU extensions, as of glibc 2.2.4. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 548 | #ifdef FASYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 549 | if (PyModule_AddIntMacro(m, FASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 550 | #endif |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 551 | #ifdef F_SETLEASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 552 | if (PyModule_AddIntMacro(m, F_SETLEASE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 553 | #endif |
| 554 | #ifdef F_GETLEASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 555 | if (PyModule_AddIntMacro(m, F_GETLEASE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 556 | #endif |
| 557 | #ifdef F_NOTIFY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 558 | if (PyModule_AddIntMacro(m, F_NOTIFY)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 559 | #endif |
| 560 | /* Old BSD flock(). */ |
| 561 | #ifdef F_EXLCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 562 | if (PyModule_AddIntMacro(m, F_EXLCK)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 563 | #endif |
| 564 | #ifdef F_SHLCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 565 | if (PyModule_AddIntMacro(m, F_SHLCK)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 566 | #endif |
| 567 | |
Ruben Vorderman | 23c0fb8 | 2020-10-20 01:30:02 +0200 | [diff] [blame] | 568 | /* Linux specifics */ |
| 569 | #ifdef F_SETPIPE_SZ |
| 570 | if (PyModule_AddIntMacro(m, F_SETPIPE_SZ)) return -1; |
| 571 | #endif |
| 572 | #ifdef F_GETPIPE_SZ |
| 573 | if (PyModule_AddIntMacro(m, F_GETPIPE_SZ)) return -1; |
| 574 | #endif |
| 575 | |
Charles-François Natali | 23e1ecb | 2011-11-02 18:58:25 +0100 | [diff] [blame] | 576 | /* OS X specifics */ |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 577 | #ifdef F_FULLFSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 578 | if (PyModule_AddIntMacro(m, F_FULLFSYNC)) return -1; |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 579 | #endif |
Charles-François Natali | 23e1ecb | 2011-11-02 18:58:25 +0100 | [diff] [blame] | 580 | #ifdef F_NOCACHE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 581 | if (PyModule_AddIntMacro(m, F_NOCACHE)) return -1; |
Charles-François Natali | 23e1ecb | 2011-11-02 18:58:25 +0100 | [diff] [blame] | 582 | #endif |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 583 | |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 584 | /* For F_{GET|SET}FL */ |
| 585 | #ifdef FD_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 586 | if (PyModule_AddIntMacro(m, FD_CLOEXEC)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 587 | #endif |
| 588 | |
| 589 | /* For F_NOTIFY */ |
| 590 | #ifdef DN_ACCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 591 | if (PyModule_AddIntMacro(m, DN_ACCESS)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 592 | #endif |
| 593 | #ifdef DN_MODIFY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 594 | if (PyModule_AddIntMacro(m, DN_MODIFY)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 595 | #endif |
| 596 | #ifdef DN_CREATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 597 | if (PyModule_AddIntMacro(m, DN_CREATE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 598 | #endif |
| 599 | #ifdef DN_DELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 600 | if (PyModule_AddIntMacro(m, DN_DELETE)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 601 | #endif |
| 602 | #ifdef DN_RENAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 603 | if (PyModule_AddIntMacro(m, DN_RENAME)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 604 | #endif |
| 605 | #ifdef DN_ATTRIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 606 | if (PyModule_AddIntMacro(m, DN_ATTRIB)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 607 | #endif |
| 608 | #ifdef DN_MULTISHOT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 609 | if (PyModule_AddIntMacro(m, DN_MULTISHOT)) return -1; |
Martin v. Löwis | 1baeba6 | 2001-12-28 21:08:12 +0000 | [diff] [blame] | 610 | #endif |
| 611 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 612 | #ifdef HAVE_STROPTS_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | /* Unix 98 guarantees that these are in stropts.h. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 614 | if (PyModule_AddIntMacro(m, I_PUSH)) return -1; |
| 615 | if (PyModule_AddIntMacro(m, I_POP)) return -1; |
| 616 | if (PyModule_AddIntMacro(m, I_LOOK)) return -1; |
| 617 | if (PyModule_AddIntMacro(m, I_FLUSH)) return -1; |
| 618 | if (PyModule_AddIntMacro(m, I_FLUSHBAND)) return -1; |
| 619 | if (PyModule_AddIntMacro(m, I_SETSIG)) return -1; |
| 620 | if (PyModule_AddIntMacro(m, I_GETSIG)) return -1; |
| 621 | if (PyModule_AddIntMacro(m, I_FIND)) return -1; |
| 622 | if (PyModule_AddIntMacro(m, I_PEEK)) return -1; |
| 623 | if (PyModule_AddIntMacro(m, I_SRDOPT)) return -1; |
| 624 | if (PyModule_AddIntMacro(m, I_GRDOPT)) return -1; |
| 625 | if (PyModule_AddIntMacro(m, I_NREAD)) return -1; |
| 626 | if (PyModule_AddIntMacro(m, I_FDINSERT)) return -1; |
| 627 | if (PyModule_AddIntMacro(m, I_STR)) return -1; |
| 628 | if (PyModule_AddIntMacro(m, I_SWROPT)) return -1; |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 629 | #ifdef I_GWROPT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | /* despite the comment above, old-ish glibcs miss a couple... */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 631 | if (PyModule_AddIntMacro(m, I_GWROPT)) return -1; |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 632 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 633 | if (PyModule_AddIntMacro(m, I_SENDFD)) return -1; |
| 634 | if (PyModule_AddIntMacro(m, I_RECVFD)) return -1; |
| 635 | if (PyModule_AddIntMacro(m, I_LIST)) return -1; |
| 636 | if (PyModule_AddIntMacro(m, I_ATMARK)) return -1; |
| 637 | if (PyModule_AddIntMacro(m, I_CKBAND)) return -1; |
| 638 | if (PyModule_AddIntMacro(m, I_GETBAND)) return -1; |
| 639 | if (PyModule_AddIntMacro(m, I_CANPUT)) return -1; |
| 640 | if (PyModule_AddIntMacro(m, I_SETCLTIME)) return -1; |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 641 | #ifdef I_GETCLTIME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 642 | if (PyModule_AddIntMacro(m, I_GETCLTIME)) return -1; |
Michael W. Hudson | 505c4c2 | 2003-05-09 10:45:20 +0000 | [diff] [blame] | 643 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 644 | if (PyModule_AddIntMacro(m, I_LINK)) return -1; |
| 645 | if (PyModule_AddIntMacro(m, I_UNLINK)) return -1; |
| 646 | if (PyModule_AddIntMacro(m, I_PLINK)) return -1; |
| 647 | if (PyModule_AddIntMacro(m, I_PUNLINK)) return -1; |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 648 | #endif |
Christian Heimes | 8cbb5b6 | 2019-05-31 18:32:33 +0200 | [diff] [blame] | 649 | #ifdef F_ADD_SEALS |
| 650 | /* Linux: file sealing for memfd_create() */ |
| 651 | if (PyModule_AddIntMacro(m, F_ADD_SEALS)) return -1; |
| 652 | if (PyModule_AddIntMacro(m, F_GET_SEALS)) return -1; |
| 653 | if (PyModule_AddIntMacro(m, F_SEAL_SEAL)) return -1; |
| 654 | if (PyModule_AddIntMacro(m, F_SEAL_SHRINK)) return -1; |
| 655 | if (PyModule_AddIntMacro(m, F_SEAL_GROW)) return -1; |
| 656 | if (PyModule_AddIntMacro(m, F_SEAL_WRITE)) return -1; |
| 657 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | return 0; |
Guido van Rossum | f4e32c7 | 1997-07-31 19:39:54 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Dong-hee Na | e9684fa | 2020-06-02 01:12:24 +0900 | [diff] [blame] | 661 | static int |
| 662 | fcntl_exec(PyObject *module) |
| 663 | { |
| 664 | if (all_ins(module) < 0) { |
| 665 | return -1; |
| 666 | } |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static PyModuleDef_Slot fcntl_slots[] = { |
| 671 | {Py_mod_exec, fcntl_exec}, |
| 672 | {0, NULL} |
| 673 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 674 | |
| 675 | static struct PyModuleDef fcntlmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | PyModuleDef_HEAD_INIT, |
Dong-hee Na | e9684fa | 2020-06-02 01:12:24 +0900 | [diff] [blame] | 677 | .m_name = "fcntl", |
| 678 | .m_doc = module_doc, |
| 679 | .m_size = 0, |
| 680 | .m_methods = fcntl_methods, |
| 681 | .m_slots = fcntl_slots, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 682 | }; |
| 683 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 684 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 685 | PyInit_fcntl(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 686 | { |
Dong-hee Na | e9684fa | 2020-06-02 01:12:24 +0900 | [diff] [blame] | 687 | return PyModuleDef_Init(&fcntlmodule); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 688 | } |