• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #if defined(__NetBSD__)
11 #include <sys/param.h>
12 #if (NetBSD < 199712)
13 #include <nlist.h>
14 #include <link.h>
15 #define dlerror() "error in dynamic linking"
16 #endif
17 #endif /* NetBSD */
18 
19 #ifdef HAVE_DLFCN_H
20 #include <dlfcn.h>
21 #endif
22 
23 #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
24 #define LEAD_UNDERSCORE "_"
25 #else
26 #define LEAD_UNDERSCORE ""
27 #endif
28 
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 */
34 
35 const char *_PyImport_DynLoadFiletab[] = {
36 #ifdef __CYGWIN__
37     ".dll",
38 #else  /* !__CYGWIN__ */
39     "." SOABI ".so",
40     ".abi" PYTHON_ABI_STRING ".so",
41     ".so",
42 #endif  /* __CYGWIN__ */
43     NULL,
44 };
45 
46 static struct {
47     dev_t dev;
48     ino_t ino;
49     void *handle;
50 } handles[128];
51 static int nhandles = 0;
52 
53 
54 dl_funcptr
_PyImport_FindSharedFuncptr(const char * prefix,const char * shortname,const char * pathname,FILE * fp)55 _PyImport_FindSharedFuncptr(const char *prefix,
56                             const char *shortname,
57                             const char *pathname, FILE *fp)
58 {
59     dl_funcptr p;
60     void *handle;
61     char funcname[258];
62     char pathbuf[260];
63     int dlopenflags=0;
64 
65     if (strchr(pathname, '/') == NULL) {
66         /* Prefix bare filename with "./" */
67         PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
68         pathname = pathbuf;
69     }
70 
71     PyOS_snprintf(funcname, sizeof(funcname),
72                   LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname);
73 
74     if (fp != NULL) {
75         int i;
76         struct _Py_stat_struct status;
77         if (_Py_fstat(fileno(fp), &status) == -1)
78             return NULL;
79         for (i = 0; i < nhandles; i++) {
80             if (status.st_dev == handles[i].dev &&
81                 status.st_ino == handles[i].ino) {
82                 p = (dl_funcptr) dlsym(handles[i].handle,
83                                        funcname);
84                 return p;
85             }
86         }
87         if (nhandles < 128) {
88             handles[nhandles].dev = status.st_dev;
89             handles[nhandles].ino = status.st_ino;
90         }
91     }
92 
93     dlopenflags = PyThreadState_GET()->interp->dlopenflags;
94 
95     handle = dlopen(pathname, dlopenflags);
96 
97     if (handle == NULL) {
98         PyObject *mod_name;
99         PyObject *path;
100         PyObject *error_ob;
101         const char *error = dlerror();
102         if (error == NULL)
103             error = "unknown dlopen() error";
104         error_ob = PyUnicode_FromString(error);
105         if (error_ob == NULL)
106             return NULL;
107         mod_name = PyUnicode_FromString(shortname);
108         if (mod_name == NULL) {
109             Py_DECREF(error_ob);
110             return NULL;
111         }
112         path = PyUnicode_FromString(pathname);
113         if (path == NULL) {
114             Py_DECREF(error_ob);
115             Py_DECREF(mod_name);
116             return NULL;
117         }
118         PyErr_SetImportError(error_ob, mod_name, path);
119         Py_DECREF(error_ob);
120         Py_DECREF(mod_name);
121         Py_DECREF(path);
122         return NULL;
123     }
124     if (fp != NULL && nhandles < 128)
125         handles[nhandles++].handle = handle;
126     p = (dl_funcptr) dlsym(handle, funcname);
127     return p;
128 }
129