blob: 927f4c0ac8287e77540c0a1068da4f1fff2353d3 [file] [log] [blame]
Guido van Rossum34162a11994-05-23 12:37:57 +00001
2/* dl module */
3
Roger E. Masse0e120321996-12-13 20:33:44 +00004#include "Python.h"
Guido van Rossum34162a11994-05-23 12:37:57 +00005
6#include <dlfcn.h>
7
8#ifndef RTLD_LAZY
9#define RTLD_LAZY 1
10#endif
11
Thomas Wouters334fb892000-07-25 12:56:38 +000012typedef void *PyUnivPtr;
Guido van Rossum34162a11994-05-23 12:37:57 +000013typedef struct {
Roger E. Masse0e120321996-12-13 20:33:44 +000014 PyObject_HEAD
15 PyUnivPtr *dl_handle;
Guido van Rossum34162a11994-05-23 12:37:57 +000016} dlobject;
17
Jeremy Hylton938ace62002-07-17 16:30:39 +000018static PyTypeObject Dltype;
Guido van Rossum34162a11994-05-23 12:37:57 +000019
Roger E. Masse0e120321996-12-13 20:33:44 +000020static PyObject *Dlerror;
Guido van Rossum34162a11994-05-23 12:37:57 +000021
Roger E. Masse0e120321996-12-13 20:33:44 +000022static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000023newdlobject(PyUnivPtr *handle)
Guido van Rossum34162a11994-05-23 12:37:57 +000024{
25 dlobject *xp;
Guido van Rossumb18618d2000-05-03 23:44:39 +000026 xp = PyObject_New(dlobject, &Dltype);
Guido van Rossum34162a11994-05-23 12:37:57 +000027 if (xp == NULL)
28 return NULL;
29 xp->dl_handle = handle;
Roger E. Masse0e120321996-12-13 20:33:44 +000030 return (PyObject *)xp;
Guido van Rossum34162a11994-05-23 12:37:57 +000031}
32
33static void
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000034dl_dealloc(dlobject *xp)
Guido van Rossum34162a11994-05-23 12:37:57 +000035{
36 if (xp->dl_handle != NULL)
37 dlclose(xp->dl_handle);
Guido van Rossumb18618d2000-05-03 23:44:39 +000038 PyObject_Del(xp);
Guido van Rossum34162a11994-05-23 12:37:57 +000039}
40
Roger E. Masse0e120321996-12-13 20:33:44 +000041static PyObject *
Neal Norwitzb82d34f2002-03-31 15:43:28 +000042dl_close(dlobject *xp)
Guido van Rossum34162a11994-05-23 12:37:57 +000043{
Guido van Rossum34162a11994-05-23 12:37:57 +000044 if (xp->dl_handle != NULL) {
45 dlclose(xp->dl_handle);
46 xp->dl_handle = NULL;
47 }
Roger E. Masse0e120321996-12-13 20:33:44 +000048 Py_INCREF(Py_None);
49 return Py_None;
Guido van Rossum34162a11994-05-23 12:37:57 +000050}
51
Roger E. Masse0e120321996-12-13 20:33:44 +000052static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000053dl_sym(dlobject *xp, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +000054{
55 char *name;
Roger E. Masse0e120321996-12-13 20:33:44 +000056 PyUnivPtr *func;
Neal Norwitzb82d34f2002-03-31 15:43:28 +000057 if (PyString_Check(args)) {
58 name = PyString_AS_STRING(args);
59 } else {
60 PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
61 args->ob_type->tp_name);
Guido van Rossum34162a11994-05-23 12:37:57 +000062 return NULL;
Neal Norwitzb82d34f2002-03-31 15:43:28 +000063 }
Guido van Rossum34162a11994-05-23 12:37:57 +000064 func = dlsym(xp->dl_handle, name);
65 if (func == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +000066 Py_INCREF(Py_None);
67 return Py_None;
Guido van Rossum34162a11994-05-23 12:37:57 +000068 }
Roger E. Masse0e120321996-12-13 20:33:44 +000069 return PyInt_FromLong((long)func);
Guido van Rossum34162a11994-05-23 12:37:57 +000070}
71
Roger E. Masse0e120321996-12-13 20:33:44 +000072static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000073dl_call(dlobject *xp, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +000074{
Roger E. Masse0e120321996-12-13 20:33:44 +000075 PyObject *name;
Fred Drake4747a182000-10-11 21:44:02 +000076 long (*func)(long, long, long, long, long,
77 long, long, long, long, long);
Guido van Rossum34162a11994-05-23 12:37:57 +000078 long alist[10];
79 long res;
80 int i;
Roger E. Masse0e120321996-12-13 20:33:44 +000081 int n = PyTuple_Size(args);
Guido van Rossum34162a11994-05-23 12:37:57 +000082 if (n < 1) {
Roger E. Masse0e120321996-12-13 20:33:44 +000083 PyErr_SetString(PyExc_TypeError, "at least a name is needed");
Guido van Rossum34162a11994-05-23 12:37:57 +000084 return NULL;
85 }
Roger E. Masse0e120321996-12-13 20:33:44 +000086 name = PyTuple_GetItem(args, 0);
87 if (!PyString_Check(name)) {
88 PyErr_SetString(PyExc_TypeError,
89 "function name must be a string");
Guido van Rossum34162a11994-05-23 12:37:57 +000090 return NULL;
91 }
Andrew M. Kuchlinga1a690f2001-02-22 15:52:55 +000092 func = (long (*)(long, long, long, long, long,
93 long, long, long, long, long))
94 dlsym(xp->dl_handle, PyString_AsString(name));
Guido van Rossum34162a11994-05-23 12:37:57 +000095 if (func == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +000096 PyErr_SetString(PyExc_ValueError, dlerror());
Guido van Rossum34162a11994-05-23 12:37:57 +000097 return NULL;
98 }
99 if (n-1 > 10) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000100 PyErr_SetString(PyExc_TypeError,
101 "too many arguments (max 10)");
Guido van Rossum34162a11994-05-23 12:37:57 +0000102 return NULL;
103 }
104 for (i = 1; i < n; i++) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000105 PyObject *v = PyTuple_GetItem(args, i);
106 if (PyInt_Check(v))
107 alist[i-1] = PyInt_AsLong(v);
108 else if (PyString_Check(v))
109 alist[i-1] = (long)PyString_AsString(v);
110 else if (v == Py_None)
Guido van Rossum34162a11994-05-23 12:37:57 +0000111 alist[i-1] = (long) ((char *)NULL);
112 else {
Roger E. Masse0e120321996-12-13 20:33:44 +0000113 PyErr_SetString(PyExc_TypeError,
Guido van Rossum34162a11994-05-23 12:37:57 +0000114 "arguments must be int, string or None");
115 return NULL;
116 }
117 }
118 for (; i <= 10; i++)
119 alist[i-1] = 0;
120 res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
121 alist[5], alist[6], alist[7], alist[8], alist[9]);
Roger E. Masse0e120321996-12-13 20:33:44 +0000122 return PyInt_FromLong(res);
Guido van Rossum34162a11994-05-23 12:37:57 +0000123}
124
Roger E. Masse0e120321996-12-13 20:33:44 +0000125static PyMethodDef dlobject_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000126 {"call", (PyCFunction)dl_call, METH_VARARGS},
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000127 {"sym", (PyCFunction)dl_sym, METH_O},
128 {"close", (PyCFunction)dl_close, METH_NOARGS},
Roger E. Masse0e120321996-12-13 20:33:44 +0000129 {NULL, NULL} /* Sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000130};
131
Roger E. Masse0e120321996-12-13 20:33:44 +0000132static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000133dl_getattr(dlobject *xp, char *name)
Guido van Rossum34162a11994-05-23 12:37:57 +0000134{
Roger E. Masse0e120321996-12-13 20:33:44 +0000135 return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000136}
137
138
Roger E. Masse0e120321996-12-13 20:33:44 +0000139static PyTypeObject Dltype = {
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +0000140 PyObject_HEAD_INIT(NULL)
Guido van Rossum34162a11994-05-23 12:37:57 +0000141 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000142 "dl.dl", /*tp_name*/
Guido van Rossum34162a11994-05-23 12:37:57 +0000143 sizeof(dlobject), /*tp_basicsize*/
144 0, /*tp_itemsize*/
145 /* methods */
146 (destructor)dl_dealloc, /*tp_dealloc*/
147 0, /*tp_print*/
148 (getattrfunc)dl_getattr,/*tp_getattr*/
149 0, /*tp_setattr*/
150 0, /*tp_compare*/
151 0, /*tp_repr*/
152 0, /*tp_as_number*/
153 0, /*tp_as_sequence*/
154 0, /*tp_as_mapping*/
155 0, /*tp_hash*/
156};
157
Roger E. Masse0e120321996-12-13 20:33:44 +0000158static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000159dl_open(PyObject *self, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000160{
161 char *name;
162 int mode;
Roger E. Masse0e120321996-12-13 20:33:44 +0000163 PyUnivPtr *handle;
Martin v. Löwis93227272002-01-01 20:18:30 +0000164 if (sizeof(int) != sizeof(long) ||
165 sizeof(long) != sizeof(char *)) {
166 PyErr_SetString(PyExc_SystemError,
167 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
168 return NULL;
169 }
170
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000171 if (PyArg_ParseTuple(args, "z:open", &name))
Guido van Rossum34162a11994-05-23 12:37:57 +0000172 mode = RTLD_LAZY;
173 else {
Roger E. Masse0e120321996-12-13 20:33:44 +0000174 PyErr_Clear();
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000175 if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
Guido van Rossum34162a11994-05-23 12:37:57 +0000176 return NULL;
177#ifndef RTLD_NOW
178 if (mode != RTLD_LAZY) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000179 PyErr_SetString(PyExc_ValueError, "mode must be 1");
Guido van Rossum34162a11994-05-23 12:37:57 +0000180 return NULL;
181 }
182#endif
183 }
184 handle = dlopen(name, mode);
185 if (handle == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000186 PyErr_SetString(Dlerror, dlerror());
Guido van Rossum34162a11994-05-23 12:37:57 +0000187 return NULL;
188 }
189 return newdlobject(handle);
190}
191
Roger E. Masse0e120321996-12-13 20:33:44 +0000192static PyMethodDef dl_methods[] = {
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000193 {"open", dl_open, METH_VARARGS},
Guido van Rossum34162a11994-05-23 12:37:57 +0000194 {NULL, NULL} /* sentinel */
195};
196
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000197/* From socketmodule.c
198 * Convenience routine to export an integer value.
199 *
200 * Errors are silently ignored, for better or for worse...
201 */
202static void
203insint(PyObject *d, char *name, int value)
204{
205 PyObject *v = PyInt_FromLong((long) value);
206 if (!v || PyDict_SetItemString(d, name, v))
207 PyErr_Clear();
208
209 Py_XDECREF(v);
210}
211
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000212PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000213initdl(void)
Guido van Rossum34162a11994-05-23 12:37:57 +0000214{
Roger E. Masse0e120321996-12-13 20:33:44 +0000215 PyObject *m, *d, *x;
Guido van Rossum34162a11994-05-23 12:37:57 +0000216
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +0000217 /* Initialize object type */
218 Dltype.ob_type = &PyType_Type;
219
Guido van Rossum34162a11994-05-23 12:37:57 +0000220 /* Create the module and add the functions */
Roger E. Masse0e120321996-12-13 20:33:44 +0000221 m = Py_InitModule("dl", dl_methods);
Guido van Rossum34162a11994-05-23 12:37:57 +0000222
223 /* Add some symbolic constants to the module */
Roger E. Masse0e120321996-12-13 20:33:44 +0000224 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000225 Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
Roger E. Masse0e120321996-12-13 20:33:44 +0000226 PyDict_SetItemString(d, "error", x);
227 x = PyInt_FromLong((long)RTLD_LAZY);
228 PyDict_SetItemString(d, "RTLD_LAZY", x);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000229#define INSINT(X) insint(d,#X,X)
Guido van Rossum34162a11994-05-23 12:37:57 +0000230#ifdef RTLD_NOW
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000231 INSINT(RTLD_NOW);
232#endif
233#ifdef RTLD_NOLOAD
234 INSINT(RTLD_NOLOAD);
235#endif
236#ifdef RTLD_GLOBAL
237 INSINT(RTLD_GLOBAL);
238#endif
239#ifdef RTLD_LOCAL
240 INSINT(RTLD_LOCAL);
241#endif
242#ifdef RTLD_PARENT
243 INSINT(RTLD_PARENT);
244#endif
245#ifdef RTLD_GROUP
246 INSINT(RTLD_GROUP);
247#endif
248#ifdef RTLD_WORLD
249 INSINT(RTLD_WORLD);
250#endif
251#ifdef RTLD_NODELETE
252 INSINT(RTLD_NODELETE);
Guido van Rossum34162a11994-05-23 12:37:57 +0000253#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000254}