summaryrefslogtreecommitdiffstats
path: root/Modules/dlmodule.c
blob: 1f1ce462b1130d1f94033b05168cc627b76ee9b1 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* dl module */

#include "allobjects.h"
#include "modsupport.h"

#include <dlfcn.h>

#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif

typedef struct {
	OB_HEAD
	ANY *dl_handle;
} dlobject;

staticforward typeobject Dltype;

static object *Dlerror;

static object *
newdlobject(handle)
	ANY *handle;
{
	dlobject *xp;
	xp = NEWOBJ(dlobject, &Dltype);
	if (xp == NULL)
		return NULL;
	xp->dl_handle = handle;
	return (object *)xp;
}

static void
dl_dealloc(xp)
	dlobject *xp;
{
	if (xp->dl_handle != NULL)
		dlclose(xp->dl_handle);
	DEL(xp);
}

static object *
dl_close(xp, args)
	dlobject *xp;
	object *args;
{
	if (!getargs(args, ""))
		return NULL;
	if (xp->dl_handle != NULL) {
		dlclose(xp->dl_handle);
		xp->dl_handle = NULL;
	}
	INCREF(None);
	return None;
}

static object *
dl_sym(xp, args)
	dlobject *xp;
	object *args;
{
	char *name;
	ANY *func;
	if (!getargs(args, "s", &name))
		return NULL;
	func = dlsym(xp->dl_handle, name);
	if (func == NULL) {
		INCREF(None);
		return None;
	}
	return newintobject((long)func);
}

static object *
dl_call(xp, args)
	dlobject *xp;
	object *args; /* (varargs) */
{
	object *name;
	long (*func)();
	long alist[10];
	long res;
	int i;
	int n = gettuplesize(args);
	if (n < 1) {
		err_setstr(TypeError, "at least a name is needed");
		return NULL;
	}
	name = gettupleitem(args, 0);
	if (!is_stringobject(name)) {
		err_setstr(TypeError, "function name must be a string");
		return NULL;
	}
	func = dlsym(xp->dl_handle, getstringvalue(name));
	if (func == NULL) {
		err_setstr(ValueError, dlerror());
		return NULL;
	}
	if (n-1 > 10) {
		err_setstr(TypeError, "too many arguments (max 10)");
		return NULL;
	}
	for (i = 1; i < n; i++) {
		object *v = gettupleitem(args, i);
		if (is_intobject(v))
			alist[i-1] = getintvalue(v);
		else if (is_stringobject(v))
			alist[i-1] = (long)getstringvalue(v);
		else if (v == None)
			alist[i-1] = (long) ((char *)NULL);
		else {
			err_setstr(TypeError,
				   "arguments must be int, string or None");
			return NULL;
		}
	}
	for (; i <= 10; i++)
		alist[i-1] = 0;
	res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
		      alist[5], alist[6], alist[7], alist[8], alist[9]);
	return newintobject(res);
}

static struct methodlist dlobject_methods[] = {
	{"call",	(method)dl_call,	1 /* varargs */},
	{"sym", 	(method)dl_sym},
	{"close",	(method)dl_close},
	{NULL,  	NULL}			/* Sentinel */
};

static object *
dl_getattr(xp, name)
	dlobject *xp;
	char *name;
{
	return findmethod(dlobject_methods, (object *)xp, name);
}


static typeobject Dltype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"dl",			/*tp_name*/
	sizeof(dlobject),	/*tp_basicsize*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)dl_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)dl_getattr,/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
};

static object *
dl_open(self, args)
	object *self;
	object *args;
{
	char *name;
	int mode;
	ANY *handle;
	if (getargs(args, "z", &name))
		mode = RTLD_LAZY;
	else {
		err_clear();
		if (!getargs(args, "(zi)", &name, &mode))
			return NULL;
#ifndef RTLD_NOW
		if (mode != RTLD_LAZY) {
			err_setstr(ValueError, "mode must be 1");
			return NULL;
		}
#endif
	}
	handle = dlopen(name, mode);
	if (handle == NULL) {
		err_setstr(Dlerror, dlerror());
		return NULL;
	}
	return newdlobject(handle);
}

static struct methodlist dl_methods[] = {
	{"open",	dl_open},
	{NULL,		NULL}		/* sentinel */
};

void
initdl()
{
	object *m, *d, *x;

	if (sizeof(int) != sizeof(long) ||
	    sizeof(long) != sizeof(char *))
		fatal(
 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");

	/* Create the module and add the functions */
	m = initmodule("dl", dl_methods);

	/* Add some symbolic constants to the module */
	d = getmoduledict(m);
	Dlerror = x = newstringobject("dl.error");
	dictinsert(d, "error", x);
	x = newintobject((long)RTLD_LAZY);
	dictinsert(d, "RTLD_LAZY", x);
#ifdef RTLD_NOW
	x = newintobject((long)RTLD_NOW);
	dictinsert(d, "RTLD_NOW", x);
#endif

	/* Check for errors */
	if (err_occurred())
		fatal("can't initialize module dl");
}