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