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> |
Martin v. Löwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 10 | #if defined(__NetBSD__) |
| 11 | #include <sys/param.h> |
| 12 | #if (NetBSD < 199712) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 13 | #include <nlist.h> |
| 14 | #include <link.h> |
| 15 | #define dlerror() "error in dynamic linking" |
Martin v. Löwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 16 | #endif |
| 17 | #endif /* NetBSD */ |
| 18 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 19 | #ifdef HAVE_DLFCN_H |
| 20 | #include <dlfcn.h> |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 21 | #else |
| 22 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 23 | #include "dlfcn.h" |
| 24 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 25 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 26 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 27 | #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 28 | #define LEAD_UNDERSCORE "_" |
| 29 | #else |
| 30 | #define LEAD_UNDERSCORE "" |
| 31 | #endif |
| 32 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 33 | |
| 34 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 35 | #ifdef __CYGWIN__ |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 36 | {".dll", "rb", C_EXTENSION}, |
Guido van Rossum | fef1243 | 2001-01-10 21:17:27 +0000 | [diff] [blame] | 37 | {"module.dll", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 38 | #else |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 39 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 40 | {".pyd", "rb", C_EXTENSION}, |
| 41 | {".dll", "rb", C_EXTENSION}, |
| 42 | #else |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 43 | #ifdef __VMS |
| 44 | {".exe", "rb", C_EXTENSION}, |
| 45 | {".EXE", "rb", C_EXTENSION}, |
| 46 | {"module.exe", "rb", C_EXTENSION}, |
| 47 | {"MODULE.EXE", "rb", C_EXTENSION}, |
| 48 | #else |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 49 | {".so", "rb", C_EXTENSION}, |
| 50 | {"module.so", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 51 | #endif |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 52 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 53 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 54 | {0, 0} |
| 55 | }; |
| 56 | |
| 57 | static struct { |
| 58 | dev_t dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 59 | #ifdef __VMS |
| 60 | ino_t ino[3]; |
| 61 | #else |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 62 | ino_t ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 63 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 64 | void *handle; |
| 65 | } handles[128]; |
| 66 | static int nhandles = 0; |
| 67 | |
| 68 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 69 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 70 | const char *pathname, FILE *fp) |
| 71 | { |
| 72 | dl_funcptr p; |
| 73 | void *handle; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 74 | char funcname[258]; |
| 75 | char pathbuf[260]; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 76 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 77 | |
| 78 | if (strchr(pathname, '/') == NULL) { |
| 79 | /* Prefix bare filename with "./" */ |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 80 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 81 | pathname = pathbuf; |
| 82 | } |
| 83 | |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 84 | PyOS_snprintf(funcname, sizeof(funcname), |
| 85 | LEAD_UNDERSCORE "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 86 | |
| 87 | if (fp != NULL) { |
| 88 | int i; |
| 89 | struct stat statb; |
| 90 | fstat(fileno(fp), &statb); |
| 91 | for (i = 0; i < nhandles; i++) { |
| 92 | if (statb.st_dev == handles[i].dev && |
| 93 | statb.st_ino == handles[i].ino) { |
| 94 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 95 | funcname); |
| 96 | return p; |
| 97 | } |
| 98 | } |
| 99 | if (nhandles < 128) { |
| 100 | handles[nhandles].dev = statb.st_dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 101 | #ifdef __VMS |
| 102 | handles[nhandles].ino[0] = statb.st_ino[0]; |
| 103 | handles[nhandles].ino[1] = statb.st_ino[1]; |
| 104 | handles[nhandles].ino[2] = statb.st_ino[2]; |
| 105 | #else |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 106 | handles[nhandles].ino = statb.st_ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 111 | #if !(defined(PYOS_OS2) && defined(PYCC_GCC)) |
Nicholas Bastin | 2786d90 | 2004-03-25 02:16:23 +0000 | [diff] [blame] | 112 | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 113 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 115 | if (Py_VerboseFlag) |
Martin v. Löwis | 507a485 | 2004-10-03 08:51:35 +0000 | [diff] [blame] | 116 | PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname, |
| 117 | dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 118 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 119 | #ifdef __VMS |
| 120 | /* VMS currently don't allow a pathname, use a logical name instead */ |
| 121 | /* Concatenate 'python_module_' and shortname */ |
| 122 | /* so "import vms.bar" will use the logical python_module_bar */ |
| 123 | /* As C module use only one name space this is probably not a */ |
| 124 | /* important limitation */ |
| 125 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s", |
| 126 | shortname); |
| 127 | pathname = pathbuf; |
| 128 | #endif |
| 129 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 130 | handle = dlopen(pathname, dlopenflags); |
| 131 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 132 | if (handle == NULL) { |
Hye-Shik Chang | 23f391c | 2006-01-04 01:30:17 +0000 | [diff] [blame] | 133 | const char *error = dlerror(); |
Neal Norwitz | 67715f0 | 2005-11-09 06:59:35 +0000 | [diff] [blame] | 134 | if (error == NULL) |
| 135 | error = "unknown dlopen() error"; |
| 136 | PyErr_SetString(PyExc_ImportError, error); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | if (fp != NULL && nhandles < 128) |
| 140 | handles[nhandles++].handle = handle; |
| 141 | p = (dl_funcptr) dlsym(handle, funcname); |
| 142 | return p; |
| 143 | } |