Subversion Repositories HelenOS

Rev

Rev 4348 | Details | Compare with Previous | Last modification | View Log | RSS feed

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