52,12 → 52,10 |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/loader.h> |
#include <ipc/ns.h> |
#include <macros.h> |
#include <loader/pcb.h> |
#include <console.h> |
#include <errno.h> |
#include <async.h> |
#include <string.h> |
#include <as.h> |
|
#include <elf.h> |
78,13 → 76,6 |
/** Buffer holding all arguments */ |
static char *arg_buf = NULL; |
|
/** Number of preset files */ |
static int filc = 0; |
/** Preset files vector */ |
static fdi_node_t **filv = NULL; |
/** Buffer holding all preset files */ |
static fdi_node_t *fil_buf = NULL; |
|
static elf_info_t prog_info; |
static elf_info_t interp_info; |
|
93,7 → 84,7 |
/** Used to limit number of connections to one. */ |
static bool connected; |
|
static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
task_id_t task_id; |
120,7 → 111,7 |
* @param rid |
* @param request |
*/ |
static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t len; |
156,14 → 147,14 |
* @param rid |
* @param request |
*/ |
static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request) |
static void loader_set_args(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t buf_size, arg_size; |
size_t buf_len, arg_len; |
char *p; |
int n; |
|
if (!ipc_data_write_receive(&callid, &buf_size)) { |
if (!ipc_data_write_receive(&callid, &buf_len)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
179,7 → 170,7 |
argv = NULL; |
} |
|
arg_buf = malloc(buf_size + 1); |
arg_buf = malloc(buf_len + 1); |
if (!arg_buf) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
186,9 → 177,10 |
return; |
} |
|
ipc_data_write_finalize(callid, arg_buf, buf_size); |
ipc_data_write_finalize(callid, arg_buf, buf_len); |
ipc_answer_0(rid, EOK); |
|
arg_buf[buf_size] = '\0'; |
arg_buf[buf_len] = '\0'; |
|
/* |
* Count number of arguments |
195,9 → 187,9 |
*/ |
p = arg_buf; |
n = 0; |
while (p < arg_buf + buf_size) { |
arg_size = str_size(p); |
p = p + arg_size + 1; |
while (p < arg_buf + buf_len) { |
arg_len = strlen(p); |
p = p + arg_len + 1; |
++n; |
} |
|
206,93 → 198,28 |
|
if (argv == NULL) { |
free(arg_buf); |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
|
/* |
* Fill argv with argument pointers |
*/ |
p = arg_buf; |
n = 0; |
while (p < arg_buf + buf_size) { |
while (p < arg_buf + buf_len) { |
argv[n] = p; |
|
arg_size = str_size(p); |
p = p + arg_size + 1; |
arg_len = strlen(p); |
p = p + arg_len + 1; |
++n; |
} |
|
argc = n; |
argv[n] = NULL; |
|
ipc_answer_0(rid, EOK); |
} |
|
/** Receive a call setting preset files of the program to execute. |
* |
* @param rid |
* @param request |
*/ |
static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_callid_t callid; |
size_t buf_size; |
if (!ipc_data_write_receive(&callid, &buf_size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
if ((buf_size % sizeof(fdi_node_t)) != 0) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
if (fil_buf != NULL) { |
free(fil_buf); |
fil_buf = NULL; |
} |
|
if (filv != NULL) { |
free(filv); |
filv = NULL; |
} |
|
fil_buf = malloc(buf_size); |
if (!fil_buf) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
ipc_data_write_finalize(callid, fil_buf, buf_size); |
|
int count = buf_size / sizeof(fdi_node_t); |
|
/* Allocate filvv */ |
filv = malloc((count + 1) * sizeof(fdi_node_t *)); |
|
if (filv == NULL) { |
free(fil_buf); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* |
* Fill filv with argument pointers |
*/ |
int i; |
for (i = 0; i < count; i++) |
filv[i] = &fil_buf[i]; |
|
filc = count; |
filv[count] = NULL; |
|
ipc_answer_0(rid, EOK); |
} |
|
/** Load the previously selected program. |
* |
* @param rid |
299,12 → 226,12 |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
static int ldr_load(ipc_callid_t rid, ipc_call_t *request) |
static int loader_load(ipc_callid_t rid, ipc_call_t *request) |
{ |
int rc; |
|
rc = elf_load_file(pathname, 0, &prog_info); |
if (rc != EE_OK) { |
if (rc < 0) { |
DPRINTF("Failed to load executable '%s'.\n", pathname); |
ipc_answer_0(rid, EINVAL); |
return 1; |
315,9 → 242,6 |
pcb.argc = argc; |
pcb.argv = argv; |
|
pcb.filc = filc; |
pcb.filv = filv; |
|
if (prog_info.interp == NULL) { |
/* Statically linked program */ |
is_dyn_linked = false; |
326,7 → 250,7 |
} |
|
rc = elf_load_file(prog_info.interp, 0, &interp_info); |
if (rc != EE_OK) { |
if (rc < 0) { |
DPRINTF("Failed to load interpreter '%s.'\n", |
prog_info.interp); |
ipc_answer_0(rid, EINVAL); |
346,12 → 270,12 |
* @param request |
* @return 0 on success, !0 on error. |
*/ |
static void ldr_run(ipc_callid_t rid, ipc_call_t *request) |
static void loader_run(ipc_callid_t rid, ipc_call_t *request) |
{ |
const char *cp; |
|
/* Set the task name. */ |
cp = str_rchr(pathname, '/'); |
cp = strrchr(pathname, '/'); |
cp = (cp == NULL) ? pathname : (cp + 1); |
task_set_name(cp); |
|
359,15 → 283,17 |
/* Dynamically linked program */ |
DPRINTF("Run ELF interpreter.\n"); |
DPRINTF("Entry point: 0x%lx\n", interp_info.entry); |
console_close(); |
|
ipc_answer_0(rid, EOK); |
elf_run(&interp_info, &pcb); |
} else { |
/* Statically linked program */ |
console_close(); |
ipc_answer_0(rid, EOK); |
elf_run(&prog_info, &pcb); |
} |
|
} |
|
/* Not reached */ |
} |
|
376,7 → 302,7 |
* Receive and carry out commands (of which the last one should be |
* to execute the loaded program). |
*/ |
static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall) |
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
ipc_callid_t callid; |
ipc_call_t call; |
404,22 → 330,19 |
case IPC_M_PHONE_HUNGUP: |
exit(0); |
case LOADER_GET_TASKID: |
ldr_get_taskid(callid, &call); |
loader_get_taskid(callid, &call); |
continue; |
case LOADER_SET_PATHNAME: |
ldr_set_pathname(callid, &call); |
loader_set_pathname(callid, &call); |
continue; |
case LOADER_SET_ARGS: |
ldr_set_args(callid, &call); |
loader_set_args(callid, &call); |
continue; |
case LOADER_SET_FILES: |
ldr_set_files(callid, &call); |
continue; |
case LOADER_LOAD: |
ldr_load(callid, &call); |
loader_load(callid, &call); |
continue; |
case LOADER_RUN: |
ldr_run(callid, &call); |
loader_run(callid, &call); |
/* Not reached */ |
default: |
retval = ENOENT; |
439,24 → 362,16 |
int main(int argc, char *argv[]) |
{ |
ipcarg_t phonead; |
task_id_t id; |
int rc; |
|
|
connected = false; |
|
/* Introduce this task to the NS (give it our task ID). */ |
id = task_get_id(); |
rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id)); |
if (rc != EOK) |
return -1; |
|
|
/* Set a handler of incomming connections. */ |
async_set_client_connection(ldr_connection); |
async_set_client_connection(loader_connection); |
|
/* Register at naming service. */ |
if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0) |
return -2; |
|
return -1; |
|
async_manager(); |
|
/* Never reached */ |