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 <bool.h>
|
---|
50 | #include <fcntl.h>
|
---|
51 | #include <sys/types.h>
|
---|
52 | #include <ipc/ipc.h>
|
---|
53 | #include <ipc/services.h>
|
---|
54 | #include <ipc/loader.h>
|
---|
55 | #include <ipc/ns.h>
|
---|
56 | #include <macros.h>
|
---|
57 | #include <loader/pcb.h>
|
---|
58 | #include <errno.h>
|
---|
59 | #include <async.h>
|
---|
60 | #include <str.h>
|
---|
61 | #include <as.h>
|
---|
62 |
|
---|
63 | #include <elf.h>
|
---|
64 | #include <elf_load.h>
|
---|
65 |
|
---|
66 | #define DPRINTF(...)
|
---|
67 |
|
---|
68 | /** Pathname of the file that will be loaded */
|
---|
69 | static char *pathname = NULL;
|
---|
70 |
|
---|
71 | /** The Program control block */
|
---|
72 | static pcb_t pcb;
|
---|
73 |
|
---|
74 | /** Current working directory */
|
---|
75 | static char *cwd = NULL;
|
---|
76 |
|
---|
77 | /** Number of arguments */
|
---|
78 | static int argc = 0;
|
---|
79 | /** Argument vector */
|
---|
80 | static char **argv = NULL;
|
---|
81 | /** Buffer holding all arguments */
|
---|
82 | static char *arg_buf = NULL;
|
---|
83 |
|
---|
84 | /** Number of preset files */
|
---|
85 | static int filc = 0;
|
---|
86 | /** Preset files vector */
|
---|
87 | static fdi_node_t **filv = NULL;
|
---|
88 | /** Buffer holding all preset files */
|
---|
89 | static fdi_node_t *fil_buf = NULL;
|
---|
90 |
|
---|
91 | static elf_info_t prog_info;
|
---|
92 | static elf_info_t interp_info;
|
---|
93 |
|
---|
94 | static bool is_dyn_linked;
|
---|
95 |
|
---|
96 | /** Used to limit number of connections to one. */
|
---|
97 | static bool connected;
|
---|
98 |
|
---|
99 | static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
|
---|
100 | {
|
---|
101 | ipc_callid_t callid;
|
---|
102 | task_id_t task_id;
|
---|
103 | size_t len;
|
---|
104 |
|
---|
105 | task_id = task_get_id();
|
---|
106 |
|
---|
107 | if (!async_data_read_receive(&callid, &len)) {
|
---|
108 | ipc_answer_0(callid, EINVAL);
|
---|
109 | ipc_answer_0(rid, EINVAL);
|
---|
110 | return;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (len > sizeof(task_id))
|
---|
114 | len = sizeof(task_id);
|
---|
115 |
|
---|
116 | async_data_read_finalize(callid, &task_id, len);
|
---|
117 | ipc_answer_0(rid, EOK);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Receive a call setting the current working directory.
|
---|
121 | *
|
---|
122 | * @param rid
|
---|
123 | * @param request
|
---|
124 | */
|
---|
125 | static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
|
---|
126 | {
|
---|
127 | char *buf;
|
---|
128 | int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
|
---|
129 |
|
---|
130 | if (rc == EOK) {
|
---|
131 | if (cwd != NULL)
|
---|
132 | free(cwd);
|
---|
133 |
|
---|
134 | cwd = buf;
|
---|
135 | }
|
---|
136 |
|
---|
137 | ipc_answer_0(rid, rc);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Receive a call setting pathname of the program to execute.
|
---|
141 | *
|
---|
142 | * @param rid
|
---|
143 | * @param request
|
---|
144 | */
|
---|
145 | static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
|
---|
146 | {
|
---|
147 | char *buf;
|
---|
148 | int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
|
---|
149 |
|
---|
150 | if (rc == EOK) {
|
---|
151 | if (pathname != NULL)
|
---|
152 | free(pathname);
|
---|
153 |
|
---|
154 | pathname = buf;
|
---|
155 | }
|
---|
156 |
|
---|
157 | ipc_answer_0(rid, rc);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Receive a call setting arguments of the program to execute.
|
---|
161 | *
|
---|
162 | * @param rid
|
---|
163 | * @param request
|
---|
164 | */
|
---|
165 | static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
|
---|
166 | {
|
---|
167 | char *buf;
|
---|
168 | size_t buf_size;
|
---|
169 | int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
|
---|
170 |
|
---|
171 | if (rc == EOK) {
|
---|
172 | /*
|
---|
173 | * Count number of arguments
|
---|
174 | */
|
---|
175 | char *cur = buf;
|
---|
176 | int count = 0;
|
---|
177 |
|
---|
178 | while (cur < buf + buf_size) {
|
---|
179 | size_t arg_size = str_size(cur);
|
---|
180 | cur += arg_size + 1;
|
---|
181 | count++;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Allocate new argv
|
---|
186 | */
|
---|
187 | char **_argv = (char **) malloc((count + 1) * sizeof(char *));
|
---|
188 | if (_argv == NULL) {
|
---|
189 | free(buf);
|
---|
190 | ipc_answer_0(rid, ENOMEM);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Fill the new argv with argument pointers
|
---|
196 | */
|
---|
197 | cur = buf;
|
---|
198 | count = 0;
|
---|
199 | while (cur < buf + buf_size) {
|
---|
200 | _argv[count] = cur;
|
---|
201 |
|
---|
202 | size_t arg_size = str_size(cur);
|
---|
203 | cur += arg_size + 1;
|
---|
204 | count++;
|
---|
205 | }
|
---|
206 | _argv[count] = NULL;
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Copy temporary data to global variables
|
---|
210 | */
|
---|
211 | if (arg_buf != NULL)
|
---|
212 | free(arg_buf);
|
---|
213 |
|
---|
214 | if (argv != NULL)
|
---|
215 | free(argv);
|
---|
216 |
|
---|
217 | argc = count;
|
---|
218 | arg_buf = buf;
|
---|
219 | argv = _argv;
|
---|
220 | }
|
---|
221 |
|
---|
222 | ipc_answer_0(rid, rc);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** Receive a call setting preset files of the program to execute.
|
---|
226 | *
|
---|
227 | * @param rid
|
---|
228 | * @param request
|
---|
229 | */
|
---|
230 | static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request)
|
---|
231 | {
|
---|
232 | fdi_node_t *buf;
|
---|
233 | size_t buf_size;
|
---|
234 | int rc = async_data_write_accept((void **) &buf, false, 0, 0,
|
---|
235 | sizeof(fdi_node_t), &buf_size);
|
---|
236 |
|
---|
237 | if (rc == EOK) {
|
---|
238 | int count = buf_size / sizeof(fdi_node_t);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Allocate new filv
|
---|
242 | */
|
---|
243 | fdi_node_t **_filv = (fdi_node_t **) calloc(count + 1, sizeof(fdi_node_t *));
|
---|
244 | if (_filv == NULL) {
|
---|
245 | free(buf);
|
---|
246 | ipc_answer_0(rid, ENOMEM);
|
---|
247 | return;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Fill the new filv with argument pointers
|
---|
252 | */
|
---|
253 | int i;
|
---|
254 | for (i = 0; i < count; i++)
|
---|
255 | _filv[i] = &buf[i];
|
---|
256 |
|
---|
257 | _filv[count] = NULL;
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Copy temporary data to global variables
|
---|
261 | */
|
---|
262 | if (fil_buf != NULL)
|
---|
263 | free(fil_buf);
|
---|
264 |
|
---|
265 | if (filv != NULL)
|
---|
266 | free(filv);
|
---|
267 |
|
---|
268 | filc = count;
|
---|
269 | fil_buf = buf;
|
---|
270 | filv = _filv;
|
---|
271 | }
|
---|
272 |
|
---|
273 | ipc_answer_0(rid, EOK);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /** Load the previously selected program.
|
---|
277 | *
|
---|
278 | * @param rid
|
---|
279 | * @param request
|
---|
280 | * @return 0 on success, !0 on error.
|
---|
281 | */
|
---|
282 | static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
|
---|
283 | {
|
---|
284 | int rc;
|
---|
285 |
|
---|
286 | rc = elf_load_file(pathname, 0, &prog_info);
|
---|
287 | if (rc != EE_OK) {
|
---|
288 | DPRINTF("Failed to load executable '%s'.\n", pathname);
|
---|
289 | ipc_answer_0(rid, EINVAL);
|
---|
290 | return 1;
|
---|
291 | }
|
---|
292 |
|
---|
293 | elf_create_pcb(&prog_info, &pcb);
|
---|
294 |
|
---|
295 | pcb.cwd = cwd;
|
---|
296 |
|
---|
297 | pcb.argc = argc;
|
---|
298 | pcb.argv = argv;
|
---|
299 |
|
---|
300 | pcb.filc = filc;
|
---|
301 | pcb.filv = filv;
|
---|
302 |
|
---|
303 | if (prog_info.interp == NULL) {
|
---|
304 | /* Statically linked program */
|
---|
305 | is_dyn_linked = false;
|
---|
306 | ipc_answer_0(rid, EOK);
|
---|
307 | return 0;
|
---|
308 | }
|
---|
309 |
|
---|
310 | rc = elf_load_file(prog_info.interp, 0, &interp_info);
|
---|
311 | if (rc != EE_OK) {
|
---|
312 | DPRINTF("Failed to load interpreter '%s.'\n",
|
---|
313 | prog_info.interp);
|
---|
314 | ipc_answer_0(rid, EINVAL);
|
---|
315 | return 1;
|
---|
316 | }
|
---|
317 |
|
---|
318 | is_dyn_linked = true;
|
---|
319 | ipc_answer_0(rid, EOK);
|
---|
320 |
|
---|
321 | return 0;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /** Run the previously loaded program.
|
---|
326 | *
|
---|
327 | * @param rid
|
---|
328 | * @param request
|
---|
329 | * @return 0 on success, !0 on error.
|
---|
330 | */
|
---|
331 | static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
|
---|
332 | {
|
---|
333 | const char *cp;
|
---|
334 |
|
---|
335 | /* Set the task name. */
|
---|
336 | cp = str_rchr(pathname, '/');
|
---|
337 | cp = (cp == NULL) ? pathname : (cp + 1);
|
---|
338 | task_set_name(cp);
|
---|
339 |
|
---|
340 | if (is_dyn_linked == true) {
|
---|
341 | /* Dynamically linked program */
|
---|
342 | DPRINTF("Run ELF interpreter.\n");
|
---|
343 | DPRINTF("Entry point: %p\n", interp_info.entry);
|
---|
344 |
|
---|
345 | ipc_answer_0(rid, EOK);
|
---|
346 | elf_run(&interp_info, &pcb);
|
---|
347 | } else {
|
---|
348 | /* Statically linked program */
|
---|
349 | ipc_answer_0(rid, EOK);
|
---|
350 | elf_run(&prog_info, &pcb);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* Not reached */
|
---|
354 | }
|
---|
355 |
|
---|
356 | /** Handle loader connection.
|
---|
357 | *
|
---|
358 | * Receive and carry out commands (of which the last one should be
|
---|
359 | * to execute the loaded program).
|
---|
360 | */
|
---|
361 | static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
362 | {
|
---|
363 | ipc_callid_t callid;
|
---|
364 | ipc_call_t call;
|
---|
365 | int retval;
|
---|
366 |
|
---|
367 | /* Already have a connection? */
|
---|
368 | if (connected) {
|
---|
369 | ipc_answer_0(iid, ELIMIT);
|
---|
370 | return;
|
---|
371 | }
|
---|
372 |
|
---|
373 | connected = true;
|
---|
374 |
|
---|
375 | /* Accept the connection */
|
---|
376 | ipc_answer_0(iid, EOK);
|
---|
377 |
|
---|
378 | /* Ignore parameters, the connection is already open */
|
---|
379 | (void) iid;
|
---|
380 | (void) icall;
|
---|
381 |
|
---|
382 | while (1) {
|
---|
383 | callid = async_get_call(&call);
|
---|
384 |
|
---|
385 | switch (IPC_GET_IMETHOD(call)) {
|
---|
386 | case IPC_M_PHONE_HUNGUP:
|
---|
387 | exit(0);
|
---|
388 | case LOADER_GET_TASKID:
|
---|
389 | ldr_get_taskid(callid, &call);
|
---|
390 | continue;
|
---|
391 | case LOADER_SET_CWD:
|
---|
392 | ldr_set_cwd(callid, &call);
|
---|
393 | continue;
|
---|
394 | case LOADER_SET_PATHNAME:
|
---|
395 | ldr_set_pathname(callid, &call);
|
---|
396 | continue;
|
---|
397 | case LOADER_SET_ARGS:
|
---|
398 | ldr_set_args(callid, &call);
|
---|
399 | continue;
|
---|
400 | case LOADER_SET_FILES:
|
---|
401 | ldr_set_files(callid, &call);
|
---|
402 | continue;
|
---|
403 | case LOADER_LOAD:
|
---|
404 | ldr_load(callid, &call);
|
---|
405 | continue;
|
---|
406 | case LOADER_RUN:
|
---|
407 | ldr_run(callid, &call);
|
---|
408 | /* Not reached */
|
---|
409 | default:
|
---|
410 | retval = ENOENT;
|
---|
411 | break;
|
---|
412 | }
|
---|
413 | if (IPC_GET_IMETHOD(call) != IPC_M_PHONE_HUNGUP) {
|
---|
414 | DPRINTF("Responding EINVAL to method %d.\n",
|
---|
415 | IPC_GET_IMETHOD(call));
|
---|
416 | ipc_answer_0(callid, EINVAL);
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /** Program loader main function.
|
---|
422 | */
|
---|
423 | int main(int argc, char *argv[])
|
---|
424 | {
|
---|
425 | sysarg_t phonead;
|
---|
426 | task_id_t id;
|
---|
427 | int rc;
|
---|
428 |
|
---|
429 | connected = false;
|
---|
430 |
|
---|
431 | /* Introduce this task to the NS (give it our task ID). */
|
---|
432 | id = task_get_id();
|
---|
433 | rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id));
|
---|
434 | if (rc != EOK)
|
---|
435 | return -1;
|
---|
436 |
|
---|
437 | /* Set a handler of incomming connections. */
|
---|
438 | async_set_client_connection(ldr_connection);
|
---|
439 |
|
---|
440 | /* Register at naming service. */
|
---|
441 | if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
|
---|
442 | return -2;
|
---|
443 |
|
---|
444 | async_manager();
|
---|
445 |
|
---|
446 | /* Never reached */
|
---|
447 | return 0;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** @}
|
---|
451 | */
|
---|