aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/sys/module/exec.c
blob: 03d11cb9489551d68afe9f7759e0876310880f15 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * exec.c
 *
 *  Created on: Aug 14, 2008
 *      Author: Stefan Bucur <stefanb@zytor.com>
 */

#include <sys/module.h>
#include <sys/exec.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <setjmp.h>
#include <alloca.h>

#define DBG_PRINT(fmt, args...)	fprintf(stderr, "[EXEC] " fmt, ##args)

static struct elf_module    *mod_root = NULL;
struct elf_module *__syslinux_current = NULL;

int exec_init(void)
{
	int res;

	res = modules_init();
	if (res != 0)
		return res;

	// Load the root module
	mod_root = module_alloc(EXEC_ROOT_NAME);

	if (mod_root == NULL)
		return -1;

	res = module_load_shallow(mod_root, 0);
	if (res != 0) {
		mod_root = NULL;
		return res;
	}

	return 0;
}

int get_module_type(struct elf_module *module)
{
	if(module->main_func) return EXEC_MODULE;
	else if(module->init_func) return LIB_MODULE;
	return UNKNOWN_MODULE;
}

int load_library(const char *name)
{
	int res;
	struct elf_module *module = module_alloc(name);

	if (module == NULL)
		return -1;

	res = module_load(module);
	if (res != 0) {
		module_unload(module);
		return res;
	}

	if (module->main_func != NULL) {
		DBG_PRINT("Cannot load executable module as library.\n");
		module_unload(module);
		return -1;
	}

	if (module->init_func != NULL) {
		res = (*(module->init_func))();
		if (res)
			DBG_PRINT("Initialization error! function returned: %d\n", res);
	} else {
		DBG_PRINT("No initialization function present.\n");
	}

	if (res != 0) {
		module_unload(module);
		return res;
	}

	return 0;
}

int unload_library(const char *name)
{
	int res;
	struct elf_module *module = module_find(name);

	if (module == NULL)
		return -1;

	if (!module_unloadable(module)) {
		return -1;
	}

	if (module->exit_func != NULL) {
		(*(module->exit_func))();
	}

	res = module_unload(module);
	return res;
}

jmp_buf __process_exit_jmp;

#if 0
int spawnv(const char *name, const char **argv)
{
	int res, ret_val = 0;
	const char **arg;
	int argc;
	char **argp, **args;
	struct elf_module *previous;
	malloc_tag_t prev_mem_tag;

	struct elf_module *module = module_alloc(name);

	if (module == NULL)
		return -1;

	res = module_load(module);
	if (res != 0) {
		module_unload(module);
		return res;
	}

	if (module->main_func == NULL) {
		// We can't execute without a main function
		module_unload(module);
		return -1;
	}
	/*if (module->main_func != NULL) {
		const char **last_arg = argv;
		void *old_tag;
		while (*last_arg != NULL)
			last_arg++;

		// Setup the memory allocation context
		old_tag = __mem_get_tag_global();
		__mem_set_tag_global(module);

		// Execute the program
		ret_val = (*(module->main_func))(last_arg - argv, argv);

		// Clean up the allocation context
		__free_tagged(module);
		// Restore the allocation context
		__mem_set_tag_global(old_tag);
	} else {
		// We can't execute without a main function
		module_unload(module);
		return -1;
	}*/
	// Set up the process context
	previous = __syslinux_current;
	prev_mem_tag = __mem_get_tag_global();

	// Setup the new process context
	__syslinux_current = module;
	__mem_set_tag_global((malloc_tag_t)module);

	// Generate a new process copy of argv (on the stack)
	argc = 0;
	for (arg = argv; *arg; arg++)
		argc++;

	args = alloca((argc+1) * sizeof(char *));

	for (arg = argv, argp = args; *arg; arg++, argp++) {
		size_t l = strlen(*arg)+1;
		*argp = alloca(l);
		memcpy(*argp, *arg, l);
	}

	*args = NULL;

	// Execute the program
	ret_val = setjmp(module->u.x.process_exit);

	if (ret_val)
		ret_val--;		/* Valid range is 0-255 */
	else if (!module->main_func)
		ret_val = -1;
	else
		exit((*module->main_func)(argc, args)); /* Actually run! */

	// Clean up the allocation context
	__free_tagged(module);
	// Restore the allocation context
	__mem_set_tag_global(prev_mem_tag);
	// Restore the process context
	__syslinux_current = previous;

	res = module_unload(module);

	if (res != 0) {
		return res;
	}

	return ((unsigned int)ret_val & 0xFF);
}

int spawnl(const char *name, const char *arg, ...)
{
	/*
	 * NOTE: We assume the standard ABI specification for the i386
	 * architecture. This code may not work if used in other
	 * circumstances, including non-variadic functions, different
	 * architectures and calling conventions.
	 */
	return spawnv(name, &arg);
}
#endif

struct elf_module *cur_module;

int spawn_load(const char *name,const char **argv)
{
	int res, ret_val = 0;
	const char **arg;
	int argc;
	char **argp, **args;
	struct elf_module *previous;
	//malloc_tag_t prev_mem_tag;
	struct elf_module *module = module_alloc(name);
	struct elf_module *prev_module;

	int type;

	mp("enter: name = %s", name);

	if (module == NULL)
		return -1;

	if (!strcmp(cur_module->name, module->name)) {
		mp("We is running this module %s already!", module->name);

		/*
		 * If we're already running the module and it's of
		 * type EXEC_MODULE, then just return. We don't reload
		 * the module because that might cause us to re-run
		 * the init functions, which will cause us to run the
		 * MODULE_MAIN function, which will take control of
		 * this process.
		 *
		 * This can happen if some other EXEC_MODULE is
		 * resolving a symbol that is exported by the current
		 * EXEC_MODULE.
		 */
		if (get_module_type(module) == EXEC_MODULE)
			return 0;
	}

	res = module_load(module);
	if (res != 0) {
		module_unload(module);
		return res;
	}

	type = get_module_type(module);
	prev_module = cur_module;
	cur_module = module;

	mp("type = %d, prev = %s, cur = %s",
		type, prev_module->name, cur_module->name);

	if(type==LIB_MODULE)
	{
		if (module->init_func != NULL) {
			res = (*(module->init_func))();
			DBG_PRINT("Initialization function returned: %d\n", res);
		} else {
			DBG_PRINT("No initialization function present.\n");
		}

		if (res != 0) {
			cur_module = prev_module;
			module_unload(module);
			return res;
		}
		return 0;
	}
	else if(type==EXEC_MODULE)
	{
		previous = __syslinux_current;
		//prev_mem_tag = __mem_get_tag_global();

		// Setup the new process context
		__syslinux_current = module;
		//__mem_set_tag_global((malloc_tag_t)module);

		// Generate a new process copy of argv (on the stack)
		argc = 0;
		for (arg = argv; *arg; arg++)
			argc++;

		args = alloca((argc+1) * sizeof(char *));

		for (arg = argv, argp = args; *arg; arg++, argp++) {
			size_t l = strlen(*arg)+1;
			*argp = alloca(l);
			memcpy(*argp, *arg, l);
		}

		*args = NULL;

		// Execute the program
		ret_val = setjmp(module->u.x.process_exit);

		if (ret_val)
			ret_val--;		/* Valid range is 0-255 */
		else if (!module->main_func)
			ret_val = -1;
		else
			exit((*module->main_func)(argc, args)); /* Actually run! */


		// Clean up the allocation context
		//__free_tagged(module);
		// Restore the allocation context
		//__mem_set_tag_global(prev_mem_tag);
		// Restore the process context
		__syslinux_current = previous;

		cur_module = prev_module;
		res = module_unload(module);

		if (res != 0) {
			return res;
		}

		return ((unsigned int)ret_val & 0xFF);
	}
	/*
	module_unload(module);
	return -1;
	*/
}

int module_load_dependencies(const char *name,const char *dep_file)
{
	FILE *d_file=fopen(dep_file,"r");
	char line[2048],aux[2048],temp_name[MODULE_NAME_SIZE],slbz[24];
	int i=0,j=0,res=0;

	if(d_file==NULL)
	{
		DBG_PRINT("Could not open object file '%s'\n",dep_file);
		return -1;
	}

	/* Note from feng:
	 * new modues.dep has line like this:
	 *	a.c32: b.32 c.c32 d.c32
	 * with desktop glibc
	 *	sscanf(line,"%[^:]: %[^\n]", temp_name, aux);
	 * works, which doesn't work here
	 */
	memset(temp_name, 0, sizeof(temp_name));
	memset(aux, 0, sizeof(aux));
	while (1) {
		if(fgets(line,2048,d_file)==NULL)
			break;

		//sscanf(line,"%s %[^\t\n]s",temp_name,aux);
		//sscanf(line,"%[^:]: %[^\n]", temp_name, aux);
		//sscanf(line,"%[^:]: %[^\n]\n", temp_name, aux);

		sscanf(line,"%[^:]:", temp_name);
		if (!strncmp(name, temp_name, strlen(name))) {
			/* The next 2 chars should be ':' and ' ' */
			i = strlen(temp_name);
			if (line[i] != ':' || line[i+1] != ' ')
				break;

			i +=2;
			j = 0;
			while (line[i] != '\n')
				aux[j++] = line[i++];
			aux[j] = '\0';
			//mp("found dependency: temp_name = %s, aux = %s, name = %s", temp_name, aux, name);
			break;
		}
	}
	fclose(d_file);

	/* Reuse temp_name for dependent module name buffer */
	memset(temp_name, 0, sizeof(temp_name));
	i = 0;
	while (aux[i]) {
		sscanf(aux + i, "%s", temp_name);
		//mp("load module: %s", temp_name);
		i += strlen(temp_name);
		i++;	/* skip a space */

		if (strlen(temp_name)) {
			char *argv[2] = { NULL, NULL };

			module_load_dependencies(temp_name, MODULES_DEP);
			if (spawn_load(temp_name, argv) < 0)
				continue;
		}
	}

	return 0;
}

void exec_term(void)
{
	modules_term();
}