-
Notifications
You must be signed in to change notification settings - Fork 2
/
dlhack.c
50 lines (45 loc) · 1.25 KB
/
dlhack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Python.h>
#include <dlfcn.h>
/* This is a fake perl module that will look for the real thing ('perl2.so')
* in sys.path and then load this one with the RTLD_GLOBAL set in order to
* make the symbols available for extension modules that perl might load.
*/
extern void initperl()
{
void* handle;
int i, npath, len;
char buf[1024];
PyObject *path = PySys_GetObject("path");
if (path == NULL || !PyList_Check(path)) {
PyErr_SetString(PyExc_ImportError,
"sys.path must be a list of directory names");
return;
}
npath = PyList_Size(path);
for (i = 0; i < npath; i++) {
PyObject *v = PyList_GetItem(path, i);
if (!PyString_Check(v))
continue;
len = PyString_Size(v);
if (len + 10 >= sizeof(buf))
continue; /* Too long */
strcpy(buf, PyString_AsString(v));
if (buf[0] != '/')
continue; /* Not absolute */
if (strlen(buf) != len)
continue; /* v contains '\0' */
strcpy(buf+len, "/perl2.so");
handle = dlopen(buf, RTLD_NOW | RTLD_GLOBAL);
if (handle) {
void (*f)() = dlsym(handle, "initperl2");
if (f) {
f();
}
else {
PyErr_SetString(PyExc_ImportError, "initperl2 entry point not found");
}
return;
}
}
PyErr_SetString(PyExc_ImportError, "perl2.so not found");
}