source: mainline/uspace/srv/loader/main.c@ 17341d4

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

Move rtld internals out of loader. Stop misusing rtld instance from current environment for loading dynamically linked executables.

  • 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;
[17341d4]271 printf("dynamic=%p rtld_env=%p\n", pcb.dynamic, pcb.rtld_runtime);
[bbdbf86]272
[a6dffb8]273 async_answer_0(rid, rc);
[c98e6ee]274 return 0;
275}
276
[4470e26]277/** Run the previously loaded program.
278 *
279 * @param rid
280 * @param request
281 * @return 0 on success, !0 on error.
282 */
[bbdbf86]283static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
[4470e26]284{
[20f1597]285 const char *cp;
[2f57690]286
[a6dffb8]287 DPRINTF("Set task name\n");
288
[bc18d63]289 /* Set the task name. */
[7afb4a5]290 cp = str_rchr(pathname, '/');
[20f1597]291 cp = (cp == NULL) ? pathname : (cp + 1);
292 task_set_name(cp);
[2f57690]293
[a6dffb8]294 /* Run program */
295 DPRINTF("Reply OK\n");
296 async_answer_0(rid, EOK);
297 DPRINTF("Jump to entry point at %p\n", pcb.entry);
[17341d4]298 entry_point_jmp(prog_info.finfo.entry, &pcb);
[bbdbf86]299
[4470e26]300 /* Not reached */
301}
302
[c98e6ee]303/** Handle loader connection.
304 *
305 * Receive and carry out commands (of which the last one should be
306 * to execute the loaded program).
307 */
[9934f7d]308static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c98e6ee]309{
[bfd1546]310 /* Already have a connection? */
311 if (connected) {
[ffa2c8ef]312 async_answer_0(iid, ELIMIT);
[bfd1546]313 return;
314 }
[2f57690]315
[bfd1546]316 connected = true;
317
318 /* Accept the connection */
[ffa2c8ef]319 async_answer_0(iid, EOK);
[2f57690]320
[c98e6ee]321 /* Ignore parameters, the connection is already open */
[2f57690]322 (void) icall;
323
[79ae36dd]324 while (true) {
325 int retval;
326 ipc_call_t call;
327 ipc_callid_t callid = async_get_call(&call);
[2f57690]328
[79ae36dd]329 if (!IPC_GET_IMETHOD(call))
[86e3d62]330 exit(0);
[79ae36dd]331
332 switch (IPC_GET_IMETHOD(call)) {
[47e0a05b]333 case LOADER_GET_TASKID:
[bbdbf86]334 ldr_get_taskid(callid, &call);
[47e0a05b]335 continue;
[622cdbe]336 case LOADER_SET_CWD:
337 ldr_set_cwd(callid, &call);
338 continue;
[c98e6ee]339 case LOADER_SET_PATHNAME:
[bbdbf86]340 ldr_set_pathname(callid, &call);
[c98e6ee]341 continue;
342 case LOADER_SET_ARGS:
[bbdbf86]343 ldr_set_args(callid, &call);
344 continue;
345 case LOADER_SET_FILES:
346 ldr_set_files(callid, &call);
[4470e26]347 continue;
348 case LOADER_LOAD:
[bbdbf86]349 ldr_load(callid, &call);
[4470e26]350 continue;
[c98e6ee]351 case LOADER_RUN:
[bbdbf86]352 ldr_run(callid, &call);
[4470e26]353 /* Not reached */
[c98e6ee]354 default:
[8c3bc75]355 retval = EINVAL;
[c98e6ee]356 break;
357 }
[8c3bc75]358
[79ae36dd]359 async_answer_0(callid, retval);
[c98e6ee]360 }
361}
362
363/** Program loader main function.
364 */
365int main(int argc, char *argv[])
366{
[b688fd8]367 async_set_fallback_port_handler(ldr_connection, NULL);
[007e6efa]368
[5d96851b]369 /* Introduce this task to the NS (give it our task ID). */
[007e6efa]370 task_id_t id = task_get_id();
[79ae36dd]371 int rc = ns_intro(id);
[5d96851b]372 if (rc != EOK)
[b39b5cb]373 return rc;
[2f57690]374
[566992e1]375 /* Create port */
376 port_id_t port;
377 rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
378 if (rc != EOK)
379 return rc;
380
[bfd1546]381 /* Register at naming service. */
[566992e1]382 rc = service_register(SERVICE_LOADER);
[b39b5cb]383 if (rc != EOK)
384 return rc;
[007e6efa]385
[c98e6ee]386 async_manager();
[2f57690]387
[bfd1546]388 /* Never reached */
[c98e6ee]389 return 0;
390}
391
392/** @}
393 */
Note: See TracBrowser for help on using the repository browser.