myinsmod.c 801 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #define _GNU_SOURCE
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <sys/syscall.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
  10. int main(int argc, char **argv) {
  11. const char *params;
  12. int fd;
  13. size_t image_size;
  14. struct stat st;
  15. void *image;
  16. if (argc < 2) {
  17. puts("Usage ./prog mymodule.ko [args]");
  18. return EXIT_FAILURE;
  19. }
  20. if (argc < 3) {
  21. params = "";
  22. } else {
  23. params = argv[2];
  24. }
  25. fd = open(argv[1], O_RDONLY);
  26. fstat(fd, &st);
  27. image_size = st.st_size;
  28. image = malloc(image_size);
  29. read(fd, image, image_size);
  30. close(fd);
  31. if (init_module(image, image_size, params) != 0) {
  32. perror("init_module");
  33. return EXIT_FAILURE;
  34. }
  35. free(image);
  36. return EXIT_SUCCESS;
  37. }