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

Last change on this file since 2482192 was 2482192, checked in by Matěj Volf <git@…>, 4 months ago

fix initialization of fibril.exit_hooks

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