linux.conf.au The Compulsory Module Rant #include #include #include #include #include #include #include #define FAIL(msg, ...) do { fprintf(stderr,msg,##__VA_ARGS__); exit(1); } while(0) int main(int argc, char *argv[]) { int fd, len = 0, max = 1000, ret; void *buf = malloc(max); if (argc != 2 && argc != 3) FAIL("Usage: insmod filename [options]\n"); if ((fd = open(argv[1], O_RDONLY)) < 0) FAIL("Can't open %s: %s\n", argv[1], strerror(errno)); while ((ret = read(fd, buf + len, max-len)) > 0) { len += ret; if (len == max) buf = realloc(buf, max *= 2); } if (ret < 0) FAIL("Reading %s: %s\n", argv[1], strerror(errno)); if (syscall(__NR_init_module, buf, len, argv[2] ?: "") < 0) FAIL("Error inserting `%s': %s\n", argv[1], strerror(errno)); exit(0); }