mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
Userland test programs
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
obj-m += $(addsuffix .o, $(notdir $(basename $(wildcard $(BR2_EXTERNAL_KERNEL_MODULE_PATH)/*.c))))
|
||||
ccflags-y := -Wno-declaration-after-statement -std=gnu99
|
||||
|
||||
.PHONY: all clean
|
||||
.PHONY: all clean test
|
||||
|
||||
all:
|
||||
all: test
|
||||
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' modules
|
||||
|
||||
clean:
|
||||
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' clean
|
||||
|
||||
test:
|
||||
$(MAKE) -C '$(PWD)/test'
|
||||
|
||||
@@ -43,7 +43,9 @@ void cleanup_module(void)
|
||||
{
|
||||
/* This waits for the work to finish. From docstring: */
|
||||
/* > Cancel @work and wait for its execution to finish. */
|
||||
cancel_work_sync(&work);
|
||||
cancel_work(&work);
|
||||
|
||||
/*cancel_work(&work);*/
|
||||
|
||||
destroy_workqueue(queue);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ define KERNEL_MODULE_BUILD_CMDS
|
||||
endef
|
||||
|
||||
define KERNEL_MODULE_INSTALL_TARGET_CMDS
|
||||
$(INSTALL) -D -m 0755 $(@D)/*.ko '$(TARGET_DIR)'
|
||||
$(INSTALL) -D -m 0655 $(@D)/*.ko '$(TARGET_DIR)'
|
||||
$(INSTALL) -D -m 0755 $(@D)/test/ins_rm_mod '$(TARGET_DIR)'
|
||||
endef
|
||||
|
||||
$(eval $(kernel-module))
|
||||
|
||||
9
kernel_module/test/Makefile
Normal file
9
kernel_module/test/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
CC = gcc
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
ins_rm_mod: ins_rm_mod.c
|
||||
$(CC) -o '$@' '$<'
|
||||
|
||||
clean:
|
||||
rm ins_rm_mod
|
||||
9
kernel_module/test/README.md
Normal file
9
kernel_module/test/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Test
|
||||
|
||||
Userland C programs used to test our kernel module.
|
||||
|
||||
`sh` programs are simpler, and installed copied directly with an overlay.
|
||||
|
||||
C programs require cross compiling, but give us more control over system calls.
|
||||
|
||||
These programs can also be compiled and used on host.
|
||||
BIN
kernel_module/test/ins_rm_mod
Executable file
BIN
kernel_module/test/ins_rm_mod
Executable file
Binary file not shown.
36
kernel_module/test/ins_rm_mod.c
Normal file
36
kernel_module/test/ins_rm_mod.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#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);
|
||||
struct stat st;
|
||||
fstat(fd, &st);
|
||||
size_t image_size = st.st_size;
|
||||
void *image = malloc(image_size);
|
||||
read(fd, image, image_size);
|
||||
close(fd);
|
||||
if (init_module(image, image_size, "") != 0) {
|
||||
perror("init_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
free(image);
|
||||
if (delete_module("hello", O_NONBLOCK) != 0) {
|
||||
perror("delete_modul");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user