Subversion Repositories HelenOS

Rev

Rev 3684 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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