fcntlmodule.c 6.58 KB
Newer Older
1
/***********************************************************
2 3
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
4 5 6

                        All Rights Reserved

7 8
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
9
provided that the above copyright notice appear in all copies and that
10
both that copyright notice and this permission notice appear in
11
supporting documentation, and that the names of Stichting Mathematisch
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
29 30 31 32 33

******************************************************************/

/* fcntl module */

Roger E. Masse's avatar
Roger E. Masse committed
34
#include "Python.h"
35

Guido van Rossum's avatar
Guido van Rossum committed
36 37 38 39 40 41 42 43
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif

44
#include <sys/ioctl.h>
45 46
#include <fcntl.h>

47 48 49

/* fcntl(fd, opt, [arg]) */

Roger E. Masse's avatar
Roger E. Masse committed
50
static PyObject *
51
fcntl_fcntl(self, args)
Roger E. Masse's avatar
Roger E. Masse committed
52 53
	PyObject *self; /* Not used */
	PyObject *args;
54 55 56 57 58 59 60 61 62
{
	int fd;
	int code;
	int arg;
	int ret;
	char *str;
	int len;
	char buf[1024];

Roger E. Masse's avatar
Roger E. Masse committed
63
	if (PyArg_Parse(args, "(iis#)", &fd, &code, &str, &len)) {
64
		if (len > sizeof buf) {
Roger E. Masse's avatar
Roger E. Masse committed
65 66
			PyErr_SetString(PyExc_ValueError,
					"fcntl string arg too long");
67 68 69
			return NULL;
		}
		memcpy(buf, str, len);
Roger E. Masse's avatar
Roger E. Masse committed
70
		Py_BEGIN_ALLOW_THREADS
71
		ret = fcntl(fd, code, buf);
Roger E. Masse's avatar
Roger E. Masse committed
72
		Py_END_ALLOW_THREADS
73
		if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
74
			PyErr_SetFromErrno(PyExc_IOError);
75 76
			return NULL;
		}
Roger E. Masse's avatar
Roger E. Masse committed
77
		return PyString_FromStringAndSize(buf, len);
78 79
	}

Roger E. Masse's avatar
Roger E. Masse committed
80 81
	PyErr_Clear();
	if (PyArg_Parse(args, "(ii)", &fd, &code))
82 83
		arg = 0;
	else {
Roger E. Masse's avatar
Roger E. Masse committed
84 85
		PyErr_Clear();
		if (!PyArg_Parse(args, "(iii)", &fd, &code, &arg))
86 87
			return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
88
	Py_BEGIN_ALLOW_THREADS
89
	ret = fcntl(fd, code, arg);
Roger E. Masse's avatar
Roger E. Masse committed
90
	Py_END_ALLOW_THREADS
91
	if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
92
		PyErr_SetFromErrno(PyExc_IOError);
93 94
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
95
	return PyInt_FromLong((long)ret);
96 97 98 99 100
}


/* ioctl(fd, opt, [arg]) */

Roger E. Masse's avatar
Roger E. Masse committed
101
static PyObject *
102
fcntl_ioctl(self, args)
Roger E. Masse's avatar
Roger E. Masse committed
103 104
	PyObject *self; /* Not used */
	PyObject *args;
105 106 107 108 109 110 111 112 113
{
	int fd;
	int code;
	int arg;
	int ret;
	char *str;
	int len;
	char buf[1024];

Roger E. Masse's avatar
Roger E. Masse committed
114
	if (PyArg_Parse(args, "(iis#)", &fd, &code, &str, &len)) {
115
		if (len > sizeof buf) {
Roger E. Masse's avatar
Roger E. Masse committed
116 117
			PyErr_SetString(PyExc_ValueError,
					"ioctl string arg too long");
118 119 120
			return NULL;
		}
		memcpy(buf, str, len);
Roger E. Masse's avatar
Roger E. Masse committed
121
		Py_BEGIN_ALLOW_THREADS
122
		ret = ioctl(fd, code, buf);
Roger E. Masse's avatar
Roger E. Masse committed
123
		Py_END_ALLOW_THREADS
124
		if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
125
			PyErr_SetFromErrno(PyExc_IOError);
126 127
			return NULL;
		}
Roger E. Masse's avatar
Roger E. Masse committed
128
		return PyString_FromStringAndSize(buf, len);
129 130
	}

Roger E. Masse's avatar
Roger E. Masse committed
131 132
	PyErr_Clear();
	if (PyArg_Parse(args, "(ii)", &fd, &code))
133 134
		arg = 0;
	else {
Roger E. Masse's avatar
Roger E. Masse committed
135 136
		PyErr_Clear();
		if (!PyArg_Parse(args, "(iii)", &fd, &code, &arg))
137 138
			return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
139
	Py_BEGIN_ALLOW_THREADS
140
	ret = ioctl(fd, code, arg);
Roger E. Masse's avatar
Roger E. Masse committed
141
	Py_END_ALLOW_THREADS
142
	if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
143
		PyErr_SetFromErrno(PyExc_IOError);
144 145
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
146
	return PyInt_FromLong((long)ret);
147 148 149
}


Guido van Rossum's avatar
Guido van Rossum committed
150 151
/* flock(fd, operation) */

Roger E. Masse's avatar
Roger E. Masse committed
152
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
153
fcntl_flock(self, args)
Roger E. Masse's avatar
Roger E. Masse committed
154 155
	PyObject *self; /* Not used */
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
156 157 158 159 160
{
	int fd;
	int code;
	int ret;

Roger E. Masse's avatar
Roger E. Masse committed
161
	if (!PyArg_Parse(args, "(ii)", &fd, &code))
Guido van Rossum's avatar
Guido van Rossum committed
162 163
		return NULL;

Roger E. Masse's avatar
Roger E. Masse committed
164
	Py_BEGIN_ALLOW_THREADS
165
#ifdef HAVE_FLOCK
Guido van Rossum's avatar
Guido van Rossum committed
166
	ret = flock(fd, code);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#else

#ifndef LOCK_SH
#define LOCK_SH		1	/* shared lock */
#define LOCK_EX		2	/* exclusive lock */
#define LOCK_NB		4	/* don't block when locking */
#define LOCK_UN		8	/* unlock */
#endif
	{
		struct flock l;
		if (code == LOCK_UN)
			l.l_type = F_UNLCK;
		else if (code & LOCK_SH)
			l.l_type = F_RDLCK;
		else if (code & LOCK_EX)
			l.l_type = F_WRLCK;
		else {
Roger E. Masse's avatar
Roger E. Masse committed
184 185
			PyErr_SetString(PyExc_ValueError,
					"unrecognized flock argument");
186 187 188 189 190 191
			return NULL;
		}
		l.l_whence = l.l_start = l.l_len = 0;
		ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
	}
#endif /* HAVE_FLOCK */
Roger E. Masse's avatar
Roger E. Masse committed
192
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
193
	if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
194
		PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum's avatar
Guido van Rossum committed
195 196
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
197 198
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
199 200
}

Guido van Rossum's avatar
Guido van Rossum committed
201
/* lockf(fd, operation) */
Roger E. Masse's avatar
Roger E. Masse committed
202
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
203
fcntl_lockf(self, args)
Roger E. Masse's avatar
Roger E. Masse committed
204 205
	PyObject *self; /* Not used */
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
206 207
{
	int fd, code, len = 0, start = 0, whence = 0, ret;
Guido van Rossum's avatar
Guido van Rossum committed
208

Guido van Rossum's avatar
Guido van Rossum committed
209 210 211 212
	if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len, 
			       &start, &whence))
	    return NULL;

Roger E. Masse's avatar
Roger E. Masse committed
213
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#ifndef LOCK_SH
#define LOCK_SH		1	/* shared lock */
#define LOCK_EX		2	/* exclusive lock */
#define LOCK_NB		4	/* don't block when locking */
#define LOCK_UN		8	/* unlock */
#endif
	{
		struct flock l;
		if (code == LOCK_UN)
			l.l_type = F_UNLCK;
		else if (code & LOCK_SH)
			l.l_type = F_RDLCK;
		else if (code & LOCK_EX)
			l.l_type = F_WRLCK;
		else {
Roger E. Masse's avatar
Roger E. Masse committed
229 230
			PyErr_SetString(PyExc_ValueError,
					"unrecognized flock argument");
Guido van Rossum's avatar
Guido van Rossum committed
231 232 233 234 235 236 237
			return NULL;
		}
		l.l_len = len;
		l.l_start = start;
		l.l_whence = whence;
		ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
	}
Roger E. Masse's avatar
Roger E. Masse committed
238
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
239
	if (ret < 0) {
Roger E. Masse's avatar
Roger E. Masse committed
240
		PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum's avatar
Guido van Rossum committed
241 242
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
243 244
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
245
}
Guido van Rossum's avatar
Guido van Rossum committed
246

247 248
/* List of functions */

Roger E. Masse's avatar
Roger E. Masse committed
249
static PyMethodDef fcntl_methods[] = {
250 251
	{"fcntl",	fcntl_fcntl},
	{"ioctl",	fcntl_ioctl},
Guido van Rossum's avatar
Guido van Rossum committed
252
	{"flock",	fcntl_flock},
Guido van Rossum's avatar
Guido van Rossum committed
253
	{"lockf",       fcntl_lockf, 1},
254 255 256 257 258 259
	{NULL,		NULL}		/* sentinel */
};


/* Module initialisation */

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int
ins(d, symbol, value)
        PyObject* d;
        char* symbol;
        long value;
{
        PyObject* v = PyInt_FromLong(value);
        if (!v || PyDict_SetItemString(d, symbol, v) < 0)
                return -1;

        Py_DECREF(v);
        return 0;
}

static int
all_ins(d)
        PyObject* d;
{
        if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
        if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
        if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
        if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
Guido van Rossum's avatar
Guido van Rossum committed
282
	return 0;
283 284
}

285 286 287
void
initfcntl()
{
Roger E. Masse's avatar
Roger E. Masse committed
288
	PyObject *m, *d;
289 290

	/* Create the module and add the functions */
Roger E. Masse's avatar
Roger E. Masse committed
291
	m = Py_InitModule("fcntl", fcntl_methods);
292 293

	/* Add some symbolic constants to the module */
Roger E. Masse's avatar
Roger E. Masse committed
294
	d = PyModule_GetDict(m);
295
	all_ins(d);
296 297

	/* Check for errors */
Roger E. Masse's avatar
Roger E. Masse committed
298 299
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module fcntl");
300
}