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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4122410 was 4122410, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve Doxygen documentaion

This is stil WiP. A number of libraries, drivers and services were
converted to using a more hierarchical and decentralized scheme when it
comes to specifying to which doxygen group they belong.

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[c98e6ee]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 * @{
[2f57690]31 */
[c98e6ee]32/**
33 * @file
34 *
35 * The program loader is a special init binary. Its image is used
36 * to create a new task upon a @c task_spawn syscall. The syscall
37 * returns the id of a phone connected to the newly created task.
38 *
39 * The caller uses this phone to send the pathname and various other
40 * information to the loader. This is normally done by the C library
41 * and completely hidden from applications.
42 */
43
44#include <stdio.h>
45#include <stdlib.h>
[3e6a98c5]46#include <stdbool.h>
[8d2dd7f2]47#include <stddef.h>
[bfd1546]48#include <ipc/services.h>
[c98e6ee]49#include <ipc/loader.h>
[79ae36dd]50#include <ns.h>
[c98e6ee]51#include <loader/pcb.h>
[f798178]52#include <entry_point.h>
[c98e6ee]53#include <errno.h>
54#include <async.h>
[19f857a]55#include <str.h>
[c98e6ee]56#include <as.h>
[90b8d58]57#include <elf/elf.h>
[bfdb5af1]58#include <elf/elf_load.h>
[7171760]59#include <vfs/vfs.h>
[bb9ec2d]60#include <vfs/inbox.h>
[c98e6ee]61
[40abf56]62#ifdef CONFIG_RTLD
63#include <rtld/rtld.h>
64#endif
65
[e3787a0]66#define DPRINTF(...) ((void) 0)
[1ea99cc]67
[bb9ec2d]68/** File that will be loaded */
69static char *progname = NULL;
70static int program_fd = -1;
[c98e6ee]71
72/** The Program control block */
73static pcb_t pcb;
74
[622cdbe]75/** Current working directory */
76static char *cwd = NULL;
77
[c98e6ee]78/** Number of arguments */
79static int argc = 0;
80/** Argument vector */
81static char **argv = NULL;
82/** Buffer holding all arguments */
83static char *arg_buf = NULL;
84
[bb9ec2d]85/** Inbox entries. */
86static struct pcb_inbox_entry inbox[INBOX_MAX_ENTRIES];
87static int inbox_entries = 0;
[bbdbf86]88
[4470e26]89static elf_info_t prog_info;
90
[bfd1546]91/** Used to limit number of connections to one. */
[007e6efa]92static bool connected = false;
[4470e26]93
[984a9ba]94static void ldr_get_taskid(ipc_call_t *req)
[47e0a05b]95{
[984a9ba]96 ipc_call_t call;
[47e0a05b]97 task_id_t task_id;
98 size_t len;
[a35b458]99
[47e0a05b]100 task_id = task_get_id();
[a35b458]101
[984a9ba]102 if (!async_data_read_receive(&call, &len)) {
103 async_answer_0(&call, EINVAL);
104 async_answer_0(req, EINVAL);
[47e0a05b]105 return;
106 }
[a35b458]107
[2f57690]108 if (len > sizeof(task_id))
109 len = sizeof(task_id);
[a35b458]110
[e3787a0]111 DPRINTF("LOADER_GET_TASKID() = %lu\n", (unsigned long) task_id);
[984a9ba]112 async_data_read_finalize(&call, &task_id, len);
113 async_answer_0(req, EOK);
[47e0a05b]114}
115
[622cdbe]116/** Receive a call setting the current working directory.
[c98e6ee]117 *
118 */
[984a9ba]119static void ldr_set_cwd(ipc_call_t *req)
[c98e6ee]120{
[472c09d]121 char *buf;
[b7fd2a0]122 errno_t rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
[a35b458]123
[472c09d]124 if (rc == EOK) {
125 if (cwd != NULL)
126 free(cwd);
[a35b458]127
[472c09d]128 cwd = buf;
[c98e6ee]129 }
[a35b458]130
[e3787a0]131 DPRINTF("LOADER_SET_CWD('%s')\n", cwd);
[984a9ba]132 async_answer_0(req, rc);
[622cdbe]133}
[47e0a05b]134
[bb9ec2d]135/** Receive a call setting the program to execute.
[c98e6ee]136 *
137 */
[984a9ba]138static void ldr_set_program(ipc_call_t *req)
[c98e6ee]139{
[984a9ba]140 ipc_call_t call;
[bb9ec2d]141 size_t namesize;
[984a9ba]142 if (!async_data_write_receive(&call, &namesize)) {
143 async_answer_0(req, EINVAL);
[bb9ec2d]144 return;
145 }
146
[3bacee1]147 char *name = malloc(namesize);
[984a9ba]148 // FIXME: check return value
149
150 errno_t rc = async_data_write_finalize(&call, name, namesize);
[bb9ec2d]151 if (rc != EOK) {
[984a9ba]152 async_answer_0(req, EINVAL);
[bb9ec2d]153 return;
154 }
155
[f77c1c9]156 int file;
157 rc = vfs_receive_handle(true, &file);
158 if (rc != EOK) {
[984a9ba]159 async_answer_0(req, EINVAL);
[bb9ec2d]160 return;
[c98e6ee]161 }
[a35b458]162
[e3787a0]163 DPRINTF("LOADER_SET_PROGRAM('%s')\n", name);
164
[bb9ec2d]165 progname = name;
166 program_fd = file;
[984a9ba]167 async_answer_0(req, EOK);
[c98e6ee]168}
169
170/** Receive a call setting arguments of the program to execute.
171 *
172 */
[984a9ba]173static void ldr_set_args(ipc_call_t *req)
[c98e6ee]174{
[472c09d]175 char *buf;
176 size_t buf_size;
[b7fd2a0]177 errno_t rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
[a35b458]178
[472c09d]179 if (rc == EOK) {
180 /*
181 * Count number of arguments
182 */
183 char *cur = buf;
184 int count = 0;
[a35b458]185
[472c09d]186 while (cur < buf + buf_size) {
187 size_t arg_size = str_size(cur);
188 cur += arg_size + 1;
189 count++;
190 }
[a35b458]191
[472c09d]192 /*
193 * Allocate new argv
194 */
195 char **_argv = (char **) malloc((count + 1) * sizeof(char *));
196 if (_argv == NULL) {
197 free(buf);
[984a9ba]198 async_answer_0(req, ENOMEM);
[472c09d]199 return;
200 }
[a35b458]201
[472c09d]202 /*
203 * Fill the new argv with argument pointers
204 */
205 cur = buf;
206 count = 0;
207 while (cur < buf + buf_size) {
208 _argv[count] = cur;
[a35b458]209
[472c09d]210 size_t arg_size = str_size(cur);
211 cur += arg_size + 1;
212 count++;
213 }
214 _argv[count] = NULL;
[a35b458]215
[472c09d]216 /*
217 * Copy temporary data to global variables
218 */
219 if (arg_buf != NULL)
220 free(arg_buf);
[a35b458]221
[472c09d]222 if (argv != NULL)
223 free(argv);
[a35b458]224
[e3787a0]225 for (int i = 0; i < count; i++)
226 DPRINTF("LOADER_SET_ARGS('%s')\n", _argv[i]);
227
[472c09d]228 argc = count;
229 arg_buf = buf;
230 argv = _argv;
[c98e6ee]231 }
[a35b458]232
[984a9ba]233 async_answer_0(req, rc);
[c98e6ee]234}
235
[bb9ec2d]236/** Receive a call setting inbox files of the program to execute.
[bbdbf86]237 *
238 */
[984a9ba]239static void ldr_add_inbox(ipc_call_t *req)
[bbdbf86]240{
[bb9ec2d]241 if (inbox_entries == INBOX_MAX_ENTRIES) {
[984a9ba]242 async_answer_0(req, ERANGE);
[b7f69f2]243 return;
[bb9ec2d]244 }
[7171760]245
[984a9ba]246 ipc_call_t call;
[bb9ec2d]247 size_t namesize;
[984a9ba]248 if (!async_data_write_receive(&call, &namesize)) {
249 async_answer_0(req, EINVAL);
[bb9ec2d]250 return;
251 }
252
[3bacee1]253 char *name = malloc(namesize);
[984a9ba]254 errno_t rc = async_data_write_finalize(&call, name, namesize);
[bb9ec2d]255 if (rc != EOK) {
[984a9ba]256 async_answer_0(req, EINVAL);
[bb9ec2d]257 return;
258 }
259
[f77c1c9]260 int file;
261 rc = vfs_receive_handle(true, &file);
262 if (rc != EOK) {
[984a9ba]263 async_answer_0(req, EINVAL);
[bb9ec2d]264 return;
[bbdbf86]265 }
[7171760]266
[e3787a0]267 DPRINTF("LOADER_ADD_INBOX('%s')\n", name);
268
[ea56098]269 /*
270 * We need to set the root early for dynamically linked binaries so
271 * that the loader can use it too.
272 */
273 if (str_cmp(name, "root") == 0)
274 vfs_root_set(file);
275
[bb9ec2d]276 inbox[inbox_entries].name = name;
277 inbox[inbox_entries].file = file;
278 inbox_entries++;
[984a9ba]279 async_answer_0(req, EOK);
[bbdbf86]280}
281
[4470e26]282/** Load the previously selected program.
[c98e6ee]283 *
284 * @return 0 on success, !0 on error.
[984a9ba]285 *
[c98e6ee]286 */
[984a9ba]287static int ldr_load(ipc_call_t *req)
[c98e6ee]288{
[e3787a0]289 DPRINTF("LOADER_LOAD()\n");
290
[bb9ec2d]291 int rc = elf_load(program_fd, &prog_info);
[409b0d6]292 if (rc != EE_OK) {
[bb9ec2d]293 DPRINTF("Failed to load executable for '%s'.\n", progname);
[984a9ba]294 async_answer_0(req, EINVAL);
[c98e6ee]295 return 1;
296 }
[a35b458]297
[e3787a0]298 DPRINTF("Loaded.\n");
299
[40abf56]300#ifdef CONFIG_RTLD
301 if (prog_info.env) {
302 pcb.tcb = rtld_tls_make(prog_info.env);
303 } else {
304 pcb.tcb = tls_make(prog_info.finfo.base);
305 }
306#else
307 pcb.tcb = tls_make(prog_info.finfo.base);
308#endif
309
[17341d4]310 elf_set_pcb(&prog_info, &pcb);
[a35b458]311
[e3787a0]312 DPRINTF("PCB set.\n");
313
[622cdbe]314 pcb.cwd = cwd;
[a35b458]315
[c98e6ee]316 pcb.argc = argc;
317 pcb.argv = argv;
[a35b458]318
[bb9ec2d]319 pcb.inbox = inbox;
320 pcb.inbox_entries = inbox_entries;
[a35b458]321
[e3787a0]322 DPRINTF("Answering.\n");
[984a9ba]323 async_answer_0(req, EOK);
[c98e6ee]324 return 0;
325}
326
[4470e26]327/** Run the previously loaded program.
328 *
329 * @return 0 on success, !0 on error.
[984a9ba]330 *
[4470e26]331 */
[984a9ba]332static __attribute__((noreturn)) void ldr_run(ipc_call_t *req)
[4470e26]333{
[a6dffb8]334 DPRINTF("Set task name\n");
335
[bc18d63]336 /* Set the task name. */
[bb9ec2d]337 task_set_name(progname);
[a35b458]338
[a6dffb8]339 /* Run program */
340 DPRINTF("Reply OK\n");
[984a9ba]341 async_answer_0(req, EOK);
[40abf56]342
[a6dffb8]343 DPRINTF("Jump to entry point at %p\n", pcb.entry);
[40abf56]344
345 __tcb_reset();
[17341d4]346 entry_point_jmp(prog_info.finfo.entry, &pcb);
[a35b458]347
[4470e26]348 /* Not reached */
349}
350
[c98e6ee]351/** Handle loader connection.
352 *
353 * Receive and carry out commands (of which the last one should be
354 * to execute the loaded program).
[984a9ba]355 *
[c98e6ee]356 */
[984a9ba]357static void ldr_connection(ipc_call_t *icall, void *arg)
[c98e6ee]358{
[bfd1546]359 /* Already have a connection? */
360 if (connected) {
[984a9ba]361 async_answer_0(icall, ELIMIT);
[bfd1546]362 return;
363 }
[a35b458]364
[bfd1546]365 connected = true;
[a35b458]366
[bfd1546]367 /* Accept the connection */
[984a9ba]368 async_answer_0(icall, EOK);
[a35b458]369
[c98e6ee]370 /* Ignore parameters, the connection is already open */
[2f57690]371 (void) icall;
[a35b458]372
[79ae36dd]373 while (true) {
[b7fd2a0]374 errno_t retval;
[79ae36dd]375 ipc_call_t call;
[984a9ba]376 async_get_call(&call);
[a35b458]377
[79ae36dd]378 if (!IPC_GET_IMETHOD(call))
[86e3d62]379 exit(0);
[a35b458]380
[79ae36dd]381 switch (IPC_GET_IMETHOD(call)) {
[47e0a05b]382 case LOADER_GET_TASKID:
[984a9ba]383 ldr_get_taskid(&call);
[47e0a05b]384 continue;
[622cdbe]385 case LOADER_SET_CWD:
[984a9ba]386 ldr_set_cwd(&call);
[622cdbe]387 continue;
[bb9ec2d]388 case LOADER_SET_PROGRAM:
[984a9ba]389 ldr_set_program(&call);
[c98e6ee]390 continue;
391 case LOADER_SET_ARGS:
[984a9ba]392 ldr_set_args(&call);
[bbdbf86]393 continue;
[bb9ec2d]394 case LOADER_ADD_INBOX:
[984a9ba]395 ldr_add_inbox(&call);
[4470e26]396 continue;
397 case LOADER_LOAD:
[984a9ba]398 ldr_load(&call);
[4470e26]399 continue;
[c98e6ee]400 case LOADER_RUN:
[984a9ba]401 ldr_run(&call);
[4470e26]402 /* Not reached */
[c98e6ee]403 default:
[8c3bc75]404 retval = EINVAL;
[c98e6ee]405 break;
406 }
[a35b458]407
[984a9ba]408 async_answer_0(&call, retval);
[c98e6ee]409 }
410}
411
412/** Program loader main function.
413 */
414int main(int argc, char *argv[])
415{
[5d96851b]416 /* Introduce this task to the NS (give it our task ID). */
[007e6efa]417 task_id_t id = task_get_id();
[b7fd2a0]418 errno_t rc = ns_intro(id);
[5d96851b]419 if (rc != EOK)
[b39b5cb]420 return rc;
[a35b458]421
[bfd1546]422 /* Register at naming service. */
[9b1baac]423 rc = service_register(SERVICE_LOADER, INTERFACE_LOADER,
424 ldr_connection, NULL);
[b39b5cb]425 if (rc != EOK)
426 return rc;
[a35b458]427
[c98e6ee]428 async_manager();
[a35b458]429
[bfd1546]430 /* Never reached */
[c98e6ee]431 return 0;
432}
433
434/** @}
435 */
Note: See TracBrowser for help on using the repository browser.