source: mainline/uspace/srv/loader/main.c@ 9c96634

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

Reduce the number of files that include <sys/types.h>

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