source: mainline/uspace/srv/loader/main.c@ 930f5c3

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

Cherry-pick a change from lp:~zarevucky-jiri/helenos/vfs-2.5 r1937

  • Property mode set to 100644
File size: 8.2 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>
48#include <unistd.h>
[3e6a98c5]49#include <stdbool.h>
[c98e6ee]50#include <fcntl.h>
51#include <sys/types.h>
[bfd1546]52#include <ipc/services.h>
[c98e6ee]53#include <ipc/loader.h>
[79ae36dd]54#include <ns.h>
[c98e6ee]55#include <loader/pcb.h>
[f798178]56#include <entry_point.h>
[c98e6ee]57#include <errno.h>
58#include <async.h>
[19f857a]59#include <str.h>
[c98e6ee]60#include <as.h>
[90b8d58]61#include <elf/elf.h>
[bfdb5af1]62#include <elf/elf_load.h>
[7171760]63#include <vfs/vfs.h>
[c98e6ee]64
[7fb3f1c]65#define DPRINTF(...)
[1ea99cc]66
[c98e6ee]67/** Pathname of the file that will be loaded */
68static char *pathname = NULL;
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
[bbdbf86]83/** Number of preset files */
[7171760]84static unsigned int filc = 0;
[bbdbf86]85
[4470e26]86static elf_info_t prog_info;
87
[bfd1546]88/** Used to limit number of connections to one. */
[007e6efa]89static bool connected = false;
[4470e26]90
[bbdbf86]91static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
[47e0a05b]92{
93 ipc_callid_t callid;
94 task_id_t task_id;
95 size_t len;
[2f57690]96
[47e0a05b]97 task_id = task_get_id();
[2f57690]98
[0da4e41]99 if (!async_data_read_receive(&callid, &len)) {
[ffa2c8ef]100 async_answer_0(callid, EINVAL);
101 async_answer_0(rid, EINVAL);
[47e0a05b]102 return;
103 }
[2f57690]104
105 if (len > sizeof(task_id))
106 len = sizeof(task_id);
107
[0da4e41]108 async_data_read_finalize(callid, &task_id, len);
[ffa2c8ef]109 async_answer_0(rid, EOK);
[47e0a05b]110}
111
[622cdbe]112/** Receive a call setting the current working directory.
[c98e6ee]113 *
114 * @param rid
115 * @param request
116 */
[622cdbe]117static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
[c98e6ee]118{
[472c09d]119 char *buf;
[eda925a]120 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
[2f57690]121
[472c09d]122 if (rc == EOK) {
123 if (cwd != NULL)
124 free(cwd);
125
126 cwd = buf;
[c98e6ee]127 }
[2f57690]128
[ffa2c8ef]129 async_answer_0(rid, rc);
[622cdbe]130}
[47e0a05b]131
[c98e6ee]132/** Receive a call setting pathname of the program to execute.
133 *
134 * @param rid
135 * @param request
136 */
[bbdbf86]137static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
[c98e6ee]138{
[472c09d]139 char *buf;
[eda925a]140 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
[2f57690]141
[472c09d]142 if (rc == EOK) {
143 if (pathname != NULL)
144 free(pathname);
145
146 pathname = buf;
[c98e6ee]147 }
[2f57690]148
[ffa2c8ef]149 async_answer_0(rid, rc);
[c98e6ee]150}
151
152/** Receive a call setting arguments of the program to execute.
153 *
154 * @param rid
155 * @param request
156 */
[bbdbf86]157static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
[c98e6ee]158{
[472c09d]159 char *buf;
160 size_t buf_size;
[eda925a]161 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
[472c09d]162
163 if (rc == EOK) {
164 /*
165 * Count number of arguments
166 */
167 char *cur = buf;
168 int count = 0;
169
170 while (cur < buf + buf_size) {
171 size_t arg_size = str_size(cur);
172 cur += arg_size + 1;
173 count++;
174 }
[2f57690]175
[472c09d]176 /*
177 * Allocate new argv
178 */
179 char **_argv = (char **) malloc((count + 1) * sizeof(char *));
180 if (_argv == NULL) {
181 free(buf);
[ffa2c8ef]182 async_answer_0(rid, ENOMEM);
[472c09d]183 return;
184 }
185
186 /*
187 * Fill the new argv with argument pointers
188 */
189 cur = buf;
190 count = 0;
191 while (cur < buf + buf_size) {
192 _argv[count] = cur;
193
194 size_t arg_size = str_size(cur);
195 cur += arg_size + 1;
196 count++;
197 }
198 _argv[count] = NULL;
[2f57690]199
[472c09d]200 /*
201 * Copy temporary data to global variables
202 */
203 if (arg_buf != NULL)
204 free(arg_buf);
205
206 if (argv != NULL)
207 free(argv);
208
209 argc = count;
210 arg_buf = buf;
211 argv = _argv;
[c98e6ee]212 }
[2f57690]213
[ffa2c8ef]214 async_answer_0(rid, rc);
[c98e6ee]215}
216
[bbdbf86]217/** Receive a call setting preset files of the program to execute.
218 *
219 * @param rid
220 * @param request
221 */
222static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request)
223{
[7171760]224 size_t count = IPC_GET_ARG1(*request);
225
226 for (filc = 0; filc < count; filc++) {
[354b642]227 int fd = vfs_receive_handle();
228 if (fd < 0) {
[7171760]229 break;
[472c09d]230 }
[27b76ca]231 assert(fd == (int) filc);
[bbdbf86]232 }
[7171760]233
[ffa2c8ef]234 async_answer_0(rid, EOK);
[bbdbf86]235}
236
[4470e26]237/** Load the previously selected program.
[c98e6ee]238 *
239 * @param rid
240 * @param request
241 * @return 0 on success, !0 on error.
242 */
[bbdbf86]243static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
[c98e6ee]244{
245 int rc;
[2f57690]246
[17341d4]247 rc = elf_load(pathname, &prog_info);
[409b0d6]248 if (rc != EE_OK) {
[06b2b7f]249 DPRINTF("Failed to load executable '%s'.\n", pathname);
[ffa2c8ef]250 async_answer_0(rid, EINVAL);
[c98e6ee]251 return 1;
252 }
[2f57690]253
[17341d4]254 elf_set_pcb(&prog_info, &pcb);
[2f57690]255
[622cdbe]256 pcb.cwd = cwd;
257
[c98e6ee]258 pcb.argc = argc;
259 pcb.argv = argv;
[2f57690]260
[bbdbf86]261 pcb.filc = filc;
262
[a6dffb8]263 async_answer_0(rid, rc);
[c98e6ee]264 return 0;
265}
266
[4470e26]267/** Run the previously loaded program.
268 *
269 * @param rid
270 * @param request
271 * @return 0 on success, !0 on error.
272 */
[bbdbf86]273static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
[4470e26]274{
[20f1597]275 const char *cp;
[2f57690]276
[a6dffb8]277 DPRINTF("Set task name\n");
278
[bc18d63]279 /* Set the task name. */
[7afb4a5]280 cp = str_rchr(pathname, '/');
[20f1597]281 cp = (cp == NULL) ? pathname : (cp + 1);
282 task_set_name(cp);
[2f57690]283
[a6dffb8]284 /* Run program */
285 DPRINTF("Reply OK\n");
286 async_answer_0(rid, EOK);
287 DPRINTF("Jump to entry point at %p\n", pcb.entry);
[17341d4]288 entry_point_jmp(prog_info.finfo.entry, &pcb);
[bbdbf86]289
[4470e26]290 /* Not reached */
291}
292
[c98e6ee]293/** Handle loader connection.
294 *
295 * Receive and carry out commands (of which the last one should be
296 * to execute the loaded program).
297 */
[9934f7d]298static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c98e6ee]299{
[bfd1546]300 /* Already have a connection? */
301 if (connected) {
[ffa2c8ef]302 async_answer_0(iid, ELIMIT);
[bfd1546]303 return;
304 }
[2f57690]305
[bfd1546]306 connected = true;
307
308 /* Accept the connection */
[ffa2c8ef]309 async_answer_0(iid, EOK);
[2f57690]310
[c98e6ee]311 /* Ignore parameters, the connection is already open */
[2f57690]312 (void) icall;
313
[79ae36dd]314 while (true) {
315 int retval;
316 ipc_call_t call;
317 ipc_callid_t callid = async_get_call(&call);
[2f57690]318
[79ae36dd]319 if (!IPC_GET_IMETHOD(call))
[86e3d62]320 exit(0);
[79ae36dd]321
322 switch (IPC_GET_IMETHOD(call)) {
[47e0a05b]323 case LOADER_GET_TASKID:
[bbdbf86]324 ldr_get_taskid(callid, &call);
[47e0a05b]325 continue;
[622cdbe]326 case LOADER_SET_CWD:
327 ldr_set_cwd(callid, &call);
328 continue;
[c98e6ee]329 case LOADER_SET_PATHNAME:
[bbdbf86]330 ldr_set_pathname(callid, &call);
[c98e6ee]331 continue;
332 case LOADER_SET_ARGS:
[bbdbf86]333 ldr_set_args(callid, &call);
334 continue;
335 case LOADER_SET_FILES:
336 ldr_set_files(callid, &call);
[4470e26]337 continue;
338 case LOADER_LOAD:
[bbdbf86]339 ldr_load(callid, &call);
[4470e26]340 continue;
[c98e6ee]341 case LOADER_RUN:
[bbdbf86]342 ldr_run(callid, &call);
[4470e26]343 /* Not reached */
[c98e6ee]344 default:
[8c3bc75]345 retval = EINVAL;
[c98e6ee]346 break;
347 }
[8c3bc75]348
[79ae36dd]349 async_answer_0(callid, retval);
[c98e6ee]350 }
351}
352
353/** Program loader main function.
354 */
355int main(int argc, char *argv[])
356{
[b688fd8]357 async_set_fallback_port_handler(ldr_connection, NULL);
[007e6efa]358
[5d96851b]359 /* Introduce this task to the NS (give it our task ID). */
[007e6efa]360 task_id_t id = task_get_id();
[79ae36dd]361 int rc = ns_intro(id);
[5d96851b]362 if (rc != EOK)
[b39b5cb]363 return rc;
[2f57690]364
[566992e1]365 /* Create port */
366 port_id_t port;
367 rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
368 if (rc != EOK)
369 return rc;
370
[bfd1546]371 /* Register at naming service. */
[566992e1]372 rc = service_register(SERVICE_LOADER);
[b39b5cb]373 if (rc != EOK)
374 return rc;
[007e6efa]375
[c98e6ee]376 async_manager();
[2f57690]377
[bfd1546]378 /* Never reached */
[c98e6ee]379 return 0;
380}
381
382/** @}
383 */
Note: See TracBrowser for help on using the repository browser.