Subversion Repositories HelenOS

Rev

Rev 4377 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
Line 50... Line 50...
50
#include <fcntl.h>
50
#include <fcntl.h>
51
#include <sys/types.h>
51
#include <sys/types.h>
52
#include <ipc/ipc.h>
52
#include <ipc/ipc.h>
53
#include <ipc/services.h>
53
#include <ipc/services.h>
54
#include <ipc/loader.h>
54
#include <ipc/loader.h>
-
 
55
#include <ipc/ns.h>
-
 
56
#include <macros.h>
55
#include <loader/pcb.h>
57
#include <loader/pcb.h>
56
#include <console.h>
-
 
57
#include <errno.h>
58
#include <errno.h>
58
#include <async.h>
59
#include <async.h>
-
 
60
#include <string.h>
59
#include <as.h>
61
#include <as.h>
60
 
62
 
61
#include <elf.h>
63
#include <elf.h>
62
#include <elf_load.h>
64
#include <elf_load.h>
63
 
65
 
Line 74... Line 76...
74
/** Argument vector */
76
/** Argument vector */
75
static char **argv = NULL;
77
static char **argv = NULL;
76
/** Buffer holding all arguments */
78
/** Buffer holding all arguments */
77
static char *arg_buf = NULL;
79
static char *arg_buf = NULL;
78
 
80
 
-
 
81
/** Number of preset files */
-
 
82
static int filc = 0;
-
 
83
/** Preset files vector */
-
 
84
static fdi_node_t **filv = NULL;
-
 
85
/** Buffer holding all preset files */
-
 
86
static fdi_node_t *fil_buf = NULL;
-
 
87
 
79
static elf_info_t prog_info;
88
static elf_info_t prog_info;
80
static elf_info_t interp_info;
89
static elf_info_t interp_info;
81
 
90
 
82
static bool is_dyn_linked;
91
static bool is_dyn_linked;
83
 
92
 
84
/** Used to limit number of connections to one. */
93
/** Used to limit number of connections to one. */
85
static bool connected;
94
static bool connected;
86
 
95
 
87
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
96
static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
88
{
97
{
89
    ipc_callid_t callid;
98
    ipc_callid_t callid;
90
    task_id_t task_id;
99
    task_id_t task_id;
91
    size_t len;
100
    size_t len;
92
   
101
   
Line 109... Line 118...
109
/** Receive a call setting pathname of the program to execute.
118
/** Receive a call setting pathname of the program to execute.
110
 *
119
 *
111
 * @param rid
120
 * @param rid
112
 * @param request
121
 * @param request
113
 */
122
 */
114
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
123
static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
115
{
124
{
116
    ipc_callid_t callid;
125
    ipc_callid_t callid;
117
    size_t len;
126
    size_t len;
118
    char *name_buf;
127
    char *name_buf;
119
   
128
   
Line 145... Line 154...
145
/** Receive a call setting arguments of the program to execute.
154
/** Receive a call setting arguments of the program to execute.
146
 *
155
 *
147
 * @param rid
156
 * @param rid
148
 * @param request
157
 * @param request
149
 */
158
 */
150
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
159
static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
151
{
160
{
152
    ipc_callid_t callid;
161
    ipc_callid_t callid;
153
    size_t buf_size, arg_size;
162
    size_t buf_size, arg_size;
154
    char *p;
163
    char *p;
155
    int n;
164
    int n;
Line 218... Line 227...
218
    argv[n] = NULL;
227
    argv[n] = NULL;
219
 
228
 
220
    ipc_answer_0(rid, EOK);
229
    ipc_answer_0(rid, EOK);
221
}
230
}
222
 
231
 
-
 
232
/** Receive a call setting preset files of the program to execute.
-
 
233
 *
-
 
234
 * @param rid
-
 
235
 * @param request
-
 
236
 */
-
 
237
static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request)
-
 
238
{
-
 
239
    ipc_callid_t callid;
-
 
240
    size_t buf_size;
-
 
241
    if (!ipc_data_write_receive(&callid, &buf_size)) {
-
 
242
        ipc_answer_0(callid, EINVAL);
-
 
243
        ipc_answer_0(rid, EINVAL);
-
 
244
        return;
-
 
245
    }
-
 
246
   
-
 
247
    if ((buf_size % sizeof(fdi_node_t)) != 0) {
-
 
248
        ipc_answer_0(callid, EINVAL);
-
 
249
        ipc_answer_0(rid, EINVAL);
-
 
250
        return;
-
 
251
    }
-
 
252
   
-
 
253
    if (fil_buf != NULL) {
-
 
254
        free(fil_buf);
-
 
255
        fil_buf = NULL;
-
 
256
    }
-
 
257
   
-
 
258
    if (filv != NULL) {
-
 
259
        free(filv);
-
 
260
        filv = NULL;
-
 
261
    }
-
 
262
   
-
 
263
    fil_buf = malloc(buf_size);
-
 
264
    if (!fil_buf) {
-
 
265
        ipc_answer_0(callid, ENOMEM);
-
 
266
        ipc_answer_0(rid, ENOMEM);
-
 
267
        return;
-
 
268
    }
-
 
269
   
-
 
270
    ipc_data_write_finalize(callid, fil_buf, buf_size);
-
 
271
   
-
 
272
    int count = buf_size / sizeof(fdi_node_t);
-
 
273
   
-
 
274
    /* Allocate filvv */
-
 
275
    filv = malloc((count + 1) * sizeof(fdi_node_t *));
-
 
276
   
-
 
277
    if (filv == NULL) {
-
 
278
        free(fil_buf);
-
 
279
        ipc_answer_0(rid, ENOMEM);
-
 
280
        return;
-
 
281
    }
-
 
282
   
-
 
283
    /*
-
 
284
     * Fill filv with argument pointers
-
 
285
     */
-
 
286
    int i;
-
 
287
    for (i = 0; i < count; i++)
-
 
288
        filv[i] = &fil_buf[i];
-
 
289
   
-
 
290
    filc = count;
-
 
291
    filv[count] = NULL;
-
 
292
   
-
 
293
    ipc_answer_0(rid, EOK);
-
 
294
}
-
 
295
 
223
/** Load the previously selected program.
296
/** Load the previously selected program.
224
 *
297
 *
225
 * @param rid
298
 * @param rid
226
 * @param request
299
 * @param request
227
 * @return 0 on success, !0 on error.
300
 * @return 0 on success, !0 on error.
228
 */
301
 */
229
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
302
static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
230
{
303
{
231
    int rc;
304
    int rc;
232
   
305
   
233
    rc = elf_load_file(pathname, 0, &prog_info);
306
    rc = elf_load_file(pathname, 0, &prog_info);
234
    if (rc != EE_OK) {
307
    if (rc != EE_OK) {
Line 240... Line 313...
240
    elf_create_pcb(&prog_info, &pcb);
313
    elf_create_pcb(&prog_info, &pcb);
241
   
314
   
242
    pcb.argc = argc;
315
    pcb.argc = argc;
243
    pcb.argv = argv;
316
    pcb.argv = argv;
244
   
317
   
-
 
318
    pcb.filc = filc;
-
 
319
    pcb.filv = filv;
-
 
320
   
245
    if (prog_info.interp == NULL) {
321
    if (prog_info.interp == NULL) {
246
        /* Statically linked program */
322
        /* Statically linked program */
247
        is_dyn_linked = false;
323
        is_dyn_linked = false;
248
        ipc_answer_0(rid, EOK);
324
        ipc_answer_0(rid, EOK);
249
        return 0;
325
        return 0;
Line 268... Line 344...
268
 *
344
 *
269
 * @param rid
345
 * @param rid
270
 * @param request
346
 * @param request
271
 * @return 0 on success, !0 on error.
347
 * @return 0 on success, !0 on error.
272
 */
348
 */
273
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
349
static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
274
{
350
{
275
    const char *cp;
351
    const char *cp;
276
   
352
   
277
    /* Set the task name. */
353
    /* Set the task name. */
278
    cp = str_rchr(pathname, '/');
354
    cp = str_rchr(pathname, '/');
Line 281... Line 357...
281
   
357
   
282
    if (is_dyn_linked == true) {
358
    if (is_dyn_linked == true) {
283
        /* Dynamically linked program */
359
        /* Dynamically linked program */
284
        DPRINTF("Run ELF interpreter.\n");
360
        DPRINTF("Run ELF interpreter.\n");
285
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
361
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
286
        console_close();
-
 
287
       
362
       
288
        ipc_answer_0(rid, EOK);
363
        ipc_answer_0(rid, EOK);
289
        elf_run(&interp_info, &pcb);
364
        elf_run(&interp_info, &pcb);
290
    } else {
365
    } else {
291
        /* Statically linked program */
366
        /* Statically linked program */
292
        console_close();
-
 
293
        ipc_answer_0(rid, EOK);
367
        ipc_answer_0(rid, EOK);
294
        elf_run(&prog_info, &pcb);
368
        elf_run(&prog_info, &pcb);
295
    }
369
    }
296
 
370
   
297
    /* Not reached */
371
    /* Not reached */
298
}
372
}
299
 
373
 
300
/** Handle loader connection.
374
/** Handle loader connection.
301
 *
375
 *
302
 * Receive and carry out commands (of which the last one should be
376
 * Receive and carry out commands (of which the last one should be
303
 * to execute the loaded program).
377
 * to execute the loaded program).
304
 */
378
 */
305
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
379
static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall)
306
{
380
{
307
    ipc_callid_t callid;
381
    ipc_callid_t callid;
308
    ipc_call_t call;
382
    ipc_call_t call;
309
    int retval;
383
    int retval;
310
   
384
   
Line 328... Line 402...
328
       
402
       
329
        switch (IPC_GET_METHOD(call)) {
403
        switch (IPC_GET_METHOD(call)) {
330
        case IPC_M_PHONE_HUNGUP:
404
        case IPC_M_PHONE_HUNGUP:
331
            exit(0);
405
            exit(0);
332
        case LOADER_GET_TASKID:
406
        case LOADER_GET_TASKID:
333
            loader_get_taskid(callid, &call);
407
            ldr_get_taskid(callid, &call);
334
            continue;
408
            continue;
335
        case LOADER_SET_PATHNAME:
409
        case LOADER_SET_PATHNAME:
336
            loader_set_pathname(callid, &call);
410
            ldr_set_pathname(callid, &call);
337
            continue;
411
            continue;
338
        case LOADER_SET_ARGS:
412
        case LOADER_SET_ARGS:
339
            loader_set_args(callid, &call);
413
            ldr_set_args(callid, &call);
-
 
414
            continue;
-
 
415
        case LOADER_SET_FILES:
-
 
416
            ldr_set_files(callid, &call);
340
            continue;
417
            continue;
341
        case LOADER_LOAD:
418
        case LOADER_LOAD:
342
            loader_load(callid, &call);
419
            ldr_load(callid, &call);
343
            continue;
420
            continue;
344
        case LOADER_RUN:
421
        case LOADER_RUN:
345
            loader_run(callid, &call);
422
            ldr_run(callid, &call);
346
            /* Not reached */
423
            /* Not reached */
347
        default:
424
        default:
348
            retval = ENOENT;
425
            retval = ENOENT;
349
            break;
426
            break;
350
        }
427
        }
Line 360... Line 437...
360
/** Program loader main function.
437
/** Program loader main function.
361
 */
438
 */
362
int main(int argc, char *argv[])
439
int main(int argc, char *argv[])
363
{
440
{
364
    ipcarg_t phonead;
441
    ipcarg_t phonead;
-
 
442
    task_id_t id;
-
 
443
    int rc;
365
   
444
 
366
    connected = false;
445
    connected = false;
367
   
446
 
-
 
447
    /* Introduce this task to the NS (give it our task ID). */
-
 
448
    id = task_get_id();
-
 
449
    rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id));
-
 
450
    if (rc != EOK)
-
 
451
        return -1;
-
 
452
 
368
    /* Set a handler of incomming connections. */
453
    /* Set a handler of incomming connections. */
369
    async_set_client_connection(loader_connection);
454
    async_set_client_connection(ldr_connection);
370
   
455
   
371
    /* Register at naming service. */
456
    /* Register at naming service. */
372
    if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
457
    if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
373
        return -1;
458
        return -2;
374
   
459
 
375
    async_manager();
460
    async_manager();
376
   
461
   
377
    /* Never reached */
462
    /* Never reached */
378
    return 0;
463
    return 0;
379
}
464
}