blob: 4af7e300c687c76d1632d1d61ee80d0f92e2fafc [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008#ifdef __VMS
9#include <unistd.h>
10#endif
11
Guido van Rossum34162a11994-05-23 12:37:57 +000012#ifndef RTLD_LAZY
13#define RTLD_LAZY 1
14#endif
15
Thomas Wouters334fb892000-07-25 12:56:38 +000016typedef void *PyUnivPtr;
Guido van Rossum34162a11994-05-23 12:37:57 +000017typedef struct {
Roger E. Masse0e120321996-12-13 20:33:44 +000018 PyObject_HEAD
19 PyUnivPtr *dl_handle;
Guido van Rossum34162a11994-05-23 12:37:57 +000020} dlobject;
21
Jeremy Hylton938ace62002-07-17 16:30:39 +000022static PyTypeObject Dltype;
Guido van Rossum34162a11994-05-23 12:37:57 +000023
Roger E. Masse0e120321996-12-13 20:33:44 +000024static PyObject *Dlerror;
Guido van Rossum34162a11994-05-23 12:37:57 +000025
Roger E. Masse0e120321996-12-13 20:33:44 +000026static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000027newdlobject(PyUnivPtr *handle)
Guido van Rossum34162a11994-05-23 12:37:57 +000028{
29 dlobject *xp;
Guido van Rossumb18618d2000-05-03 23:44:39 +000030 xp = PyObject_New(dlobject, &Dltype);
Guido van Rossum34162a11994-05-23 12:37:57 +000031 if (xp == NULL)
32 return NULL;
33 xp->dl_handle = handle;
Roger E. Masse0e120321996-12-13 20:33:44 +000034 return (PyObject *)xp;
Guido van Rossum34162a11994-05-23 12:37:57 +000035}
36
37static void
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000038dl_dealloc(dlobject *xp)
Guido van Rossum34162a11994-05-23 12:37:57 +000039{
40 if (xp->dl_handle != NULL)
41 dlclose(xp->dl_handle);
Guido van Rossumb18618d2000-05-03 23:44:39 +000042 PyObject_Del(xp);
Guido van Rossum34162a11994-05-23 12:37:57 +000043}
44
Roger E. Masse0e120321996-12-13 20:33:44 +000045static PyObject *
Neal Norwitzb82d34f2002-03-31 15:43:28 +000046dl_close(dlobject *xp)
Guido van Rossum34162a11994-05-23 12:37:57 +000047{
Guido van Rossum34162a11994-05-23 12:37:57 +000048 if (xp->dl_handle != NULL) {
49 dlclose(xp->dl_handle);
50 xp->dl_handle = NULL;
51 }
Roger E. Masse0e120321996-12-13 20:33:44 +000052 Py_INCREF(Py_None);
53 return Py_None;
Guido van Rossum34162a11994-05-23 12:37:57 +000054}
55
Roger E. Masse0e120321996-12-13 20:33:44 +000056static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000057dl_sym(dlobject *xp, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +000058{
59 char *name;
Roger E. Masse0e120321996-12-13 20:33:44 +000060 PyUnivPtr *func;
Neal Norwitzb82d34f2002-03-31 15:43:28 +000061 if (PyString_Check(args)) {
62 name = PyString_AS_STRING(args);
63 } else {
64 PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
65 args->ob_type->tp_name);
Guido van Rossum34162a11994-05-23 12:37:57 +000066 return NULL;
Neal Norwitzb82d34f2002-03-31 15:43:28 +000067 }
Guido van Rossum34162a11994-05-23 12:37:57 +000068 func = dlsym(xp->dl_handle, name);
69 if (func == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +000070 Py_INCREF(Py_None);
71 return Py_None;
Guido van Rossum34162a11994-05-23 12:37:57 +000072 }
Roger E. Masse0e120321996-12-13 20:33:44 +000073 return PyInt_FromLong((long)func);
Guido van Rossum34162a11994-05-23 12:37:57 +000074}
75
Roger E. Masse0e120321996-12-13 20:33:44 +000076static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000077dl_call(dlobject *xp, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +000078{
Roger E. Masse0e120321996-12-13 20:33:44 +000079 PyObject *name;
Fred Drake4747a182000-10-11 21:44:02 +000080 long (*func)(long, long, long, long, long,
81 long, long, long, long, long);
Guido van Rossum34162a11994-05-23 12:37:57 +000082 long alist[10];
83 long res;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084 Py_ssize_t i;
85 Py_ssize_t n = PyTuple_Size(args);
Guido van Rossum34162a11994-05-23 12:37:57 +000086 if (n < 1) {
Roger E. Masse0e120321996-12-13 20:33:44 +000087 PyErr_SetString(PyExc_TypeError, "at least a name is needed");
Guido van Rossum34162a11994-05-23 12:37:57 +000088 return NULL;
89 }
Roger E. Masse0e120321996-12-13 20:33:44 +000090 name = PyTuple_GetItem(args, 0);
91 if (!PyString_Check(name)) {
92 PyErr_SetString(PyExc_TypeError,
93 "function name must be a string");
Guido van Rossum34162a11994-05-23 12:37:57 +000094 return NULL;
95 }
Andrew M. Kuchlinga1a690f2001-02-22 15:52:55 +000096 func = (long (*)(long, long, long, long, long,
97 long, long, long, long, long))
98 dlsym(xp->dl_handle, PyString_AsString(name));
Guido van Rossum34162a11994-05-23 12:37:57 +000099 if (func == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000100 PyErr_SetString(PyExc_ValueError, dlerror());
Guido van Rossum34162a11994-05-23 12:37:57 +0000101 return NULL;
102 }
103 if (n-1 > 10) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000104 PyErr_SetString(PyExc_TypeError,
105 "too many arguments (max 10)");
Guido van Rossum34162a11994-05-23 12:37:57 +0000106 return NULL;
107 }
108 for (i = 1; i < n; i++) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000109 PyObject *v = PyTuple_GetItem(args, i);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000110 if (PyInt_Check(v)) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000111 alist[i-1] = PyInt_AsLong(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000112 if (alist[i-1] == -1 && PyErr_Occurred())
113 return NULL;
114 } else if (PyString_Check(v))
Roger E. Masse0e120321996-12-13 20:33:44 +0000115 alist[i-1] = (long)PyString_AsString(v);
116 else if (v == Py_None)
Guido van Rossum34162a11994-05-23 12:37:57 +0000117 alist[i-1] = (long) ((char *)NULL);
118 else {
Roger E. Masse0e120321996-12-13 20:33:44 +0000119 PyErr_SetString(PyExc_TypeError,
Guido van Rossum34162a11994-05-23 12:37:57 +0000120 "arguments must be int, string or None");
121 return NULL;
122 }
123 }
124 for (; i <= 10; i++)
125 alist[i-1] = 0;
126 res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
127 alist[5], alist[6], alist[7], alist[8], alist[9]);
Roger E. Masse0e120321996-12-13 20:33:44 +0000128 return PyInt_FromLong(res);
Guido van Rossum34162a11994-05-23 12:37:57 +0000129}
130
Roger E. Masse0e120321996-12-13 20:33:44 +0000131static PyMethodDef dlobject_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000132 {"call", (PyCFunction)dl_call, METH_VARARGS},
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000133 {"sym", (PyCFunction)dl_sym, METH_O},
134 {"close", (PyCFunction)dl_close, METH_NOARGS},
Roger E. Masse0e120321996-12-13 20:33:44 +0000135 {NULL, NULL} /* Sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000136};
137
Roger E. Masse0e120321996-12-13 20:33:44 +0000138static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000139dl_getattr(dlobject *xp, char *name)
Guido van Rossum34162a11994-05-23 12:37:57 +0000140{
Roger E. Masse0e120321996-12-13 20:33:44 +0000141 return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000142}
143
144
Roger E. Masse0e120321996-12-13 20:33:44 +0000145static PyTypeObject Dltype = {
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +0000146 PyObject_HEAD_INIT(NULL)
Guido van Rossum34162a11994-05-23 12:37:57 +0000147 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000148 "dl.dl", /*tp_name*/
Guido van Rossum34162a11994-05-23 12:37:57 +0000149 sizeof(dlobject), /*tp_basicsize*/
150 0, /*tp_itemsize*/
151 /* methods */
152 (destructor)dl_dealloc, /*tp_dealloc*/
153 0, /*tp_print*/
154 (getattrfunc)dl_getattr,/*tp_getattr*/
155 0, /*tp_setattr*/
156 0, /*tp_compare*/
157 0, /*tp_repr*/
158 0, /*tp_as_number*/
159 0, /*tp_as_sequence*/
160 0, /*tp_as_mapping*/
161 0, /*tp_hash*/
162};
163
Roger E. Masse0e120321996-12-13 20:33:44 +0000164static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000165dl_open(PyObject *self, PyObject *args)
Guido van Rossum34162a11994-05-23 12:37:57 +0000166{
167 char *name;
168 int mode;
Roger E. Masse0e120321996-12-13 20:33:44 +0000169 PyUnivPtr *handle;
Martin v. Löwis93227272002-01-01 20:18:30 +0000170 if (sizeof(int) != sizeof(long) ||
171 sizeof(long) != sizeof(char *)) {
172 PyErr_SetString(PyExc_SystemError,
173 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
174 return NULL;
175 }
176
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000177 if (PyArg_ParseTuple(args, "z:open", &name))
Guido van Rossum34162a11994-05-23 12:37:57 +0000178 mode = RTLD_LAZY;
179 else {
Roger E. Masse0e120321996-12-13 20:33:44 +0000180 PyErr_Clear();
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000181 if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
Guido van Rossum34162a11994-05-23 12:37:57 +0000182 return NULL;
183#ifndef RTLD_NOW
184 if (mode != RTLD_LAZY) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000185 PyErr_SetString(PyExc_ValueError, "mode must be 1");
Guido van Rossum34162a11994-05-23 12:37:57 +0000186 return NULL;
187 }
188#endif
189 }
190 handle = dlopen(name, mode);
191 if (handle == NULL) {
Roger E. Masse0e120321996-12-13 20:33:44 +0000192 PyErr_SetString(Dlerror, dlerror());
Guido van Rossum34162a11994-05-23 12:37:57 +0000193 return NULL;
194 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000195#ifdef __VMS
196 /* Under OpenVMS dlopen doesn't do any check, just save the name
197 * for later use, so we have to check if the file is readable,
198 * the name can be a logical or a file from SYS$SHARE.
199 */
200 if (access(name, R_OK)) {
201 char fname[strlen(name) + 20];
202 strcpy(fname, "SYS$SHARE:");
203 strcat(fname, name);
204 strcat(fname, ".EXE");
205 if (access(fname, R_OK)) {
206 dlclose(handle);
207 PyErr_SetString(Dlerror,
208 "File not found or protection violation");
209 return NULL;
210 }
211 }
212#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000213 return newdlobject(handle);
214}
215
Roger E. Masse0e120321996-12-13 20:33:44 +0000216static PyMethodDef dl_methods[] = {
Neal Norwitzb82d34f2002-03-31 15:43:28 +0000217 {"open", dl_open, METH_VARARGS},
Guido van Rossum34162a11994-05-23 12:37:57 +0000218 {NULL, NULL} /* sentinel */
219};
220
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000221/* From socketmodule.c
222 * Convenience routine to export an integer value.
223 *
224 * Errors are silently ignored, for better or for worse...
225 */
226static void
227insint(PyObject *d, char *name, int value)
228{
229 PyObject *v = PyInt_FromLong((long) value);
230 if (!v || PyDict_SetItemString(d, name, v))
231 PyErr_Clear();
232
233 Py_XDECREF(v);
234}
235
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000236PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000237initdl(void)
Guido van Rossum34162a11994-05-23 12:37:57 +0000238{
Roger E. Masse0e120321996-12-13 20:33:44 +0000239 PyObject *m, *d, *x;
Guido van Rossum34162a11994-05-23 12:37:57 +0000240
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +0000241 /* Initialize object type */
242 Dltype.ob_type = &PyType_Type;
243
Guido van Rossum34162a11994-05-23 12:37:57 +0000244 /* Create the module and add the functions */
Roger E. Masse0e120321996-12-13 20:33:44 +0000245 m = Py_InitModule("dl", dl_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000246 if (m == NULL)
247 return;
Guido van Rossum34162a11994-05-23 12:37:57 +0000248
249 /* Add some symbolic constants to the module */
Roger E. Masse0e120321996-12-13 20:33:44 +0000250 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000251 Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
Roger E. Masse0e120321996-12-13 20:33:44 +0000252 PyDict_SetItemString(d, "error", x);
253 x = PyInt_FromLong((long)RTLD_LAZY);
254 PyDict_SetItemString(d, "RTLD_LAZY", x);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000255#define INSINT(X) insint(d,#X,X)
Guido van Rossum34162a11994-05-23 12:37:57 +0000256#ifdef RTLD_NOW
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000257 INSINT(RTLD_NOW);
258#endif
259#ifdef RTLD_NOLOAD
260 INSINT(RTLD_NOLOAD);
261#endif
262#ifdef RTLD_GLOBAL
263 INSINT(RTLD_GLOBAL);
264#endif
265#ifdef RTLD_LOCAL
266 INSINT(RTLD_LOCAL);
267#endif
268#ifdef RTLD_PARENT
269 INSINT(RTLD_PARENT);
270#endif
271#ifdef RTLD_GROUP
272 INSINT(RTLD_GROUP);
273#endif
274#ifdef RTLD_WORLD
275 INSINT(RTLD_WORLD);
276#endif
277#ifdef RTLD_NODELETE
278 INSINT(RTLD_NODELETE);
Guido van Rossum34162a11994-05-23 12:37:57 +0000279#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000280}