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