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