source: mainline/uspace/srv/loader/main.c@ 153c7a29

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 153c7a29 was 153c7a29, checked in by Jiri Svoboda <jiri@…>, 9 years ago

Since dlopen() sets up runtime_env, we would no longer use the static TLS. Thus set up runtime_env right away and convert static TLS to dynamic TLS.

  • Property mode set to 100644
File size: 8.5 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 async_exch_t *vfs_exch = vfs_exchange_begin();
227
228 for (filc = 0; filc < count; filc++) {
229 ipc_callid_t callid;
[27b76ca]230 int fd;
[7171760]231
232 if (!async_state_change_receive(&callid, NULL, NULL, NULL)) {
233 async_answer_0(callid, EINVAL);
234 break;
[472c09d]235 }
[7171760]236 async_state_change_finalize(callid, vfs_exch);
[6afc9d7]237 fd = vfs_fd_wait();
[27b76ca]238 assert(fd == (int) filc);
[bbdbf86]239 }
[7171760]240
241 vfs_exchange_end(vfs_exch);
242
[ffa2c8ef]243 async_answer_0(rid, EOK);
[bbdbf86]244}
245
[4470e26]246/** Load the previously selected program.
[c98e6ee]247 *
248 * @param rid
249 * @param request
250 * @return 0 on success, !0 on error.
251 */
[bbdbf86]252static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
[c98e6ee]253{
254 int rc;
[2f57690]255
[17341d4]256 rc = elf_load(pathname, &prog_info);
[409b0d6]257 if (rc != EE_OK) {
[06b2b7f]258 DPRINTF("Failed to load executable '%s'.\n", pathname);
[ffa2c8ef]259 async_answer_0(rid, EINVAL);
[c98e6ee]260 return 1;
261 }
[2f57690]262
[17341d4]263 elf_set_pcb(&prog_info, &pcb);
[2f57690]264
[622cdbe]265 pcb.cwd = cwd;
266
[c98e6ee]267 pcb.argc = argc;
268 pcb.argv = argv;
[2f57690]269
[bbdbf86]270 pcb.filc = filc;
271
[a6dffb8]272 async_answer_0(rid, rc);
[c98e6ee]273 return 0;
274}
275
[4470e26]276/** Run the previously loaded program.
277 *
278 * @param rid
279 * @param request
280 * @return 0 on success, !0 on error.
281 */
[bbdbf86]282static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
[4470e26]283{
[20f1597]284 const char *cp;
[2f57690]285
[a6dffb8]286 DPRINTF("Set task name\n");
287
[bc18d63]288 /* Set the task name. */
[7afb4a5]289 cp = str_rchr(pathname, '/');
[20f1597]290 cp = (cp == NULL) ? pathname : (cp + 1);
291 task_set_name(cp);
[2f57690]292
[a6dffb8]293 /* Run program */
294 DPRINTF("Reply OK\n");
295 async_answer_0(rid, EOK);
296 DPRINTF("Jump to entry point at %p\n", pcb.entry);
[17341d4]297 entry_point_jmp(prog_info.finfo.entry, &pcb);
[bbdbf86]298
[4470e26]299 /* Not reached */
300}
301
[c98e6ee]302/** Handle loader connection.
303 *
304 * Receive and carry out commands (of which the last one should be
305 * to execute the loaded program).
306 */
[9934f7d]307static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c98e6ee]308{
[bfd1546]309 /* Already have a connection? */
310 if (connected) {
[ffa2c8ef]311 async_answer_0(iid, ELIMIT);
[bfd1546]312 return;
313 }
[2f57690]314
[bfd1546]315 connected = true;
316
317 /* Accept the connection */
[ffa2c8ef]318 async_answer_0(iid, EOK);
[2f57690]319
[c98e6ee]320 /* Ignore parameters, the connection is already open */
[2f57690]321 (void) icall;
322
[79ae36dd]323 while (true) {
324 int retval;
325 ipc_call_t call;
326 ipc_callid_t callid = async_get_call(&call);
[2f57690]327
[79ae36dd]328 if (!IPC_GET_IMETHOD(call))
[86e3d62]329 exit(0);
[79ae36dd]330
331 switch (IPC_GET_IMETHOD(call)) {
[47e0a05b]332 case LOADER_GET_TASKID:
[bbdbf86]333 ldr_get_taskid(callid, &call);
[47e0a05b]334 continue;
[622cdbe]335 case LOADER_SET_CWD:
336 ldr_set_cwd(callid, &call);
337 continue;
[c98e6ee]338 case LOADER_SET_PATHNAME:
[bbdbf86]339 ldr_set_pathname(callid, &call);
[c98e6ee]340 continue;
341 case LOADER_SET_ARGS:
[bbdbf86]342 ldr_set_args(callid, &call);
343 continue;
344 case LOADER_SET_FILES:
345 ldr_set_files(callid, &call);
[4470e26]346 continue;
347 case LOADER_LOAD:
[bbdbf86]348 ldr_load(callid, &call);
[4470e26]349 continue;
[c98e6ee]350 case LOADER_RUN:
[bbdbf86]351 ldr_run(callid, &call);
[4470e26]352 /* Not reached */
[c98e6ee]353 default:
[8c3bc75]354 retval = EINVAL;
[c98e6ee]355 break;
356 }
[8c3bc75]357
[79ae36dd]358 async_answer_0(callid, retval);
[c98e6ee]359 }
360}
361
362/** Program loader main function.
363 */
364int main(int argc, char *argv[])
365{
[b688fd8]366 async_set_fallback_port_handler(ldr_connection, NULL);
[007e6efa]367
[5d96851b]368 /* Introduce this task to the NS (give it our task ID). */
[007e6efa]369 task_id_t id = task_get_id();
[79ae36dd]370 int rc = ns_intro(id);
[5d96851b]371 if (rc != EOK)
[b39b5cb]372 return rc;
[2f57690]373
[566992e1]374 /* Create port */
375 port_id_t port;
376 rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
377 if (rc != EOK)
378 return rc;
379
[bfd1546]380 /* Register at naming service. */
[566992e1]381 rc = service_register(SERVICE_LOADER);
[b39b5cb]382 if (rc != EOK)
383 return rc;
[007e6efa]384
[c98e6ee]385 async_manager();
[2f57690]386
[bfd1546]387 /* Never reached */
[c98e6ee]388 return 0;
389}
390
391/** @}
392 */
Note: See TracBrowser for help on using the repository browser.