insmod: Support passing arguments to modules

This commit is contained in:
K. Lange 2021-10-20 10:33:35 +09:00
parent c366ea2dfd
commit 32c3bf1825
4 changed files with 31 additions and 16 deletions

View File

@ -12,16 +12,13 @@
int main(int argc, char * argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <modulepath>\n", argv[0]);
fprintf(stderr, "Usage: %s <modulepath> [ARGS...]\n", argv[0]);
return 1;
}
int ret = 0;
for (int i = 1; i < argc; ++i) {
int status = sysfunc(TOARU_SYS_FUNC_INSMOD, &argv[i]);
if (status < 0) {
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[i], strerror(errno));
ret = 1;
int status = sysfunc(TOARU_SYS_FUNC_INSMOD, &argv[1]);
if (status != 0) {
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], strerror(errno));
return status;
}
}
return ret;
return 0;
}

View File

@ -27,10 +27,10 @@ hashmap_t * modules_get_list(void) {
return _modules_table;
}
int elf_module(const char * path) {
int elf_module(char ** args) {
Elf64_Header header;
fs_node_t * file = kopen(path,0);
fs_node_t * file = kopen(args[0],0);
if (!file) {
return -ENOENT;
@ -169,7 +169,11 @@ int elf_module(const char * path) {
hashmap_set(_modules_table, moduleData->name, loadedData);
return moduleData->init(0,NULL);
/* Count arguments */
int argc = 0;
for (char ** aa = args; *aa; ++aa) ++argc;
return moduleData->init(argc, args);
}
int elf_exec(const char * path, fs_node_t * file, int argc, const char *const argv[], const char *const env[], int interp) {

View File

@ -58,7 +58,7 @@ static long sys_sbrk(ssize_t size) {
return (long)out;
}
extern int elf_module(const char * path);
extern int elf_module(char ** args);
static long sys_sysfunc(long fn, char ** args) {
/* FIXME: Most of these should be top-level, many are hacks/broken in Misaka */
@ -93,7 +93,8 @@ static long sys_sysfunc(long fn, char ** args) {
if (!args) return -EFAULT;
PTR_VALIDATE(args[0]);
if (!args[0]) return -EFAULT;
return elf_module(args[0]);
for (char ** aa = args; *aa; ++aa) { PTR_VALIDATE(*aa); }
return elf_module(args);
case TOARU_SYS_FUNC_SETHEAP: {
/* I'm not really sure how this should be done...

View File

@ -1,9 +1,22 @@
#include <kernel/printf.h>
#include <kernel/module.h>
#include <kernel/assert.h>
#include <kernel/misc.h>
static int init(int argc, char * argv[]) {
printf("Hello, modules.\n");
dprintf("Hello, modules.\n");
dprintf("Received %d arguments.\n", argc);
if (argc > 1 && !strcmp(argv[1], "--traceback")) {
arch_dump_traceback();
} else if (argc > 1 && !strcmp(argv[1], "--fail")) {
return 1;
} else if (argc > 1 && !strcmp(argv[1], "--crash")) {
*(volatile int*)0x60000000 = 42;
} else if (argc > 1 && !strcmp(argv[1], "--assert")) {
assert(0 && "Intentional failure.");
}
return 0;
}