blob: d109e29e7c94e626254583990fd09311568ba19a [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)
24 return 0;
25 *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{
35 int fd;
36 int code;
37 int arg;
38 int ret;
39 char *str;
Thomas Wouters26cc63f2006-03-02 00:21:10 +000040 Py_ssize_t len;
Guido van Rossum02975121992-08-17 08:55:12 +000041 char buf[1024];
42
Fred Drake152a25e2001-05-09 21:02:02 +000043 if (PyArg_ParseTuple(args, "O&is#:fcntl",
44 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +000045 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +000046 PyErr_SetString(PyExc_ValueError,
47 "fcntl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +000048 return NULL;
49 }
50 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +000051 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000052 ret = fcntl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +000053 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +000054 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000055 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000056 return NULL;
57 }
Roger E. Masse919213a1996-12-17 17:42:22 +000058 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +000059 }
60
Roger E. Masse919213a1996-12-17 17:42:22 +000061 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +000062 arg = 0;
Fred Drake152a25e2001-05-09 21:02:02 +000063 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 Rossuma2214c32000-08-02 20:46:51 +000067 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +000068 }
Roger E. Masse919213a1996-12-17 17:42:22 +000069 Py_BEGIN_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000070 ret = fcntl(fd, code, arg);
Roger E. Masse919213a1996-12-17 17:42:22 +000071 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +000072 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +000073 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +000074 return NULL;
75 }
Roger E. Masse919213a1996-12-17 17:42:22 +000076 return PyInt_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\
85defaults to 0; it may be an int or a string. If arg is given as a string,\n\
86the return value of fcntl is a string of that length, containing the\n\
87resulting 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\
89is 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{
98 int fd;
99 int code;
100 int arg;
101 int ret;
102 char *str;
Thomas Wouters26cc63f2006-03-02 00:21:10 +0000103 Py_ssize_t len;
Michael W. Hudson02d74f62004-11-30 14:31:54 +0000104 int mutate_arg = 1;
Guido van Rossum02975121992-08-17 08:55:12 +0000105 char buf[1024];
106
Michael W. Hudson8137bea2005-07-27 20:24:40 +0000107 if (PyArg_ParseTuple(args, "O&Iw#|i:ioctl",
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000108 conv_descriptor, &fd, &code,
109 &str, &len, &mutate_arg)) {
110 char *arg;
111
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000112 if (mutate_arg) {
113 if (len <= sizeof buf) {
114 memcpy(buf, str, len);
115 arg = buf;
116 }
117 else {
118 arg = str;
119 }
120 }
121 else {
122 if (len > sizeof buf) {
123 PyErr_SetString(PyExc_ValueError,
124 "ioctl string arg too long");
125 return NULL;
126 }
127 else {
128 memcpy(buf, str, len);
129 arg = buf;
130 }
131 }
132 if (buf == arg) {
133 Py_BEGIN_ALLOW_THREADS /* think array.resize() */
134 ret = ioctl(fd, code, arg);
135 Py_END_ALLOW_THREADS
136 }
137 else {
138 ret = ioctl(fd, code, arg);
139 }
140 if (mutate_arg && (len < sizeof buf)) {
141 memcpy(str, buf, len);
142 }
143 if (ret < 0) {
144 PyErr_SetFromErrno(PyExc_IOError);
145 return NULL;
146 }
147 if (mutate_arg) {
148 return PyInt_FromLong(ret);
149 }
150 else {
151 return PyString_FromStringAndSize(buf, len);
152 }
153 }
154
155 PyErr_Clear();
Michael W. Hudson8137bea2005-07-27 20:24:40 +0000156 if (PyArg_ParseTuple(args, "O&Is#:ioctl",
Fred Drake152a25e2001-05-09 21:02:02 +0000157 conv_descriptor, &fd, &code, &str, &len)) {
Guido van Rossum02975121992-08-17 08:55:12 +0000158 if (len > sizeof buf) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000159 PyErr_SetString(PyExc_ValueError,
160 "ioctl string arg too long");
Guido van Rossum02975121992-08-17 08:55:12 +0000161 return NULL;
162 }
163 memcpy(buf, str, len);
Roger E. Masse919213a1996-12-17 17:42:22 +0000164 Py_BEGIN_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000165 ret = ioctl(fd, code, buf);
Roger E. Masse919213a1996-12-17 17:42:22 +0000166 Py_END_ALLOW_THREADS
Guido van Rossum903f4871995-10-07 19:18:22 +0000167 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000168 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000169 return NULL;
170 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000171 return PyString_FromStringAndSize(buf, len);
Guido van Rossum02975121992-08-17 08:55:12 +0000172 }
173
Roger E. Masse919213a1996-12-17 17:42:22 +0000174 PyErr_Clear();
Guido van Rossuma2214c32000-08-02 20:46:51 +0000175 arg = 0;
Fred Drake460f0692001-05-14 21:02:36 +0000176 if (!PyArg_ParseTuple(args,
Michael W. Hudson8137bea2005-07-27 20:24:40 +0000177 "O&I|i;ioctl requires a file or file descriptor,"
Hye-Shik Chang97bb8ad2005-07-28 05:57:19 +0000178 " an integer and optionally an integer or buffer argument",
Fred Drake152a25e2001-05-09 21:02:02 +0000179 conv_descriptor, &fd, &code, &arg)) {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000180 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +0000181 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000182 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000183#ifdef __VMS
184 ret = ioctl(fd, code, (void *)arg);
185#else
Guido van Rossum02975121992-08-17 08:55:12 +0000186 ret = ioctl(fd, code, arg);
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000187#endif
Roger E. Masse919213a1996-12-17 17:42:22 +0000188 Py_END_ALLOW_THREADS
Guido van Rossum02975121992-08-17 08:55:12 +0000189 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000190 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum02975121992-08-17 08:55:12 +0000191 return NULL;
192 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000193 return PyInt_FromLong((long)ret);
Guido van Rossum02975121992-08-17 08:55:12 +0000194}
195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(ioctl_doc,
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000197"ioctl(fd, opt[, arg[, mutate_flag]])\n\
Guido van Rossum185ead61998-11-23 15:32:55 +0000198\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000199Perform the requested operation on file descriptor fd. The operation is\n\
Neal Norwitz47308802003-06-30 01:54:04 +0000200defined by opt and is operating system dependent. Typically these codes are\n\
Michael W. Hudsonf0089982003-03-03 12:29:42 +0000201retrieved from the fcntl or termios library modules.\n\
202\n\
203The argument arg is optional, and defaults to 0; it may be an int or a\n\
204buffer containing character data (most likely a string or an array). \n\
205\n\
206If the argument is a mutable buffer (such as an array) and if the\n\
207mutate_flag argument (which is only allowed in this case) is true then the\n\
208buffer is (in effect) passed to the operating system and changes made by\n\
209the OS will be reflected in the contents of the buffer after the call has\n\
210returned. The return value is the integer returned by the ioctl system\n\
211call.\n\
212\n\
213If the argument is a mutable buffer and the mutable_flag argument is not\n\
214passed or is false, the behavior is as if a string had been passed. This\n\
215behavior will change in future releases of Python.\n\
216\n\
217If the argument is an immutable buffer (most likely a string) then a copy\n\
218of the buffer is passed to the operating system and the return value is a\n\
219string of the same length containing whatever the operating system put in\n\
220the buffer. The length of the arg buffer in this case is not allowed to\n\
221exceed 1024 bytes.\n\
222\n\
223If the arg given is an integer or if none is specified, the result value is\n\
224an integer corresponding to the return value of the ioctl call in the C\n\
225code.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000226
Guido van Rossum02975121992-08-17 08:55:12 +0000227
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000228/* flock(fd, operation) */
229
Roger E. Masse919213a1996-12-17 17:42:22 +0000230static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000231fcntl_flock(PyObject *self, PyObject *args)
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000232{
233 int fd;
234 int code;
235 int ret;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000236
Fred Drake152a25e2001-05-09 21:02:02 +0000237 if (!PyArg_ParseTuple(args, "O&i:flock",
238 conv_descriptor, &fd, &code))
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000239 return NULL;
240
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000241#ifdef HAVE_FLOCK
Guido van Rossum056bad91999-01-06 18:44:23 +0000242 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000243 ret = flock(fd, code);
Guido van Rossum056bad91999-01-06 18:44:23 +0000244 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000245#else
246
247#ifndef LOCK_SH
248#define LOCK_SH 1 /* shared lock */
249#define LOCK_EX 2 /* exclusive lock */
250#define LOCK_NB 4 /* don't block when locking */
251#define LOCK_UN 8 /* unlock */
252#endif
253 {
254 struct flock l;
255 if (code == LOCK_UN)
256 l.l_type = F_UNLCK;
257 else if (code & LOCK_SH)
258 l.l_type = F_RDLCK;
259 else if (code & LOCK_EX)
260 l.l_type = F_WRLCK;
261 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000262 PyErr_SetString(PyExc_ValueError,
263 "unrecognized flock argument");
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000264 return NULL;
265 }
266 l.l_whence = l.l_start = l.l_len = 0;
Guido van Rossum056bad91999-01-06 18:44:23 +0000267 Py_BEGIN_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000268 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000269 Py_END_ALLOW_THREADS
Guido van Rossum3c0b79c1996-06-11 15:11:34 +0000270 }
271#endif /* HAVE_FLOCK */
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000272 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000273 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000274 return NULL;
275 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000276 Py_INCREF(Py_None);
277 return Py_None;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000278}
279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000280PyDoc_STRVAR(flock_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000281"flock(fd, operation)\n\
282\n\
283Perform the lock operation op on file descriptor fd. See the Unix \n\
284manual flock(3) for details. (On some systems, this function is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000285emulated using fcntl().)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000286
287
Guido van Rossumc8643641996-09-11 23:17:20 +0000288/* lockf(fd, operation) */
Roger E. Masse919213a1996-12-17 17:42:22 +0000289static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000290fcntl_lockf(PyObject *self, PyObject *args)
Guido van Rossumc8643641996-09-11 23:17:20 +0000291{
Guido van Rossum056bad91999-01-06 18:44:23 +0000292 int fd, code, ret, whence = 0;
293 PyObject *lenobj = NULL, *startobj = NULL;
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000294
Fred Drake152a25e2001-05-09 21:02:02 +0000295 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
296 conv_descriptor, &fd, &code,
Guido van Rossum056bad91999-01-06 18:44:23 +0000297 &lenobj, &startobj, &whence))
Guido van Rossumc8643641996-09-11 23:17:20 +0000298 return NULL;
299
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000300#if defined(PYOS_OS2) && defined(PYCC_GCC)
301 PyErr_SetString(PyExc_NotImplementedError,
302 "lockf not supported on OS/2 (EMX)");
303 return NULL;
304#else
Guido van Rossumc8643641996-09-11 23:17:20 +0000305#ifndef LOCK_SH
306#define LOCK_SH 1 /* shared lock */
307#define LOCK_EX 2 /* exclusive lock */
308#define LOCK_NB 4 /* don't block when locking */
309#define LOCK_UN 8 /* unlock */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000310#endif /* LOCK_SH */
Guido van Rossumc8643641996-09-11 23:17:20 +0000311 {
312 struct flock l;
313 if (code == LOCK_UN)
314 l.l_type = F_UNLCK;
315 else if (code & LOCK_SH)
316 l.l_type = F_RDLCK;
317 else if (code & LOCK_EX)
318 l.l_type = F_WRLCK;
319 else {
Roger E. Masse919213a1996-12-17 17:42:22 +0000320 PyErr_SetString(PyExc_ValueError,
321 "unrecognized flock argument");
Guido van Rossumc8643641996-09-11 23:17:20 +0000322 return NULL;
323 }
Guido van Rossum056bad91999-01-06 18:44:23 +0000324 l.l_start = l.l_len = 0;
325 if (startobj != NULL) {
326#if !defined(HAVE_LARGEFILE_SUPPORT)
327 l.l_start = PyInt_AsLong(startobj);
328#else
329 l.l_start = PyLong_Check(startobj) ?
330 PyLong_AsLongLong(startobj) :
331 PyInt_AsLong(startobj);
332#endif
333 if (PyErr_Occurred())
334 return NULL;
335 }
336 if (lenobj != NULL) {
337#if !defined(HAVE_LARGEFILE_SUPPORT)
338 l.l_len = PyInt_AsLong(lenobj);
339#else
340 l.l_len = PyLong_Check(lenobj) ?
341 PyLong_AsLongLong(lenobj) :
342 PyInt_AsLong(lenobj);
343#endif
344 if (PyErr_Occurred())
345 return NULL;
346 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000347 l.l_whence = whence;
Guido van Rossum056bad91999-01-06 18:44:23 +0000348 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000349 ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
Guido van Rossum056bad91999-01-06 18:44:23 +0000350 Py_END_ALLOW_THREADS
Guido van Rossumc8643641996-09-11 23:17:20 +0000351 }
Guido van Rossumc8643641996-09-11 23:17:20 +0000352 if (ret < 0) {
Roger E. Masse919213a1996-12-17 17:42:22 +0000353 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumc8643641996-09-11 23:17:20 +0000354 return NULL;
355 }
Roger E. Masse919213a1996-12-17 17:42:22 +0000356 Py_INCREF(Py_None);
357 return Py_None;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000358#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
Guido van Rossumc8643641996-09-11 23:17:20 +0000359}
Guido van Rossum3539b1e1996-05-23 22:56:38 +0000360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000361PyDoc_STRVAR(lockf_doc,
Sjoerd Mullender82e00d62001-01-25 10:10:39 +0000362"lockf (fd, operation, length=0, start=0, whence=0)\n\
363\n\
364This is essentially a wrapper around the fcntl() locking calls. fd is the\n\
365file descriptor of the file to lock or unlock, and operation is one of the\n\
366following values:\n\
367\n\
368 LOCK_UN - unlock\n\
369 LOCK_SH - acquire a shared lock\n\
370 LOCK_EX - acquire an exclusive lock\n\
371\n\
372When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with\n\
373LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n\
374lock cannot be acquired, an IOError will be raised and the exception will\n\
375have an errno attribute set to EACCES or EAGAIN (depending on the operating\n\
376system -- for portability, check for either value).\n\
377\n\
378length is the number of bytes to lock, with the default meaning to lock to\n\
379EOF. start is the byte offset, relative to whence, to that the lock\n\
380starts. whence is as with fileobj.seek(), specifically:\n\
381\n\
382 0 - relative to the start of the file (SEEK_SET)\n\
383 1 - relative to the current buffer position (SEEK_CUR)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384 2 - relative to the end of the file (SEEK_END)");
Guido van Rossum185ead61998-11-23 15:32:55 +0000385
Guido van Rossum02975121992-08-17 08:55:12 +0000386/* List of functions */
387
Roger E. Masse919213a1996-12-17 17:42:22 +0000388static PyMethodDef fcntl_methods[] = {
Guido van Rossuma2214c32000-08-02 20:46:51 +0000389 {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
390 {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
391 {"flock", fcntl_flock, METH_VARARGS, flock_doc},
392 {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
Guido van Rossum02975121992-08-17 08:55:12 +0000393 {NULL, NULL} /* sentinel */
394};
395
396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(module_doc,
Guido van Rossum185ead61998-11-23 15:32:55 +0000398"This module performs file control and I/O control on file \n\
399descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
400routines. File descriptors can be obtained with the fileno() method of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401a file or socket object.");
Guido van Rossum185ead61998-11-23 15:32:55 +0000402
Guido van Rossum02975121992-08-17 08:55:12 +0000403/* Module initialisation */
404
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000405static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000406ins(PyObject* d, char* symbol, long value)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000407{
408 PyObject* v = PyInt_FromLong(value);
409 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
410 return -1;
411
412 Py_DECREF(v);
413 return 0;
414}
415
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000416#define INS(x) if (ins(d, #x, (long)x)) return -1
417
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000418static int
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000419all_ins(PyObject* d)
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000420{
421 if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
422 if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
423 if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
424 if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000425/* GNU extensions, as of glibc 2.2.4 */
426#ifdef LOCK_MAND
427 if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1;
428#endif
429#ifdef LOCK_READ
430 if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1;
431#endif
432#ifdef LOCK_WRITE
433 if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1;
434#endif
435#ifdef LOCK_RW
436 if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1;
437#endif
438
Fred Drake152a25e2001-05-09 21:02:02 +0000439#ifdef F_DUPFD
440 if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1;
441#endif
442#ifdef F_GETFD
443 if (ins(d, "F_GETFD", (long)F_GETFD)) return -1;
444#endif
445#ifdef F_SETFD
446 if (ins(d, "F_SETFD", (long)F_SETFD)) return -1;
447#endif
448#ifdef F_GETFL
449 if (ins(d, "F_GETFL", (long)F_GETFL)) return -1;
450#endif
451#ifdef F_SETFL
452 if (ins(d, "F_SETFL", (long)F_SETFL)) return -1;
453#endif
454#ifdef F_GETLK
455 if (ins(d, "F_GETLK", (long)F_GETLK)) return -1;
456#endif
457#ifdef F_SETLK
458 if (ins(d, "F_SETLK", (long)F_SETLK)) return -1;
459#endif
460#ifdef F_SETLKW
461 if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1;
462#endif
463#ifdef F_GETOWN
464 if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1;
465#endif
466#ifdef F_SETOWN
467 if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1;
468#endif
469#ifdef F_GETSIG
470 if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1;
471#endif
472#ifdef F_SETSIG
473 if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1;
474#endif
475#ifdef F_RDLCK
476 if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1;
477#endif
478#ifdef F_WRLCK
479 if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1;
480#endif
481#ifdef F_UNLCK
482 if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1;
483#endif
Martin v. Löwis1baeba62001-12-28 21:08:12 +0000484/* LFS constants */
485#ifdef F_GETLK64
486 if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1;
487#endif
488#ifdef F_SETLK64
489 if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1;
490#endif
491#ifdef F_SETLKW64
492 if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1;
493#endif
494/* GNU extensions, as of glibc 2.2.4. */
495#ifdef F_SETLEASE
496 if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1;
497#endif
498#ifdef F_GETLEASE
499 if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1;
500#endif
501#ifdef F_NOTIFY
502 if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1;
503#endif
504/* Old BSD flock(). */
505#ifdef F_EXLCK
506 if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1;
507#endif
508#ifdef F_SHLCK
509 if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1;
510#endif
511
512/* For F_{GET|SET}FL */
513#ifdef FD_CLOEXEC
514 if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1;
515#endif
516
517/* For F_NOTIFY */
518#ifdef DN_ACCESS
519 if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1;
520#endif
521#ifdef DN_MODIFY
522 if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1;
523#endif
524#ifdef DN_CREATE
525 if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1;
526#endif
527#ifdef DN_DELETE
528 if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1;
529#endif
530#ifdef DN_RENAME
531 if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1;
532#endif
533#ifdef DN_ATTRIB
534 if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1;
535#endif
536#ifdef DN_MULTISHOT
537 if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1;
538#endif
539
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000540#ifdef HAVE_STROPTS_H
541 /* Unix 98 guarantees that these are in stropts.h. */
542 INS(I_PUSH);
543 INS(I_POP);
544 INS(I_LOOK);
545 INS(I_FLUSH);
546 INS(I_FLUSHBAND);
547 INS(I_SETSIG);
548 INS(I_GETSIG);
549 INS(I_FIND);
550 INS(I_PEEK);
551 INS(I_SRDOPT);
552 INS(I_GRDOPT);
553 INS(I_NREAD);
554 INS(I_FDINSERT);
555 INS(I_STR);
556 INS(I_SWROPT);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000557#ifdef I_GWROPT
558 /* despite the comment above, old-ish glibcs miss a couple... */
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000559 INS(I_GWROPT);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000560#endif
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000561 INS(I_SENDFD);
562 INS(I_RECVFD);
563 INS(I_LIST);
564 INS(I_ATMARK);
565 INS(I_CKBAND);
566 INS(I_GETBAND);
567 INS(I_CANPUT);
568 INS(I_SETCLTIME);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000569#ifdef I_GETCLTIME
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000570 INS(I_GETCLTIME);
Michael W. Hudson505c4c22003-05-09 10:45:20 +0000571#endif
Martin v. Löwis14e73b12003-01-01 09:51:12 +0000572 INS(I_LINK);
573 INS(I_UNLINK);
574 INS(I_PLINK);
575 INS(I_PUNLINK);
576#endif
577
Guido van Rossum7c141031997-08-15 02:52:08 +0000578 return 0;
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000579}
580
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000581PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000582initfcntl(void)
Guido van Rossum02975121992-08-17 08:55:12 +0000583{
Roger E. Masse919213a1996-12-17 17:42:22 +0000584 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +0000585
Guido van Rossum185ead61998-11-23 15:32:55 +0000586 /* Create the module and add the functions and documentation */
587 m = Py_InitModule3("fcntl", fcntl_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000588 if (m == NULL)
589 return;
Guido van Rossum02975121992-08-17 08:55:12 +0000590
591 /* Add some symbolic constants to the module */
Roger E. Masse919213a1996-12-17 17:42:22 +0000592 d = PyModule_GetDict(m);
Guido van Rossumf4e32c71997-07-31 19:39:54 +0000593 all_ins(d);
Guido van Rossum02975121992-08-17 08:55:12 +0000594}