From 7a98e998c5cda484611d19854649ab8535f503d4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 14 Feb 2023 17:09:59 +0100 Subject: add simplet init system --- lib/exec/exec.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/exec/exec.h | 6 ++++++ lib/sys/unmount.h | 11 +++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/exec/exec.c create mode 100644 lib/exec/exec.h create mode 100644 lib/sys/unmount.h (limited to 'lib') diff --git a/lib/exec/exec.c b/lib/exec/exec.c new file mode 100644 index 0000000..42fd2c1 --- /dev/null +++ b/lib/exec/exec.c @@ -0,0 +1,38 @@ +#include "exec.h" +#include "../sys/execve.h" +#include "../env/env.h" +#include "../cstr/cstr.h" +#include "../malloc/malloc.h" + +int exec(const char **argv) +{ + const char *path = getenv("PATH"); + const char *end; + char *filename; + + while (*path) { + end = path; + + while (*end && *end != ':') ++end; + + filename = malloc((end - path) + cstr_length(argv[0]) + 2); + + for (int i = 0; i < end - path; ++i) + filename[i] = path[i]; + + filename[end - path] = '/'; + + for (int i = 0; i < cstr_length(argv[0]) + 1; ++i) + filename[(end - path) + i + 1] = argv[0][i]; + + execve(filename, argv, getenvp()); + free(filename); + + path = end; + + if (*path == ':') + ++path; + } + + return execve(argv[0], argv, getenvp()); +} diff --git a/lib/exec/exec.h b/lib/exec/exec.h new file mode 100644 index 0000000..fe7e811 --- /dev/null +++ b/lib/exec/exec.h @@ -0,0 +1,6 @@ +#ifndef EXEC_H +#define EXEC_H + +int exec(const char **argv); + +#endif diff --git a/lib/sys/unmount.h b/lib/sys/unmount.h new file mode 100644 index 0000000..1331e65 --- /dev/null +++ b/lib/sys/unmount.h @@ -0,0 +1,11 @@ +#ifndef UNMOUNT_H +#define UNMOUNT_H + +#include "syscalls.h" + +int unmount(char *pathname, int flags) +{ + return syscall(UMOUNT2, pathname, flags); +} + +#endif -- cgit v1.2.3-70-g09d2