source: mainline/uspace/srv/loader/main.c@ b4cbef1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b4cbef1 was b4cbef1, checked in by Martin Decky <martin@…>, 15 years ago

add minimal data size check into async_data_receive
introduce more convenience wrappers for common IPC patterns:

async_data_read_forward_fast
async_data_void
async_data_forward_fast

  • Property mode set to 100644
File size: 9.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 <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 <string.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 */
69static char *pathname = NULL;
70
71/** The Program control block */
72static pcb_t pcb;
73
74/** Current working directory */
75static char *cwd = NULL;
76
77/** Number of arguments */
78static int argc = 0;
79/** Argument vector */
80static char **argv = NULL;
81/** Buffer holding all arguments */
82static char *arg_buf = NULL;
83
84/** Number of preset files */
85static int filc = 0;
86/** Preset files vector */
87static fdi_node_t **filv = NULL;
88/** Buffer holding all preset files */
89static fdi_node_t *fil_buf = NULL;
90
91static elf_info_t prog_info;
92static elf_info_t interp_info;
93
94static bool is_dyn_linked;
95
96/** Used to limit number of connections to one. */
97static bool connected;
98
99static 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 */
125static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
126{
127 char *buf;
128 int rc = async_string_receive(&buf, 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 */
145static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
146{
147 char *buf;
148 int rc = async_string_receive(&buf, 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 */
165static 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_string_receive(&buf, 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 */
230static 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_receive(&buf, 0, 0, sizeof(fdi_node_t), &buf_size);
235
236 if (rc == EOK) {
237 int count = buf_size / sizeof(fdi_node_t);
238
239 /*
240 * Allocate new filv
241 */
242 fdi_node_t **_filv = (fdi_node_t *) malloc((count + 1) * sizeof(fdi_node_t *));
243 if (_filv == NULL) {
244 free(buf);
245 ipc_answer_0(rid, ENOMEM);
246 return;
247 }
248
249 /*
250 * Fill the new filv with argument pointers
251 */
252 int i;
253 for (i = 0; i < count; i++)
254 _filv[i] = &buf[i];
255
256 _filv[count] = NULL;
257
258 /*
259 * Copy temporary data to global variables
260 */
261 if (fil_buf != NULL)
262 free(fil_buf);
263
264 if (filv != NULL)
265 free(filv);
266
267 filc = count;
268 fil_buf = buf;
269 filv = _filv;
270 }
271
272 ipc_answer_0(rid, EOK);
273}
274
275/** Load the previously selected program.
276 *
277 * @param rid
278 * @param request
279 * @return 0 on success, !0 on error.
280 */
281static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
282{
283 int rc;
284
285 rc = elf_load_file(pathname, 0, &prog_info);
286 if (rc != EE_OK) {
287 DPRINTF("Failed to load executable '%s'.\n", pathname);
288 ipc_answer_0(rid, EINVAL);
289 return 1;
290 }
291
292 elf_create_pcb(&prog_info, &pcb);
293
294 pcb.cwd = cwd;
295
296 pcb.argc = argc;
297 pcb.argv = argv;
298
299 pcb.filc = filc;
300 pcb.filv = filv;
301
302 if (prog_info.interp == NULL) {
303 /* Statically linked program */
304 is_dyn_linked = false;
305 ipc_answer_0(rid, EOK);
306 return 0;
307 }
308
309 rc = elf_load_file(prog_info.interp, 0, &interp_info);
310 if (rc != EE_OK) {
311 DPRINTF("Failed to load interpreter '%s.'\n",
312 prog_info.interp);
313 ipc_answer_0(rid, EINVAL);
314 return 1;
315 }
316
317 is_dyn_linked = true;
318 ipc_answer_0(rid, EOK);
319
320 return 0;
321}
322
323
324/** Run the previously loaded program.
325 *
326 * @param rid
327 * @param request
328 * @return 0 on success, !0 on error.
329 */
330static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
331{
332 const char *cp;
333
334 /* Set the task name. */
335 cp = str_rchr(pathname, '/');
336 cp = (cp == NULL) ? pathname : (cp + 1);
337 task_set_name(cp);
338
339 if (is_dyn_linked == true) {
340 /* Dynamically linked program */
341 DPRINTF("Run ELF interpreter.\n");
342 DPRINTF("Entry point: %p\n", interp_info.entry);
343
344 ipc_answer_0(rid, EOK);
345 elf_run(&interp_info, &pcb);
346 } else {
347 /* Statically linked program */
348 ipc_answer_0(rid, EOK);
349 elf_run(&prog_info, &pcb);
350 }
351
352 /* Not reached */
353}
354
355/** Handle loader connection.
356 *
357 * Receive and carry out commands (of which the last one should be
358 * to execute the loaded program).
359 */
360static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall)
361{
362 ipc_callid_t callid;
363 ipc_call_t call;
364 int retval;
365
366 /* Already have a connection? */
367 if (connected) {
368 ipc_answer_0(iid, ELIMIT);
369 return;
370 }
371
372 connected = true;
373
374 /* Accept the connection */
375 ipc_answer_0(iid, EOK);
376
377 /* Ignore parameters, the connection is already open */
378 (void) iid;
379 (void) icall;
380
381 while (1) {
382 callid = async_get_call(&call);
383
384 switch (IPC_GET_METHOD(call)) {
385 case IPC_M_PHONE_HUNGUP:
386 exit(0);
387 case LOADER_GET_TASKID:
388 ldr_get_taskid(callid, &call);
389 continue;
390 case LOADER_SET_CWD:
391 ldr_set_cwd(callid, &call);
392 continue;
393 case LOADER_SET_PATHNAME:
394 ldr_set_pathname(callid, &call);
395 continue;
396 case LOADER_SET_ARGS:
397 ldr_set_args(callid, &call);
398 continue;
399 case LOADER_SET_FILES:
400 ldr_set_files(callid, &call);
401 continue;
402 case LOADER_LOAD:
403 ldr_load(callid, &call);
404 continue;
405 case LOADER_RUN:
406 ldr_run(callid, &call);
407 /* Not reached */
408 default:
409 retval = ENOENT;
410 break;
411 }
412 if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
413 IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
414 DPRINTF("Responding EINVAL to method %d.\n",
415 IPC_GET_METHOD(call));
416 ipc_answer_0(callid, EINVAL);
417 }
418 }
419}
420
421/** Program loader main function.
422 */
423int main(int argc, char *argv[])
424{
425 ipcarg_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 */
Note: See TracBrowser for help on using the repository browser.