diff options
| author | turret <turret@duck.com> | 2023-11-19 19:13:40 -0600 | 
|---|---|---|
| committer | turret <turret@duck.com> | 2023-11-19 19:13:40 -0600 | 
| commit | 40bff7788865b519b79b1ee7eb9851914010ffb4 (patch) | |
| tree | b11af475d4eb933acb2d610c555d6715f76def4e | |
| parent | a8b2282eb88f24c3c5f461e1557fa2cf76ebc251 (diff) | |
| download | discord-bot-skeleton-40bff7788865b519b79b1ee7eb9851914010ffb4.tar.gz discord-bot-skeleton-40bff7788865b519b79b1ee7eb9851914010ffb4.tar.bz2 discord-bot-skeleton-40bff7788865b519b79b1ee7eb9851914010ffb4.zip  | |
misc: minor bugs and comments
sprinkle a little bit of commenting throughout the codebase.  hopefully
i dont regret what i've written
- clone: bottom of stack is passed through
- print: write newline at end of message
- initcall: functions return void type
very basic net startup.  hopefully i dont regret implementing the
websocket protocol myself in a language as holy as C.
| -rw-r--r-- | include/init.h | 2 | ||||
| -rw-r--r-- | init/init.c | 7 | ||||
| -rw-r--r-- | init/log.c | 12 | ||||
| -rw-r--r-- | init/subsys.c | 12 | ||||
| -rw-r--r-- | init/util.c | 1 | ||||
| -rw-r--r-- | net/net.c | 16 | 
6 files changed, 46 insertions, 4 deletions
diff --git a/include/init.h b/include/init.h index c9d8cba..79e60f1 100644 --- a/include/init.h +++ b/include/init.h @@ -1,7 +1,7 @@  #ifndef __INIT_H  #define __INIT_H -typedef int (*initcall_t)(void); +typedef void (*initcall_t)(void);  typedef initcall_t initcall_entry_t;  #define __define_initcall(fn, id) \ diff --git a/init/init.c b/init/init.c index 5363082..7c2d6a6 100644 --- a/init/init.c +++ b/init/init.c @@ -10,6 +10,10 @@  static unsigned long __1bsafebuf      __attribute__((used)) __attribute__((section(".1bsafebuf.init"))) = 0; +/* We start initcall levels at [1] instead of [0], so we must adjust +   in code for this minor design choice. Math is done on the level passed +   through i.e. do_initcall_level so that you can call it with (1) and have +   the expected initcall (l1_initcall) run. */  extern initcall_entry_t __initcall1_start[];  extern initcall_entry_t __initcall2_start[];  extern initcall_entry_t __initcall3_start[]; @@ -46,6 +50,9 @@ int main(void)  {      do_initcalls(); +    /* Reaper. Much like init. */ +    // BUG: doesnt actually work?? we have defunct processes still +    // TODO: fix bug      static sigset_t set;      sigaddset(&set, SIGCHLD);      sigprocmask(SIG_BLOCK, &set, NULL); @@ -28,6 +28,8 @@ int print(const char *fmt, ...)          fmt += 2;      } +    /* we essentially print the user's raw input to its own buffer, +       later we will parse it and print out ANSI colors and what not */      char buf[512];      va_list ap; @@ -42,13 +44,20 @@ int print(const char *fmt, ...)              break;      } -    /* spin lock, at the cost of architecture portability */ +    /* spin lock, at the cost of architecture portability +       concurrency is something that we need to adjust for, and the +       console will be scrambled and unreadable if we allow writing all +       at the same time. I considered simply writing all at once, but +       ended up just not caring enough to the point where spinlocks +       prevail. */      __asm__(".spin_lock:");      __asm__("mov rax, 1");      __asm__("xchg rax, [console_lock]");      __asm__("test rax, rax");      __asm__("jnz .spin_lock"); +    /* we want to support stuff without colons, but frankly I havent +       tested this at time of writing. will find out later */      if(buf[colon] == ':') {          writeputs(ANSI_RESET);          writeputs(colors[loglevel]); @@ -63,6 +72,7 @@ int print(const char *fmt, ...)          writeputs(buf);      }      writeputs(ANSI_RESET); +    write(STDOUT_FILENO, "\n", 1);      console_lock = 0;      return 0; diff --git a/init/subsys.c b/init/subsys.c index 0435f1a..62ee54e 100644 --- a/init/subsys.c +++ b/init/subsys.c @@ -21,7 +21,9 @@ struct subsystem_info {  int __subsystem_entry(struct subsystem_info *info)  { - +    /* entry point from clone(). we setup the process name so we know +       what we are looking at from a glance in a ps view or htop or +       whatever. */      char *name = malloc(16 * sizeof(char));      snprintf(name, 16, NAME_SHORTHAND ": %s", info->fn_name);      name[15] = '\0'; @@ -30,6 +32,7 @@ int __subsystem_entry(struct subsystem_info *info)      int ret = info->fn(); +    /* we are expected to free info before  exiting */      free(info);      return ret; @@ -37,15 +40,20 @@ int __subsystem_entry(struct subsystem_info *info)  int __impl_start_subsystem(char *fn_name, int (*fn)(void))  { +    /* because CLONE_VM is being set, our stack is not duplicated and +       therefore we need to map a stack */      void *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK | MAP_PRIVATE, -1, 0);      if((long)stack < 0)          die("subsys mmap:"); +    /* the libc gods have graced us with the ability to pass one (1) arg +       to the function. struct required. the absence of a free is not a +       memory leak because we free it above. */      struct subsystem_info *info = malloc(sizeof(struct subsystem_info));      info->fn_name = fn_name;      info->fn = fn; -    int pid = clone((int (*)(void *))__subsystem_entry, stack, CLONE_FILES | CLONE_VM, info); +    int pid = clone((int (*)(void *))__subsystem_entry, (void *)((long)stack + STACK_SIZE), CLONE_FILES | CLONE_VM, info);      if(pid < 0) {          munmap(stack, STACK_SIZE);          die("subsys clone:"); diff --git a/init/util.c b/init/util.c index 1a08234..4411632 100644 --- a/init/util.c +++ b/init/util.c @@ -3,6 +3,7 @@  #include <stdlib.h>  #include <string.h> +/* thank you suckless team (see libsl) */  void die(const char *fmt, ...)  {      va_list ap; @@ -1,2 +1,18 @@ +#include <unistd.h> + +#include <init.h> +#include <log.h>  #include <subsys.h> +int net_subsystem(void) +{ +    print(LOG_NOTICE "net: starting net subsystem"); +    usleep(10000); // do net stuff +    return 0; +} + +void net_initcall() +{ +    start_subsystem(net_subsystem); +} +l1_initcall(net_initcall);  | 
