34,10 → 34,14 |
*/ |
|
#include <task.h> |
#include <ipc/ipc.h> |
#include <ipc/loader.h> |
#include <libc.h> |
#include <string.h> |
#include <stdlib.h> |
#include <async.h> |
#include <errno.h> |
#include <loader/loader.h> |
#include <vfs/vfs.h> |
|
task_id_t task_get_id(void) |
{ |
48,11 → 52,73 |
return task_id; |
} |
|
/** Create a new task by running an executable from the filesystem. |
static int task_spawn_loader(void) |
{ |
int phone_id, rc; |
|
rc = __SYSCALL1(SYS_PROGRAM_SPAWN_LOADER, (sysarg_t) &phone_id); |
if (rc != 0) |
return rc; |
|
return phone_id; |
} |
|
static int loader_set_args(int phone_id, const char *argv[]) |
{ |
aid_t req; |
ipc_call_t answer; |
ipcarg_t rc; |
|
const char **ap; |
char *dp; |
char *arg_buf; |
size_t buffer_size; |
size_t len; |
|
/* |
* Serialize the arguments into a single array. First |
* compute size of the buffer needed. |
*/ |
ap = argv; |
buffer_size = 0; |
while (*ap != NULL) { |
buffer_size += strlen(*ap) + 1; |
++ap; |
} |
|
arg_buf = malloc(buffer_size); |
if (arg_buf == NULL) return ENOMEM; |
|
/* Now fill the buffer with null-terminated argument strings */ |
ap = argv; |
dp = arg_buf; |
while (*ap != NULL) { |
strcpy(dp, *ap); |
dp += strlen(*ap) + 1; |
|
++ap; |
} |
|
/* Send serialized arguments to the loader */ |
|
req = async_send_0(phone_id, LOADER_SET_ARGS, &answer); |
rc = ipc_data_write_start(phone_id, (void *)arg_buf, buffer_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return rc; |
} |
|
async_wait_for(req, &rc); |
if (rc != EOK) return rc; |
|
/* Free temporary buffer */ |
free(arg_buf); |
|
return EOK; |
} |
|
/** Create a new task by running an executable from VFS. |
* |
* This is really just a convenience wrapper over the more complicated |
* loader API. |
* |
* @param path pathname of the binary to execute |
* @param argv command-line arguments |
* @return ID of the newly created task or zero on error. |
59,41 → 125,74 |
*/ |
task_id_t task_spawn(const char *path, char *const argv[]) |
{ |
loader_t *ldr; |
task_id_t task_id; |
int phone_id; |
ipc_call_t answer; |
aid_t req; |
int rc; |
ipcarg_t retval; |
|
char *pa; |
size_t pa_len; |
task_id_t task_id; |
|
pa = absolutize(path, &pa_len); |
if (!pa) |
return 0; |
|
/* Spawn a program loader */ |
ldr = loader_spawn(); |
if (ldr == NULL) |
phone_id = task_spawn_loader(); |
if (phone_id < 0) |
return 0; |
|
/* |
* Say hello so that the loader knows the incoming connection's |
* phone hash. |
*/ |
rc = async_req_0_0(phone_id, LOADER_HELLO); |
if (rc != EOK) |
return 0; |
|
/* Get task ID. */ |
rc = loader_get_task_id(ldr, &task_id); |
if (rc != EOK) |
req = async_send_0(phone_id, LOADER_GET_TASKID, &answer); |
rc = ipc_data_read_start(phone_id, &task_id, sizeof(task_id)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
goto error; |
} |
|
async_wait_for(req, &retval); |
if (retval != EOK) |
goto error; |
|
/* Send program pathname */ |
rc = loader_set_pathname(ldr, path); |
if (rc != EOK) |
req = async_send_0(phone_id, LOADER_SET_PATHNAME, &answer); |
rc = ipc_data_write_start(phone_id, (void *)pa, pa_len); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
return 1; |
} |
|
async_wait_for(req, &retval); |
if (retval != EOK) |
goto error; |
|
/* Send arguments */ |
rc = loader_set_args(ldr, argv); |
rc = loader_set_args(phone_id, argv); |
if (rc != EOK) |
goto error; |
|
/* Request loader to start the program */ |
rc = loader_start_program(ldr); |
rc = async_req_0_0(phone_id, LOADER_RUN); |
if (rc != EOK) |
goto error; |
|
/* Success */ |
ipc_hangup(phone_id); |
return task_id; |
|
/* Error exit */ |
error: |
loader_abort(ldr); |
ipc_hangup(phone_id); |
return 0; |
} |
|