source: mainline/uspace/srv/loader/main.c@ fb6f1a5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fb6f1a5 was fb6f1a5, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Implement some of C99 inttypes.h header (formatting macros for stdint.h). Use in appropriate places. Also replace use of 0x%lx with %p.

  • Property mode set to 100644
File size: 10.4 KB
Line 
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
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.
44 */
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <bool.h>
50#include <fcntl.h>
51#include <sys/types.h>
52#include <ipc/ipc.h>
53#include <ipc/services.h>
54#include <ipc/loader.h>
55#include <ipc/ns.h>
56#include <macros.h>
57#include <loader/pcb.h>
58#include <errno.h>
59#include <async.h>
60#include <string.h>
61#include <as.h>
62
63#include <elf.h>
64#include <elf_load.h>
65
66#define DPRINTF(...)
67
68/** Pathname of the file that will be loaded */
69static char *pathname = NULL;
70
71/** The Program control block */
72static pcb_t pcb;
73
74/** Current working directory */
75static char *cwd = NULL;
76
77/** Number of arguments */
78static int argc = 0;
79/** Argument vector */
80static char **argv = NULL;
81/** Buffer holding all arguments */
82static char *arg_buf = NULL;
83
84/** Number of preset files */
85static int filc = 0;
86/** Preset files vector */
87static fdi_node_t **filv = NULL;
88/** Buffer holding all preset files */
89static fdi_node_t *fil_buf = NULL;
90
91static elf_info_t prog_info;
92static elf_info_t interp_info;
93
94static bool is_dyn_linked;
95
96/** Used to limit number of connections to one. */
97static bool connected;
98
99static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
100{
101 ipc_callid_t callid;
102 task_id_t task_id;
103 size_t len;
104
105 task_id = task_get_id();
106
107 if (!async_data_read_receive(&callid, &len)) {
108 ipc_answer_0(callid, EINVAL);
109 ipc_answer_0(rid, EINVAL);
110 return;
111 }
112
113 if (len > sizeof(task_id))
114 len = sizeof(task_id);
115
116 async_data_read_finalize(callid, &task_id, len);
117 ipc_answer_0(rid, EOK);
118}
119
120/** Receive a call setting the current working directory.
121 *
122 * @param rid
123 * @param request
124 */
125static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
126{
127 ipc_callid_t callid;
128 size_t len;
129
130 if (!async_data_write_receive(&callid, &len)) {
131 ipc_answer_0(callid, EINVAL);
132 ipc_answer_0(rid, EINVAL);
133 return;
134 }
135
136 cwd = malloc(len + 1);
137 if (!cwd) {
138 ipc_answer_0(callid, ENOMEM);
139 ipc_answer_0(rid, ENOMEM);
140 return;
141 }
142
143 async_data_write_finalize(callid, cwd, len);
144 cwd[len] = '\0';
145
146 ipc_answer_0(rid, EOK);
147}
148
149/** Receive a call setting pathname of the program to execute.
150 *
151 * @param rid
152 * @param request
153 */
154static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
155{
156 ipc_callid_t callid;
157 size_t len;
158 char *name_buf;
159
160 if (!async_data_write_receive(&callid, &len)) {
161 ipc_answer_0(callid, EINVAL);
162 ipc_answer_0(rid, EINVAL);
163 return;
164 }
165
166 name_buf = malloc(len + 1);
167 if (!name_buf) {
168 ipc_answer_0(callid, ENOMEM);
169 ipc_answer_0(rid, ENOMEM);
170 return;
171 }
172
173 async_data_write_finalize(callid, name_buf, len);
174 ipc_answer_0(rid, EOK);
175
176 if (pathname != NULL) {
177 free(pathname);
178 pathname = NULL;
179 }
180
181 name_buf[len] = '\0';
182 pathname = name_buf;
183}
184
185/** Receive a call setting arguments of the program to execute.
186 *
187 * @param rid
188 * @param request
189 */
190static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
191{
192 ipc_callid_t callid;
193 size_t buf_size, arg_size;
194 char *p;
195 int n;
196
197 if (!async_data_write_receive(&callid, &buf_size)) {
198 ipc_answer_0(callid, EINVAL);
199 ipc_answer_0(rid, EINVAL);
200 return;
201 }
202
203 if (arg_buf != NULL) {
204 free(arg_buf);
205 arg_buf = NULL;
206 }
207
208 if (argv != NULL) {
209 free(argv);
210 argv = NULL;
211 }
212
213 arg_buf = malloc(buf_size + 1);
214 if (!arg_buf) {
215 ipc_answer_0(callid, ENOMEM);
216 ipc_answer_0(rid, ENOMEM);
217 return;
218 }
219
220 async_data_write_finalize(callid, arg_buf, buf_size);
221
222 arg_buf[buf_size] = '\0';
223
224 /*
225 * Count number of arguments
226 */
227 p = arg_buf;
228 n = 0;
229 while (p < arg_buf + buf_size) {
230 arg_size = str_size(p);
231 p = p + arg_size + 1;
232 ++n;
233 }
234
235 /* Allocate argv */
236 argv = malloc((n + 1) * sizeof(char *));
237
238 if (argv == NULL) {
239 free(arg_buf);
240 ipc_answer_0(rid, ENOMEM);
241 return;
242 }
243
244 /*
245 * Fill argv with argument pointers
246 */
247 p = arg_buf;
248 n = 0;
249 while (p < arg_buf + buf_size) {
250 argv[n] = p;
251
252 arg_size = str_size(p);
253 p = p + arg_size + 1;
254 ++n;
255 }
256
257 argc = n;
258 argv[n] = NULL;
259
260 ipc_answer_0(rid, EOK);
261}
262
263/** Receive a call setting preset files of the program to execute.
264 *
265 * @param rid
266 * @param request
267 */
268static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request)
269{
270 ipc_callid_t callid;
271 size_t buf_size;
272 if (!async_data_write_receive(&callid, &buf_size)) {
273 ipc_answer_0(callid, EINVAL);
274 ipc_answer_0(rid, EINVAL);
275 return;
276 }
277
278 if ((buf_size % sizeof(fdi_node_t)) != 0) {
279 ipc_answer_0(callid, EINVAL);
280 ipc_answer_0(rid, EINVAL);
281 return;
282 }
283
284 if (fil_buf != NULL) {
285 free(fil_buf);
286 fil_buf = NULL;
287 }
288
289 if (filv != NULL) {
290 free(filv);
291 filv = NULL;
292 }
293
294 fil_buf = malloc(buf_size);
295 if (!fil_buf) {
296 ipc_answer_0(callid, ENOMEM);
297 ipc_answer_0(rid, ENOMEM);
298 return;
299 }
300
301 async_data_write_finalize(callid, fil_buf, buf_size);
302
303 int count = buf_size / sizeof(fdi_node_t);
304
305 /* Allocate filvv */
306 filv = malloc((count + 1) * sizeof(fdi_node_t *));
307
308 if (filv == NULL) {
309 free(fil_buf);
310 ipc_answer_0(rid, ENOMEM);
311 return;
312 }
313
314 /*
315 * Fill filv with argument pointers
316 */
317 int i;
318 for (i = 0; i < count; i++)
319 filv[i] = &fil_buf[i];
320
321 filc = count;
322 filv[count] = NULL;
323
324 ipc_answer_0(rid, EOK);
325}
326
327/** Load the previously selected program.
328 *
329 * @param rid
330 * @param request
331 * @return 0 on success, !0 on error.
332 */
333static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
334{
335 int rc;
336
337 rc = elf_load_file(pathname, 0, &prog_info);
338 if (rc != EE_OK) {
339 DPRINTF("Failed to load executable '%s'.\n", pathname);
340 ipc_answer_0(rid, EINVAL);
341 return 1;
342 }
343
344 elf_create_pcb(&prog_info, &pcb);
345
346 pcb.cwd = cwd;
347
348 pcb.argc = argc;
349 pcb.argv = argv;
350
351 pcb.filc = filc;
352 pcb.filv = filv;
353
354 if (prog_info.interp == NULL) {
355 /* Statically linked program */
356 is_dyn_linked = false;
357 ipc_answer_0(rid, EOK);
358 return 0;
359 }
360
361 rc = elf_load_file(prog_info.interp, 0, &interp_info);
362 if (rc != EE_OK) {
363 DPRINTF("Failed to load interpreter '%s.'\n",
364 prog_info.interp);
365 ipc_answer_0(rid, EINVAL);
366 return 1;
367 }
368
369 is_dyn_linked = true;
370 ipc_answer_0(rid, EOK);
371
372 return 0;
373}
374
375
376/** Run the previously loaded program.
377 *
378 * @param rid
379 * @param request
380 * @return 0 on success, !0 on error.
381 */
382static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
383{
384 const char *cp;
385
386 /* Set the task name. */
387 cp = str_rchr(pathname, '/');
388 cp = (cp == NULL) ? pathname : (cp + 1);
389 task_set_name(cp);
390
391 if (is_dyn_linked == true) {
392 /* Dynamically linked program */
393 DPRINTF("Run ELF interpreter.\n");
394 DPRINTF("Entry point: %p\n", interp_info.entry);
395
396 ipc_answer_0(rid, EOK);
397 elf_run(&interp_info, &pcb);
398 } else {
399 /* Statically linked program */
400 ipc_answer_0(rid, EOK);
401 elf_run(&prog_info, &pcb);
402 }
403
404 /* Not reached */
405}
406
407/** Handle loader connection.
408 *
409 * Receive and carry out commands (of which the last one should be
410 * to execute the loaded program).
411 */
412static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall)
413{
414 ipc_callid_t callid;
415 ipc_call_t call;
416 int retval;
417
418 /* Already have a connection? */
419 if (connected) {
420 ipc_answer_0(iid, ELIMIT);
421 return;
422 }
423
424 connected = true;
425
426 /* Accept the connection */
427 ipc_answer_0(iid, EOK);
428
429 /* Ignore parameters, the connection is already open */
430 (void) iid;
431 (void) icall;
432
433 while (1) {
434 callid = async_get_call(&call);
435
436 switch (IPC_GET_METHOD(call)) {
437 case IPC_M_PHONE_HUNGUP:
438 exit(0);
439 case LOADER_GET_TASKID:
440 ldr_get_taskid(callid, &call);
441 continue;
442 case LOADER_SET_CWD:
443 ldr_set_cwd(callid, &call);
444 continue;
445 case LOADER_SET_PATHNAME:
446 ldr_set_pathname(callid, &call);
447 continue;
448 case LOADER_SET_ARGS:
449 ldr_set_args(callid, &call);
450 continue;
451 case LOADER_SET_FILES:
452 ldr_set_files(callid, &call);
453 continue;
454 case LOADER_LOAD:
455 ldr_load(callid, &call);
456 continue;
457 case LOADER_RUN:
458 ldr_run(callid, &call);
459 /* Not reached */
460 default:
461 retval = ENOENT;
462 break;
463 }
464 if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
465 IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
466 DPRINTF("Responding EINVAL to method %d.\n",
467 IPC_GET_METHOD(call));
468 ipc_answer_0(callid, EINVAL);
469 }
470 }
471}
472
473/** Program loader main function.
474 */
475int main(int argc, char *argv[])
476{
477 ipcarg_t phonead;
478 task_id_t id;
479 int rc;
480
481 connected = false;
482
483 /* Introduce this task to the NS (give it our task ID). */
484 id = task_get_id();
485 rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id));
486 if (rc != EOK)
487 return -1;
488
489 /* Set a handler of incomming connections. */
490 async_set_client_connection(ldr_connection);
491
492 /* Register at naming service. */
493 if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
494 return -2;
495
496 async_manager();
497
498 /* Never reached */
499 return 0;
500}
501
502/** @}
503 */
Note: See TracBrowser for help on using the repository browser.