Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 7 | Copyright (c) 2000, BeOpen.com. |
| 8 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 9 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 10 | All rights reserved. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 12 | See the file "Misc/COPYRIGHT" for information on usage and |
| 13 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 14 | |
| 15 | ******************************************************************/ |
| 16 | |
| 17 | /* Support for dynamic loading of extension modules */ |
| 18 | |
| 19 | #include "Python.h" |
| 20 | #include "importdl.h" |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #if defined(__NetBSD__) && (NetBSD < 199712) |
| 25 | #include <nlist.h> |
| 26 | #include <link.h> |
| 27 | #define dlerror() "error in dynamic linking" |
| 28 | #else |
| 29 | #ifdef HAVE_DLFCN_H |
| 30 | #include <dlfcn.h> |
| 31 | #endif |
| 32 | #endif |
| 33 | |
| 34 | #ifndef RTLD_LAZY |
| 35 | #define RTLD_LAZY 1 |
| 36 | #endif |
| 37 | |
| 38 | |
| 39 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 40 | {".so", "rb", C_EXTENSION}, |
| 41 | {"module.so", "rb", C_EXTENSION}, |
| 42 | {0, 0} |
| 43 | }; |
| 44 | |
| 45 | static struct { |
| 46 | dev_t dev; |
| 47 | ino_t ino; |
| 48 | void *handle; |
| 49 | } handles[128]; |
| 50 | static int nhandles = 0; |
| 51 | |
| 52 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 53 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 54 | const char *pathname, FILE *fp) |
| 55 | { |
| 56 | dl_funcptr p; |
| 57 | void *handle; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 58 | char funcname[258]; |
| 59 | char pathbuf[260]; |
| 60 | |
| 61 | if (strchr(pathname, '/') == NULL) { |
| 62 | /* Prefix bare filename with "./" */ |
| 63 | sprintf(pathbuf, "./%-.255s", pathname); |
| 64 | pathname = pathbuf; |
| 65 | } |
| 66 | |
| 67 | /* ### should there be a leading underscore for some platforms? */ |
| 68 | sprintf(funcname, "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 69 | |
| 70 | if (fp != NULL) { |
| 71 | int i; |
| 72 | struct stat statb; |
| 73 | fstat(fileno(fp), &statb); |
| 74 | for (i = 0; i < nhandles; i++) { |
| 75 | if (statb.st_dev == handles[i].dev && |
| 76 | statb.st_ino == handles[i].ino) { |
| 77 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 78 | funcname); |
| 79 | return p; |
| 80 | } |
| 81 | } |
| 82 | if (nhandles < 128) { |
| 83 | handles[nhandles].dev = statb.st_dev; |
| 84 | handles[nhandles].ino = statb.st_ino; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #ifdef RTLD_NOW |
| 89 | /* RTLD_NOW: resolve externals now |
| 90 | (i.e. core dump now if some are missing) */ |
| 91 | handle = dlopen(pathname, RTLD_NOW); |
| 92 | #else |
| 93 | if (Py_VerboseFlag) |
| 94 | printf("dlopen(\"%s\", %d);\n", pathname, |
| 95 | RTLD_LAZY); |
| 96 | handle = dlopen(pathname, RTLD_LAZY); |
| 97 | #endif /* RTLD_NOW */ |
| 98 | if (handle == NULL) { |
| 99 | PyErr_SetString(PyExc_ImportError, dlerror()); |
| 100 | return NULL; |
| 101 | } |
| 102 | if (fp != NULL && nhandles < 128) |
| 103 | handles[nhandles++].handle = handle; |
| 104 | p = (dl_funcptr) dlsym(handle, funcname); |
| 105 | return p; |
| 106 | } |