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
Line 
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 * @brief Loads and runs programs from VFS.
31 * @{
32 */
33/**
34 * @file
35 * @brief Loads and runs programs from VFS.
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>
49#include <stdbool.h>
50#include <fcntl.h>
51#include <sys/types.h>
52#include <ipc/services.h>
53#include <ipc/loader.h>
54#include <ns.h>
55#include <loader/pcb.h>
56#include <entry_point.h>
57#include <errno.h>
58#include <async.h>
59#include <str.h>
60#include <as.h>
61#include <elf/elf.h>
62#include <elf/elf_load.h>
63#include <vfs/vfs.h>
64
65#define DPRINTF(...)
66
67/** Pathname of the file that will be loaded */
68static char *pathname = NULL;
69
70/** The Program control block */
71static pcb_t pcb;
72
73/** Current working directory */
74static char *cwd = NULL;
75
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
83/** Number of preset files */
84static unsigned int filc = 0;
85
86static elf_info_t prog_info;
87
88/** Used to limit number of connections to one. */
89static bool connected = false;
90
91static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
92{
93 ipc_callid_t callid;
94 task_id_t task_id;
95 size_t len;
96
97 task_id = task_get_id();
98
99 if (!async_data_read_receive(&callid, &len)) {
100 async_answer_0(callid, EINVAL);
101 async_answer_0(rid, EINVAL);
102 return;
103 }
104
105 if (len > sizeof(task_id))
106 len = sizeof(task_id);
107
108 async_data_read_finalize(callid, &task_id, len);
109 async_answer_0(rid, EOK);
110}
111
112/** Receive a call setting the current working directory.
113 *
114 * @param rid
115 * @param request
116 */
117static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
118{
119 char *buf;
120 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
121
122 if (rc == EOK) {
123 if (cwd != NULL)
124 free(cwd);
125
126 cwd = buf;
127 }
128
129 async_answer_0(rid, rc);
130}
131
132/** Receive a call setting pathname of the program to execute.
133 *
134 * @param rid
135 * @param request
136 */
137static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
138{
139 char *buf;
140 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
141
142 if (rc == EOK) {
143 if (pathname != NULL)
144 free(pathname);
145
146 pathname = buf;
147 }
148
149 async_answer_0(rid, rc);
150}
151
152/** Receive a call setting arguments of the program to execute.
153 *
154 * @param rid
155 * @param request
156 */
157static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
158{
159 char *buf;
160 size_t buf_size;
161 int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
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 }
175
176 /*
177 * Allocate new argv
178 */
179 char **_argv = (char **) malloc((count + 1) * sizeof(char *));
180 if (_argv == NULL) {
181 free(buf);
182 async_answer_0(rid, ENOMEM);
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;
199
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;
212 }
213
214 async_answer_0(rid, rc);
215}
216
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{
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;
230 int fd;
231
232 if (!async_state_change_receive(&callid, NULL, NULL, NULL)) {
233 async_answer_0(callid, EINVAL);
234 break;
235 }
236 async_state_change_finalize(callid, vfs_exch);
237 fd = vfs_fd_wait();
238 assert(fd == (int) filc);
239 }
240
241 vfs_exchange_end(vfs_exch);
242
243 async_answer_0(rid, EOK);
244}
245
246/** Load the previously selected program.
247 *
248 * @param rid
249 * @param request
250 * @return 0 on success, !0 on error.
251 */
252static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
253{
254 int rc;
255
256 rc = elf_load(pathname, &prog_info);
257 if (rc != EE_OK) {
258 DPRINTF("Failed to load executable '%s'.\n", pathname);
259 async_answer_0(rid, EINVAL);
260 return 1;
261 }
262
263 elf_set_pcb(&prog_info, &pcb);
264
265 pcb.cwd = cwd;
266
267 pcb.argc = argc;
268 pcb.argv = argv;
269
270 pcb.filc = filc;
271
272 async_answer_0(rid, rc);
273 return 0;
274}
275
276/** Run the previously loaded program.
277 *
278 * @param rid
279 * @param request
280 * @return 0 on success, !0 on error.
281 */
282static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
283{
284 const char *cp;
285
286 DPRINTF("Set task name\n");
287
288 /* Set the task name. */
289 cp = str_rchr(pathname, '/');
290 cp = (cp == NULL) ? pathname : (cp + 1);
291 task_set_name(cp);
292
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);
297 entry_point_jmp(prog_info.finfo.entry, &pcb);
298
299 /* Not reached */
300}
301
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 */
307static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
308{
309 /* Already have a connection? */
310 if (connected) {
311 async_answer_0(iid, ELIMIT);
312 return;
313 }
314
315 connected = true;
316
317 /* Accept the connection */
318 async_answer_0(iid, EOK);
319
320 /* Ignore parameters, the connection is already open */
321 (void) icall;
322
323 while (true) {
324 int retval;
325 ipc_call_t call;
326 ipc_callid_t callid = async_get_call(&call);
327
328 if (!IPC_GET_IMETHOD(call))
329 exit(0);
330
331 switch (IPC_GET_IMETHOD(call)) {
332 case LOADER_GET_TASKID:
333 ldr_get_taskid(callid, &call);
334 continue;
335 case LOADER_SET_CWD:
336 ldr_set_cwd(callid, &call);
337 continue;
338 case LOADER_SET_PATHNAME:
339 ldr_set_pathname(callid, &call);
340 continue;
341 case LOADER_SET_ARGS:
342 ldr_set_args(callid, &call);
343 continue;
344 case LOADER_SET_FILES:
345 ldr_set_files(callid, &call);
346 continue;
347 case LOADER_LOAD:
348 ldr_load(callid, &call);
349 continue;
350 case LOADER_RUN:
351 ldr_run(callid, &call);
352 /* Not reached */
353 default:
354 retval = EINVAL;
355 break;
356 }
357
358 async_answer_0(callid, retval);
359 }
360}
361
362/** Program loader main function.
363 */
364int main(int argc, char *argv[])
365{
366 async_set_fallback_port_handler(ldr_connection, NULL);
367
368 /* Introduce this task to the NS (give it our task ID). */
369 task_id_t id = task_get_id();
370 int rc = ns_intro(id);
371 if (rc != EOK)
372 return rc;
373
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
380 /* Register at naming service. */
381 rc = service_register(SERVICE_LOADER);
382 if (rc != EOK)
383 return rc;
384
385 async_manager();
386
387 /* Never reached */
388 return 0;
389}
390
391/** @}
392 */
Note: See TracBrowser for help on using the repository browser.