mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
Rename test to user
This commit is contained in:
15
kernel_module/user/Makefile
Normal file
15
kernel_module/user/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
.PHONY: clean
|
||||
|
||||
CC ?= gcc
|
||||
IN_EXT ?= .c
|
||||
OUT_EXT ?= .out
|
||||
|
||||
OUTS := $(addsuffix $(OUT_EXT), $(basename $(wildcard *$(IN_EXT))))
|
||||
|
||||
all: $(OUTS)
|
||||
|
||||
%$(OUT_EXT): %$(IN_EXT)
|
||||
$(CC) -o '$@' '$<'
|
||||
|
||||
clean:
|
||||
rm -f *'$(OUT_EXT)'
|
||||
12
kernel_module/user/README.md
Normal file
12
kernel_module/user/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# User
|
||||
|
||||
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.
|
||||
|
||||
1. [myinsmod](myinsmod.c)
|
||||
1. [myrmmod](myrmmod.c)
|
||||
11
kernel_module/user/init_hello.c
Normal file
11
kernel_module/user/init_hello.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/* Replacement init example for when we feel like running
|
||||
* a single non-interactive single executable Linux distro. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
puts("hello world");
|
||||
while (1)
|
||||
sleep(0xFFFFFFFF);
|
||||
}
|
||||
35
kernel_module/user/ioctl.c
Normal file
35
kernel_module/user/ioctl.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../ioctl.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd, ret;
|
||||
|
||||
if (argc < 4) {
|
||||
puts("Usage: ./prog <ioctl-file> <cmd> <arg>");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
ret = ioctl(fd, IOCTL0, argv[3]);
|
||||
if (ret == -1) {
|
||||
perror("ioctl");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf("ret = %d\n", ret);
|
||||
printf("errno = %d\n", errno);
|
||||
close(fd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
34
kernel_module/user/myinsmod.c
Normal file
34
kernel_module/user/myinsmod.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#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 init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
||||
|
||||
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);
|
||||
image_size = st.st_size;
|
||||
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);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
22
kernel_module/user/myrmmod.c
Normal file
22
kernel_module/user/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_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
34
kernel_module/user/poll.c
Normal file
34
kernel_module/user/poll.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <fcntl.h> /* creat, O_CREAT */
|
||||
#include <poll.h> /* poll */
|
||||
#include <stdio.h> /* printf, puts, snprintf */
|
||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||
#include <unistd.h> /* read */
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char buf[1024], path[1024];
|
||||
int fd, i, n;
|
||||
short revents;
|
||||
struct pollfd pfd;
|
||||
|
||||
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
while (1) {
|
||||
puts("loop");
|
||||
i = poll(&pfd, 1, -1);
|
||||
if (i == -1) {
|
||||
perror("poll");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
revents = pfd.revents;
|
||||
if (revents & POLLIN) {
|
||||
n = read(pfd.fd, buf, sizeof(buf));
|
||||
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user