Subversion Repositories HelenOS

Rev

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

Rev 4012 Rev 4014
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 86... Line 86...
86
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)
87
{
87
{
88
    ipc_callid_t callid;
88
    ipc_callid_t callid;
89
    task_id_t task_id;
89
    task_id_t task_id;
90
    size_t len;
90
    size_t len;
91
 
91
   
92
    task_id = task_get_id();
92
    task_id = task_get_id();
93
 
93
   
94
    if (!ipc_data_read_receive(&callid, &len)) {
94
    if (!ipc_data_read_receive(&callid, &len)) {
95
        ipc_answer_0(callid, EINVAL);
95
        ipc_answer_0(callid, EINVAL);
96
        ipc_answer_0(rid, EINVAL);
96
        ipc_answer_0(rid, EINVAL);
97
        return;
97
        return;
98
    }
98
    }
99
 
99
   
100
    if (len > sizeof(task_id)) len = sizeof(task_id);
100
    if (len > sizeof(task_id))
-
 
101
        len = sizeof(task_id);
101
 
102
   
102
    ipc_data_read_finalize(callid, &task_id, len);
103
    ipc_data_read_finalize(callid, &task_id, len);
103
    ipc_answer_0(rid, EOK);
104
    ipc_answer_0(rid, EOK);
104
}
105
}
105
 
106
 
106
 
107
 
Line 112... Line 113...
112
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
113
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
113
{
114
{
114
    ipc_callid_t callid;
115
    ipc_callid_t callid;
115
    size_t len;
116
    size_t len;
116
    char *name_buf;
117
    char *name_buf;
117
 
118
   
118
    if (!ipc_data_write_receive(&callid, &len)) {
119
    if (!ipc_data_write_receive(&callid, &len)) {
119
        ipc_answer_0(callid, EINVAL);
120
        ipc_answer_0(callid, EINVAL);
120
        ipc_answer_0(rid, EINVAL);
121
        ipc_answer_0(rid, EINVAL);
121
        return;
122
        return;
122
    }
123
    }
123
 
124
   
124
    name_buf = malloc(len + 1);
125
    name_buf = malloc(len + 1);
125
    if (!name_buf) {
126
    if (!name_buf) {
126
        ipc_answer_0(callid, ENOMEM);
127
        ipc_answer_0(callid, ENOMEM);
127
        ipc_answer_0(rid, ENOMEM);
128
        ipc_answer_0(rid, ENOMEM);
128
        return;
129
        return;
129
    }
130
    }
130
 
131
   
131
    ipc_data_write_finalize(callid, name_buf, len);
132
    ipc_data_write_finalize(callid, name_buf, len);
132
    ipc_answer_0(rid, EOK);
133
    ipc_answer_0(rid, EOK);
133
 
134
   
134
    if (pathname != NULL) {
135
    if (pathname != NULL) {
135
        free(pathname);
136
        free(pathname);
136
        pathname = NULL;
137
        pathname = NULL;
137
    }
138
    }
138
 
139
   
139
    name_buf[len] = '\0';
140
    name_buf[len] = '\0';
140
    pathname = name_buf;
141
    pathname = name_buf;
141
}
142
}
142
 
143
 
143
/** Receive a call setting arguments of the program to execute.
144
/** Receive a call setting arguments of the program to execute.
Line 149... Line 150...
149
{
150
{
150
    ipc_callid_t callid;
151
    ipc_callid_t callid;
151
    size_t buf_len, arg_len;
152
    size_t buf_len, arg_len;
152
    char *p;
153
    char *p;
153
    int n;
154
    int n;
154
 
155
   
155
    if (!ipc_data_write_receive(&callid, &buf_len)) {
156
    if (!ipc_data_write_receive(&callid, &buf_len)) {
156
        ipc_answer_0(callid, EINVAL);
157
        ipc_answer_0(callid, EINVAL);
157
        ipc_answer_0(rid, EINVAL);
158
        ipc_answer_0(rid, EINVAL);
158
        return;
159
        return;
159
    }
160
    }
160
 
161
   
161
    if (arg_buf != NULL) {
162
    if (arg_buf != NULL) {
162
        free(arg_buf);
163
        free(arg_buf);
163
        arg_buf = NULL;
164
        arg_buf = NULL;
164
    }
165
    }
165
 
166
   
166
    if (argv != NULL) {
167
    if (argv != NULL) {
167
        free(argv);
168
        free(argv);
168
        argv = NULL;
169
        argv = NULL;
169
    }
170
    }
170
 
171
   
171
    arg_buf = malloc(buf_len + 1);
172
    arg_buf = malloc(buf_len + 1);
172
    if (!arg_buf) {
173
    if (!arg_buf) {
173
        ipc_answer_0(callid, ENOMEM);
174
        ipc_answer_0(callid, ENOMEM);
174
        ipc_answer_0(rid, ENOMEM);
175
        ipc_answer_0(rid, ENOMEM);
175
        return;
176
        return;
176
    }
177
    }
177
 
178
   
178
    ipc_data_write_finalize(callid, arg_buf, buf_len);
179
    ipc_data_write_finalize(callid, arg_buf, buf_len);
179
    ipc_answer_0(rid, EOK);
180
    ipc_answer_0(rid, EOK);
180
 
181
   
181
    arg_buf[buf_len] = '\0';
182
    arg_buf[buf_len] = '\0';
182
 
183
   
183
    /*
184
    /*
184
     * Count number of arguments
185
     * Count number of arguments
185
     */
186
     */
186
    p = arg_buf;
187
    p = arg_buf;
187
    n = 0;
188
    n = 0;
188
    while (p < arg_buf + buf_len) {
189
    while (p < arg_buf + buf_len) {
189
        arg_len = strlen(p);
190
        arg_len = strlen(p);
190
        p = p + arg_len + 1;
191
        p = p + arg_len + 1;
191
        ++n;
192
        ++n;
192
    }
193
    }
193
 
194
   
194
    /* Allocate argv */
195
    /* Allocate argv */
195
    argv = malloc((n + 1) * sizeof(char *));
196
    argv = malloc((n + 1) * sizeof(char *));
196
 
197
   
197
    if (argv == NULL) {
198
    if (argv == NULL) {
198
        free(arg_buf);
199
        free(arg_buf);
199
        ipc_answer_0(callid, ENOMEM);
200
        ipc_answer_0(callid, ENOMEM);
200
        ipc_answer_0(rid, ENOMEM);
201
        ipc_answer_0(rid, ENOMEM);
201
        return;
202
        return;
202
    }
203
    }
203
 
204
   
204
    /*
205
    /*
205
     * Fill argv with argument pointers
206
     * Fill argv with argument pointers
206
     */
207
     */
207
    p = arg_buf;
208
    p = arg_buf;
208
    n = 0;
209
    n = 0;
209
    while (p < arg_buf + buf_len) {
210
    while (p < arg_buf + buf_len) {
210
        argv[n] = p;
211
        argv[n] = p;
211
 
212
       
212
        arg_len = strlen(p);
213
        arg_len = strlen(p);
213
        p = p + arg_len + 1;
214
        p = p + arg_len + 1;
214
        ++n;
215
        ++n;
215
    }
216
    }
216
 
217
   
217
    argc = n;
218
    argc = n;
218
    argv[n] = NULL;
219
    argv[n] = NULL;
219
}
220
}
220
 
221
 
221
/** Load the previously selected program.
222
/** Load the previously selected program.
Line 225... Line 226...
225
 * @return 0 on success, !0 on error.
226
 * @return 0 on success, !0 on error.
226
 */
227
 */
227
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
228
static int loader_load(ipc_callid_t rid, ipc_call_t *request)
228
{
229
{
229
    int rc;
230
    int rc;
230
 
231
   
231
    rc = elf_load_file(pathname, 0, &prog_info);
232
    rc = elf_load_file(pathname, 0, &prog_info);
232
    if (rc < 0) {
233
    if (rc < 0) {
233
        DPRINTF("Failed to load executable '%s'.\n", pathname);
234
        DPRINTF("Failed to load executable '%s'.\n", pathname);
234
        ipc_answer_0(rid, EINVAL);
235
        ipc_answer_0(rid, EINVAL);
235
        return 1;
236
        return 1;
236
    }
237
    }
237
 
238
   
238
    elf_create_pcb(&prog_info, &pcb);
239
    elf_create_pcb(&prog_info, &pcb);
239
 
240
   
240
    pcb.argc = argc;
241
    pcb.argc = argc;
241
    pcb.argv = argv;
242
    pcb.argv = argv;
242
 
243
   
243
    if (prog_info.interp == NULL) {
244
    if (prog_info.interp == NULL) {
244
        /* Statically linked program */
245
        /* Statically linked program */
245
        is_dyn_linked = false;
246
        is_dyn_linked = false;
246
        ipc_answer_0(rid, EOK);
247
        ipc_answer_0(rid, EOK);
247
        return 0;
248
        return 0;
248
    }
249
    }
249
 
250
   
250
    rc = elf_load_file(prog_info.interp, 0, &interp_info);
251
    rc = elf_load_file(prog_info.interp, 0, &interp_info);
251
    if (rc < 0) {
252
    if (rc < 0) {
252
        DPRINTF("Failed to load interpreter '%s.'\n",
253
        DPRINTF("Failed to load interpreter '%s.'\n",
253
            prog_info.interp);
254
            prog_info.interp);
254
        ipc_answer_0(rid, EINVAL);
255
        ipc_answer_0(rid, EINVAL);
255
        return 1;
256
        return 1;
256
    }
257
    }
257
 
258
   
258
    is_dyn_linked = true;
259
    is_dyn_linked = true;
259
    ipc_answer_0(rid, EOK);
260
    ipc_answer_0(rid, EOK);
260
 
261
   
261
    return 0;
262
    return 0;
262
}
263
}
263
 
264
 
264
 
265
 
265
/** Run the previously loaded program.
266
/** Run the previously loaded program.
Line 269... Line 270...
269
 * @return 0 on success, !0 on error.
270
 * @return 0 on success, !0 on error.
270
 */
271
 */
271
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
272
static void loader_run(ipc_callid_t rid, ipc_call_t *request)
272
{
273
{
273
    const char *cp;
274
    const char *cp;
274
 
275
   
275
    /* Set the task name. */
276
    /* Set the task name. */
276
    cp = strrchr(pathname, '/');
277
    cp = strrchr(pathname, '/');
277
    cp = (cp == NULL) ? pathname : (cp + 1);
278
    cp = (cp == NULL) ? pathname : (cp + 1);
278
    task_set_name(cp);
279
    task_set_name(cp);
279
 
280
   
280
    if (is_dyn_linked == true) {
281
    if (is_dyn_linked == true) {
281
        /* Dynamically linked program */
282
        /* Dynamically linked program */
282
        DPRINTF("Run ELF interpreter.\n");
283
        DPRINTF("Run ELF interpreter.\n");
283
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
284
        DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
284
        close_console();
285
        close_console();
285
 
286
       
286
        ipc_answer_0(rid, EOK);
287
        ipc_answer_0(rid, EOK);
287
        elf_run(&interp_info, &pcb);
288
        elf_run(&interp_info, &pcb);
288
 
-
 
289
    } else {
289
    } else {
290
        /* Statically linked program */
290
        /* Statically linked program */
291
        close_console();
291
        close_console();
292
        ipc_answer_0(rid, EOK);
292
        ipc_answer_0(rid, EOK);
293
        elf_run(&prog_info, &pcb);
293
        elf_run(&prog_info, &pcb);
Line 304... Line 304...
304
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
304
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
305
{
305
{
306
    ipc_callid_t callid;
306
    ipc_callid_t callid;
307
    ipc_call_t call;
307
    ipc_call_t call;
308
    int retval;
308
    int retval;
309
 
309
   
310
    /* Already have a connection? */
310
    /* Already have a connection? */
311
    if (connected) {
311
    if (connected) {
312
        ipc_answer_0(iid, ELIMIT);
312
        ipc_answer_0(iid, ELIMIT);
313
        return;
313
        return;
314
    }
314
    }
315
 
315
   
316
    connected = true;
316
    connected = true;
317
   
317
   
318
    /* Accept the connection */
318
    /* Accept the connection */
319
    ipc_answer_0(iid, EOK);
319
    ipc_answer_0(iid, EOK);
320
 
320
   
321
    /* Ignore parameters, the connection is already open */
321
    /* Ignore parameters, the connection is already open */
-
 
322
    (void) iid;
322
    (void)iid; (void)icall;
323
    (void) icall;
323
 
324
   
324
    while (1) {
325
    while (1) {
325
        callid = async_get_call(&call);
326
        callid = async_get_call(&call);
326
 
327
       
327
        switch (IPC_GET_METHOD(call)) {
328
        switch (IPC_GET_METHOD(call)) {
328
        case IPC_M_PHONE_HUNGUP:
329
        case IPC_M_PHONE_HUNGUP:
329
            exit(0);
330
            exit(0);
330
        case LOADER_GET_TASKID:
331
        case LOADER_GET_TASKID:
331
            loader_get_taskid(callid, &call);
332
            loader_get_taskid(callid, &call);
Line 358... Line 359...
358
/** Program loader main function.
359
/** Program loader main function.
359
 */
360
 */
360
int main(int argc, char *argv[])
361
int main(int argc, char *argv[])
361
{
362
{
362
    ipcarg_t phonead;
363
    ipcarg_t phonead;
363
 
364
   
364
    connected = false;
365
    connected = false;
365
   
366
   
366
    /* Set a handler of incomming connections. */
367
    /* Set a handler of incomming connections. */
367
    async_set_client_connection(loader_connection);
368
    async_set_client_connection(loader_connection);
368
 
369
   
369
    /* Register at naming service. */
370
    /* Register at naming service. */
370
    if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
371
    if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
371
        return -1;
372
        return -1;
372
   
373
   
373
    async_manager();
374
    async_manager();
374
 
375
   
375
    /* Never reached */
376
    /* Never reached */
376
    return 0;
377
    return 0;
377
}
378
}
378
 
379
 
379
/** @}
380
/** @}