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