aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

insmod.c (1431B)


      1 
      2 #if FEATURE_LINUX_SYSCALL
      3 #ifndef _GNU_SOURCE
      4 #define _GNU_SOURCE
      5 #endif
      6 #endif
      7 #include <sys/stat.h>
      8 #include <sys/syscall.h>
      9 
     10 #include <fcntl.h>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 #include <unistd.h>
     15 
     16 #include "util.h"
     17 
     18 static void
     19 usage(void)
     20 {
     21   eprintf("usage: %s filename [args...]\n", argv0);
     22 }
     23 
     24 // ?man insmod: insert a module into the Linux kernel
     25 // ?man arguments: filename [args ...]
     26 // ?man insmod inserts a kernel module from filename into the running kernel
     27 int
     28 main(int argc, char *argv[])
     29 {
     30   char       *buf = NULL, *opts = NULL;
     31   size_t      blen, plen        = 0;
     32   int         i, fd;
     33   ssize_t     n;
     34   struct stat sb;
     35 
     36   ARGBEGIN
     37   {
     38     default:
     39       usage();
     40   }
     41   ARGEND;
     42 
     43   if (argc < 1)
     44     usage();
     45 
     46   fd = open(argv[0], O_RDONLY);
     47   if (fd < 0)
     48     eprintf("open %s:", argv[0]);
     49   if (fstat(fd, &sb) < 0)
     50     eprintf("stat %s:", argv[0]);
     51   blen = sb.st_size;
     52   buf  = emalloc(blen);
     53 
     54   n = read(fd, buf, blen);
     55   if (n < 0 || (size_t)n != blen)
     56     eprintf("read:");
     57 
     58   argc--;
     59   argv++;
     60 
     61   for (i = 0; i < argc; i++)
     62     plen += strlen(argv[i]);
     63   if (plen > 0) {
     64     plen += argc;
     65     opts = ecalloc(1, plen);
     66     for (i = 0; i < argc; i++) {
     67       strcat(opts, argv[i]);
     68       if (i + 1 < argc)
     69         strcat(opts, " ");
     70     }
     71   }
     72 
     73   if (syscall(__NR_init_module, buf, blen, !opts ? "" : opts) < 0)
     74     eprintf("init_module:");
     75 
     76   free(opts);
     77   free(buf);
     78   return 0;
     79 }