blob: 360b54ee4cbbd7dd5ac867801a022d568461e82d [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001
2/* fcntl module */
3
Roger E. Masse919213a1996-12-17 17:42:22 +00004#include "Python.h"
Guido van Rossum02975121992-08-17 08:55:12 +00005
Guido van Rossuma376cc51996-12-05 23:43:35 +00006#ifdef HAVE_SYS_FILE_H
7#include <sys/file.h>
8#endif
9
Guido van Rossum3d65fa31996-12-09 18:49:14 +000010#include <sys/ioctl.h>
Guido van Rossum3c0b79c1996-06-11 15:11:34 +000011#include <fcntl.h>
12
Guido van Rossum02975121992-08-17 08:55:12 +000013
Fred Drake152a25e2001-05-09 21:02:02 +000014static int
15conv_descriptor(PyObject *object, int *target)
16{
17 int fd = PyObject_AsFileDescriptor(object);
18
19 if (fd < 0)
20 return 0;
21 *target = fd;
22 return 1;
23}
24
25
Guido van Rossum02975121992-08-17 08:55:12 +000026/* fcntl(fd, opt, [arg]) */
27
Roger E. Masse919213a1996-12-17 17:42:22 +000028static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000029fcntl_fcntl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000030{
31 int fd;
32 int code;
33 int arg;
34 int ret;
35 char *str;
36 int len;
37 char buf[1024];
38
Fred Drake152a25e2001-05-09 21:02:02 +000039 if (PyArg_ParseTuple(args, "O&is#:fcntl",
40 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +000041 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +000042 PyErr_SetString(PyExc_ValueError,
43 "fcntl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +000044 return NULL;
45 }
46 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +000047 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000048 ret = fcntl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +000049 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000050 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000051 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000052 return NULL;
53 }
Roger E. Masse919213a1996-12-17 17:42:22 +000054 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +000055 }
56
Roger E. Masse919213a1996-12-17 17:42:22 +000057 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +000058 arg = 0;
Fred Drake152a25e2001-05-09 21:02:02 +000059 if (!PyArg_ParseTuple(args,
60 "O&i|i;fcntl requires a file or file descriptor,"
61 " an integer and optionally a third integer or a string",
62 conv_descriptor, &fd, &code, &arg)) {
Guido van Rossuma2214c32000-08-02 20:46:51 +000063 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +000064 }
Roger E. Masse919213a1996-12-17 17:42:22 +000065 Py_BEGIN_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000066 ret = fcntl(fd, code, arg);
Roger E. Masse919213a1996-12-17 17:42:22 +000067 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000068 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000069 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000070 return NULL;
71 }
Roger E. Masse919213a1996-12-17 17:42:22 +000072 return PyInt_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +000073}
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(fcntl_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +000076"fcntl(fd, opt, [arg])\n\
77\n\
78Perform the requested operation on file descriptor fd. The operation\n\
Fred Drake1d531992001-05-10 15:54:32 +000079is defined by op and is operating system dependent. These constants are\n\
80available from the fcntl module. The argument arg is optional, and\n\
81defaults to 0; it may be an int or a string. If arg is given as a string,\n\
82the return value of fcntl is a string of that length, containing the\n\
83resulting value put in the arg buffer by the operating system.The length\n\
84of the arg string is not allowed to exceed 1024 bytes. If the arg given\n\
85is an integer or if none is specified, the result value is an integer\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086corresponding to the return value of the fcntl call in the C code.");
Guido van Rossum185ead61998-11-23 15:32:55 +000087
Guido van Rossum02975121992-08-17 08:55:12 +000088
89/* ioctl(fd, opt, [arg]) */
90
Roger E. Masse919213a1996-12-17 17:42:22 +000091static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000092fcntl_ioctl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000093{
94 int fd;
95 int code;
96 int arg;
97 int ret;
98 char *str;
99 int len;
100 char buf[1024];
101
Fred Drake152a25e2001-05-09 21:02:02 +0000102 if (PyArg_ParseTuple(args, "O&is#:ioctl",
103 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +0000104 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000105 PyErr_SetString(PyExc_ValueError,
106 "ioctl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +0000107 return NULL;
108 }
109 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +0000110 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000111 ret = ioctl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +0000112 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000113 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000114 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000115 return NULL;
116 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000117 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +0000118 }
119
Roger E. Masse919213a1996-12-17 17:42:22 +0000120 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +0000121 arg = 0;
Fred Drake460f0692001-05-14 21:02:36 +0000122 if (!PyArg_ParseTuple(args,
123 "O&i|i;ioctl requires a file or file descriptor,"
124 " an integer and optionally a third integer or a string",
Fred Drake152a25e2001-05-09 21:02:02 +0000125 conv_descriptor, &fd, &code, &arg)) {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000126 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +0000127 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000128 Py_BEGIN_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +0000129 ret = ioctl(fd, code, arg);
Roger E. Masse919213a1996-12-17 17:42:22 +0000130 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +0000131 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000132 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000133 return NULL;
134 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000135 return PyInt_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +0000136}
137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000138PyDoc_STRVAR(ioctl_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000139"ioctl(fd, opt, [arg])\n\
140\n\
141Perform the requested operation on file descriptor fd. The operation\n\
142is defined by op and is operating system dependent. Typically these\n\
143codes can be retrieved from the library module IOCTL. The argument arg\n\
Guido van Rossuma2214c32000-08-02 20:46:51 +0000144is optional, and defaults to 0; it may be an int or a string. If arg is\n\
145given as a string, the return value of ioctl is a string of that length,\n\
146containing the resulting value put in the arg buffer by the operating system.\n\
147The length of the arg string is not allowed to exceed 1024 bytes. If the arg\n\
148given is an integer or if none is specified, the result value is an integer\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149corresponding to the return value of the ioctl call in the C code.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000150
Guido van Rossum02975121992-08-17 08:55:12 +0000151
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000152/* flock(fd, operation) */
153
Roger E. Masse919213a1996-12-17 17:42:22 +0000154static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000155fcntl_flock(PyObject *self, PyObject *args)
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000156{
157 int fd;
158 int code;
159 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000160
Fred Drake152a25e2001-05-09 21:02:02 +0000161 if (!PyArg_ParseTuple(args, "O&i:flock",
162 conv_descriptor, &fd, &code))
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000163 return NULL;
164
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000165#ifdef HAVE_FLOCK
Guido van Rossum056bad91999-01-06 18:44:23 +0000166 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000167 ret = flock(fd, code);
Guido van Rossum056bad91999-01-06 18:44:23 +0000168 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000169#else
170
171#ifndef LOCK_SH
172#define LOCK_SH 1 /* shared lock */
173#define LOCK_EX 2 /* exclusive lock */
174#define LOCK_NB 4 /* don't block when locking */
175#define LOCK_UN 8 /* unlock */
176#endif
177 {
178 struct flock l;
179 if (code == LOCK_UN)
180 l.l_type = F_UNLCK;
181 else if (code & LOCK_SH)
182 l.l_type = F_RDLCK;
183 else if (code & LOCK_EX)
184 l.l_type = F_WRLCK;
185 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000186 PyErr_SetString(PyExc_ValueError,
187 "unrecognized flock argument");
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000188 return NULL;
189 }
190 l.l_whence = l.l_start = l.l_len = 0;
Guido van Rossum056bad91999-01-06 18:44:23 +0000191 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000192 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000193 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000194 }
195#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000196 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000197 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000198 return NULL;
199 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000200 Py_INCREF(Py_None);
201 return Py_None;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000202}
203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204PyDoc_STRVAR(flock_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000205"flock(fd, operation)\n\
206\n\
207Perform the lock operation op on file descriptor fd. See the Unix \n\
208manual flock(3) for details. (On some systems, this function is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209emulated using fcntl().)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000210
211
Guido van Rossumc8643641996-09-11 23:17:20 +0000212/* lockf(fd, operation) */
Roger E. Masse919213a1996-12-17 17:42:22 +0000213static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000214fcntl_lockf(PyObject *self, PyObject *args)
Guido van Rossumc8643641996-09-11 23:17:20 +0000215{
Guido van Rossum056bad91999-01-06 18:44:23 +0000216 int fd, code, ret, whence = 0;
217 PyObject *lenobj = NULL, *startobj = NULL;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000218
Fred Drake152a25e2001-05-09 21:02:02 +0000219 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
220 conv_descriptor, &fd, &code,
Guido van Rossum056bad91999-01-06 18:44:23 +0000221 &lenobj, &startobj, &whence))
Guido van Rossumc8643641996-09-11 23:17:20 +0000222 return NULL;
223
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000224#if defined(PYOS_OS2) && defined(PYCC_GCC)
225 PyErr_SetString(PyExc_NotImplementedError,
226 "lockf not supported on OS/2 (EMX)");
227 return NULL;
228#else
Guido van Rossumc8643641996-09-11 23:17:20 +0000229#ifndef LOCK_SH
230#define LOCK_SH 1 /* shared lock */
231#define LOCK_EX 2 /* exclusive lock */
232#define LOCK_NB 4 /* don't block when locking */
233#define LOCK_UN 8 /* unlock */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000234#endif /* LOCK_SH */
Guido van Rossumc8643641996-09-11 23:17:20 +0000235 {
236 struct flock l;
237 if (code == LOCK_UN)
238 l.l_type = F_UNLCK;
239 else if (code & LOCK_SH)
240 l.l_type = F_RDLCK;
241 else if (code & LOCK_EX)
242 l.l_type = F_WRLCK;
243 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000244 PyErr_SetString(PyExc_ValueError,
245 "unrecognized flock argument");
Guido van Rossumc8643641996-09-11 23:17:20 +0000246 return NULL;
247 }
Guido van Rossum056bad91999-01-06 18:44:23 +0000248 l.l_start = l.l_len = 0;
249 if (startobj != NULL) {
250#if !defined(HAVE_LARGEFILE_SUPPORT)
251 l.l_start = PyInt_AsLong(startobj);
252#else
253 l.l_start = PyLong_Check(startobj) ?
254 PyLong_AsLongLong(startobj) :
255 PyInt_AsLong(startobj);
256#endif
257 if (PyErr_Occurred())
258 return NULL;
259 }
260 if (lenobj != NULL) {
261#if !defined(HAVE_LARGEFILE_SUPPORT)
262 l.l_len = PyInt_AsLong(lenobj);
263#else
264 l.l_len = PyLong_Check(lenobj) ?
265 PyLong_AsLongLong(lenobj) :
266 PyInt_AsLong(lenobj);
267#endif
268 if (PyErr_Occurred())
269 return NULL;
270 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000271 l.l_whence = whence;
Guido van Rossum056bad91999-01-06 18:44:23 +0000272 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000273 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000274 Py_END_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000275 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000276 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000277 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumc8643641996-09-11 23:17:20 +0000278 return NULL;
279 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000280 Py_INCREF(Py_None);
281 return Py_None;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000282#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
Guido van Rossumc8643641996-09-11 23:17:20 +0000283}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000285PyDoc_STRVAR(lockf_doc,
Sjoerd Mullender82e00d62001-01-25 10:10:39 +0000286"lockf (fd, operation, length=0, start=0, whence=0)\n\
287\n\
288This is essentially a wrapper around the fcntl() locking calls. fd is the\n\
289file descriptor of the file to lock or unlock, and operation is one of the\n\
290following values:\n\
291\n\
292 LOCK_UN - unlock\n\
293 LOCK_SH - acquire a shared lock\n\
294 LOCK_EX - acquire an exclusive lock\n\
295\n\
296When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\
297LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\
298lock cannot be acquired, an IOError will be raised and the exception will\n\
299have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\
300system -- for portability, check for either value).\n\
301\n\
302length is the number of bytes to lock, with the default meaning to lock to\n\
303EOF. start is the byte offset, relative to whence, to that the lock\n\
304starts. whence is as with fileobj.seek(), specifically:\n\
305\n\
306 0 - relative to the start of the file (SEEK_SET)\n\
307 1 - relative to the current buffer position (SEEK_CUR)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000308 2 - relative to the end of the file (SEEK_END)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000309
Guido van Rossum02975121992-08-17 08:55:12 +0000310/* List of functions */
311
Roger E. Masse919213a1996-12-17 17:42:22 +0000312static PyMethodDef fcntl_methods[] = {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000313 {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
314 {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
315 {"flock", fcntl_flock, METH_VARARGS, flock_doc},
316 {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
Guido van Rossum02975121992-08-17 08:55:12 +0000317 {NULL, NULL} /* sentinel */
318};
319
320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000321PyDoc_STRVAR(module_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000322"This module performs file control and I/O control on file \n\
323descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
324routines. File descriptors can be obtained with the fileno() method of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325a file or socket object.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000326
Guido van Rossum02975121992-08-17 08:55:12 +0000327/* Module initialisation */
328
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000329static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000330ins(PyObject* d, char* symbol, long value)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000331{
332 PyObject* v = PyInt_FromLong(value);
333 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
334 return -1;
335
336 Py_DECREF(v);
337 return 0;
338}
339
340static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000341all_ins(PyObject* d)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000342{
343 if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
344 if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
345 if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
346 if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000347/* GNU extensions, as of glibc 2.2.4 */
348#ifdef LOCK_MAND
349 if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1;
350#endif
351#ifdef LOCK_READ
352 if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1;
353#endif
354#ifdef LOCK_WRITE
355 if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1;
356#endif
357#ifdef LOCK_RW
358 if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1;
359#endif
360
Fred Drake152a25e2001-05-09 21:02:02 +0000361#ifdef F_DUPFD
362 if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1;
363#endif
364#ifdef F_GETFD
365 if (ins(d, "F_GETFD", (long)F_GETFD)) return -1;
366#endif
367#ifdef F_SETFD
368 if (ins(d, "F_SETFD", (long)F_SETFD)) return -1;
369#endif
370#ifdef F_GETFL
371 if (ins(d, "F_GETFL", (long)F_GETFL)) return -1;
372#endif
373#ifdef F_SETFL
374 if (ins(d, "F_SETFL", (long)F_SETFL)) return -1;
375#endif
376#ifdef F_GETLK
377 if (ins(d, "F_GETLK", (long)F_GETLK)) return -1;
378#endif
379#ifdef F_SETLK
380 if (ins(d, "F_SETLK", (long)F_SETLK)) return -1;
381#endif
382#ifdef F_SETLKW
383 if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1;
384#endif
385#ifdef F_GETOWN
386 if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1;
387#endif
388#ifdef F_SETOWN
389 if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1;
390#endif
391#ifdef F_GETSIG
392 if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1;
393#endif
394#ifdef F_SETSIG
395 if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1;
396#endif
397#ifdef F_RDLCK
398 if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1;
399#endif
400#ifdef F_WRLCK
401 if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1;
402#endif
403#ifdef F_UNLCK
404 if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1;
405#endif
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000406/* LFS constants */
407#ifdef F_GETLK64
408 if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1;
409#endif
410#ifdef F_SETLK64
411 if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1;
412#endif
413#ifdef F_SETLKW64
414 if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1;
415#endif
416/* GNU extensions, as of glibc 2.2.4. */
417#ifdef F_SETLEASE
418 if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1;
419#endif
420#ifdef F_GETLEASE
421 if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1;
422#endif
423#ifdef F_NOTIFY
424 if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1;
425#endif
426/* Old BSD flock(). */
427#ifdef F_EXLCK
428 if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1;
429#endif
430#ifdef F_SHLCK
431 if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1;
432#endif
433
434/* For F_{GET|SET}FL */
435#ifdef FD_CLOEXEC
436 if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1;
437#endif
438
439/* For F_NOTIFY */
440#ifdef DN_ACCESS
441 if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1;
442#endif
443#ifdef DN_MODIFY
444 if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1;
445#endif
446#ifdef DN_CREATE
447 if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1;
448#endif
449#ifdef DN_DELETE
450 if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1;
451#endif
452#ifdef DN_RENAME
453 if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1;
454#endif
455#ifdef DN_ATTRIB
456 if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1;
457#endif
458#ifdef DN_MULTISHOT
459 if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1;
460#endif
461
Guido van Rossum7c141031997-08-15 02:52:08 +0000462 return 0;
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000463}
464
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000465PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000466initfcntl(void)
Guido van Rossum02975121992-08-17 08:55:12 +0000467{
Roger E. Masse919213a1996-12-17 17:42:22 +0000468 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +0000469
Guido van Rossum185ead61998-11-23 15:32:55 +0000470 /* Create the module and add the functions and documentation */
471 m = Py_InitModule3("fcntl", fcntl_methods, module_doc);
Guido van Rossum02975121992-08-17 08:55:12 +0000472
473 /* Add some symbolic constants to the module */
Roger E. Masse919213a1996-12-17 17:42:22 +0000474 d = PyModule_GetDict(m);
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000475 all_ins(d);
Guido van Rossum02975121992-08-17 08:55:12 +0000476}