Subversion Repositories HelenOS

Rev

Rev 3787 | Rev 3983 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3787 Rev 3896
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup loader
29
/** @addtogroup loader
30
 * @brief   Loads and runs programs from VFS.
30
 * @brief   Loads and runs programs from VFS.
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Loads and runs programs from VFS.
35
 * @brief   Loads and runs programs from VFS.
36
 *
36
 *
37
 * The program loader is a special init binary. Its image is used
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
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.
39
 * returns the id of a phone connected to the newly created task.
40
 *
40
 *
41
 * The caller uses this phone to send the pathname and various other
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
42
 * information to the loader. This is normally done by the C library
43
 * and completely hidden from applications.
43
 * and completely hidden from applications.
44
 */
44
 */
45
 
45
 
46
#include <stdio.h>
46
#include <stdio.h>
47
#include <stdlib.h>
47
#include <stdlib.h>
48
#include <unistd.h>
48
#include <unistd.h>
49
#include <bool.h>
49
#include <bool.h>
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/loader.h>
54
#include <ipc/loader.h>
54
#include <loader/pcb.h>
55
#include <loader/pcb.h>
55
#include <errno.h>
56
#include <errno.h>
56
#include <async.h>
57
#include <async.h>
57
#include <as.h>
58
#include <as.h>
58
 
59
 
59
#include <elf.h>
60
#include <elf.h>
60
#include <elf_load.h>
61
#include <elf_load.h>
61
 
62
 
62
#define DPRINTF(...)
63
#define DPRINTF(...)
63
 
64
 
64
/** Pathname of the file that will be loaded */
65
/** Pathname of the file that will be loaded */
65
static char *pathname = NULL;
66
static char *pathname = NULL;
66
 
67
 
67
/** The Program control block */
68
/** The Program control block */
68
static pcb_t pcb;
69
static pcb_t pcb;
69
 
70
 
70
/** Number of arguments */
71
/** Number of arguments */
71
static int argc = 0;
72
static int argc = 0;
72
/** Argument vector */
73
/** Argument vector */
73
static char **argv = NULL;
74
static char **argv = NULL;
74
/** Buffer holding all arguments */
75
/** Buffer holding all arguments */
75
static char *arg_buf = NULL;
76
static char *arg_buf = NULL;
76
 
77
 
77
static elf_info_t prog_info;
78
static elf_info_t prog_info;
78
static elf_info_t interp_info;
79
static elf_info_t interp_info;
79
 
80
 
80
static bool is_dyn_linked;
81
static bool is_dyn_linked;
81
 
82
 
-
 
83
/** Used to limit number of connections to one. */
-
 
84
static bool connected;
82
 
85
 
83
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
86
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request)
84
{
87
{
85
    ipc_callid_t callid;
88
    ipc_callid_t callid;
86
    task_id_t task_id;
89
    task_id_t task_id;
87
    size_t len;
90
    size_t len;
88
 
91
 
89
    task_id = task_get_id();
92
    task_id = task_get_id();
90
 
93
 
91
    if (!ipc_data_read_receive(&callid, &len)) {
94
    if (!ipc_data_read_receive(&callid, &len)) {
92
        ipc_answer_0(callid, EINVAL);
95
        ipc_answer_0(callid, EINVAL);
93
        ipc_answer_0(rid, EINVAL);
96
        ipc_answer_0(rid, EINVAL);
94
        return;
97
        return;
95
    }
98
    }
96
 
99
 
97
    if (len > sizeof(task_id)) len = sizeof(task_id);
100
    if (len > sizeof(task_id)) len = sizeof(task_id);
98
 
101
 
99
    ipc_data_read_finalize(callid, &task_id, len);
102
    ipc_data_read_finalize(callid, &task_id, len);
100
    ipc_answer_0(rid, EOK);
103
    ipc_answer_0(rid, EOK);
101
}
104
}
102
 
105
 
103
 
106
 
104
/** Receive a call setting pathname of the program to execute.
107
/** Receive a call setting pathname of the program to execute.
105
 *
108
 *
106
 * @param rid
109
 * @param rid
107
 * @param request
110
 * @param request
108
 */
111
 */
109
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
112
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
110
{
113
{
111
    ipc_callid_t callid;
114
    ipc_callid_t callid;
112
    size_t len;
115
    size_t len;
113
    char *name_buf;
116
    char *name_buf;
114
 
117
 
115
    if (!ipc_data_write_receive(&callid, &len)) {
118
    if (!ipc_data_write_receive(&callid, &len)) {
116
        ipc_answer_0(callid, EINVAL);
119
        ipc_answer_0(callid, EINVAL);
117
        ipc_answer_0(rid, EINVAL);
120
        ipc_answer_0(rid, EINVAL);
118
        return;
121
        return;
119
    }
122
    }
120
 
123
 
121
    name_buf = malloc(len + 1);
124
    name_buf = malloc(len + 1);
122
    if (!name_buf) {
125
    if (!name_buf) {
123
        ipc_answer_0(callid, ENOMEM);
126
        ipc_answer_0(callid, ENOMEM);
124
        ipc_answer_0(rid, ENOMEM);
127
        ipc_answer_0(rid, ENOMEM);
125
        return;
128
        return;
126
    }
129
    }
127
 
130
 
128
    ipc_data_write_finalize(callid, name_buf, len);
131
    ipc_data_write_finalize(callid, name_buf, len);
129
    ipc_answer_0(rid, EOK);
132
    ipc_answer_0(rid, EOK);
130
 
133
 
131
    if (pathname != NULL) {
134
    if (pathname != NULL) {
132
        free(pathname);
135
        free(pathname);
133
        pathname = NULL;
136
        pathname = NULL;
134
    }
137
    }
135
 
138
 
136
    name_buf[len] = '\0';
139
    name_buf[len] = '\0';
137
    pathname = name_buf;
140
    pathname = name_buf;
138
}
141
}
139
 
142
 
140
/** Receive a call setting arguments of the program to execute.
143
/** Receive a call setting arguments of the program to execute.
141
 *
144
 *
142
 * @param rid
145
 * @param rid
143
 * @param request
146
 * @param request
144
 */
147
 */
145
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
148
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request)
146
{
149
{
147
    ipc_callid_t callid;
150
    ipc_callid_t callid;
148
    size_t buf_len, arg_len;
151
    size_t buf_len, arg_len;
149
    char *p;
152
    char *p;
150
    int n;
153
    int n;
151
 
154
 
152
    if (!ipc_data_write_receive(&callid, &buf_len)) {
155
    if (!ipc_data_write_receive(&callid, &buf_len)) {
153
        ipc_answer_0(callid, EINVAL);
156
        ipc_answer_0(callid, EINVAL);
154
        ipc_answer_0(rid, EINVAL);
157
        ipc_answer_0(rid, EINVAL);
155
        return;
158
        return;
156
    }
159
    }
157
 
160
 
158
    if (arg_buf != NULL) {
161
    if (arg_buf != NULL) {
159
        free(arg_buf);
162
        free(arg_buf);
160
        arg_buf = NULL;
163
        arg_buf = NULL;
161
    }
164
    }
162
 
165
 
163
    if (argv != NULL) {
166
    if (argv != NULL) {
164
        free(argv);
167
        free(argv);
165
        argv = NULL;
168
        argv = NULL;
166
    }
169
    }
167
 
170
 
168
    arg_buf = malloc(buf_len + 1);
171
    arg_buf = malloc(buf_len + 1);
169
    if (!arg_buf) {
172
    if (!arg_buf) {
170
        ipc_answer_0(callid, ENOMEM);
173
        ipc_answer_0(callid, ENOMEM);
171
        ipc_answer_0(rid, ENOMEM);
174
        ipc_answer_0(rid, ENOMEM);
172
        return;
175
        return;
173
    }
176
    }
174
 
177
 
175
    ipc_data_write_finalize(callid, arg_buf, buf_len);
178
    ipc_data_write_finalize(callid, arg_buf, buf_len);
176
    ipc_answer_0(rid, EOK);
179
    ipc_answer_0(rid, EOK);
177
 
180
 
178
    arg_buf[buf_len] = '\0';
181
    arg_buf[buf_len] = '\0';
179
 
182
 
180
    /*
183
    /*
181
     * Count number of arguments
184
     * Count number of arguments
182
     */
185
     */
183
    p = arg_buf;
186
    p = arg_buf;
184
    n = 0;
187
    n = 0;
185
    while (p < arg_buf + buf_len) {
188
    while (p < arg_buf + buf_len) {
186
        arg_len = strlen(p);
189
        arg_len = strlen(p);
187
        p = p + arg_len + 1;
190
        p = p + arg_len + 1;
188
        ++n;
191
        ++n;
189
    }
192
    }
190
 
193
 
191
    /* Allocate argv */
194
    /* Allocate argv */
192
    argv = malloc((n + 1) * sizeof(char *));
195
    argv = malloc((n + 1) * sizeof(char *));
193
 
196
 
194
    if (argv == NULL) {
197
    if (argv == NULL) {
195
        free(arg_buf);
198
        free(arg_buf);
196
        ipc_answer_0(callid, ENOMEM);
199
        ipc_answer_0(callid, ENOMEM);
197
        ipc_answer_0(rid, ENOMEM);
200
        ipc_answer_0(rid, ENOMEM);
198
        return;
201
        return;
199
    }
202
    }
200
 
203
 
201
    /*
204
    /*
202
     * Fill argv with argument pointers
205
     * Fill argv with argument pointers
203
     */
206
     */
204
    p = arg_buf;
207
    p = arg_buf;
205
    n = 0;
208
    n = 0;
206
    while (p < arg_buf + buf_len) {
209
    while (p < arg_buf + buf_len) {
207
        argv[n] = p;
210
        argv[n] = p;
208
 
211
 
209
        arg_len = strlen(p);
212
        arg_len = strlen(p);
210
        p = p + arg_len + 1;
213
        p = p + arg_len + 1;
211
        ++n;
214
        ++n;
212
    }
215
    }
213
 
216
 
214
    argc = n;
217
    argc = n;
215
    argv[n] = NULL;
218
    argv[n] = NULL;
216
}
219
}
217
 
220
 
218
/** Load the previously selected program.
221
/** Load the previously selected program.
219
 *
222
 *
220
 * @param rid
223
 * @param rid
221
 * @param request
224
 * @param request
222
 * @return 0 on success, !0 on error.
225
 * @return 0 on success, !0 on error.
223
 */
226
 */
224
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
227
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
225
{
228
{
226
    int rc;
229
    int rc;
227
 
230
 
228
    rc = elf_load_file(pathname, 0, &prog_info);
231
    rc = elf_load_file(pathname, 0, &prog_info);
229
    if (rc < 0) {
232
    if (rc < 0) {
230
        DPRINTF("Failed to load executable '%s'.\n", pathname);
233
        DPRINTF("Failed to load executable '%s'.\n", pathname);
231
        ipc_answer_0(rid, EINVAL);
234
        ipc_answer_0(rid, EINVAL);
232
        return 1;
235
        return 1;
233
    }
236
    }
234
 
237
 
235
    elf_create_pcb(&prog_info, &pcb);
238
    elf_create_pcb(&prog_info, &pcb);
236
 
239
 
237
    pcb.argc = argc;
240
    pcb.argc = argc;
238
    pcb.argv = argv;
241
    pcb.argv = argv;
239
 
242
 
240
    if (prog_info.interp == NULL) {
243
    if (prog_info.interp == NULL) {
241
        /* Statically linked program */
244
        /* Statically linked program */
242
        is_dyn_linked = false;
245
        is_dyn_linked = false;
243
        ipc_answer_0(rid, EOK);
246
        ipc_answer_0(rid, EOK);
244
        return 0;
247
        return 0;
245
    }
248
    }
246
 
249
 
247
    rc = elf_load_file(prog_info.interp, 0, &interp_info);
250
    rc = elf_load_file(prog_info.interp, 0, &interp_info);
248
    if (rc < 0) {
251
    if (rc < 0) {
249
        DPRINTF("Failed to load interpreter '%s.'\n",
252
        DPRINTF("Failed to load interpreter '%s.'\n",
250
            prog_info.interp);
253
            prog_info.interp);
251
        ipc_answer_0(rid, EINVAL);
254
        ipc_answer_0(rid, EINVAL);
252
        return 1;
255
        return 1;
253
    }
256
    }
254
 
257
 
255
    is_dyn_linked = true;
258
    is_dyn_linked = true;
256
    ipc_answer_0(rid, EOK);
259
    ipc_answer_0(rid, EOK);
257
 
260
 
258
    return 0;
261
    return 0;
259
}
262
}
260
 
263
 
261
 
264
 
262
/** Run the previously loaded program.
265
/** Run the previously loaded program.
263
 *
266
 *
264
 * @param rid
267
 * @param rid
265
 * @param request
268
 * @param request
266
 * @return 0 on success, !0 on error.
269
 * @return 0 on success, !0 on error.
267
 */
270
 */
268
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
271
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
269
{
272
{
270
    if (is_dyn_linked == true) {
273
    if (is_dyn_linked == true) {
271
        /* Dynamically linked program */
274
        /* Dynamically linked program */
272
        DPRINTF("Run ELF interpreter.\n");
275
        DPRINTF("Run ELF interpreter.\n");
273
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
276
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
274
        close_console();
277
        close_console();
275
 
278
 
276
        ipc_answer_0(rid, EOK);
279
        ipc_answer_0(rid, EOK);
277
        elf_run(&interp_info, &pcb);
280
        elf_run(&interp_info, &pcb);
278
 
281
 
279
    } else {
282
    } else {
280
        /* Statically linked program */
283
        /* Statically linked program */
281
        close_console();
284
        close_console();
282
        ipc_answer_0(rid, EOK);
285
        ipc_answer_0(rid, EOK);
283
        elf_run(&prog_info, &pcb);
286
        elf_run(&prog_info, &pcb);
284
    }
287
    }
285
 
288
 
286
    /* Not reached */
289
    /* Not reached */
287
}
290
}
288
 
291
 
289
/** Handle loader connection.
292
/** Handle loader connection.
290
 *
293
 *
291
 * Receive and carry out commands (of which the last one should be
294
 * Receive and carry out commands (of which the last one should be
292
 * to execute the loaded program).
295
 * to execute the loaded program).
293
 */
296
 */
294
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
297
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
295
{
298
{
296
    ipc_callid_t callid;
299
    ipc_callid_t callid;
297
    ipc_call_t call;
300
    ipc_call_t call;
298
    int retval;
301
    int retval;
299
 
302
 
-
 
303
    /* Already have a connection? */
-
 
304
    if (connected) {
-
 
305
        ipc_answer_0(iid, ELIMIT);
-
 
306
        return;
-
 
307
    }
-
 
308
 
-
 
309
    connected = true;
-
 
310
   
-
 
311
    /* Accept the connection */
-
 
312
    ipc_answer_0(iid, EOK);
-
 
313
 
300
    /* Ignore parameters, the connection is already open */
314
    /* Ignore parameters, the connection is already open */
301
    (void)iid; (void)icall;
315
    (void)iid; (void)icall;
302
 
316
 
303
    while (1) {
317
    while (1) {
304
        callid = async_get_call(&call);
318
        callid = async_get_call(&call);
305
 
319
 
306
        switch (IPC_GET_METHOD(call)) {
320
        switch (IPC_GET_METHOD(call)) {
307
        case IPC_M_PHONE_HUNGUP:
321
        case IPC_M_PHONE_HUNGUP:
308
            exit(0);
322
            exit(0);
309
        case LOADER_GET_TASKID:
323
        case LOADER_GET_TASKID:
310
            loader_get_taskid(callid, &call);
324
            loader_get_taskid(callid, &call);
311
            continue;
325
            continue;
312
        case LOADER_SET_PATHNAME:
326
        case LOADER_SET_PATHNAME:
313
            loader_set_pathname(callid, &call);
327
            loader_set_pathname(callid, &call);
314
            continue;
328
            continue;
315
        case LOADER_SET_ARGS:
329
        case LOADER_SET_ARGS:
316
            loader_set_args(callid, &call);
330
            loader_set_args(callid, &call);
317
            continue;
331
            continue;
318
        case LOADER_LOAD:
332
        case LOADER_LOAD:
319
            loader_load(callid, &call);
333
            loader_load(callid, &call);
320
            continue;
334
            continue;
321
        case LOADER_RUN:
335
        case LOADER_RUN:
322
            loader_run(callid, &call);
336
            loader_run(callid, &call);
323
            /* Not reached */
337
            /* Not reached */
324
        default:
338
        default:
325
            retval = ENOENT;
339
            retval = ENOENT;
326
            break;
340
            break;
327
        }
341
        }
328
        if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
342
        if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
329
            IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
343
            IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
330
            DPRINTF("Responding EINVAL to method %d.\n",
344
            DPRINTF("Responding EINVAL to method %d.\n",
331
                IPC_GET_METHOD(call));
345
                IPC_GET_METHOD(call));
332
            ipc_answer_0(callid, EINVAL);
346
            ipc_answer_0(callid, EINVAL);
333
        }
347
        }
334
    }
348
    }
335
}
349
}
336
 
350
 
337
/** Program loader main function.
351
/** Program loader main function.
338
 */
352
 */
339
int main(int argc, char *argv[])
353
int main(int argc, char *argv[])
340
{
354
{
341
    ipc_callid_t callid;
-
 
342
    ipc_call_t call;
-
 
343
    ipcarg_t phone_hash;
355
    ipcarg_t phonead;
344
 
-
 
345
    /* The first call only communicates the incoming phone hash */
-
 
346
    callid = ipc_wait_for_call(&call);
-
 
347
 
356
 
348
    if (IPC_GET_METHOD(call) != LOADER_HELLO) {
-
 
349
        if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
-
 
350
            ipc_answer_0(callid, EINVAL);
-
 
351
        return 1;
357
    connected = false;
352
    }
358
   
353
 
-
 
354
    ipc_answer_0(callid, EOK);
359
    /* Set a handler of incomming connections. */
355
    phone_hash = call.in_phone_hash;
360
    async_set_client_connection(loader_connection);
356
 
361
 
357
    /*
-
 
358
     * Up until now async must not be used as it couldn't
362
    /* Register at naming service. */
359
     * handle incoming requests. (Which means e.g. printf()
363
    if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
360
     * cannot be used)
364
        return -1;
361
     */
365
   
362
    async_new_connection(phone_hash, 0, NULL, loader_connection);
-
 
363
    async_manager();
366
    async_manager();
364
 
367
 
365
    /* not reached */
368
    /* Never reached */
366
    return 0;
369
    return 0;
367
}
370
}
368
 
371
 
369
/** @}
372
/** @}
370
 */
373
 */
371
 
374