Subversion Repositories HelenOS

Rev

Rev 3677 | Rev 3690 | Go to most recent revision | 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
30
 * @brief   Loads and runs programs from VFS.
31
 * @{
32
 */
33
/**
34
 * @file
3101 svoboda 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.
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>
3148 svoboda 53
#include <ipc/loader.h>
3160 svoboda 54
#include <loader/pcb.h>
3004 svoboda 55
#include <errno.h>
3148 svoboda 56
#include <async.h>
2959 svoboda 57
#include <as.h>
2928 svoboda 58
 
2969 svoboda 59
#include <elf.h>
60
#include <elf_load.h>
2928 svoboda 61
 
3681 svoboda 62
void program_run(void *entry, pcb_t *pcb);
63
 
3101 svoboda 64
/** Pathname of the file that will be loaded */
3004 svoboda 65
static char *pathname = NULL;
66
 
3170 svoboda 67
/** The Program control block */
68
static pcb_t pcb;
69
 
3174 svoboda 70
/** Number of arguments */
71
static int argc = 0;
72
/** Argument vector */
73
static char **argv = NULL;
74
/** Buffer holding all arguments */
75
static char *arg_buf = NULL;
76
 
3474 svoboda 77
static elf_info_t prog_info;
78
static elf_info_t interp_info;
79
 
80
static bool is_dyn_linked;
81
 
82
 
83
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
3448 svoboda 84
{
85
    ipc_callid_t callid;
86
    task_id_t task_id;
87
    size_t len;
88
 
89
    task_id = task_get_id();
90
 
91
    if (!ipc_data_read_receive(&callid, &len)) {
92
        ipc_answer_0(callid, EINVAL);
93
        ipc_answer_0(rid, EINVAL);
94
        return;
95
    }
96
 
97
    if (len > sizeof(task_id)) len = sizeof(task_id);
98
 
3535 svoboda 99
    ipc_data_read_finalize(callid, &task_id, len);
3448 svoboda 100
    ipc_answer_0(rid, EOK);
101
}
102
 
103
 
3101 svoboda 104
/** Receive a call setting pathname of the program to execute.
105
 *
106
 * @param rid
107
 * @param request
108
 */
3174 svoboda 109
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
2928 svoboda 110
{
3148 svoboda 111
    ipc_callid_t callid;
3004 svoboda 112
    size_t len;
113
    char *name_buf;
114
 
115
    if (!ipc_data_write_receive(&callid, &len)) {
116
        ipc_answer_0(callid, EINVAL);
117
        ipc_answer_0(rid, EINVAL);
118
        return;
119
    }
3148 svoboda 120
 
3004 svoboda 121
    name_buf = malloc(len + 1);
122
    if (!name_buf) {
3148 svoboda 123
        ipc_answer_0(callid, ENOMEM);
3004 svoboda 124
        ipc_answer_0(rid, ENOMEM);
125
        return;
126
    }
127
 
3148 svoboda 128
    ipc_data_write_finalize(callid, name_buf, len);
129
    ipc_answer_0(rid, EOK);
3004 svoboda 130
 
131
    if (pathname != NULL) {
132
        free(pathname);
133
        pathname = NULL;
134
    }
135
 
3148 svoboda 136
    name_buf[len] = '\0';
3004 svoboda 137
    pathname = name_buf;
138
}
139
 
3174 svoboda 140
/** Receive a call setting arguments of the program to execute.
141
 *
142
 * @param rid
143
 * @param request
144
 */
145
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
146
{
147
    ipc_callid_t callid;
148
    size_t buf_len, arg_len;
149
    char *p;
150
    int n;
151
 
152
    if (!ipc_data_write_receive(&callid, &buf_len)) {
153
        ipc_answer_0(callid, EINVAL);
154
        ipc_answer_0(rid, EINVAL);
155
        return;
156
    }
157
 
158
    if (arg_buf != NULL) {
159
        free(arg_buf);
160
        arg_buf = NULL;
161
    }
162
 
163
    if (argv != NULL) {
164
        free(argv);
165
        argv = NULL;
166
    }
167
 
168
    arg_buf = malloc(buf_len + 1);
169
    if (!arg_buf) {
170
        ipc_answer_0(callid, ENOMEM);
171
        ipc_answer_0(rid, ENOMEM);
172
        return;
173
    }
174
 
175
    ipc_data_write_finalize(callid, arg_buf, buf_len);
176
    ipc_answer_0(rid, EOK);
177
 
178
    arg_buf[buf_len] = '\0';
179
 
180
    /*
181
     * Count number of arguments
182
     */
183
    p = arg_buf;
184
    n = 0;
185
    while (p < arg_buf + buf_len) {
186
        arg_len = strlen(p);
187
        p = p + arg_len + 1;
188
        ++n;
189
    }
190
 
191
    /* Allocate argv */
192
    argv = malloc((n + 1) * sizeof(char *));
193
 
194
    if (argv == NULL) {
195
        free(arg_buf);
196
        ipc_answer_0(callid, ENOMEM);
197
        ipc_answer_0(rid, ENOMEM);
198
        return;
199
    }
200
 
201
    /*
202
     * Fill argv with argument pointers
203
     */
204
    p = arg_buf;
205
    n = 0;
206
    while (p < arg_buf + buf_len) {
207
        argv[n] = p;
208
 
209
        arg_len = strlen(p);
210
        p = p + arg_len + 1;
211
        ++n;
212
    }
213
 
214
    argc = n;
215
    argv[n] = NULL;
216
}
217
 
3474 svoboda 218
/** Load the previously selected program.
3101 svoboda 219
 *
220
 * @param rid
221
 * @param request
222
 * @return 0 on success, !0 on error.
223
 */
3474 svoboda 224
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
3004 svoboda 225
{
226
    int rc;
227
 
3552 svoboda 228
    rc = elf_load_file(pathname, 0, 0, &prog_info);
2928 svoboda 229
    if (rc < 0) {
3677 svoboda 230
        printf("Failed to load executable '%s'.\n", pathname);
3155 svoboda 231
        ipc_answer_0(rid, EINVAL);
2928 svoboda 232
        return 1;
233
    }
234
 
3170 svoboda 235
    elf_create_pcb(&prog_info, &pcb);
2928 svoboda 236
 
3174 svoboda 237
    pcb.argc = argc;
238
    pcb.argv = argv;
3171 svoboda 239
 
3004 svoboda 240
    if (prog_info.interp == NULL) {
241
        /* Statically linked program */
3474 svoboda 242
        is_dyn_linked = false;
3155 svoboda 243
        ipc_answer_0(rid, EOK);
3004 svoboda 244
        return 0;
245
    }
2988 svoboda 246
 
3677 svoboda 247
    printf("Load ELF interpreter '%s'\n", prog_info.interp);
248
    rc = elf_load_file(prog_info.interp, 0, 0, &interp_info);
2959 svoboda 249
    if (rc < 0) {
3677 svoboda 250
        printf("Failed to load interpreter '%s.'\n", prog_info.interp);
3155 svoboda 251
        ipc_answer_0(rid, EINVAL);
2959 svoboda 252
        return 1;
253
    }
254
 
3677 svoboda 255
    printf("Run interpreter.\n");
3400 svoboda 256
    printf("entry point: 0x%lx\n", interp_info.entry);
257
    printf("pcb address: 0x%lx\n", &pcb);
3155 svoboda 258
 
3474 svoboda 259
    is_dyn_linked = true;
3155 svoboda 260
    ipc_answer_0(rid, EOK);
2989 svoboda 261
 
3004 svoboda 262
    return 0;
263
}
264
 
3474 svoboda 265
 
266
/** Run the previously loaded program.
267
 *
268
 * @param rid
269
 * @param request
270
 * @return 0 on success, !0 on error.
271
 */
272
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
273
{
274
    if (is_dyn_linked == true) {
275
        /* Dynamically linked program */
276
        printf("run dynamic linker\n");
3554 svoboda 277
        printf("entry point: 0x%lx\n", interp_info.entry);
3474 svoboda 278
        close_console();
279
 
280
        ipc_answer_0(rid, EOK);
3681 svoboda 281
        program_run(interp_info.entry, &pcb);
3474 svoboda 282
 
283
    } else {
284
        /* Statically linked program */
285
        close_console();
286
        ipc_answer_0(rid, EOK);
3681 svoboda 287
        program_run(prog_info.entry, &pcb);
3474 svoboda 288
    }
289
 
290
    /* Not reached */
291
}
292
 
3148 svoboda 293
/** Handle loader connection.
3101 svoboda 294
 *
295
 * Receive and carry out commands (of which the last one should be
296
 * to execute the loaded program).
297
 */
3148 svoboda 298
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
3004 svoboda 299
{
300
    ipc_callid_t callid;
301
    ipc_call_t call;
302
    int retval;
303
 
3148 svoboda 304
    /* Ignore parameters, the connection is already open */
305
    (void)iid; (void)icall;
306
 
3004 svoboda 307
    while (1) {
3148 svoboda 308
        callid = async_get_call(&call);
3474 svoboda 309
 
3004 svoboda 310
        switch (IPC_GET_METHOD(call)) {
3561 svoboda 311
        case IPC_M_PHONE_HUNGUP:
312
            exit(0);
3448 svoboda 313
        case LOADER_GET_TASKID:
314
            loader_get_taskid(callid, &call);
315
            continue;
3148 svoboda 316
        case LOADER_SET_PATHNAME:
3174 svoboda 317
            loader_set_pathname(callid, &call);
3148 svoboda 318
            continue;
3174 svoboda 319
        case LOADER_SET_ARGS:
320
            loader_set_args(callid, &call);
3474 svoboda 321
            continue;
322
        case LOADER_LOAD:
323
            loader_load(callid, &call);
324
            continue;
3148 svoboda 325
        case LOADER_RUN:
3174 svoboda 326
            loader_run(callid, &call);
3474 svoboda 327
            /* Not reached */
3004 svoboda 328
        default:
329
            retval = ENOENT;
330
            break;
331
        }
3174 svoboda 332
        if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
333
            IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
334
            printf("responding EINVAL to method %d\n",
335
                IPC_GET_METHOD(call));
3004 svoboda 336
            ipc_answer_0(callid, EINVAL);
337
        }
338
    }
3148 svoboda 339
}
3004 svoboda 340
 
3148 svoboda 341
/** Program loader main function.
342
 */
343
int main(int argc, char *argv[])
344
{
345
    ipc_callid_t callid;
346
    ipc_call_t call;
347
    ipcarg_t phone_hash;
348
 
349
    /* The first call only communicates the incoming phone hash */
350
    callid = ipc_wait_for_call(&call);
351
 
352
    if (IPC_GET_METHOD(call) != LOADER_HELLO) {
353
        if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
354
            ipc_answer_0(callid, EINVAL);
355
        return 1;
356
    }
357
 
358
    ipc_answer_0(callid, EOK);
359
    phone_hash = call.in_phone_hash;
360
 
361
    /*
3170 svoboda 362
     * Up until now async must not be used as it couldn't
363
     * handle incoming requests. (Which means e.g. printf()
364
     * cannot be used)
3148 svoboda 365
     */
366
    async_new_connection(phone_hash, 0, NULL, loader_connection);
367
    async_manager();
368
 
2928 svoboda 369
    /* not reached */
370
    return 0;
371
}
372
 
373
/** @}
374
 */