dlmodule.c 5.29 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;

18
static 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 *
42
dl_close(dlobject *xp)
43 44 45 46 47
{
	if (xp->dl_handle != NULL) {
		dlclose(xp->dl_handle);
		xp->dl_handle = NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
48 49
	Py_INCREF(Py_None);
	return Py_None;
50 51
}

Roger E. Masse's avatar
Roger E. Masse committed
52
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
53
dl_sym(dlobject *xp, PyObject *args)
54 55
{
	char *name;
Roger E. Masse's avatar
Roger E. Masse committed
56
	PyUnivPtr *func;
57 58 59 60 61
	if (PyString_Check(args)) {
		name = PyString_AS_STRING(args);
	} else {
		PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
			     args->ob_type->tp_name);
62
		return NULL;
63
	}
64 65
	func = dlsym(xp->dl_handle, name);
	if (func == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
66 67
		Py_INCREF(Py_None);
		return Py_None;
68
	}
Roger E. Masse's avatar
Roger E. Masse committed
69
	return PyInt_FromLong((long)func);
70 71
}

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

Roger E. Masse's avatar
Roger E. Masse committed
125
static PyMethodDef dlobject_methods[] = {
126
	{"call",	(PyCFunction)dl_call, METH_VARARGS},
127 128
	{"sym", 	(PyCFunction)dl_sym, METH_O},
	{"close",	(PyCFunction)dl_close, METH_NOARGS},
Roger E. Masse's avatar
Roger E. Masse committed
129
	{NULL,  	NULL}			 /* Sentinel */
130 131
};

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


Roger E. Masse's avatar
Roger E. Masse committed
139
static PyTypeObject Dltype = {
140
	PyObject_HEAD_INIT(NULL)
141
	0,			/*ob_size*/
142
	"dl.dl",		/*tp_name*/
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	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
158
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
159
dl_open(PyObject *self, PyObject *args)
160 161 162
{
	char *name;
	int mode;
Roger E. Masse's avatar
Roger E. Masse committed
163
	PyUnivPtr *handle;
164 165 166 167 168 169 170
	if (sizeof(int) != sizeof(long) ||
	    sizeof(long) != sizeof(char *)) {
		PyErr_SetString(PyExc_SystemError,
 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
		return NULL;
	}

171
	if (PyArg_ParseTuple(args, "z:open", &name))
172 173
		mode = RTLD_LAZY;
	else {
Roger E. Masse's avatar
Roger E. Masse committed
174
		PyErr_Clear();
175
		if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
176 177 178
			return NULL;
#ifndef RTLD_NOW
		if (mode != RTLD_LAZY) {
Roger E. Masse's avatar
Roger E. Masse committed
179
			PyErr_SetString(PyExc_ValueError, "mode must be 1");
180 181 182 183 184 185
			return NULL;
		}
#endif
	}
	handle = dlopen(name, mode);
	if (handle == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
186
		PyErr_SetString(Dlerror, dlerror());
187 188 189 190 191
		return NULL;
	}
	return newdlobject(handle);
}

Roger E. Masse's avatar
Roger E. Masse committed
192
static PyMethodDef dl_methods[] = {
193
	{"open",	dl_open, METH_VARARGS},
194 195 196
	{NULL,		NULL}		/* sentinel */
};

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/* 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);
}

212
PyMODINIT_FUNC
213
initdl(void)
214
{
Roger E. Masse's avatar
Roger E. Masse committed
215
	PyObject *m, *d, *x;
216

217 218 219
	/* Initialize object type */
	Dltype.ob_type = &PyType_Type;

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

	/* Add some symbolic constants to the module */
Roger E. Masse's avatar
Roger E. Masse committed
224
	d = PyModule_GetDict(m);
225
	Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
Roger E. Masse's avatar
Roger E. Masse committed
226 227 228
	PyDict_SetItemString(d, "error", x);
	x = PyInt_FromLong((long)RTLD_LAZY);
	PyDict_SetItemString(d, "RTLD_LAZY", x);
229
#define INSINT(X)    insint(d,#X,X)
230
#ifdef RTLD_NOW
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        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);
253 254
#endif
}