Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | |
| 2 | /* Support for dynamic loading of extension modules */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | #include "importdl.h" |
| 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #if defined(__NetBSD__) && (NetBSD < 199712) |
| 10 | #include <nlist.h> |
| 11 | #include <link.h> |
| 12 | #define dlerror() "error in dynamic linking" |
| 13 | #else |
| 14 | #ifdef HAVE_DLFCN_H |
| 15 | #include <dlfcn.h> |
| 16 | #endif |
| 17 | #endif |
| 18 | |
Martin v. Löwis | 36546db | 2001-09-05 14:24:43 +0000 | [diff] [blame] | 19 | #if defined(__OpenBSD__) && !defined(__ELF__) |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 20 | #define LEAD_UNDERSCORE "_" |
| 21 | #else |
| 22 | #define LEAD_UNDERSCORE "" |
| 23 | #endif |
| 24 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 25 | |
| 26 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 27 | #ifdef __CYGWIN__ |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 28 | {".dll", "rb", C_EXTENSION}, |
Guido van Rossum | fef1243 | 2001-01-10 21:17:27 +0000 | [diff] [blame] | 29 | {"module.dll", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 30 | #else |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 31 | {".so", "rb", C_EXTENSION}, |
| 32 | {"module.so", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 33 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 34 | {0, 0} |
| 35 | }; |
| 36 | |
| 37 | static struct { |
| 38 | dev_t dev; |
| 39 | ino_t ino; |
| 40 | void *handle; |
| 41 | } handles[128]; |
| 42 | static int nhandles = 0; |
| 43 | |
| 44 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 45 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 46 | const char *pathname, FILE *fp) |
| 47 | { |
| 48 | dl_funcptr p; |
| 49 | void *handle; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 50 | char funcname[258]; |
| 51 | char pathbuf[260]; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 52 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 53 | |
| 54 | if (strchr(pathname, '/') == NULL) { |
| 55 | /* Prefix bare filename with "./" */ |
| 56 | sprintf(pathbuf, "./%-.255s", pathname); |
| 57 | pathname = pathbuf; |
| 58 | } |
| 59 | |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 60 | sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 61 | |
| 62 | if (fp != NULL) { |
| 63 | int i; |
| 64 | struct stat statb; |
| 65 | fstat(fileno(fp), &statb); |
| 66 | for (i = 0; i < nhandles; i++) { |
| 67 | if (statb.st_dev == handles[i].dev && |
| 68 | statb.st_ino == handles[i].ino) { |
| 69 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 70 | funcname); |
| 71 | return p; |
| 72 | } |
| 73 | } |
| 74 | if (nhandles < 128) { |
| 75 | handles[nhandles].dev = statb.st_dev; |
| 76 | handles[nhandles].ino = statb.st_ino; |
| 77 | } |
| 78 | } |
| 79 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 80 | dlopenflags = PyThreadState_Get()->interp->dlopenflags; |
| 81 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 82 | if (Py_VerboseFlag) |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 83 | printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags); |
| 84 | |
| 85 | handle = dlopen(pathname, dlopenflags); |
| 86 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 87 | if (handle == NULL) { |
| 88 | PyErr_SetString(PyExc_ImportError, dlerror()); |
| 89 | return NULL; |
| 90 | } |
| 91 | if (fp != NULL && nhandles < 128) |
| 92 | handles[nhandles++].handle = handle; |
| 93 | p = (dl_funcptr) dlsym(handle, funcname); |
| 94 | return p; |
| 95 | } |