blob: 3f150487007c4bf05501658c2bf7707a16e754e7 [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
Neal Norwitz2a30cd02006-07-10 01:18:57 +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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000029 dlobject *xp;
30 xp = PyObject_New(dlobject, &Dltype);
31 if (xp == NULL)
32 return NULL;
33 xp->dl_handle = handle;
34 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000040 if (xp->dl_handle != NULL)
41 dlclose(xp->dl_handle);
42 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000048 if (xp->dl_handle != NULL) {
49 dlclose(xp->dl_handle);
50 xp->dl_handle = NULL;
51 }
52 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000059 char *name;
60 PyUnivPtr *func;
61 if (PyString_Check(args)) {
62 name = PyString_AS_STRING(args);
63 } else {
64 PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
65 Py_TYPE(args)->tp_name);
66 return NULL;
67 }
68 func = dlsym(xp->dl_handle, name);
69 if (func == NULL) {
70 Py_INCREF(Py_None);
71 return Py_None;
72 }
73 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079 PyObject *name;
80 long (*func)(long, long, long, long, long,
81 long, long, long, long, long);
82 long alist[10];
83 long res;
84 Py_ssize_t i;
85 Py_ssize_t n = PyTuple_Size(args);
86 if (n < 1) {
87 PyErr_SetString(PyExc_TypeError, "at least a name is needed");
88 return NULL;
89 }
90 name = PyTuple_GetItem(args, 0);
91 if (!PyString_Check(name)) {
92 PyErr_SetString(PyExc_TypeError,
93 "function name must be a string");
94 return NULL;
95 }
96 func = (long (*)(long, long, long, long, long,
97 long, long, long, long, long))
98 dlsym(xp->dl_handle, PyString_AsString(name));
99 if (func == NULL) {
100 PyErr_SetString(PyExc_ValueError, dlerror());
101 return NULL;
102 }
103 if (n-1 > 10) {
104 PyErr_SetString(PyExc_TypeError,
105 "too many arguments (max 10)");
106 return NULL;
107 }
108 for (i = 1; i < n; i++) {
109 PyObject *v = PyTuple_GetItem(args, i);
Serhiy Storchaka48c8bf22018-07-31 09:09:36 +0300110 if (_PyAnyInt_Check(v)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 alist[i-1] = PyInt_AsLong(v);
Serhiy Storchaka994f04d2016-12-27 15:09:36 +0200112 if (alist[i-1] == -1 && PyErr_Occurred())
113 return NULL;
114 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000115 else if (PyString_Check(v))
116 alist[i-1] = (long)PyString_AsString(v);
117 else if (v == Py_None)
118 alist[i-1] = (long) ((char *)NULL);
119 else {
120 PyErr_SetString(PyExc_TypeError,
121 "arguments must be int, string or None");
122 return NULL;
123 }
124 }
125 for (; i <= 10; i++)
126 alist[i-1] = 0;
127 res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
128 alist[5], alist[6], alist[7], alist[8], alist[9]);
129 return PyInt_FromLong(res);
Guido van Rossum34162a11994-05-23 12:37:57 +0000130}
131
Roger E. Masse0e120321996-12-13 20:33:44 +0000132static PyMethodDef dlobject_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 {"call", (PyCFunction)dl_call, METH_VARARGS},
134 {"sym", (PyCFunction)dl_sym, METH_O},
135 {"close", (PyCFunction)dl_close, METH_NOARGS},
136 {NULL, NULL} /* Sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000137};
138
Roger E. Masse0e120321996-12-13 20:33:44 +0000139static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000140dl_getattr(dlobject *xp, char *name)
Guido van Rossum34162a11994-05-23 12:37:57 +0000141{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000142 return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
Guido van Rossum34162a11994-05-23 12:37:57 +0000143}
144
145
Roger E. Masse0e120321996-12-13 20:33:44 +0000146static PyTypeObject Dltype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 PyVarObject_HEAD_INIT(NULL, 0)
148 "dl.dl", /*tp_name*/
149 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*/
Guido van Rossum34162a11994-05-23 12:37:57 +0000162};
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 char *name;
168 int mode;
169 PyUnivPtr *handle;
170 if (sizeof(int) != sizeof(long) ||
171 sizeof(long) != sizeof(char *)) {
172 PyErr_SetString(PyExc_SystemError,
Martin v. Löwis93227272002-01-01 20:18:30 +0000173 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 return NULL;
175 }
Martin v. Löwis93227272002-01-01 20:18:30 +0000176
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 if (PyArg_ParseTuple(args, "z:open", &name))
178 mode = RTLD_LAZY;
179 else {
180 PyErr_Clear();
181 if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
182 return NULL;
Guido van Rossum34162a11994-05-23 12:37:57 +0000183#ifndef RTLD_NOW
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 if (mode != RTLD_LAZY) {
185 PyErr_SetString(PyExc_ValueError, "mode must be 1");
186 return NULL;
187 }
Guido van Rossum34162a11994-05-23 12:37:57 +0000188#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 }
190 handle = dlopen(name, mode);
191 if (handle == NULL) {
192 char *errmsg = dlerror();
193 if (!errmsg)
194 errmsg = "dlopen() error";
195 PyErr_SetString(Dlerror, errmsg);
196 return NULL;
197 }
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000198#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 /* Under OpenVMS dlopen doesn't do any check, just save the name
200 * for later use, so we have to check if the file is readable,
201 * the name can be a logical or a file from SYS$SHARE.
202 */
203 if (access(name, R_OK)) {
204 char fname[strlen(name) + 20];
205 strcpy(fname, "SYS$SHARE:");
206 strcat(fname, name);
207 strcat(fname, ".EXE");
208 if (access(fname, R_OK)) {
209 dlclose(handle);
210 PyErr_SetString(Dlerror,
211 "File not found or protection violation");
212 return NULL;
213 }
214 }
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000215#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 return newdlobject(handle);
Guido van Rossum34162a11994-05-23 12:37:57 +0000217}
218
Roger E. Masse0e120321996-12-13 20:33:44 +0000219static PyMethodDef dl_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 {"open", dl_open, METH_VARARGS},
221 {NULL, NULL} /* sentinel */
Guido van Rossum34162a11994-05-23 12:37:57 +0000222};
223
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000224/* From socketmodule.c
225 * Convenience routine to export an integer value.
226 *
227 * Errors are silently ignored, for better or for worse...
228 */
229static void
230insint(PyObject *d, char *name, int value)
231{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 PyObject *v = PyInt_FromLong((long) value);
233 if (!v || PyDict_SetItemString(d, name, v))
234 PyErr_Clear();
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000235
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 Py_XDECREF(v);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000237}
238
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000239PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000240initdl(void)
Guido van Rossum34162a11994-05-23 12:37:57 +0000241{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 PyObject *m, *d, *x;
Guido van Rossum34162a11994-05-23 12:37:57 +0000243
Brett Cannon7f874fc2008-05-10 21:20:19 +0000244 if (PyErr_WarnPy3k("the dl module has been removed in "
Brett Cannon7595c1a2008-05-11 01:09:32 +0000245 "Python 3.0; use the ctypes module instead", 2) < 0)
Martin Panterca56dd42016-09-17 07:54:55 +0000246 return;
Brett Cannon7f874fc2008-05-10 21:20:19 +0000247
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000248 /* Initialize object type */
249 Py_TYPE(&Dltype) = &PyType_Type;
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +0000250
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 /* Create the module and add the functions */
252 m = Py_InitModule("dl", dl_methods);
253 if (m == NULL)
254 return;
Guido van Rossum34162a11994-05-23 12:37:57 +0000255
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000256 /* Add some symbolic constants to the module */
257 d = PyModule_GetDict(m);
258 Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
259 PyDict_SetItemString(d, "error", x);
260 x = PyInt_FromLong((long)RTLD_LAZY);
261 PyDict_SetItemString(d, "RTLD_LAZY", x);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000262#define INSINT(X) insint(d,#X,X)
Guido van Rossum34162a11994-05-23 12:37:57 +0000263#ifdef RTLD_NOW
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000264 INSINT(RTLD_NOW);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000265#endif
266#ifdef RTLD_NOLOAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 INSINT(RTLD_NOLOAD);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000268#endif
269#ifdef RTLD_GLOBAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000270 INSINT(RTLD_GLOBAL);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000271#endif
272#ifdef RTLD_LOCAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 INSINT(RTLD_LOCAL);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000274#endif
275#ifdef RTLD_PARENT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 INSINT(RTLD_PARENT);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000277#endif
278#ifdef RTLD_GROUP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 INSINT(RTLD_GROUP);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000280#endif
281#ifdef RTLD_WORLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 INSINT(RTLD_WORLD);
Martin v. Löwisdf23f332000-09-13 16:26:10 +0000283#endif
284#ifdef RTLD_NODELETE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000285 INSINT(RTLD_NODELETE);
Guido van Rossum34162a11994-05-23 12:37:57 +0000286#endif
Guido van Rossum34162a11994-05-23 12:37:57 +0000287}