ubase/insmod.c

70 lines
1.1 KiB
C
Raw Normal View History

2013-08-07 10:43:55 +00:00
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
2014-06-30 18:03:41 +00:00
#include <sys/syscall.h>
2013-08-07 10:43:55 +00:00
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-30 18:03:41 +00:00
#include <unistd.h>
2013-08-07 10:43:55 +00:00
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s filename [args...]\n", argv0);
2013-08-07 10:43:55 +00:00
}
int
main(int argc, char *argv[])
{
char *buf = NULL, *opts = NULL;
size_t blen, plen = 0;
2013-08-07 10:43:55 +00:00
int i, fd;
ssize_t n;
struct stat sb;
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc < 1)
usage();
fd = open(argv[0], O_RDONLY);
if (fd < 0)
eprintf("open %s:", argv[0]);
if (fstat(fd, &sb) < 0)
eprintf("stat %s:", argv[0]);
blen = sb.st_size;
buf = emalloc(blen);
2013-08-07 10:43:55 +00:00
n = read(fd, buf, blen);
2014-07-09 15:39:32 +00:00
if (n < 0 || (size_t)n != blen)
2013-08-07 10:43:55 +00:00
eprintf("read:");
argc--;
argv++;
for (i = 0; i < argc; i++)
plen += strlen(argv[i]);
if (plen > 0) {
plen += argc;
opts = ecalloc(1, plen);
2013-08-07 10:43:55 +00:00
for (i = 0; i < argc; i++) {
strcat(opts, argv[i]);
if (i + 1 < argc)
strcat(opts, " ");
}
}
if (syscall(__NR_init_module, buf, blen, !opts ? "" : opts) < 0)
2013-08-07 10:43:55 +00:00
eprintf("init_module:");
free(opts);
free(buf);
2014-10-02 22:45:25 +00:00
return 0;
2013-08-07 10:43:55 +00:00
}