blob: 38ef55f0d6009557246fec2f701e92f59078935d [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001
2/* fcntl module */
3
Thomas Wouters26cc63f2006-03-02 00:21:10 +00004#define PY_SSIZE_T_CLEAN
5
Roger E. Masse919213a1996-12-17 17:42:22 +00006#include "Python.h"
Guido van Rossum02975121992-08-17 08:55:12 +00007
Guido van Rossuma376cc51996-12-05 23:43:35 +00008#ifdef HAVE_SYS_FILE_H
9#include <sys/file.h>
10#endif
11
Guido van Rossum3d65fa31996-12-09 18:49:14 +000012#include <sys/ioctl.h>
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000013#include <fcntl.h>
Martin v. Löwis14e73b12003-01-01 09:51:12 +000014#ifdef HAVE_STROPTS_H
15#include <stropts.h>
16#endif
Guido van Rossum02975121992-08-17 08:55:12 +000017
Fred Drake152a25e2001-05-09 21:02:02 +000018static int
19conv_descriptor(PyObject *object, int *target)
20{
21 int fd = PyObject_AsFileDescriptor(object);
22
23 if (fd < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 return 0;
Fred Drake152a25e2001-05-09 21:02:02 +000025 *target = fd;
26 return 1;
27}
28
29
Guido van Rossum02975121992-08-17 08:55:12 +000030/* fcntl(fd, opt, [arg]) */
31
Roger E. Masse919213a1996-12-17 17:42:22 +000032static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000033fcntl_fcntl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 int fd;
36 int code;
37 long arg;
38 int ret;
39 char *str;
40 Py_ssize_t len;
41 char buf[1024];
Guido van Rossum02975121992-08-17 08:55:12 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 if (PyArg_ParseTuple(args, "O&is#:fcntl",
44 conv_descriptor, &fd, &code, &str, &len)) {
45 if (len > sizeof buf) {
46 PyErr_SetString(PyExc_ValueError,
47 "fcntl string arg too long");
48 return NULL;
49 }
50 memcpy(buf, str, len);
51 Py_BEGIN_ALLOW_THREADS
52 ret = fcntl(fd, code, buf);
53 Py_END_ALLOW_THREADS
54 if (ret < 0) {
55 PyErr_SetFromErrno(PyExc_IOError);
56 return NULL;
57 }
58 return PyBytes_FromStringAndSize(buf, len);
59 }
Guido van Rossum02975121992-08-17 08:55:12 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyErr_Clear();
62 arg = 0;
63 if (!PyArg_ParseTuple(args,
64 "O&i|l;fcntl requires a file or file descriptor,"
65 " an integer and optionally a third integer or a string",
66 conv_descriptor, &fd, &code, &arg)) {
67 return NULL;
68 }
69 Py_BEGIN_ALLOW_THREADS
70 ret = fcntl(fd, code, arg);
71 Py_END_ALLOW_THREADS
72 if (ret < 0) {
73 PyErr_SetFromErrno(PyExc_IOError);
74 return NULL;
75 }
76 return PyLong_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +000077}
78
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(fcntl_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +000080"fcntl(fd, opt, [arg])\n\
81\n\
82Perform the requested operation on file descriptor fd. The operation\n\
Fred Drake1d531992001-05-10 15:54:32 +000083is defined by op and is operating system dependent. These constants are\n\
84available from the fcntl module. The argument arg is optional, and\n\
Georg Brandl1f94cd02010-09-05 17:09:18 +000085defaults to 0; it may be an int or a string. If arg is given as a string,\n\
Fred Drake1d531992001-05-10 15:54:32 +000086the return value of fcntl is a string of that length, containing the\n\
Georg Brandl1f94cd02010-09-05 17:09:18 +000087resulting value put in the arg buffer by the operating system. The length\n\
88of the arg string is not allowed to exceed 1024 bytes. If the arg given\n\
Fred Drake1d531992001-05-10 15:54:32 +000089is an integer or if none is specified, the result value is an integer\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090corresponding to the return value of the fcntl call in the C code.");
Guido van Rossum185ead61998-11-23 15:32:55 +000091
Guido van Rossum02975121992-08-17 08:55:12 +000092
93/* ioctl(fd, opt, [arg]) */
94
Roger E. Masse919213a1996-12-17 17:42:22 +000095static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000096fcntl_ioctl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000097{
Thomas Wouters477c8d52006-05-27 19:21:47 +000098#define IOCTL_BUFSZ 1024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 int fd;
100 /* In PyArg_ParseTuple below, we use the unsigned non-checked 'I'
101 format for the 'code' parameter because Python turns 0x8000000
102 into either a large positive number (PyLong or PyInt on 64-bit
103 platforms) or a negative number on others (32-bit PyInt)
104 whereas the system expects it to be a 32bit bit field value
105 regardless of it being passed as an int or unsigned long on
106 various platforms. See the termios.TIOCSWINSZ constant across
107 platforms for an example of thise.
Christian Heimese25f35e2008-03-20 10:49:03 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 If any of the 64bit platforms ever decide to use more than 32bits
110 in their unsigned long ioctl codes this will break and need
111 special casing based on the platform being built on.
112 */
113 unsigned int code;
114 int arg;
115 int ret;
116 Py_buffer pstr;
117 char *str;
118 Py_ssize_t len;
119 int mutate_arg = 1;
120 char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */
Guido van Rossum02975121992-08-17 08:55:12 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 if (PyArg_ParseTuple(args, "O&Iw*|i:ioctl",
123 conv_descriptor, &fd, &code,
124 &pstr, &mutate_arg)) {
125 char *arg;
126 str = pstr.buf;
127 len = pstr.len;
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 if (mutate_arg) {
130 if (len <= IOCTL_BUFSZ) {
131 memcpy(buf, str, len);
132 buf[len] = '\0';
133 arg = buf;
134 }
135 else {
136 arg = str;
137 }
138 }
139 else {
140 if (len > IOCTL_BUFSZ) {
141 PyBuffer_Release(&pstr);
142 PyErr_SetString(PyExc_ValueError,
143 "ioctl string arg too long");
144 return NULL;
145 }
146 else {
147 memcpy(buf, str, len);
148 buf[len] = '\0';
149 arg = buf;
150 }
151 }
152 if (buf == arg) {
153 Py_BEGIN_ALLOW_THREADS /* think array.resize() */
154 ret = ioctl(fd, code, arg);
155 Py_END_ALLOW_THREADS
156 }
157 else {
158 ret = ioctl(fd, code, arg);
159 }
Antoine Pitrou5e38aae2010-09-07 16:30:09 +0000160 if (mutate_arg && (len <= IOCTL_BUFSZ)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 memcpy(str, buf, len);
162 }
163 PyBuffer_Release(&pstr); /* No further access to str below this point */
164 if (ret < 0) {
165 PyErr_SetFromErrno(PyExc_IOError);
166 return NULL;
167 }
168 if (mutate_arg) {
169 return PyLong_FromLong(ret);
170 }
171 else {
172 return PyBytes_FromStringAndSize(buf, len);
173 }
174 }
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyErr_Clear();
177 if (PyArg_ParseTuple(args, "O&Is*:ioctl",
178 conv_descriptor, &fd, &code, &pstr)) {
179 str = pstr.buf;
180 len = pstr.len;
181 if (len > IOCTL_BUFSZ) {
182 PyBuffer_Release(&pstr);
183 PyErr_SetString(PyExc_ValueError,
184 "ioctl string arg too long");
185 return NULL;
186 }
187 memcpy(buf, str, len);
188 buf[len] = '\0';
189 Py_BEGIN_ALLOW_THREADS
190 ret = ioctl(fd, code, buf);
191 Py_END_ALLOW_THREADS
192 if (ret < 0) {
193 PyBuffer_Release(&pstr);
194 PyErr_SetFromErrno(PyExc_IOError);
195 return NULL;
196 }
197 PyBuffer_Release(&pstr);
198 return PyBytes_FromStringAndSize(buf, len);
199 }
Guido van Rossum02975121992-08-17 08:55:12 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 PyErr_Clear();
202 arg = 0;
203 if (!PyArg_ParseTuple(args,
204 "O&I|i;ioctl requires a file or file descriptor,"
205 " an integer and optionally an integer or buffer argument",
206 conv_descriptor, &fd, &code, &arg)) {
207 return NULL;
208 }
209 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000210#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 ret = ioctl(fd, code, (void *)arg);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000212#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 ret = ioctl(fd, code, arg);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_END_ALLOW_THREADS
216 if (ret < 0) {
217 PyErr_SetFromErrno(PyExc_IOError);
218 return NULL;
219 }
220 return PyLong_FromLong((long)ret);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221#undef IOCTL_BUFSZ
Guido van Rossum02975121992-08-17 08:55:12 +0000222}
223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000224PyDoc_STRVAR(ioctl_doc,
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000225"ioctl(fd, opt[, arg[, mutate_flag]])\n\
Guido van Rossum185ead61998-11-23 15:32:55 +0000226\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000227Perform the requested operation on file descriptor fd. The operation is\n\
Neal Norwitz47308802003-06-30 01:54:04 +0000228defined by opt and is operating system dependent. Typically these codes are\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000229retrieved from the fcntl or termios library modules.\n\
230\n\
231The argument arg is optional, and defaults to 0; it may be an int or a\n\
232buffer containing character data (most likely a string or an array). \n\
233\n\
234If the argument is a mutable buffer (such as an array) and if the\n\
235mutate_flag argument (which is only allowed in this case) is true then the\n\
236buffer is (in effect) passed to the operating system and changes made by\n\
237the OS will be reflected in the contents of the buffer after the call has\n\
238returned. The return value is the integer returned by the ioctl system\n\
239call.\n\
240\n\
241If the argument is a mutable buffer and the mutable_flag argument is not\n\
242passed or is false, the behavior is as if a string had been passed. This\n\
243behavior will change in future releases of Python.\n\
244\n\
245If the argument is an immutable buffer (most likely a string) then a copy\n\
246of the buffer is passed to the operating system and the return value is a\n\
247string of the same length containing whatever the operating system put in\n\
248the buffer. The length of the arg buffer in this case is not allowed to\n\
249exceed 1024 bytes.\n\
250\n\
251If the arg given is an integer or if none is specified, the result value is\n\
252an integer corresponding to the return value of the ioctl call in the C\n\
253code.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000254
Guido van Rossum02975121992-08-17 08:55:12 +0000255
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000256/* flock(fd, operation) */
257
Roger E. Masse919213a1996-12-17 17:42:22 +0000258static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000259fcntl_flock(PyObject *self, PyObject *args)
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 int fd;
262 int code;
263 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (!PyArg_ParseTuple(args, "O&i:flock",
266 conv_descriptor, &fd, &code))
267 return NULL;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000268
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000269#ifdef HAVE_FLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 Py_BEGIN_ALLOW_THREADS
271 ret = flock(fd, code);
272 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000273#else
274
275#ifndef LOCK_SH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276#define LOCK_SH 1 /* shared lock */
277#define LOCK_EX 2 /* exclusive lock */
278#define LOCK_NB 4 /* don't block when locking */
279#define LOCK_UN 8 /* unlock */
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 {
282 struct flock l;
283 if (code == LOCK_UN)
284 l.l_type = F_UNLCK;
285 else if (code & LOCK_SH)
286 l.l_type = F_RDLCK;
287 else if (code & LOCK_EX)
288 l.l_type = F_WRLCK;
289 else {
290 PyErr_SetString(PyExc_ValueError,
291 "unrecognized flock argument");
292 return NULL;
293 }
294 l.l_whence = l.l_start = l.l_len = 0;
295 Py_BEGIN_ALLOW_THREADS
296 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
297 Py_END_ALLOW_THREADS
298 }
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000299#endif /* HAVE_FLOCK */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (ret < 0) {
301 PyErr_SetFromErrno(PyExc_IOError);
302 return NULL;
303 }
304 Py_INCREF(Py_None);
305 return Py_None;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000306}
307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000308PyDoc_STRVAR(flock_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000309"flock(fd, operation)\n\
310\n\
311Perform the lock operation op on file descriptor fd. See the Unix \n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000312manual page for flock(3) for details. (On some systems, this function is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313emulated using fcntl().)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000314
315
Guido van Rossumc8643641996-09-11 23:17:20 +0000316/* lockf(fd, operation) */
Roger E. Masse919213a1996-12-17 17:42:22 +0000317static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000318fcntl_lockf(PyObject *self, PyObject *args)
Guido van Rossumc8643641996-09-11 23:17:20 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 int fd, code, ret, whence = 0;
321 PyObject *lenobj = NULL, *startobj = NULL;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
324 conv_descriptor, &fd, &code,
325 &lenobj, &startobj, &whence))
326 return NULL;
Guido van Rossumc8643641996-09-11 23:17:20 +0000327
Guido van Rossumc8643641996-09-11 23:17:20 +0000328#ifndef LOCK_SH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329#define LOCK_SH 1 /* shared lock */
330#define LOCK_EX 2 /* exclusive lock */
331#define LOCK_NB 4 /* don't block when locking */
332#define LOCK_UN 8 /* unlock */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000333#endif /* LOCK_SH */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 {
335 struct flock l;
336 if (code == LOCK_UN)
337 l.l_type = F_UNLCK;
338 else if (code & LOCK_SH)
339 l.l_type = F_RDLCK;
340 else if (code & LOCK_EX)
341 l.l_type = F_WRLCK;
342 else {
343 PyErr_SetString(PyExc_ValueError,
344 "unrecognized lockf argument");
345 return NULL;
346 }
347 l.l_start = l.l_len = 0;
348 if (startobj != NULL) {
Guido van Rossum056bad91999-01-06 18:44:23 +0000349#if !defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 l.l_start = PyLong_AsLong(startobj);
Guido van Rossum056bad91999-01-06 18:44:23 +0000351#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 l.l_start = PyLong_Check(startobj) ?
353 PyLong_AsLongLong(startobj) :
354 PyLong_AsLong(startobj);
Guido van Rossum056bad91999-01-06 18:44:23 +0000355#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (PyErr_Occurred())
357 return NULL;
358 }
359 if (lenobj != NULL) {
Guido van Rossum056bad91999-01-06 18:44:23 +0000360#if !defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 l.l_len = PyLong_AsLong(lenobj);
Guido van Rossum056bad91999-01-06 18:44:23 +0000362#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 l.l_len = PyLong_Check(lenobj) ?
364 PyLong_AsLongLong(lenobj) :
365 PyLong_AsLong(lenobj);
Guido van Rossum056bad91999-01-06 18:44:23 +0000366#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (PyErr_Occurred())
368 return NULL;
369 }
370 l.l_whence = whence;
371 Py_BEGIN_ALLOW_THREADS
372 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
373 Py_END_ALLOW_THREADS
374 }
375 if (ret < 0) {
376 PyErr_SetFromErrno(PyExc_IOError);
377 return NULL;
378 }
379 Py_INCREF(Py_None);
380 return Py_None;
Guido van Rossumc8643641996-09-11 23:17:20 +0000381}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383PyDoc_STRVAR(lockf_doc,
Sjoerd Mullender82e00d62001-01-25 10:10:39 +0000384"lockf (fd, operation, length=0, start=0, whence=0)\n\
385\n\
386This is essentially a wrapper around the fcntl() locking calls. fd is the\n\
387file descriptor of the file to lock or unlock, and operation is one of the\n\
388following values:\n\
389\n\
390 LOCK_UN - unlock\n\
391 LOCK_SH - acquire a shared lock\n\
392 LOCK_EX - acquire an exclusive lock\n\
393\n\
Christian Heimesfaf2f632008-01-06 16:59:19 +0000394When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n\
Sjoerd Mullender82e00d62001-01-25 10:10:39 +0000395LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\
396lock cannot be acquired, an IOError will be raised and the exception will\n\
397have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\
398system -- for portability, check for either value).\n\
399\n\
400length is the number of bytes to lock, with the default meaning to lock to\n\
401EOF. start is the byte offset, relative to whence, to that the lock\n\
402starts. whence is as with fileobj.seek(), specifically:\n\
403\n\
404 0 - relative to the start of the file (SEEK_SET)\n\
405 1 - relative to the current buffer position (SEEK_CUR)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406 2 - relative to the end of the file (SEEK_END)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000407
Guido van Rossum02975121992-08-17 08:55:12 +0000408/* List of functions */
409
Roger E. Masse919213a1996-12-17 17:42:22 +0000410static PyMethodDef fcntl_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
412 {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
413 {"flock", fcntl_flock, METH_VARARGS, flock_doc},
414 {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
415 {NULL, NULL} /* sentinel */
Guido van Rossum02975121992-08-17 08:55:12 +0000416};
417
418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000419PyDoc_STRVAR(module_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000420"This module performs file control and I/O control on file \n\
421descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
422routines. File descriptors can be obtained with the fileno() method of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000423a file or socket object.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000424
Guido van Rossum02975121992-08-17 08:55:12 +0000425/* Module initialisation */
426
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000427
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000428static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200429all_ins(PyObject* m)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000430{
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200431 if (PyModule_AddIntMacro(m, LOCK_SH)) return -1;
432 if (PyModule_AddIntMacro(m, LOCK_EX)) return -1;
433 if (PyModule_AddIntMacro(m, LOCK_NB)) return -1;
434 if (PyModule_AddIntMacro(m, LOCK_UN)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000435/* GNU extensions, as of glibc 2.2.4 */
436#ifdef LOCK_MAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200437 if (PyModule_AddIntMacro(m, LOCK_MAND)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000438#endif
439#ifdef LOCK_READ
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200440 if (PyModule_AddIntMacro(m, LOCK_READ)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000441#endif
442#ifdef LOCK_WRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200443 if (PyModule_AddIntMacro(m, LOCK_WRITE)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000444#endif
445#ifdef LOCK_RW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200446 if (PyModule_AddIntMacro(m, LOCK_RW)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000447#endif
448
Fred Drake152a25e2001-05-09 21:02:02 +0000449#ifdef F_DUPFD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200450 if (PyModule_AddIntMacro(m, F_DUPFD)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000451#endif
Victor Stinner2716d532013-01-08 00:52:40 +0100452#ifdef F_DUPFD_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200453 if (PyModule_AddIntMacro(m, F_DUPFD_CLOEXEC)) return -1;
Victor Stinner2716d532013-01-08 00:52:40 +0100454#endif
Fred Drake152a25e2001-05-09 21:02:02 +0000455#ifdef F_GETFD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200456 if (PyModule_AddIntMacro(m, F_GETFD)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000457#endif
458#ifdef F_SETFD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200459 if (PyModule_AddIntMacro(m, F_SETFD)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000460#endif
461#ifdef F_GETFL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200462 if (PyModule_AddIntMacro(m, F_GETFL)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000463#endif
464#ifdef F_SETFL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200465 if (PyModule_AddIntMacro(m, F_SETFL)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000466#endif
467#ifdef F_GETLK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200468 if (PyModule_AddIntMacro(m, F_GETLK)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000469#endif
470#ifdef F_SETLK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200471 if (PyModule_AddIntMacro(m, F_SETLK)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000472#endif
473#ifdef F_SETLKW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200474 if (PyModule_AddIntMacro(m, F_SETLKW)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000475#endif
476#ifdef F_GETOWN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200477 if (PyModule_AddIntMacro(m, F_GETOWN)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000478#endif
479#ifdef F_SETOWN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200480 if (PyModule_AddIntMacro(m, F_SETOWN)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000481#endif
482#ifdef F_GETSIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200483 if (PyModule_AddIntMacro(m, F_GETSIG)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000484#endif
485#ifdef F_SETSIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200486 if (PyModule_AddIntMacro(m, F_SETSIG)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000487#endif
488#ifdef F_RDLCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200489 if (PyModule_AddIntMacro(m, F_RDLCK)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000490#endif
491#ifdef F_WRLCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200492 if (PyModule_AddIntMacro(m, F_WRLCK)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000493#endif
494#ifdef F_UNLCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200495 if (PyModule_AddIntMacro(m, F_UNLCK)) return -1;
Fred Drake152a25e2001-05-09 21:02:02 +0000496#endif
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000497/* LFS constants */
498#ifdef F_GETLK64
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200499 if (PyModule_AddIntMacro(m, F_GETLK64)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000500#endif
501#ifdef F_SETLK64
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200502 if (PyModule_AddIntMacro(m, F_SETLK64)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000503#endif
504#ifdef F_SETLKW64
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200505 if (PyModule_AddIntMacro(m, F_SETLKW64)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000506#endif
507/* GNU extensions, as of glibc 2.2.4. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000508#ifdef FASYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200509 if (PyModule_AddIntMacro(m, FASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000510#endif
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000511#ifdef F_SETLEASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200512 if (PyModule_AddIntMacro(m, F_SETLEASE)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000513#endif
514#ifdef F_GETLEASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200515 if (PyModule_AddIntMacro(m, F_GETLEASE)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000516#endif
517#ifdef F_NOTIFY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200518 if (PyModule_AddIntMacro(m, F_NOTIFY)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000519#endif
520/* Old BSD flock(). */
521#ifdef F_EXLCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200522 if (PyModule_AddIntMacro(m, F_EXLCK)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000523#endif
524#ifdef F_SHLCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200525 if (PyModule_AddIntMacro(m, F_SHLCK)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000526#endif
527
Charles-François Natali23e1ecb2011-11-02 18:58:25 +0100528/* OS X specifics */
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000529#ifdef F_FULLFSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200530 if (PyModule_AddIntMacro(m, F_FULLFSYNC)) return -1;
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000531#endif
Charles-François Natali23e1ecb2011-11-02 18:58:25 +0100532#ifdef F_NOCACHE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200533 if (PyModule_AddIntMacro(m, F_NOCACHE)) return -1;
Charles-François Natali23e1ecb2011-11-02 18:58:25 +0100534#endif
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000535
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000536/* For F_{GET|SET}FL */
537#ifdef FD_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200538 if (PyModule_AddIntMacro(m, FD_CLOEXEC)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000539#endif
540
541/* For F_NOTIFY */
542#ifdef DN_ACCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200543 if (PyModule_AddIntMacro(m, DN_ACCESS)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000544#endif
545#ifdef DN_MODIFY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200546 if (PyModule_AddIntMacro(m, DN_MODIFY)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000547#endif
548#ifdef DN_CREATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200549 if (PyModule_AddIntMacro(m, DN_CREATE)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000550#endif
551#ifdef DN_DELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200552 if (PyModule_AddIntMacro(m, DN_DELETE)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000553#endif
554#ifdef DN_RENAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200555 if (PyModule_AddIntMacro(m, DN_RENAME)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000556#endif
557#ifdef DN_ATTRIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200558 if (PyModule_AddIntMacro(m, DN_ATTRIB)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000559#endif
560#ifdef DN_MULTISHOT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200561 if (PyModule_AddIntMacro(m, DN_MULTISHOT)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000562#endif
563
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000564#ifdef HAVE_STROPTS_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* Unix 98 guarantees that these are in stropts.h. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200566 if (PyModule_AddIntMacro(m, I_PUSH)) return -1;
567 if (PyModule_AddIntMacro(m, I_POP)) return -1;
568 if (PyModule_AddIntMacro(m, I_LOOK)) return -1;
569 if (PyModule_AddIntMacro(m, I_FLUSH)) return -1;
570 if (PyModule_AddIntMacro(m, I_FLUSHBAND)) return -1;
571 if (PyModule_AddIntMacro(m, I_SETSIG)) return -1;
572 if (PyModule_AddIntMacro(m, I_GETSIG)) return -1;
573 if (PyModule_AddIntMacro(m, I_FIND)) return -1;
574 if (PyModule_AddIntMacro(m, I_PEEK)) return -1;
575 if (PyModule_AddIntMacro(m, I_SRDOPT)) return -1;
576 if (PyModule_AddIntMacro(m, I_GRDOPT)) return -1;
577 if (PyModule_AddIntMacro(m, I_NREAD)) return -1;
578 if (PyModule_AddIntMacro(m, I_FDINSERT)) return -1;
579 if (PyModule_AddIntMacro(m, I_STR)) return -1;
580 if (PyModule_AddIntMacro(m, I_SWROPT)) return -1;
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000581#ifdef I_GWROPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* despite the comment above, old-ish glibcs miss a couple... */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200583 if (PyModule_AddIntMacro(m, I_GWROPT)) return -1;
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000584#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200585 if (PyModule_AddIntMacro(m, I_SENDFD)) return -1;
586 if (PyModule_AddIntMacro(m, I_RECVFD)) return -1;
587 if (PyModule_AddIntMacro(m, I_LIST)) return -1;
588 if (PyModule_AddIntMacro(m, I_ATMARK)) return -1;
589 if (PyModule_AddIntMacro(m, I_CKBAND)) return -1;
590 if (PyModule_AddIntMacro(m, I_GETBAND)) return -1;
591 if (PyModule_AddIntMacro(m, I_CANPUT)) return -1;
592 if (PyModule_AddIntMacro(m, I_SETCLTIME)) return -1;
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000593#ifdef I_GETCLTIME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200594 if (PyModule_AddIntMacro(m, I_GETCLTIME)) return -1;
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000595#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200596 if (PyModule_AddIntMacro(m, I_LINK)) return -1;
597 if (PyModule_AddIntMacro(m, I_UNLINK)) return -1;
598 if (PyModule_AddIntMacro(m, I_PLINK)) return -1;
599 if (PyModule_AddIntMacro(m, I_PUNLINK)) return -1;
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000600#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601
602 return 0;
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000603}
604
Martin v. Löwis1a214512008-06-11 05:26:20 +0000605
606static struct PyModuleDef fcntlmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyModuleDef_HEAD_INIT,
608 "fcntl",
609 module_doc,
610 -1,
611 fcntl_methods,
612 NULL,
613 NULL,
614 NULL,
615 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000616};
617
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000618PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000619PyInit_fcntl(void)
Guido van Rossum02975121992-08-17 08:55:12 +0000620{
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200621 PyObject *m;
Guido van Rossum02975121992-08-17 08:55:12 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Create the module and add the functions and documentation */
624 m = PyModule_Create(&fcntlmodule);
625 if (m == NULL)
626 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* Add some symbolic constants to the module */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200629 all_ins(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return m;
Guido van Rossum02975121992-08-17 08:55:12 +0000631}