aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/sys/module/exec.c
blob: 025864bcf53bc3e9bc42e1929470c305997daa7c (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
/*
 * 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 <string.h>
#include <stdarg.h>



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


static struct elf_module    *mod_root = NULL;

static char *module_get_fullname(const char *name) {
	static char name_buff[MODULE_NAME_SIZE];

	if (name == NULL)
		return NULL;

	strcpy(name_buff, EXEC_DIRECTORY);
	strcat(name_buff, name);

	return name_buff;
}

int exec_init() {
	int res;

	res = modules_init();

	if (res != 0)
		return res;

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

	if (mod_root == NULL)
		return -1;

	res = module_load_shallow(mod_root);

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

	return 0;
}

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

	if (module == NULL)
		return -1;

	res = module_load(module);


	if (res != 0) {
		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))();
		DBG_PRINT("Initialization 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(module_get_fullname(name));

	if (module == NULL)
		return -1;

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

	res = module_unload(module);

	return res;
}

int spawnv(const char *name, const char **argv) {
	int res, ret_val;

	struct elf_module *module = module_alloc(module_get_fullname(name));

	if (module == NULL)
		return -1;

	res = module_load(module);

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

	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;
	}

	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);
}


void exec_term() {
	modules_term();
}