Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 1 | |
| 2 | /* dl module */ |
| 3 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 5 | |
| 6 | #include <dlfcn.h> |
| 7 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame^] | 8 | #ifdef __VMS |
| 9 | #include <unistd.h> |
| 10 | #endif |
| 11 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 12 | #ifndef RTLD_LAZY |
| 13 | #define RTLD_LAZY 1 |
| 14 | #endif |
| 15 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 16 | typedef void *PyUnivPtr; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 17 | typedef struct { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 18 | PyObject_HEAD |
| 19 | PyUnivPtr *dl_handle; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 20 | } dlobject; |
| 21 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 22 | static PyTypeObject Dltype; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 23 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 24 | static PyObject *Dlerror; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 25 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 26 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 27 | newdlobject(PyUnivPtr *handle) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 28 | { |
| 29 | dlobject *xp; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 30 | xp = PyObject_New(dlobject, &Dltype); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 31 | if (xp == NULL) |
| 32 | return NULL; |
| 33 | xp->dl_handle = handle; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 34 | return (PyObject *)xp; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static void |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 38 | dl_dealloc(dlobject *xp) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 39 | { |
| 40 | if (xp->dl_handle != NULL) |
| 41 | dlclose(xp->dl_handle); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 42 | PyObject_Del(xp); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 45 | static PyObject * |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 46 | dl_close(dlobject *xp) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 47 | { |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 48 | if (xp->dl_handle != NULL) { |
| 49 | dlclose(xp->dl_handle); |
| 50 | xp->dl_handle = NULL; |
| 51 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 52 | Py_INCREF(Py_None); |
| 53 | return Py_None; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 56 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 57 | dl_sym(dlobject *xp, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 58 | { |
| 59 | char *name; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 60 | PyUnivPtr *func; |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 61 | 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 Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 66 | return NULL; |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 67 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 68 | func = dlsym(xp->dl_handle, name); |
| 69 | if (func == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 70 | Py_INCREF(Py_None); |
| 71 | return Py_None; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 72 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 73 | return PyInt_FromLong((long)func); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 76 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 77 | dl_call(dlobject *xp, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 78 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 79 | PyObject *name; |
Fred Drake | 4747a18 | 2000-10-11 21:44:02 +0000 | [diff] [blame] | 80 | long (*func)(long, long, long, long, long, |
| 81 | long, long, long, long, long); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 82 | long alist[10]; |
| 83 | long res; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame^] | 84 | Py_ssize_t i; |
| 85 | Py_ssize_t n = PyTuple_Size(args); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 86 | if (n < 1) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 87 | PyErr_SetString(PyExc_TypeError, "at least a name is needed"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 90 | name = PyTuple_GetItem(args, 0); |
| 91 | if (!PyString_Check(name)) { |
| 92 | PyErr_SetString(PyExc_TypeError, |
| 93 | "function name must be a string"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
Andrew M. Kuchling | a1a690f | 2001-02-22 15:52:55 +0000 | [diff] [blame] | 96 | func = (long (*)(long, long, long, long, long, |
| 97 | long, long, long, long, long)) |
| 98 | dlsym(xp->dl_handle, PyString_AsString(name)); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 99 | if (func == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 100 | PyErr_SetString(PyExc_ValueError, dlerror()); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | if (n-1 > 10) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 104 | PyErr_SetString(PyExc_TypeError, |
| 105 | "too many arguments (max 10)"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
| 108 | for (i = 1; i < n; i++) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 109 | PyObject *v = PyTuple_GetItem(args, i); |
| 110 | if (PyInt_Check(v)) |
| 111 | alist[i-1] = PyInt_AsLong(v); |
| 112 | else if (PyString_Check(v)) |
| 113 | alist[i-1] = (long)PyString_AsString(v); |
| 114 | else if (v == Py_None) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 115 | alist[i-1] = (long) ((char *)NULL); |
| 116 | else { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 117 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 118 | "arguments must be int, string or None"); |
| 119 | return NULL; |
| 120 | } |
| 121 | } |
| 122 | for (; i <= 10; i++) |
| 123 | alist[i-1] = 0; |
| 124 | res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4], |
| 125 | alist[5], alist[6], alist[7], alist[8], alist[9]); |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 126 | return PyInt_FromLong(res); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 129 | static PyMethodDef dlobject_methods[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 130 | {"call", (PyCFunction)dl_call, METH_VARARGS}, |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 131 | {"sym", (PyCFunction)dl_sym, METH_O}, |
| 132 | {"close", (PyCFunction)dl_close, METH_NOARGS}, |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 133 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 136 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 137 | dl_getattr(dlobject *xp, char *name) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 138 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 139 | return Py_FindMethod(dlobject_methods, (PyObject *)xp, name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 143 | static PyTypeObject Dltype = { |
Andrew M. Kuchling | 6efc6e7 | 2001-02-27 20:54:23 +0000 | [diff] [blame] | 144 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 145 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 146 | "dl.dl", /*tp_name*/ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 147 | sizeof(dlobject), /*tp_basicsize*/ |
| 148 | 0, /*tp_itemsize*/ |
| 149 | /* methods */ |
| 150 | (destructor)dl_dealloc, /*tp_dealloc*/ |
| 151 | 0, /*tp_print*/ |
| 152 | (getattrfunc)dl_getattr,/*tp_getattr*/ |
| 153 | 0, /*tp_setattr*/ |
| 154 | 0, /*tp_compare*/ |
| 155 | 0, /*tp_repr*/ |
| 156 | 0, /*tp_as_number*/ |
| 157 | 0, /*tp_as_sequence*/ |
| 158 | 0, /*tp_as_mapping*/ |
| 159 | 0, /*tp_hash*/ |
| 160 | }; |
| 161 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 162 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 163 | dl_open(PyObject *self, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 164 | { |
| 165 | char *name; |
| 166 | int mode; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 167 | PyUnivPtr *handle; |
Martin v. Löwis | 9322727 | 2002-01-01 20:18:30 +0000 | [diff] [blame] | 168 | if (sizeof(int) != sizeof(long) || |
| 169 | sizeof(long) != sizeof(char *)) { |
| 170 | PyErr_SetString(PyExc_SystemError, |
| 171 | "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)"); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 175 | if (PyArg_ParseTuple(args, "z:open", &name)) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 176 | mode = RTLD_LAZY; |
| 177 | else { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 178 | PyErr_Clear(); |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 179 | if (!PyArg_ParseTuple(args, "zi:open", &name, &mode)) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | #ifndef RTLD_NOW |
| 182 | if (mode != RTLD_LAZY) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 183 | PyErr_SetString(PyExc_ValueError, "mode must be 1"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
| 186 | #endif |
| 187 | } |
| 188 | handle = dlopen(name, mode); |
| 189 | if (handle == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 190 | PyErr_SetString(Dlerror, dlerror()); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame^] | 193 | #ifdef __VMS |
| 194 | /* Under OpenVMS dlopen doesn't do any check, just save the name |
| 195 | * for later use, so we have to check if the file is readable, |
| 196 | * the name can be a logical or a file from SYS$SHARE. |
| 197 | */ |
| 198 | if (access(name, R_OK)) { |
| 199 | char fname[strlen(name) + 20]; |
| 200 | strcpy(fname, "SYS$SHARE:"); |
| 201 | strcat(fname, name); |
| 202 | strcat(fname, ".EXE"); |
| 203 | if (access(fname, R_OK)) { |
| 204 | dlclose(handle); |
| 205 | PyErr_SetString(Dlerror, |
| 206 | "File not found or protection violation"); |
| 207 | return NULL; |
| 208 | } |
| 209 | } |
| 210 | #endif |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 211 | return newdlobject(handle); |
| 212 | } |
| 213 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 214 | static PyMethodDef dl_methods[] = { |
Neal Norwitz | b82d34f | 2002-03-31 15:43:28 +0000 | [diff] [blame] | 215 | {"open", dl_open, METH_VARARGS}, |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 216 | {NULL, NULL} /* sentinel */ |
| 217 | }; |
| 218 | |
Martin v. Löwis | df23f33 | 2000-09-13 16:26:10 +0000 | [diff] [blame] | 219 | /* From socketmodule.c |
| 220 | * Convenience routine to export an integer value. |
| 221 | * |
| 222 | * Errors are silently ignored, for better or for worse... |
| 223 | */ |
| 224 | static void |
| 225 | insint(PyObject *d, char *name, int value) |
| 226 | { |
| 227 | PyObject *v = PyInt_FromLong((long) value); |
| 228 | if (!v || PyDict_SetItemString(d, name, v)) |
| 229 | PyErr_Clear(); |
| 230 | |
| 231 | Py_XDECREF(v); |
| 232 | } |
| 233 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 234 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 235 | initdl(void) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 236 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 237 | PyObject *m, *d, *x; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 238 | |
Andrew M. Kuchling | 6efc6e7 | 2001-02-27 20:54:23 +0000 | [diff] [blame] | 239 | /* Initialize object type */ |
| 240 | Dltype.ob_type = &PyType_Type; |
| 241 | |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 242 | /* Create the module and add the functions */ |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 243 | m = Py_InitModule("dl", dl_methods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 244 | if (m == NULL) |
| 245 | return; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 246 | |
| 247 | /* Add some symbolic constants to the module */ |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 248 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 249 | Dlerror = x = PyErr_NewException("dl.error", NULL, NULL); |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 250 | PyDict_SetItemString(d, "error", x); |
| 251 | x = PyInt_FromLong((long)RTLD_LAZY); |
| 252 | PyDict_SetItemString(d, "RTLD_LAZY", x); |
Martin v. Löwis | df23f33 | 2000-09-13 16:26:10 +0000 | [diff] [blame] | 253 | #define INSINT(X) insint(d,#X,X) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 254 | #ifdef RTLD_NOW |
Martin v. Löwis | df23f33 | 2000-09-13 16:26:10 +0000 | [diff] [blame] | 255 | INSINT(RTLD_NOW); |
| 256 | #endif |
| 257 | #ifdef RTLD_NOLOAD |
| 258 | INSINT(RTLD_NOLOAD); |
| 259 | #endif |
| 260 | #ifdef RTLD_GLOBAL |
| 261 | INSINT(RTLD_GLOBAL); |
| 262 | #endif |
| 263 | #ifdef RTLD_LOCAL |
| 264 | INSINT(RTLD_LOCAL); |
| 265 | #endif |
| 266 | #ifdef RTLD_PARENT |
| 267 | INSINT(RTLD_PARENT); |
| 268 | #endif |
| 269 | #ifdef RTLD_GROUP |
| 270 | INSINT(RTLD_GROUP); |
| 271 | #endif |
| 272 | #ifdef RTLD_WORLD |
| 273 | INSINT(RTLD_WORLD); |
| 274 | #endif |
| 275 | #ifdef RTLD_NODELETE |
| 276 | INSINT(RTLD_NODELETE); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 277 | #endif |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 278 | } |