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> |
| 21 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 22 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 23 | #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 24 | #define LEAD_UNDERSCORE "_" |
| 25 | #else |
| 26 | #define LEAD_UNDERSCORE "" |
| 27 | #endif |
| 28 | |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 29 | /* The .so extension module ABI tag, supplied by the Makefile via |
| 30 | Makefile.pre.in and configure. This is used to discriminate between |
| 31 | incompatible .so files so that extensions for different Python builds can |
| 32 | live in the same directory. E.g. foomodule.cpython-32.so |
| 33 | */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 34 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 35 | const char *_PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 36 | #ifdef __CYGWIN__ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 37 | ".dll", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 38 | #else /* !__CYGWIN__ */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 39 | "." SOABI ".so", |
| 40 | ".abi" PYTHON_ABI_STRING ".so", |
| 41 | ".so", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 42 | #endif /* __CYGWIN__ */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 43 | NULL, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | dev_t dev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | ino_t ino; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | void *handle; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 50 | } handles[128]; |
| 51 | static int nhandles = 0; |
| 52 | |
| 53 | |
Victor Stinner | 42040fb | 2011-02-22 23:16:19 +0000 | [diff] [blame] | 54 | dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 56 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | dl_funcptr p; |
| 58 | void *handle; |
| 59 | char funcname[258]; |
| 60 | char pathbuf[260]; |
| 61 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | if (strchr(pathname, '/') == NULL) { |
| 64 | /* Prefix bare filename with "./" */ |
| 65 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
| 66 | pathname = pathbuf; |
| 67 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | PyOS_snprintf(funcname, sizeof(funcname), |
| 70 | LEAD_UNDERSCORE "PyInit_%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (fp != NULL) { |
| 73 | int i; |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame^] | 74 | struct _Py_stat_struct status; |
| 75 | if (_Py_fstat(fileno(fp), &status) == -1) |
Benjamin Peterson | 7d28b6b | 2013-07-22 22:08:09 -0700 | [diff] [blame] | 76 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | for (i = 0; i < nhandles; i++) { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame^] | 78 | if (status.st_dev == handles[i].dev && |
| 79 | status.st_ino == handles[i].ino) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 81 | funcname); |
| 82 | return p; |
| 83 | } |
| 84 | } |
| 85 | if (nhandles < 128) { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame^] | 86 | handles[nhandles].dev = status.st_dev; |
| 87 | handles[nhandles].ino = status.st_ino; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | handle = dlopen(pathname, dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | if (handle == NULL) { |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 96 | PyObject *mod_name; |
| 97 | PyObject *path; |
| 98 | PyObject *error_ob; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | const char *error = dlerror(); |
| 100 | if (error == NULL) |
| 101 | error = "unknown dlopen() error"; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 102 | error_ob = PyUnicode_FromString(error); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 103 | if (error_ob == NULL) |
| 104 | return NULL; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 105 | mod_name = PyUnicode_FromString(shortname); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 106 | if (mod_name == NULL) { |
| 107 | Py_DECREF(error_ob); |
| 108 | return NULL; |
| 109 | } |
| 110 | path = PyUnicode_FromString(pathname); |
| 111 | if (path == NULL) { |
| 112 | Py_DECREF(error_ob); |
| 113 | Py_DECREF(mod_name); |
| 114 | return NULL; |
| 115 | } |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 116 | PyErr_SetImportError(error_ob, mod_name, path); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 117 | Py_DECREF(error_ob); |
| 118 | Py_DECREF(mod_name); |
| 119 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | if (fp != NULL && nhandles < 128) |
| 123 | handles[nhandles++].handle = handle; |
| 124 | p = (dl_funcptr) dlsym(handle, funcname); |
| 125 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 126 | } |