source: mainline/uspace/srv/loader/main.c@ 73db198

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 73db198 was 31a30fa, checked in by Martin Decky <martin@…>, 8 years ago

declare routines as noreturn

  • Property mode set to 100644
File size: 9.1 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 <stdbool.h>
50#include <sys/types.h>
51#include <ipc/services.h>
52#include <ipc/loader.h>
53#include <ns.h>
54#include <loader/pcb.h>
55#include <entry_point.h>
56#include <errno.h>
57#include <async.h>
58#include <str.h>
59#include <as.h>
60#include <elf/elf.h>
61#include <elf/elf_load.h>
62#include <vfs/vfs.h>
63#include <vfs/inbox.h>
64
65#define DPRINTF(...)
66
67/** File that will be loaded */
68static char *progname = NULL;
69static int program_fd = -1;
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/** Inbox entries. */
85static struct pcb_inbox_entry inbox[INBOX_MAX_ENTRIES];
86static int inbox_entries = 0;
87
88static elf_info_t prog_info;
89
90/** Used to limit number of connections to one. */
91static bool connected = false;
92
93static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
94{
95 ipc_callid_t callid;
96 task_id_t task_id;
97 size_t len;
98
99 task_id = task_get_id();
100
101 if (!async_data_read_receive(&callid, &len)) {
102 async_answer_0(callid, EINVAL);
103 async_answer_0(rid, EINVAL);
104 return;
105 }
106
107 if (len > sizeof(task_id))
108 len = sizeof(task_id);
109
110 async_data_read_finalize(callid, &task_id, len);
111 async_answer_0(rid, EOK);
112}
113
114/** Receive a call setting the current working directory.
115 *
116 * @param rid
117 * @param request
118 */
119static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
120{
121 char *buf;
122 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
123
124 if (rc == EOK) {
125 if (cwd != NULL)
126 free(cwd);
127
128 cwd = buf;
129 }
130
131 async_answer_0(rid, rc);
132}
133
134/** Receive a call setting the program to execute.
135 *
136 * @param rid
137 * @param request
138 */
139static void ldr_set_program(ipc_callid_t rid, ipc_call_t *request)
140{
141 ipc_callid_t writeid;
142 size_t namesize;
143 if (!async_data_write_receive(&writeid, &namesize)) {
144 async_answer_0(rid, EINVAL);
145 return;
146 }
147
148 char* name = malloc(namesize);
149 int rc = async_data_write_finalize(writeid, name, namesize);
150 if (rc != EOK) {
151 async_answer_0(rid, EINVAL);
152 return;
153 }
154
155 int file = vfs_receive_handle(true);
156 if (file < 0) {
157 async_answer_0(rid, EINVAL);
158 return;
159 }
160
161 progname = name;
162 program_fd = file;
163 async_answer_0(rid, EOK);
164}
165
166/** Receive a call setting arguments of the program to execute.
167 *
168 * @param rid
169 * @param request
170 */
171static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
172{
173 char *buf;
174 size_t buf_size;
175 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
176
177 if (rc == EOK) {
178 /*
179 * Count number of arguments
180 */
181 char *cur = buf;
182 int count = 0;
183
184 while (cur < buf + buf_size) {
185 size_t arg_size = str_size(cur);
186 cur += arg_size + 1;
187 count++;
188 }
189
190 /*
191 * Allocate new argv
192 */
193 char **_argv = (char **) malloc((count + 1) * sizeof(char *));
194 if (_argv == NULL) {
195 free(buf);
196 async_answer_0(rid, ENOMEM);
197 return;
198 }
199
200 /*
201 * Fill the new argv with argument pointers
202 */
203 cur = buf;
204 count = 0;
205 while (cur < buf + buf_size) {
206 _argv[count] = cur;
207
208 size_t arg_size = str_size(cur);
209 cur += arg_size + 1;
210 count++;
211 }
212 _argv[count] = NULL;
213
214 /*
215 * Copy temporary data to global variables
216 */
217 if (arg_buf != NULL)
218 free(arg_buf);
219
220 if (argv != NULL)
221 free(argv);
222
223 argc = count;
224 arg_buf = buf;
225 argv = _argv;
226 }
227
228 async_answer_0(rid, rc);
229}
230
231/** Receive a call setting inbox files of the program to execute.
232 *
233 * @param rid
234 * @param request
235 */
236static void ldr_add_inbox(ipc_callid_t rid, ipc_call_t *request)
237{
238 if (inbox_entries == INBOX_MAX_ENTRIES) {
239 async_answer_0(rid, ERANGE);
240 return;
241 }
242
243 ipc_callid_t writeid;
244 size_t namesize;
245 if (!async_data_write_receive(&writeid, &namesize)) {
246 async_answer_0(rid, EINVAL);
247 return;
248 }
249
250 char* name = malloc(namesize);
251 int rc = async_data_write_finalize(writeid, name, namesize);
252 if (rc != EOK) {
253 async_answer_0(rid, EINVAL);
254 return;
255 }
256
257 int file = vfs_receive_handle(true);
258 if (file < 0) {
259 async_answer_0(rid, EINVAL);
260 return;
261 }
262
263 /*
264 * We need to set the root early for dynamically linked binaries so
265 * that the loader can use it too.
266 */
267 if (str_cmp(name, "root") == 0)
268 vfs_root_set(file);
269
270 inbox[inbox_entries].name = name;
271 inbox[inbox_entries].file = file;
272 inbox_entries++;
273 async_answer_0(rid, EOK);
274}
275
276/** Load the previously selected program.
277 *
278 * @param rid
279 * @param request
280 * @return 0 on success, !0 on error.
281 */
282static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
283{
284 int rc = elf_load(program_fd, &prog_info);
285 if (rc != EE_OK) {
286 DPRINTF("Failed to load executable for '%s'.\n", progname);
287 async_answer_0(rid, EINVAL);
288 return 1;
289 }
290
291 elf_set_pcb(&prog_info, &pcb);
292
293 pcb.cwd = cwd;
294
295 pcb.argc = argc;
296 pcb.argv = argv;
297
298 pcb.inbox = inbox;
299 pcb.inbox_entries = inbox_entries;
300
301 async_answer_0(rid, rc);
302 return 0;
303}
304
305/** Run the previously loaded program.
306 *
307 * @param rid
308 * @param request
309 * @return 0 on success, !0 on error.
310 */
311static __attribute__((noreturn)) void ldr_run(ipc_callid_t rid,
312 ipc_call_t *request)
313{
314 DPRINTF("Set task name\n");
315
316 /* Set the task name. */
317 task_set_name(progname);
318
319 /* Run program */
320 DPRINTF("Reply OK\n");
321 async_answer_0(rid, EOK);
322 DPRINTF("Jump to entry point at %p\n", pcb.entry);
323 entry_point_jmp(prog_info.finfo.entry, &pcb);
324
325 /* Not reached */
326}
327
328/** Handle loader connection.
329 *
330 * Receive and carry out commands (of which the last one should be
331 * to execute the loaded program).
332 */
333static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
334{
335 /* Already have a connection? */
336 if (connected) {
337 async_answer_0(iid, ELIMIT);
338 return;
339 }
340
341 connected = true;
342
343 /* Accept the connection */
344 async_answer_0(iid, EOK);
345
346 /* Ignore parameters, the connection is already open */
347 (void) icall;
348
349 while (true) {
350 int retval;
351 ipc_call_t call;
352 ipc_callid_t callid = async_get_call(&call);
353
354 if (!IPC_GET_IMETHOD(call))
355 exit(0);
356
357 switch (IPC_GET_IMETHOD(call)) {
358 case LOADER_GET_TASKID:
359 ldr_get_taskid(callid, &call);
360 continue;
361 case LOADER_SET_CWD:
362 ldr_set_cwd(callid, &call);
363 continue;
364 case LOADER_SET_PROGRAM:
365 ldr_set_program(callid, &call);
366 continue;
367 case LOADER_SET_ARGS:
368 ldr_set_args(callid, &call);
369 continue;
370 case LOADER_ADD_INBOX:
371 ldr_add_inbox(callid, &call);
372 continue;
373 case LOADER_LOAD:
374 ldr_load(callid, &call);
375 continue;
376 case LOADER_RUN:
377 ldr_run(callid, &call);
378 /* Not reached */
379 default:
380 retval = EINVAL;
381 break;
382 }
383
384 async_answer_0(callid, retval);
385 }
386}
387
388/** Program loader main function.
389 */
390int main(int argc, char *argv[])
391{
392 async_set_fallback_port_handler(ldr_connection, NULL);
393
394 /* Introduce this task to the NS (give it our task ID). */
395 task_id_t id = task_get_id();
396 int rc = ns_intro(id);
397 if (rc != EOK)
398 return rc;
399
400 /* Create port */
401 port_id_t port;
402 rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
403 if (rc != EOK)
404 return rc;
405
406 /* Register at naming service. */
407 rc = service_register(SERVICE_LOADER);
408 if (rc != EOK)
409 return rc;
410
411 async_manager();
412
413 /* Never reached */
414 return 0;
415}
416
417/** @}
418 */
Note: See TracBrowser for help on using the repository browser.