Subversion Repositories HelenOS

Rev

Rev 3561 | Rev 3681 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2928 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup loader
30
 * @brief	Loads and runs programs from VFS.
31
 * @{
32
 */ 
33
/**
34
 * @file
3101 svoboda 35
 * @brief	Loads and runs programs from VFS.
36
 *
37
 * The program loader is a special init binary. Its image is used
38
 * to create a new task upon a @c task_spawn syscall. The syscall
39
 * returns the id of a phone connected to the newly created task.
40
 *
41
 * The caller uses this phone to send the pathname and various other
42
 * information to the loader. This is normally done by the C library
43
 * and completely hidden from applications.
2928 svoboda 44
 */
45
 
46
#include <stdio.h>
3004 svoboda 47
#include <stdlib.h>
2928 svoboda 48
#include <unistd.h>
3474 svoboda 49
#include <bool.h>
2928 svoboda 50
#include <fcntl.h>
51
#include <sys/types.h>
3004 svoboda 52
#include <ipc/ipc.h>
3148 svoboda 53
#include <ipc/loader.h>
3160 svoboda 54
#include <loader/pcb.h>
3004 svoboda 55
#include <errno.h>
3148 svoboda 56
#include <async.h>
2959 svoboda 57
#include <as.h>
2928 svoboda 58
 
2969 svoboda 59
#include <elf.h>
60
#include <elf_load.h>
2928 svoboda 61
 
3101 svoboda 62
/** Pathname of the file that will be loaded */
3004 svoboda 63
static char *pathname = NULL;
64
 
3170 svoboda 65
/** The Program control block */
66
static pcb_t pcb;
67
 
3174 svoboda 68
/** Number of arguments */
69
static int argc = 0;
70
/** Argument vector */
71
static char **argv = NULL;
72
/** Buffer holding all arguments */
73
static char *arg_buf = NULL;
74
 
3474 svoboda 75
static elf_info_t prog_info;
76
static elf_info_t interp_info;
77
 
78
static bool is_dyn_linked;
79
 
80
 
81
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
3448 svoboda 82
{
83
	ipc_callid_t callid;
84
	task_id_t task_id;
85
	size_t len;
86
 
87
	task_id = task_get_id();
88
 
89
	if (!ipc_data_read_receive(&callid, &len)) {
90
		ipc_answer_0(callid, EINVAL);
91
		ipc_answer_0(rid, EINVAL);
92
		return;
93
	}
94
 
95
	if (len > sizeof(task_id)) len = sizeof(task_id);
96
 
3535 svoboda 97
	ipc_data_read_finalize(callid, &task_id, len);
3448 svoboda 98
	ipc_answer_0(rid, EOK);
99
}
100
 
101
 
3101 svoboda 102
/** Receive a call setting pathname of the program to execute.
103
 *
104
 * @param rid
105
 * @param request
106
 */
3174 svoboda 107
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
2928 svoboda 108
{
3148 svoboda 109
	ipc_callid_t callid;
3004 svoboda 110
	size_t len;
111
	char *name_buf;
112
 
113
	if (!ipc_data_write_receive(&callid, &len)) {
114
		ipc_answer_0(callid, EINVAL);
115
		ipc_answer_0(rid, EINVAL);
116
		return;
117
	}
3148 svoboda 118
 
3004 svoboda 119
	name_buf = malloc(len + 1);
120
	if (!name_buf) {
3148 svoboda 121
		ipc_answer_0(callid, ENOMEM);
3004 svoboda 122
		ipc_answer_0(rid, ENOMEM);
123
		return;
124
	}
125
 
3148 svoboda 126
	ipc_data_write_finalize(callid, name_buf, len);
127
	ipc_answer_0(rid, EOK);
3004 svoboda 128
 
129
	if (pathname != NULL) {
130
		free(pathname);
131
		pathname = NULL;
132
	}
133
 
3148 svoboda 134
	name_buf[len] = '\0';
3004 svoboda 135
	pathname = name_buf;
136
}
137
 
3174 svoboda 138
/** Receive a call setting arguments of the program to execute.
139
 *
140
 * @param rid
141
 * @param request
142
 */
143
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
144
{
145
	ipc_callid_t callid;
146
	size_t buf_len, arg_len;
147
	char *p;
148
	int n;
149
 
150
	if (!ipc_data_write_receive(&callid, &buf_len)) {
151
		ipc_answer_0(callid, EINVAL);
152
		ipc_answer_0(rid, EINVAL);
153
		return;
154
	}
155
 
156
	if (arg_buf != NULL) {
157
		free(arg_buf);
158
		arg_buf = NULL;
159
	}
160
 
161
	if (argv != NULL) {
162
		free(argv);
163
		argv = NULL;
164
	}
165
 
166
	arg_buf = malloc(buf_len + 1);
167
	if (!arg_buf) {
168
		ipc_answer_0(callid, ENOMEM);
169
		ipc_answer_0(rid, ENOMEM);
170
		return;
171
	}
172
 
173
	ipc_data_write_finalize(callid, arg_buf, buf_len);
174
	ipc_answer_0(rid, EOK);
175
 
176
	arg_buf[buf_len] = '\0';
177
 
178
	/*
179
	 * Count number of arguments
180
	 */
181
	p = arg_buf;
182
	n = 0;
183
	while (p < arg_buf + buf_len) {
184
		arg_len = strlen(p);
185
		p = p + arg_len + 1;
186
		++n;
187
	}
188
 
189
	/* Allocate argv */
190
	argv = malloc((n + 1) * sizeof(char *));
191
 
192
	if (argv == NULL) {
193
		free(arg_buf);
194
		ipc_answer_0(callid, ENOMEM);
195
		ipc_answer_0(rid, ENOMEM);
196
		return;
197
	}
198
 
199
	/*
200
	 * Fill argv with argument pointers
201
	 */
202
	p = arg_buf;
203
	n = 0;
204
	while (p < arg_buf + buf_len) {
205
		argv[n] = p;
206
 
207
		arg_len = strlen(p);
208
		p = p + arg_len + 1;
209
		++n;
210
	}
211
 
212
	argc = n;
213
	argv[n] = NULL;
214
}
215
 
3474 svoboda 216
/** Load the previously selected program.
3101 svoboda 217
 *
218
 * @param rid
219
 * @param request
220
 * @return 0 on success, !0 on error.
221
 */
3474 svoboda 222
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
3004 svoboda 223
{
224
	int rc;
225
 
3552 svoboda 226
	rc = elf_load_file(pathname, 0, 0, &prog_info);
2928 svoboda 227
	if (rc < 0) {
3677 svoboda 228
		printf("Failed to load executable '%s'.\n", pathname);
3155 svoboda 229
		ipc_answer_0(rid, EINVAL);
2928 svoboda 230
		return 1;
231
	}
232
 
3170 svoboda 233
	elf_create_pcb(&prog_info, &pcb);
2928 svoboda 234
 
3174 svoboda 235
	pcb.argc = argc;
236
	pcb.argv = argv;
3171 svoboda 237
 
3004 svoboda 238
	if (prog_info.interp == NULL) {
239
		/* Statically linked program */
3474 svoboda 240
		is_dyn_linked = false;
3155 svoboda 241
		ipc_answer_0(rid, EOK);
3004 svoboda 242
		return 0;
243
	}
2988 svoboda 244
 
3677 svoboda 245
	printf("Load ELF interpreter '%s'\n", prog_info.interp);
246
	rc = elf_load_file(prog_info.interp, 0, 0, &interp_info);
2959 svoboda 247
	if (rc < 0) {
3677 svoboda 248
		printf("Failed to load interpreter '%s.'\n", prog_info.interp);
3155 svoboda 249
		ipc_answer_0(rid, EINVAL);
2959 svoboda 250
		return 1;
251
	}
252
 
3677 svoboda 253
	printf("Run interpreter.\n");
3400 svoboda 254
	printf("entry point: 0x%lx\n", interp_info.entry);
255
	printf("pcb address: 0x%lx\n", &pcb);
3155 svoboda 256
 
3474 svoboda 257
	is_dyn_linked = true;
3155 svoboda 258
	ipc_answer_0(rid, EOK);
2989 svoboda 259
 
3004 svoboda 260
	return 0;
261
}
262
 
3474 svoboda 263
 
264
/** Run the previously loaded program.
265
 *
266
 * @param rid
267
 * @param request
268
 * @return 0 on success, !0 on error.
269
 */
270
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
271
{
272
	if (is_dyn_linked == true) {
273
		/* Dynamically linked program */
274
		printf("run dynamic linker\n");
3554 svoboda 275
		printf("entry point: 0x%lx\n", interp_info.entry);
3474 svoboda 276
		close_console();
277
 
278
		ipc_answer_0(rid, EOK);
279
		elf_run(&interp_info, &pcb);
280
 
281
	} else {
282
		/* Statically linked program */
283
		close_console();
284
		ipc_answer_0(rid, EOK);
285
		elf_run(&prog_info, &pcb);
286
	} 
287
 
288
	/* Not reached */
289
}
290
 
3148 svoboda 291
/** Handle loader connection.
3101 svoboda 292
 *
293
 * Receive and carry out commands (of which the last one should be
294
 * to execute the loaded program).
295
 */
3148 svoboda 296
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
3004 svoboda 297
{
298
	ipc_callid_t callid;
299
	ipc_call_t call;
300
	int retval;
301
 
3148 svoboda 302
	/* Ignore parameters, the connection is already open */
303
	(void)iid; (void)icall;
304
 
3004 svoboda 305
	while (1) {
3148 svoboda 306
		callid = async_get_call(&call);
3474 svoboda 307
 
3004 svoboda 308
		switch (IPC_GET_METHOD(call)) {
3561 svoboda 309
		case IPC_M_PHONE_HUNGUP:
310
			exit(0);
3448 svoboda 311
		case LOADER_GET_TASKID:
312
			loader_get_taskid(callid, &call);
313
			continue;
3148 svoboda 314
		case LOADER_SET_PATHNAME:
3174 svoboda 315
			loader_set_pathname(callid, &call);
3148 svoboda 316
			continue;
3174 svoboda 317
		case LOADER_SET_ARGS:
318
			loader_set_args(callid, &call);
3474 svoboda 319
			continue;
320
		case LOADER_LOAD:
321
			loader_load(callid, &call);
322
			continue;
3148 svoboda 323
		case LOADER_RUN:
3174 svoboda 324
			loader_run(callid, &call);
3474 svoboda 325
			/* Not reached */
3004 svoboda 326
		default:
327
			retval = ENOENT;
328
			break;
329
		}
3174 svoboda 330
		if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
331
		    IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
332
			printf("responding EINVAL to method %d\n", 
333
			    IPC_GET_METHOD(call));
3004 svoboda 334
			ipc_answer_0(callid, EINVAL);
335
		}
336
	}
3148 svoboda 337
}
3004 svoboda 338
 
3148 svoboda 339
/** Program loader main function.
340
 */
341
int main(int argc, char *argv[])
342
{
343
	ipc_callid_t callid;
344
	ipc_call_t call;
345
	ipcarg_t phone_hash;
346
 
347
	/* The first call only communicates the incoming phone hash */
348
	callid = ipc_wait_for_call(&call);
349
 
350
	if (IPC_GET_METHOD(call) != LOADER_HELLO) {
351
		if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
352
			ipc_answer_0(callid, EINVAL);
353
		return 1;
354
	}
355
 
356
	ipc_answer_0(callid, EOK);
357
	phone_hash = call.in_phone_hash;
358
 
359
	/* 
3170 svoboda 360
	 * Up until now async must not be used as it couldn't
361
	 * handle incoming requests. (Which means e.g. printf() 
362
	 * cannot be used)
3148 svoboda 363
	 */
364
	async_new_connection(phone_hash, 0, NULL, loader_connection);
365
	async_manager();
366
 
2928 svoboda 367
	/* not reached */
368
	return 0;
369
}
370
 
371
/** @}
372
 */