Compare commits

...

3 Commits

6 changed files with 73 additions and 9 deletions

View File

@ -1,4 +1,4 @@
APPS := init
APPS := init sym
APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src

View File

@ -117,7 +117,17 @@ int main()
if (fclose(config) < 0) { perror("fclose"); }
printf("\n\nPress any key to restart.\n");
sleep(2);
printf("\n\nPress any key to restart.\n\n");
sleep(2);
if (execv("/bin/sym", NULL) < 0)
{
perror("execv");
return 1;
}
return 0;
}

35
apps/src/sym.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>
int main()
{
FILE* syms = fopen("/sys/moon.sym", "r");
if (!syms)
{
perror("fopen");
return 1;
}
char buf[1025];
if (fseek(syms, 8000, SEEK_SET) < 0)
{
perror("fseek");
return 1;
}
size_t nread = fread(buf, 1024, 1, syms);
if (ferror(syms))
{
perror("fread");
return 1;
}
buf[nread] = 0;
printf("%s\n", strchr(buf, '\n') + 1);
fclose(syms);
return 0;
}

View File

@ -17,3 +17,5 @@ void* memcpy(void* dest, const void* src, size_t n);
void* memset(void* dest, int c, size_t n);
int memcmp(const void* a, const void* b, size_t n);
void* memmove(void* dest, void* src, size_t n);
char* strdup(const char* src);

View File

@ -1,4 +1,5 @@
#include <string.h>
#include "std/string.h"
#include "std/stdlib.h"
size_t strlen(const char* __s)
{
@ -122,3 +123,11 @@ void* memmove(void* dest, void* src, size_t n)
}
return dest;
}
char* strdup(const char* src)
{
size_t length = strlen(src);
char* duplicated = (char*)kmalloc(length + 1);
memcpy(duplicated, src, length + 1);
return duplicated;
}

View File

@ -4,6 +4,8 @@
#include "errno.h"
#include "interrupts/Interrupts.h"
#include "memory/MemoryManager.h"
#include "std/stdlib.h"
#include "std/string.h"
#include "sys/elf/ELFLoader.h"
#include "thread/Scheduler.h"
@ -31,22 +33,28 @@ void sys_exec(Context* context, const char* pathname)
return;
}
// FIXME: This should be done later, but since there is a very high chance that loading the executed program's ELF
// image will overwrite ours, we have to do it here.
char* kpathname = strdup(
pathname); // Since we are going to free the original task's memory, we cannot use anything coming from it.
// FIXME: This should be done later, but since there is a very high chance that loading the executed program's
// ELF image will overwrite ours, we have to do it here.
ELFLoader::release_elf_image(Scheduler::current_task()->image);
// FIXME: Check the ELF image is valid before loading it into memory. This will allow us to first check, then free
// the previous image, then load, which should reduce the chances of loading failing to almost zero.
ELFImage* image = ELFLoader::load_elf_from_filesystem(pathname);
ELFImage* image = ELFLoader::load_elf_from_filesystem(kpathname);
if (!image)
{
MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK);
Scheduler::current_task()->image = nullptr;
kwarnln("exec(): ERROR: Failed to load program. Previous program has already been freed, thus cannot "
"return to it.");
kfree(kpathname);
kerrorln("exec(): ERROR: Failed to load program. Previous program has already been freed, thus cannot "
"return to it.");
return Scheduler::task_exit(context, -255);
}
kfree(kpathname);
Interrupts::disable();
ASSERT(!Interrupts::are_enabled()); // This part is pretty sensitive.