mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
Split ins_rm_mod
This commit is contained in:
@@ -7,3 +7,6 @@ Userland C programs used to test our kernel module.
|
||||
C programs require cross compiling, but give us more control over system calls.
|
||||
|
||||
These programs can also be compiled and used on host.
|
||||
|
||||
1. [myinsmod](myinsmod.c)
|
||||
1. [myrmmod](myrmmod.c)
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
/*
|
||||
http://stackoverflow.com/questions/5947286/how-can-linux-kernel-modules-be-loaded-from-c-code/38606527#38606527
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -13,14 +8,21 @@ http://stackoverflow.com/questions/5947286/how-can-linux-kernel-modules-be-loade
|
||||
#include <stdlib.h>
|
||||
|
||||
#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
||||
#define delete_module(name, flags) syscall(__NR_delete_module, name, flags)
|
||||
|
||||
int main(void) {
|
||||
int fd = open("hello.ko", O_RDONLY);
|
||||
int main(int argc, char **argv) {
|
||||
size_t image_size;
|
||||
void *image;
|
||||
int fd;
|
||||
struct stat st;
|
||||
|
||||
if (argc != 2) {
|
||||
puts("Usage ./prog mymodule.ko");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
fstat(fd, &st);
|
||||
size_t image_size = st.st_size;
|
||||
void *image = malloc(image_size);
|
||||
image_size = st.st_size;
|
||||
image = malloc(image_size);
|
||||
read(fd, image, image_size);
|
||||
close(fd);
|
||||
if (init_module(image, image_size, "") != 0) {
|
||||
@@ -28,9 +30,5 @@ int main(void) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
free(image);
|
||||
if (delete_module("hello", O_NONBLOCK) != 0) {
|
||||
perror("delete_modul");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
22
kernel_module/test/myrmmod.c
Normal file
22
kernel_module/test/myrmmod.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define delete_module(name, flags) syscall(__NR_delete_module, name, flags)
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
puts("Usage ./prog mymodule");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (delete_module(argv[1], O_NONBLOCK) != 0) {
|
||||
perror("delete_modul");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user