source: mainline/uspace/srv/loader/main.c@ 967e7a1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 967e7a1 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 9.4 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
[40abf56]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
[bb9ec2d]292 int rc = elf_load(program_fd, &prog_info);
[409b0d6]293 if (rc != EE_OK) {
[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
[40abf56]301#ifdef CONFIG_RTLD
302 if (prog_info.env) {
303 pcb.tcb = rtld_tls_make(prog_info.env);
304 } else {
305 pcb.tcb = tls_make(prog_info.finfo.base);
306 }
307#else
308 pcb.tcb = tls_make(prog_info.finfo.base);
309#endif
310
[c74b9de]311 if (!pcb.tcb) {
312 DPRINTF("Failed to make TLS for '%s'.\n", progname);
313 async_answer_0(req, ENOMEM);
314 return 1;
315 }
316
[17341d4]317 elf_set_pcb(&prog_info, &pcb);
[a35b458]318
[e3787a0]319 DPRINTF("PCB set.\n");
320
[622cdbe]321 pcb.cwd = cwd;
[a35b458]322
[c98e6ee]323 pcb.argc = argc;
324 pcb.argv = argv;
[a35b458]325
[bb9ec2d]326 pcb.inbox = inbox;
327 pcb.inbox_entries = inbox_entries;
[a35b458]328
[e3787a0]329 DPRINTF("Answering.\n");
[984a9ba]330 async_answer_0(req, EOK);
[c98e6ee]331 return 0;
332}
333
[4470e26]334/** Run the previously loaded program.
335 *
336 * @return 0 on success, !0 on error.
[984a9ba]337 *
[4470e26]338 */
[984a9ba]339static __attribute__((noreturn)) void ldr_run(ipc_call_t *req)
[4470e26]340{
[a6dffb8]341 DPRINTF("Set task name\n");
342
[bc18d63]343 /* Set the task name. */
[bb9ec2d]344 task_set_name(progname);
[a35b458]345
[a6dffb8]346 /* Run program */
347 DPRINTF("Reply OK\n");
[984a9ba]348 async_answer_0(req, EOK);
[40abf56]349
[faf19d4]350 /*
351 * Wait for the hangup from the other side in order not to leave any
352 * unanswered IPC_M_PHONE_HUNGUP messages behind.
353 */
354 async_get_call(req);
[fafb8e5]355 assert(!ipc_get_imethod(req));
[faf19d4]356 async_answer_0(req, EOK);
357
[a6dffb8]358 DPRINTF("Jump to entry point at %p\n", pcb.entry);
[40abf56]359
[25f6bddb]360 __libc_fini();
[40abf56]361 __tcb_reset();
[17341d4]362 entry_point_jmp(prog_info.finfo.entry, &pcb);
[a35b458]363
[4470e26]364 /* Not reached */
365}
366
[c98e6ee]367/** Handle loader connection.
368 *
369 * Receive and carry out commands (of which the last one should be
370 * to execute the loaded program).
[984a9ba]371 *
[c98e6ee]372 */
[984a9ba]373static void ldr_connection(ipc_call_t *icall, void *arg)
[c98e6ee]374{
[bfd1546]375 /* Already have a connection? */
376 if (connected) {
[984a9ba]377 async_answer_0(icall, ELIMIT);
[bfd1546]378 return;
379 }
[a35b458]380
[bfd1546]381 connected = true;
[a35b458]382
[bfd1546]383 /* Accept the connection */
[beb83c1]384 async_accept_0(icall);
[a35b458]385
[c98e6ee]386 /* Ignore parameters, the connection is already open */
[2f57690]387 (void) icall;
[a35b458]388
[79ae36dd]389 while (true) {
[b7fd2a0]390 errno_t retval;
[79ae36dd]391 ipc_call_t call;
[984a9ba]392 async_get_call(&call);
[a35b458]393
[fafb8e5]394 if (!ipc_get_imethod(&call)) {
[889cdb1]395 async_answer_0(&call, EOK);
[86e3d62]396 exit(0);
[889cdb1]397 }
[a35b458]398
[fafb8e5]399 switch (ipc_get_imethod(&call)) {
[47e0a05b]400 case LOADER_GET_TASKID:
[984a9ba]401 ldr_get_taskid(&call);
[47e0a05b]402 continue;
[622cdbe]403 case LOADER_SET_CWD:
[984a9ba]404 ldr_set_cwd(&call);
[622cdbe]405 continue;
[bb9ec2d]406 case LOADER_SET_PROGRAM:
[984a9ba]407 ldr_set_program(&call);
[c98e6ee]408 continue;
409 case LOADER_SET_ARGS:
[984a9ba]410 ldr_set_args(&call);
[bbdbf86]411 continue;
[bb9ec2d]412 case LOADER_ADD_INBOX:
[984a9ba]413 ldr_add_inbox(&call);
[4470e26]414 continue;
415 case LOADER_LOAD:
[984a9ba]416 ldr_load(&call);
[4470e26]417 continue;
[c98e6ee]418 case LOADER_RUN:
[984a9ba]419 ldr_run(&call);
[4470e26]420 /* Not reached */
[c98e6ee]421 default:
[8c3bc75]422 retval = EINVAL;
[c98e6ee]423 break;
424 }
[a35b458]425
[984a9ba]426 async_answer_0(&call, retval);
[c98e6ee]427 }
428}
429
430/** Program loader main function.
431 */
432int main(int argc, char *argv[])
433{
[5d96851b]434 /* Introduce this task to the NS (give it our task ID). */
[007e6efa]435 task_id_t id = task_get_id();
[b7fd2a0]436 errno_t rc = ns_intro(id);
[5d96851b]437 if (rc != EOK)
[b39b5cb]438 return rc;
[a35b458]439
[bfd1546]440 /* Register at naming service. */
[9b1baac]441 rc = service_register(SERVICE_LOADER, INTERFACE_LOADER,
442 ldr_connection, NULL);
[b39b5cb]443 if (rc != EOK)
444 return rc;
[a35b458]445
[c98e6ee]446 async_manager();
[a35b458]447
[bfd1546]448 /* Never reached */
[c98e6ee]449 return 0;
450}
451
452/** @}
453 */
Note: See TracBrowser for help on using the repository browser.