dlmodule.c 5.17 KB
Newer Older
1 2 3

/* dl module */

Roger E. Masse's avatar
Roger E. Masse committed
4
#include "Python.h"
5 6 7 8 9 10 11

#include <dlfcn.h>

#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif

12
typedef void *PyUnivPtr;
13
typedef struct {
Roger E. Masse's avatar
Roger E. Masse committed
14 15
	PyObject_HEAD
	PyUnivPtr *dl_handle;
16 17
} dlobject;

Roger E. Masse's avatar
Roger E. Masse committed
18
staticforward PyTypeObject Dltype;
19

Roger E. Masse's avatar
Roger E. Masse committed
20
static PyObject *Dlerror;
21

Roger E. Masse's avatar
Roger E. Masse committed
22
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
23
newdlobject(PyUnivPtr *handle)
24 25
{
	dlobject *xp;
26
	xp = PyObject_New(dlobject, &Dltype);
27 28 29
	if (xp == NULL)
		return NULL;
	xp->dl_handle = handle;
Roger E. Masse's avatar
Roger E. Masse committed
30
	return (PyObject *)xp;
31 32 33
}

static void
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
34
dl_dealloc(dlobject *xp)
35 36 37
{
	if (xp->dl_handle != NULL)
		dlclose(xp->dl_handle);
38
	PyObject_Del(xp);
39 40
}

Roger E. Masse's avatar
Roger E. Masse committed
41
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
42
dl_close(dlobject *xp, PyObject *args)
43
{
Roger E. Masse's avatar
Roger E. Masse committed
44
	if (!PyArg_Parse(args, ""))
45 46 47 48 49
		return NULL;
	if (xp->dl_handle != NULL) {
		dlclose(xp->dl_handle);
		xp->dl_handle = NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
50 51
	Py_INCREF(Py_None);
	return Py_None;
52 53
}

Roger E. Masse's avatar
Roger E. Masse committed
54
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
55
dl_sym(dlobject *xp, PyObject *args)
56 57
{
	char *name;
Roger E. Masse's avatar
Roger E. Masse committed
58 59
	PyUnivPtr *func;
	if (!PyArg_Parse(args, "s", &name))
60 61 62
		return NULL;
	func = dlsym(xp->dl_handle, name);
	if (func == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
63 64
		Py_INCREF(Py_None);
		return Py_None;
65
	}
Roger E. Masse's avatar
Roger E. Masse committed
66
	return PyInt_FromLong((long)func);
67 68
}

Roger E. Masse's avatar
Roger E. Masse committed
69
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
70
dl_call(dlobject *xp, PyObject *args)
71
{
Roger E. Masse's avatar
Roger E. Masse committed
72
	PyObject *name;
73 74
	long (*func)(long, long, long, long, long,
                     long, long, long, long, long);
75 76 77
	long alist[10];
	long res;
	int i;
Roger E. Masse's avatar
Roger E. Masse committed
78
	int n = PyTuple_Size(args);
79
	if (n < 1) {
Roger E. Masse's avatar
Roger E. Masse committed
80
		PyErr_SetString(PyExc_TypeError, "at least a name is needed");
81 82
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
83 84 85 86
	name = PyTuple_GetItem(args, 0);
	if (!PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError,
				"function name must be a string");
87 88
		return NULL;
	}
89 90 91
	func = (long (*)(long, long, long, long, long, 
                         long, long, long, long, long)) 
          dlsym(xp->dl_handle, PyString_AsString(name));
92
	if (func == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
93
		PyErr_SetString(PyExc_ValueError, dlerror());
94 95 96
		return NULL;
	}
	if (n-1 > 10) {
Roger E. Masse's avatar
Roger E. Masse committed
97 98
		PyErr_SetString(PyExc_TypeError,
				"too many arguments (max 10)");
99 100 101
		return NULL;
	}
	for (i = 1; i < n; i++) {
Roger E. Masse's avatar
Roger E. Masse committed
102 103 104 105 106 107
		PyObject *v = PyTuple_GetItem(args, i);
		if (PyInt_Check(v))
			alist[i-1] = PyInt_AsLong(v);
		else if (PyString_Check(v))
			alist[i-1] = (long)PyString_AsString(v);
		else if (v == Py_None)
108 109
			alist[i-1] = (long) ((char *)NULL);
		else {
Roger E. Masse's avatar
Roger E. Masse committed
110
			PyErr_SetString(PyExc_TypeError,
111 112 113 114 115 116 117 118
				   "arguments must be int, string or None");
			return NULL;
		}
	}
	for (; i <= 10; i++)
		alist[i-1] = 0;
	res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
		      alist[5], alist[6], alist[7], alist[8], alist[9]);
Roger E. Masse's avatar
Roger E. Masse committed
119
	return PyInt_FromLong(res);
120 121
}

Roger E. Masse's avatar
Roger E. Masse committed
122 123 124 125 126
static PyMethodDef dlobject_methods[] = {
	{"call",	(PyCFunction)dl_call,	1 /* varargs */},
	{"sym", 	(PyCFunction)dl_sym},
	{"close",	(PyCFunction)dl_close},
	{NULL,  	NULL}			 /* Sentinel */
127 128
};

Roger E. Masse's avatar
Roger E. Masse committed
129
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
130
dl_getattr(dlobject *xp, char *name)
131
{
Roger E. Masse's avatar
Roger E. Masse committed
132
	return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
133 134 135
}


Roger E. Masse's avatar
Roger E. Masse committed
136
static PyTypeObject Dltype = {
137
	PyObject_HEAD_INIT(NULL)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	0,			/*ob_size*/
	"dl",			/*tp_name*/
	sizeof(dlobject),	/*tp_basicsize*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)dl_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)dl_getattr,/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
};

Roger E. Masse's avatar
Roger E. Masse committed
155
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
156
dl_open(PyObject *self, PyObject *args)
157 158 159
{
	char *name;
	int mode;
Roger E. Masse's avatar
Roger E. Masse committed
160 161
	PyUnivPtr *handle;
	if (PyArg_Parse(args, "z", &name))
162 163
		mode = RTLD_LAZY;
	else {
Roger E. Masse's avatar
Roger E. Masse committed
164 165
		PyErr_Clear();
		if (!PyArg_Parse(args, "(zi)", &name, &mode))
166 167 168
			return NULL;
#ifndef RTLD_NOW
		if (mode != RTLD_LAZY) {
Roger E. Masse's avatar
Roger E. Masse committed
169
			PyErr_SetString(PyExc_ValueError, "mode must be 1");
170 171 172 173 174 175
			return NULL;
		}
#endif
	}
	handle = dlopen(name, mode);
	if (handle == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
176
		PyErr_SetString(Dlerror, dlerror());
177 178 179 180 181
		return NULL;
	}
	return newdlobject(handle);
}

Roger E. Masse's avatar
Roger E. Masse committed
182
static PyMethodDef dl_methods[] = {
183 184 185 186
	{"open",	dl_open},
	{NULL,		NULL}		/* sentinel */
};

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/* From socketmodule.c
 * Convenience routine to export an integer value.
 *
 * Errors are silently ignored, for better or for worse...
 */
static void
insint(PyObject *d, char *name, int value)
{
	PyObject *v = PyInt_FromLong((long) value);
	if (!v || PyDict_SetItemString(d, name, v))
		PyErr_Clear();

	Py_XDECREF(v);
}

202
DL_EXPORT(void)
203
initdl(void)
204
{
Roger E. Masse's avatar
Roger E. Masse committed
205
	PyObject *m, *d, *x;
206 207

	if (sizeof(int) != sizeof(long) ||
208
	    sizeof(long) != sizeof(char *)) {
209
		PyErr_SetString(PyExc_SystemError,
210
 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
211 212
		return;
	}
213

214 215 216
	/* Initialize object type */
	Dltype.ob_type = &PyType_Type;

217
	/* Create the module and add the functions */
Roger E. Masse's avatar
Roger E. Masse committed
218
	m = Py_InitModule("dl", dl_methods);
219 220

	/* Add some symbolic constants to the module */
Roger E. Masse's avatar
Roger E. Masse committed
221
	d = PyModule_GetDict(m);
222
	Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
Roger E. Masse's avatar
Roger E. Masse committed
223 224 225
	PyDict_SetItemString(d, "error", x);
	x = PyInt_FromLong((long)RTLD_LAZY);
	PyDict_SetItemString(d, "RTLD_LAZY", x);
226
#define INSINT(X)    insint(d,#X,X)
227
#ifdef RTLD_NOW
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        INSINT(RTLD_NOW);
#endif
#ifdef RTLD_NOLOAD
        INSINT(RTLD_NOLOAD);
#endif
#ifdef RTLD_GLOBAL
        INSINT(RTLD_GLOBAL);
#endif
#ifdef RTLD_LOCAL
        INSINT(RTLD_LOCAL);
#endif
#ifdef RTLD_PARENT
        INSINT(RTLD_PARENT);
#endif
#ifdef RTLD_GROUP
        INSINT(RTLD_GROUP);
#endif
#ifdef RTLD_WORLD
        INSINT(RTLD_WORLD);
#endif
#ifdef RTLD_NODELETE
        INSINT(RTLD_NODELETE);
250 251
#endif
}