blob: 43f1773032317a323181dfdf92ce450936b0291e [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>
Martin v. Löwis14e73b12003-01-01 09:51:12 +000012#ifdef HAVE_STROPTS_H
13#include <stropts.h>
14#endif
Guido van Rossum02975121992-08-17 08:55:12 +000015
Fred Drake152a25e2001-05-09 21:02:02 +000016static int
17conv_descriptor(PyObject *object, int *target)
18{
19 int fd = PyObject_AsFileDescriptor(object);
20
21 if (fd < 0)
22 return 0;
23 *target = fd;
24 return 1;
25}
26
27
Guido van Rossum02975121992-08-17 08:55:12 +000028/* fcntl(fd, opt, [arg]) */
29
Roger E. Masse919213a1996-12-17 17:42:22 +000030static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000031fcntl_fcntl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000032{
33 int fd;
34 int code;
35 int arg;
36 int ret;
37 char *str;
38 int len;
39 char buf[1024];
40
Fred Drake152a25e2001-05-09 21:02:02 +000041 if (PyArg_ParseTuple(args, "O&is#:fcntl",
42 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +000043 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +000044 PyErr_SetString(PyExc_ValueError,
45 "fcntl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +000046 return NULL;
47 }
48 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +000049 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000050 ret = fcntl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +000051 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000052 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000053 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000054 return NULL;
55 }
Roger E. Masse919213a1996-12-17 17:42:22 +000056 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +000057 }
58
Roger E. Masse919213a1996-12-17 17:42:22 +000059 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +000060 arg = 0;
Fred Drake152a25e2001-05-09 21:02:02 +000061 if (!PyArg_ParseTuple(args,
62 "O&i|i;fcntl requires a file or file descriptor,"
63 " an integer and optionally a third integer or a string",
64 conv_descriptor, &fd, &code, &arg)) {
Guido van Rossuma2214c32000-08-02 20:46:51 +000065 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +000066 }
Roger E. Masse919213a1996-12-17 17:42:22 +000067 Py_BEGIN_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000068 ret = fcntl(fd, code, arg);
Roger E. Masse919213a1996-12-17 17:42:22 +000069 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000070 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000071 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000072 return NULL;
73 }
Roger E. Masse919213a1996-12-17 17:42:22 +000074 return PyInt_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +000075}
76
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000077PyDoc_STRVAR(fcntl_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +000078"fcntl(fd, opt, [arg])\n\
79\n\
80Perform the requested operation on file descriptor fd. The operation\n\
Fred Drake1d531992001-05-10 15:54:32 +000081is defined by op and is operating system dependent. These constants are\n\
82available from the fcntl module. The argument arg is optional, and\n\
83defaults to 0; it may be an int or a string. If arg is given as a string,\n\
84the return value of fcntl is a string of that length, containing the\n\
85resulting value put in the arg buffer by the operating system.The length\n\
86of the arg string is not allowed to exceed 1024 bytes. If the arg given\n\
87is an integer or if none is specified, the result value is an integer\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088corresponding to the return value of the fcntl call in the C code.");
Guido van Rossum185ead61998-11-23 15:32:55 +000089
Guido van Rossum02975121992-08-17 08:55:12 +000090
91/* ioctl(fd, opt, [arg]) */
92
Roger E. Masse919213a1996-12-17 17:42:22 +000093static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +000094fcntl_ioctl(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +000095{
96 int fd;
97 int code;
98 int arg;
99 int ret;
100 char *str;
101 int len;
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000102 int mutate_arg = 0;
Guido van Rossum02975121992-08-17 08:55:12 +0000103 char buf[1024];
104
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000105 if (PyArg_ParseTuple(args, "O&iw#|i:ioctl",
106 conv_descriptor, &fd, &code,
107 &str, &len, &mutate_arg)) {
108 char *arg;
109
110 if (PyTuple_Size(args) == 3) {
Martin v. Löwis77ca6c42004-06-03 12:47:26 +0000111#if (PY_MAJOR_VERSION>2) || (PY_MINOR_VERSION>=5)
112#error Remove the warning, change mutate_arg to 1
113#endif
114 if (PyErr_Warn(PyExc_FutureWarning,
115 "ioctl with mutable buffer will mutate the buffer by default in 2.5"
116 ) < 0)
117 return NULL;
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000118 mutate_arg = 0;
119 }
120 if (mutate_arg) {
121 if (len <= sizeof buf) {
122 memcpy(buf, str, len);
123 arg = buf;
124 }
125 else {
126 arg = str;
127 }
128 }
129 else {
130 if (len > sizeof buf) {
131 PyErr_SetString(PyExc_ValueError,
132 "ioctl string arg too long");
133 return NULL;
134 }
135 else {
136 memcpy(buf, str, len);
137 arg = buf;
138 }
139 }
140 if (buf == arg) {
141 Py_BEGIN_ALLOW_THREADS /* think array.resize() */
142 ret = ioctl(fd, code, arg);
143 Py_END_ALLOW_THREADS
144 }
145 else {
146 ret = ioctl(fd, code, arg);
147 }
148 if (mutate_arg && (len < sizeof buf)) {
149 memcpy(str, buf, len);
150 }
151 if (ret < 0) {
152 PyErr_SetFromErrno(PyExc_IOError);
153 return NULL;
154 }
155 if (mutate_arg) {
156 return PyInt_FromLong(ret);
157 }
158 else {
159 return PyString_FromStringAndSize(buf, len);
160 }
161 }
162
163 PyErr_Clear();
Fred Drake152a25e2001-05-09 21:02:02 +0000164 if (PyArg_ParseTuple(args, "O&is#:ioctl",
165 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +0000166 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000167 PyErr_SetString(PyExc_ValueError,
168 "ioctl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +0000169 return NULL;
170 }
171 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +0000172 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000173 ret = ioctl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +0000174 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000175 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000176 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000177 return NULL;
178 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000179 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +0000180 }
181
Roger E. Masse919213a1996-12-17 17:42:22 +0000182 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +0000183 arg = 0;
Fred Drake460f0692001-05-14 21:02:36 +0000184 if (!PyArg_ParseTuple(args,
185 "O&i|i;ioctl requires a file or file descriptor,"
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000186 " an integer and optionally a integer or buffer argument",
Fred Drake152a25e2001-05-09 21:02:02 +0000187 conv_descriptor, &fd, &code, &arg)) {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000188 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +0000189 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000190 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000191#ifdef __VMS
192 ret = ioctl(fd, code, (void *)arg);
193#else
Guido van Rossum02975121992-08-17 08:55:12 +0000194 ret = ioctl(fd, code, arg);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000195#endif
Roger E. Masse919213a1996-12-17 17:42:22 +0000196 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +0000197 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000198 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000199 return NULL;
200 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000201 return PyInt_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +0000202}
203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204PyDoc_STRVAR(ioctl_doc,
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000205"ioctl(fd, opt[, arg[, mutate_flag]])\n\
Guido van Rossum185ead61998-11-23 15:32:55 +0000206\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000207Perform the requested operation on file descriptor fd. The operation is\n\
Neal Norwitz47308802003-06-30 01:54:04 +0000208defined by opt and is operating system dependent. Typically these codes are\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000209retrieved from the fcntl or termios library modules.\n\
210\n\
211The argument arg is optional, and defaults to 0; it may be an int or a\n\
212buffer containing character data (most likely a string or an array). \n\
213\n\
214If the argument is a mutable buffer (such as an array) and if the\n\
215mutate_flag argument (which is only allowed in this case) is true then the\n\
216buffer is (in effect) passed to the operating system and changes made by\n\
217the OS will be reflected in the contents of the buffer after the call has\n\
218returned. The return value is the integer returned by the ioctl system\n\
219call.\n\
220\n\
221If the argument is a mutable buffer and the mutable_flag argument is not\n\
222passed or is false, the behavior is as if a string had been passed. This\n\
223behavior will change in future releases of Python.\n\
224\n\
225If the argument is an immutable buffer (most likely a string) then a copy\n\
226of the buffer is passed to the operating system and the return value is a\n\
227string of the same length containing whatever the operating system put in\n\
228the buffer. The length of the arg buffer in this case is not allowed to\n\
229exceed 1024 bytes.\n\
230\n\
231If the arg given is an integer or if none is specified, the result value is\n\
232an integer corresponding to the return value of the ioctl call in the C\n\
233code.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000234
Guido van Rossum02975121992-08-17 08:55:12 +0000235
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000236/* flock(fd, operation) */
237
Roger E. Masse919213a1996-12-17 17:42:22 +0000238static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000239fcntl_flock(PyObject *self, PyObject *args)
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000240{
241 int fd;
242 int code;
243 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000244
Fred Drake152a25e2001-05-09 21:02:02 +0000245 if (!PyArg_ParseTuple(args, "O&i:flock",
246 conv_descriptor, &fd, &code))
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000247 return NULL;
248
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000249#ifdef HAVE_FLOCK
Guido van Rossum056bad91999-01-06 18:44:23 +0000250 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000251 ret = flock(fd, code);
Guido van Rossum056bad91999-01-06 18:44:23 +0000252 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000253#else
254
255#ifndef LOCK_SH
256#define LOCK_SH 1 /* shared lock */
257#define LOCK_EX 2 /* exclusive lock */
258#define LOCK_NB 4 /* don't block when locking */
259#define LOCK_UN 8 /* unlock */
260#endif
261 {
262 struct flock l;
263 if (code == LOCK_UN)
264 l.l_type = F_UNLCK;
265 else if (code & LOCK_SH)
266 l.l_type = F_RDLCK;
267 else if (code & LOCK_EX)
268 l.l_type = F_WRLCK;
269 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000270 PyErr_SetString(PyExc_ValueError,
271 "unrecognized flock argument");
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000272 return NULL;
273 }
274 l.l_whence = l.l_start = l.l_len = 0;
Guido van Rossum056bad91999-01-06 18:44:23 +0000275 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000276 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000277 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000278 }
279#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000280 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000281 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000282 return NULL;
283 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000284 Py_INCREF(Py_None);
285 return Py_None;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(flock_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000289"flock(fd, operation)\n\
290\n\
291Perform the lock operation op on file descriptor fd. See the Unix \n\
292manual flock(3) for details. (On some systems, this function is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293emulated using fcntl().)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000294
295
Guido van Rossumc8643641996-09-11 23:17:20 +0000296/* lockf(fd, operation) */
Roger E. Masse919213a1996-12-17 17:42:22 +0000297static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000298fcntl_lockf(PyObject *self, PyObject *args)
Guido van Rossumc8643641996-09-11 23:17:20 +0000299{
Guido van Rossum056bad91999-01-06 18:44:23 +0000300 int fd, code, ret, whence = 0;
301 PyObject *lenobj = NULL, *startobj = NULL;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000302
Fred Drake152a25e2001-05-09 21:02:02 +0000303 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
304 conv_descriptor, &fd, &code,
Guido van Rossum056bad91999-01-06 18:44:23 +0000305 &lenobj, &startobj, &whence))
Guido van Rossumc8643641996-09-11 23:17:20 +0000306 return NULL;
307
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000308#if defined(PYOS_OS2) && defined(PYCC_GCC)
309 PyErr_SetString(PyExc_NotImplementedError,
310 "lockf not supported on OS/2 (EMX)");
311 return NULL;
312#else
Guido van Rossumc8643641996-09-11 23:17:20 +0000313#ifndef LOCK_SH
314#define LOCK_SH 1 /* shared lock */
315#define LOCK_EX 2 /* exclusive lock */
316#define LOCK_NB 4 /* don't block when locking */
317#define LOCK_UN 8 /* unlock */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000318#endif /* LOCK_SH */
Guido van Rossumc8643641996-09-11 23:17:20 +0000319 {
320 struct flock l;
321 if (code == LOCK_UN)
322 l.l_type = F_UNLCK;
323 else if (code & LOCK_SH)
324 l.l_type = F_RDLCK;
325 else if (code & LOCK_EX)
326 l.l_type = F_WRLCK;
327 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000328 PyErr_SetString(PyExc_ValueError,
329 "unrecognized flock argument");
Guido van Rossumc8643641996-09-11 23:17:20 +0000330 return NULL;
331 }
Guido van Rossum056bad91999-01-06 18:44:23 +0000332 l.l_start = l.l_len = 0;
333 if (startobj != NULL) {
334#if !defined(HAVE_LARGEFILE_SUPPORT)
335 l.l_start = PyInt_AsLong(startobj);
336#else
337 l.l_start = PyLong_Check(startobj) ?
338 PyLong_AsLongLong(startobj) :
339 PyInt_AsLong(startobj);
340#endif
341 if (PyErr_Occurred())
342 return NULL;
343 }
344 if (lenobj != NULL) {
345#if !defined(HAVE_LARGEFILE_SUPPORT)
346 l.l_len = PyInt_AsLong(lenobj);
347#else
348 l.l_len = PyLong_Check(lenobj) ?
349 PyLong_AsLongLong(lenobj) :
350 PyInt_AsLong(lenobj);
351#endif
352 if (PyErr_Occurred())
353 return NULL;
354 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000355 l.l_whence = whence;
Guido van Rossum056bad91999-01-06 18:44:23 +0000356 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000357 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000358 Py_END_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000359 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000360 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000361 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumc8643641996-09-11 23:17:20 +0000362 return NULL;
363 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000364 Py_INCREF(Py_None);
365 return Py_None;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000366#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
Guido van Rossumc8643641996-09-11 23:17:20 +0000367}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000369PyDoc_STRVAR(lockf_doc,
Sjoerd Mullender82e00d62001-01-25 10:10:39 +0000370"lockf (fd, operation, length=0, start=0, whence=0)\n\
371\n\
372This is essentially a wrapper around the fcntl() locking calls. fd is the\n\
373file descriptor of the file to lock or unlock, and operation is one of the\n\
374following values:\n\
375\n\
376 LOCK_UN - unlock\n\
377 LOCK_SH - acquire a shared lock\n\
378 LOCK_EX - acquire an exclusive lock\n\
379\n\
380When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\
381LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\
382lock cannot be acquired, an IOError will be raised and the exception will\n\
383have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\
384system -- for portability, check for either value).\n\
385\n\
386length is the number of bytes to lock, with the default meaning to lock to\n\
387EOF. start is the byte offset, relative to whence, to that the lock\n\
388starts. whence is as with fileobj.seek(), specifically:\n\
389\n\
390 0 - relative to the start of the file (SEEK_SET)\n\
391 1 - relative to the current buffer position (SEEK_CUR)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392 2 - relative to the end of the file (SEEK_END)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000393
Guido van Rossum02975121992-08-17 08:55:12 +0000394/* List of functions */
395
Roger E. Masse919213a1996-12-17 17:42:22 +0000396static PyMethodDef fcntl_methods[] = {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000397 {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
398 {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
399 {"flock", fcntl_flock, METH_VARARGS, flock_doc},
400 {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
Guido van Rossum02975121992-08-17 08:55:12 +0000401 {NULL, NULL} /* sentinel */
402};
403
404
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000405PyDoc_STRVAR(module_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000406"This module performs file control and I/O control on file \n\
407descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
408routines. File descriptors can be obtained with the fileno() method of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409a file or socket object.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000410
Guido van Rossum02975121992-08-17 08:55:12 +0000411/* Module initialisation */
412
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000413static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000414ins(PyObject* d, char* symbol, long value)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000415{
416 PyObject* v = PyInt_FromLong(value);
417 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
418 return -1;
419
420 Py_DECREF(v);
421 return 0;
422}
423
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000424#define INS(x) if (ins(d, #x, (long)x)) return -1
425
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000426static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000427all_ins(PyObject* d)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000428{
429 if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
430 if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
431 if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
432 if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000433/* GNU extensions, as of glibc 2.2.4 */
434#ifdef LOCK_MAND
435 if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1;
436#endif
437#ifdef LOCK_READ
438 if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1;
439#endif
440#ifdef LOCK_WRITE
441 if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1;
442#endif
443#ifdef LOCK_RW
444 if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1;
445#endif
446
Fred Drake152a25e2001-05-09 21:02:02 +0000447#ifdef F_DUPFD
448 if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1;
449#endif
450#ifdef F_GETFD
451 if (ins(d, "F_GETFD", (long)F_GETFD)) return -1;
452#endif
453#ifdef F_SETFD
454 if (ins(d, "F_SETFD", (long)F_SETFD)) return -1;
455#endif
456#ifdef F_GETFL
457 if (ins(d, "F_GETFL", (long)F_GETFL)) return -1;
458#endif
459#ifdef F_SETFL
460 if (ins(d, "F_SETFL", (long)F_SETFL)) return -1;
461#endif
462#ifdef F_GETLK
463 if (ins(d, "F_GETLK", (long)F_GETLK)) return -1;
464#endif
465#ifdef F_SETLK
466 if (ins(d, "F_SETLK", (long)F_SETLK)) return -1;
467#endif
468#ifdef F_SETLKW
469 if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1;
470#endif
471#ifdef F_GETOWN
472 if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1;
473#endif
474#ifdef F_SETOWN
475 if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1;
476#endif
477#ifdef F_GETSIG
478 if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1;
479#endif
480#ifdef F_SETSIG
481 if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1;
482#endif
483#ifdef F_RDLCK
484 if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1;
485#endif
486#ifdef F_WRLCK
487 if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1;
488#endif
489#ifdef F_UNLCK
490 if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1;
491#endif
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000492/* LFS constants */
493#ifdef F_GETLK64
494 if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1;
495#endif
496#ifdef F_SETLK64
497 if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1;
498#endif
499#ifdef F_SETLKW64
500 if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1;
501#endif
502/* GNU extensions, as of glibc 2.2.4. */
503#ifdef F_SETLEASE
504 if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1;
505#endif
506#ifdef F_GETLEASE
507 if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1;
508#endif
509#ifdef F_NOTIFY
510 if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1;
511#endif
512/* Old BSD flock(). */
513#ifdef F_EXLCK
514 if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1;
515#endif
516#ifdef F_SHLCK
517 if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1;
518#endif
519
520/* For F_{GET|SET}FL */
521#ifdef FD_CLOEXEC
522 if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1;
523#endif
524
525/* For F_NOTIFY */
526#ifdef DN_ACCESS
527 if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1;
528#endif
529#ifdef DN_MODIFY
530 if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1;
531#endif
532#ifdef DN_CREATE
533 if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1;
534#endif
535#ifdef DN_DELETE
536 if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1;
537#endif
538#ifdef DN_RENAME
539 if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1;
540#endif
541#ifdef DN_ATTRIB
542 if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1;
543#endif
544#ifdef DN_MULTISHOT
545 if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1;
546#endif
547
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000548#ifdef HAVE_STROPTS_H
549 /* Unix 98 guarantees that these are in stropts.h. */
550 INS(I_PUSH);
551 INS(I_POP);
552 INS(I_LOOK);
553 INS(I_FLUSH);
554 INS(I_FLUSHBAND);
555 INS(I_SETSIG);
556 INS(I_GETSIG);
557 INS(I_FIND);
558 INS(I_PEEK);
559 INS(I_SRDOPT);
560 INS(I_GRDOPT);
561 INS(I_NREAD);
562 INS(I_FDINSERT);
563 INS(I_STR);
564 INS(I_SWROPT);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000565#ifdef I_GWROPT
566 /* despite the comment above, old-ish glibcs miss a couple... */
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000567 INS(I_GWROPT);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000568#endif
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000569 INS(I_SENDFD);
570 INS(I_RECVFD);
571 INS(I_LIST);
572 INS(I_ATMARK);
573 INS(I_CKBAND);
574 INS(I_GETBAND);
575 INS(I_CANPUT);
576 INS(I_SETCLTIME);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000577#ifdef I_GETCLTIME
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000578 INS(I_GETCLTIME);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000579#endif
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000580 INS(I_LINK);
581 INS(I_UNLINK);
582 INS(I_PLINK);
583 INS(I_PUNLINK);
584#endif
585
Guido van Rossum7c141031997-08-15 02:52:08 +0000586 return 0;
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000587}
588
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000589PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000590initfcntl(void)
Guido van Rossum02975121992-08-17 08:55:12 +0000591{
Roger E. Masse919213a1996-12-17 17:42:22 +0000592 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +0000593
Guido van Rossum185ead61998-11-23 15:32:55 +0000594 /* Create the module and add the functions and documentation */
595 m = Py_InitModule3("fcntl", fcntl_methods, module_doc);
Guido van Rossum02975121992-08-17 08:55:12 +0000596
597 /* Add some symbolic constants to the module */
Roger E. Masse919213a1996-12-17 17:42:22 +0000598 d = PyModule_GetDict(m);
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000599 all_ins(d);
Guido van Rossum02975121992-08-17 08:55:12 +0000600}