From 4b939dcaabcbf7c2402192c7e6822e72609c4753 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 20 Sep 2022 20:28:58 -0300 Subject: [PATCH] init: initial implementation --- distro-files/etc/init/shell | 1 + distro-files/etc/init/shellenv | 3 + distro-files/etc/init/welcome | 3 + extras/init/Makefile | 13 +++ extras/init/main.c | 147 +++++++++++++++++++++++++++++++++ recipes/init | 22 +++++ source-recipes/init | 7 ++ 7 files changed, 196 insertions(+) create mode 100644 distro-files/etc/init/shell create mode 100644 distro-files/etc/init/shellenv create mode 100644 distro-files/etc/init/welcome create mode 100644 extras/init/Makefile create mode 100644 extras/init/main.c create mode 100644 recipes/init create mode 100644 source-recipes/init diff --git a/distro-files/etc/init/shell b/distro-files/etc/init/shell new file mode 100644 index 00000000..693c6cb4 --- /dev/null +++ b/distro-files/etc/init/shell @@ -0,0 +1 @@ +/usr/bin/bash diff --git a/distro-files/etc/init/shellenv b/distro-files/etc/init/shellenv new file mode 100644 index 00000000..1635499d --- /dev/null +++ b/distro-files/etc/init/shellenv @@ -0,0 +1,3 @@ +HOME=/ +PATH=/usr/bin/ +TERM=linux diff --git a/distro-files/etc/init/welcome b/distro-files/etc/init/welcome new file mode 100644 index 00000000..9e92726a --- /dev/null +++ b/distro-files/etc/init/welcome @@ -0,0 +1,3 @@ +Astral Operating System +Github repository: https://github.com/Mathewnd/Astral +Thank you for trying it out :) diff --git a/extras/init/Makefile b/extras/init/Makefile new file mode 100644 index 00000000..d521129d --- /dev/null +++ b/extras/init/Makefile @@ -0,0 +1,13 @@ +XCC=x86_64-astral-gcc + +all: init + +init: main.o + $(XCC) $(LDFLAGS) -o $@ $< + +%.o: %.c + $(XCC) -c $(CFLAGS) -o $@ $< + +install: + mkdir -p $(DESTDIR)/sbin/ + mv init $(DESTDIR)/sbin/init diff --git a/extras/init/main.c b/extras/init/main.c new file mode 100644 index 00000000..00a431ca --- /dev/null +++ b/extras/init/main.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +FILE* openordie(const char* name, const char* mode){ + + FILE* file = fopen(name, mode); + + if(!file){ + printf("init: Failed to open /etc/initlist: %s\n", strerror(errno)); + exit(1); + } + + return file; +} + +void oom(){ + printf("init: %s", strerror(ENOMEM)); + exit(1); +} + +void printwelcome(FILE* welcome){ + char buff[4096]; + + while(1){ + + size_t readc = fread(buff, 1, 4095, welcome); + + if(readc == 0) + break; + + buff[readc] = '\0'; + + printf("%s", buff); + + } + + printf("\n"); + +} + +int main(int argc, char* argv[]){ + + FILE* shell = openordie("/etc/init/shell", "r"); + FILE* shellenv = openordie("/etc/init/shellenv", "r"); + FILE* welcome = openordie("/etc/init/welcome", "r"); + + printwelcome(welcome); + + fclose(welcome); + + char readbuff[4096]; + size_t readc = fread(readbuff, 4095, 1, shell); + + if(ferror(shell)){ + printf("init: Failed to read from /etc/init/shell: %s\n", strerror(errno)); + exit(1); + } + + void* shellbuff = strtok(readbuff, "\n"); + + char* name = strtok(shellbuff, " "); + + // test if shell exists + + struct stat st; + + if(stat(name, &st) == -1){ + printf("init: stat failed on %s: %s\n", name, strerror(errno)); + exit(1); + } + + char* token = name; + size_t tokenc = 0; + + char** args = malloc(1); // initialize this + + + while(token){ + args = realloc(args, ++tokenc*sizeof(char*)); + if(!args) + oom(); + + args[tokenc-1] = malloc(strlen(token)+1); + + if(!args[tokenc-1]) + oom(); + + strcpy(args[tokenc-1], token); + + token = strtok(NULL, " "); + + } + + args = realloc(args, (tokenc+1)*sizeof(char*)); // for null terminator + if(!args) + oom(); + + args[tokenc] = NULL; + + char* envbuff; + size_t envsize; + + if(getline(&envbuff, envsize, shellenv) == -1){ + if(ferror(shellenv)){ + printf("init: getline failed on shellenv: %s\n", strerror(errno)); + exit(1); + } + else envbuff = NULL; + } + + + while(envbuff){ + + if(putenv(envbuff)) oom(); + + if(getline(&envbuff, envsize, shellenv) == -1 && ferror(shellenv)){ + printf("init: getline failed on shellenv: %s\n", strerror(errno)); + exit(1); + } + } + + // execute shell + + pid_t shellpid = fork(); + + if(shellpid == -1){ + printf("init: fork failed: %s\n", strerror(errno)); + exit(1); + } + + if(shellpid == 0){ + execv(name, args); + printf("init: exec failed: %s\n", strerror(errno)); + exit(1); + } + + int status; + + for(;;) waitpid(-1, &status, 0); + +} diff --git a/recipes/init b/recipes/init new file mode 100644 index 00000000..e4254a5a --- /dev/null +++ b/recipes/init @@ -0,0 +1,22 @@ +name=init +revision=1 +from_source=init +imagedeps="base-devel" +hostdeps="gcc-cross binutils-cross" +deps="mlibc" + +configure() { + true +} + +build() { + + make -C ${source_dir} -j ${parallelism} + +} + +install() { + + make -C ${source_dir} install DESTDIR=${dest_dir} + +} diff --git a/source-recipes/init b/source-recipes/init new file mode 100644 index 00000000..8abcdefa --- /dev/null +++ b/source-recipes/init @@ -0,0 +1,7 @@ +name=init +version=0 +source_dir="extras/init" + +regenerate() { + true +}