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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a0a9cc2 was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 7 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

  • 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 <stdbool.h>
49#include <stddef.h>
50#include <ipc/services.h>
51#include <ipc/loader.h>
52#include <ns.h>
53#include <loader/pcb.h>
54#include <entry_point.h>
55#include <errno.h>
56#include <async.h>
57#include <str.h>
58#include <as.h>
59#include <elf/elf.h>
60#include <elf/elf_load.h>
61#include <vfs/vfs.h>
62#include <vfs/inbox.h>
63
64#define DPRINTF(...)
65
66/** File that will be loaded */
67static char *progname = NULL;
68static int program_fd = -1;
69
70/** The Program control block */
71static pcb_t pcb;
72
73/** Current working directory */
74static char *cwd = NULL;
75
76/** Number of arguments */
77static int argc = 0;
78/** Argument vector */
79static char **argv = NULL;
80/** Buffer holding all arguments */
81static char *arg_buf = NULL;
82
83/** Inbox entries. */
84static struct pcb_inbox_entry inbox[INBOX_MAX_ENTRIES];
85static int inbox_entries = 0;
86
87static elf_info_t prog_info;
88
89/** Used to limit number of connections to one. */
90static bool connected = false;
91
92static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
93{
94 ipc_callid_t callid;
95 task_id_t task_id;
96 size_t len;
97
98 task_id = task_get_id();
99
100 if (!async_data_read_receive(&callid, &len)) {
101 async_answer_0(callid, EINVAL);
102 async_answer_0(rid, EINVAL);
103 return;
104 }
105
106 if (len > sizeof(task_id))
107 len = sizeof(task_id);
108
109 async_data_read_finalize(callid, &task_id, len);
110 async_answer_0(rid, EOK);
111}
112
113/** Receive a call setting the current working directory.
114 *
115 * @param rid
116 * @param request
117 */
118static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
119{
120 char *buf;
121 errno_t rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
122
123 if (rc == EOK) {
124 if (cwd != NULL)
125 free(cwd);
126
127 cwd = buf;
128 }
129
130 async_answer_0(rid, rc);
131}
132
133/** Receive a call setting the program to execute.
134 *
135 * @param rid
136 * @param request
137 */
138static void ldr_set_program(ipc_callid_t rid, ipc_call_t *request)
139{
140 ipc_callid_t writeid;
141 size_t namesize;
142 if (!async_data_write_receive(&writeid, &namesize)) {
143 async_answer_0(rid, EINVAL);
144 return;
145 }
146
147 char* name = malloc(namesize);
148 errno_t rc = async_data_write_finalize(writeid, name, namesize);
149 if (rc != EOK) {
150 async_answer_0(rid, EINVAL);
151 return;
152 }
153
154 int file;
155 rc = vfs_receive_handle(true, &file);
156 if (rc != EOK) {
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 errno_t 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 errno_t 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;
258 rc = vfs_receive_handle(true, &file);
259 if (rc != EOK) {
260 async_answer_0(rid, EINVAL);
261 return;
262 }
263
264 /*
265 * We need to set the root early for dynamically linked binaries so
266 * that the loader can use it too.
267 */
268 if (str_cmp(name, "root") == 0)
269 vfs_root_set(file);
270
271 inbox[inbox_entries].name = name;
272 inbox[inbox_entries].file = file;
273 inbox_entries++;
274 async_answer_0(rid, EOK);
275}
276
277/** Load the previously selected program.
278 *
279 * @param rid
280 * @param request
281 * @return 0 on success, !0 on error.
282 */
283static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
284{
285 int rc = elf_load(program_fd, &prog_info);
286 if (rc != EE_OK) {
287 DPRINTF("Failed to load executable for '%s'.\n", progname);
288 async_answer_0(rid, EINVAL);
289 return 1;
290 }
291
292 elf_set_pcb(&prog_info, &pcb);
293
294 pcb.cwd = cwd;
295
296 pcb.argc = argc;
297 pcb.argv = argv;
298
299 pcb.inbox = inbox;
300 pcb.inbox_entries = inbox_entries;
301
302 async_answer_0(rid, EOK);
303 return 0;
304}
305
306/** Run the previously loaded program.
307 *
308 * @param rid
309 * @param request
310 * @return 0 on success, !0 on error.
311 */
312static __attribute__((noreturn)) void ldr_run(ipc_callid_t rid,
313 ipc_call_t *request)
314{
315 DPRINTF("Set task name\n");
316
317 /* Set the task name. */
318 task_set_name(progname);
319
320 /* Run program */
321 DPRINTF("Reply OK\n");
322 async_answer_0(rid, EOK);
323 DPRINTF("Jump to entry point at %p\n", pcb.entry);
324 entry_point_jmp(prog_info.finfo.entry, &pcb);
325
326 /* Not reached */
327}
328
329/** Handle loader connection.
330 *
331 * Receive and carry out commands (of which the last one should be
332 * to execute the loaded program).
333 */
334static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
335{
336 /* Already have a connection? */
337 if (connected) {
338 async_answer_0(iid, ELIMIT);
339 return;
340 }
341
342 connected = true;
343
344 /* Accept the connection */
345 async_answer_0(iid, EOK);
346
347 /* Ignore parameters, the connection is already open */
348 (void) icall;
349
350 while (true) {
351 errno_t retval;
352 ipc_call_t call;
353 ipc_callid_t callid = async_get_call(&call);
354
355 if (!IPC_GET_IMETHOD(call))
356 exit(0);
357
358 switch (IPC_GET_IMETHOD(call)) {
359 case LOADER_GET_TASKID:
360 ldr_get_taskid(callid, &call);
361 continue;
362 case LOADER_SET_CWD:
363 ldr_set_cwd(callid, &call);
364 continue;
365 case LOADER_SET_PROGRAM:
366 ldr_set_program(callid, &call);
367 continue;
368 case LOADER_SET_ARGS:
369 ldr_set_args(callid, &call);
370 continue;
371 case LOADER_ADD_INBOX:
372 ldr_add_inbox(callid, &call);
373 continue;
374 case LOADER_LOAD:
375 ldr_load(callid, &call);
376 continue;
377 case LOADER_RUN:
378 ldr_run(callid, &call);
379 /* Not reached */
380 default:
381 retval = EINVAL;
382 break;
383 }
384
385 async_answer_0(callid, retval);
386 }
387}
388
389/** Program loader main function.
390 */
391int main(int argc, char *argv[])
392{
393 async_set_fallback_port_handler(ldr_connection, NULL);
394
395 /* Introduce this task to the NS (give it our task ID). */
396 task_id_t id = task_get_id();
397 errno_t rc = ns_intro(id);
398 if (rc != EOK)
399 return rc;
400
401 /* Create port */
402 port_id_t port;
403 rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
404 if (rc != EOK)
405 return rc;
406
407 /* Register at naming service. */
408 rc = service_register(SERVICE_LOADER);
409 if (rc != EOK)
410 return rc;
411
412 async_manager();
413
414 /* Never reached */
415 return 0;
416}
417
418/** @}
419 */
Note: See TracBrowser for help on using the repository browser.