mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-29 13:04:27 +01:00
Spaces to tabs
This commit is contained in:
@@ -19,23 +19,23 @@ static u32 value = 42;
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
struct dentry *file;
|
struct dentry *file;
|
||||||
dir = debugfs_create_dir("lkmc_debugfs", 0);
|
dir = debugfs_create_dir("lkmc_debugfs", 0);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
printk(KERN_ALERT "debugfs_create_dir failed");
|
printk(KERN_ALERT "debugfs_create_dir failed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
file = debugfs_create_u32("myfile", 0666, dir, &value);
|
file = debugfs_create_u32("myfile", 0666, dir, &value);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
printk(KERN_ALERT "debugfs_create_u32 failed");
|
printk(KERN_ALERT "debugfs_create_u32 failed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
debugfs_remove_recursive(dir);
|
debugfs_remove_recursive(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -1,38 +1,38 @@
|
|||||||
/*
|
/*
|
||||||
Exports the lkmc_dep which dep2.ko uses.
|
Exports the lkmc_dep which dep2.ko uses.
|
||||||
|
|
||||||
insmod /dep.ko
|
insmod /dep.ko
|
||||||
# dmesg => 0
|
# dmesg => 0
|
||||||
# dmesg => 0
|
# dmesg => 0
|
||||||
# dmesg => ...
|
# dmesg => ...
|
||||||
insmod /dep2.ko
|
insmod /dep2.ko
|
||||||
# dmesg => 1
|
# dmesg => 1
|
||||||
# dmesg => 2
|
# dmesg => 2
|
||||||
# dmesg => ...
|
# dmesg => ...
|
||||||
rmmod dep
|
rmmod dep
|
||||||
# Fails because dep2 uses it.
|
# Fails because dep2 uses it.
|
||||||
rmmod dep2
|
rmmod dep2
|
||||||
# Dmesg stops incrementing.
|
# Dmesg stops incrementing.
|
||||||
rmmod dep
|
rmmod dep
|
||||||
|
|
||||||
sys visibility:
|
sys visibility:
|
||||||
|
|
||||||
dmesg -n 1
|
dmesg -n 1
|
||||||
insmod /dep.ko
|
insmod /dep.ko
|
||||||
insmod /dep2.ko
|
insmod /dep2.ko
|
||||||
ls -l /sys/module/dep/holders
|
ls -l /sys/module/dep/holders
|
||||||
# => ../../dep2
|
# => ../../dep2
|
||||||
cat refcnt
|
cat refcnt
|
||||||
# => 1
|
# => 1
|
||||||
|
|
||||||
depmod:
|
depmod:
|
||||||
|
|
||||||
grep dep "/lib/module/"*"/depmod"
|
grep dep "/lib/module/"*"/depmod"
|
||||||
# extra/dep2.ko: extra/dep.ko
|
# extra/dep2.ko: extra/dep.ko
|
||||||
# extra/dep.ko:
|
# extra/dep.ko:
|
||||||
modprobe dep
|
modprobe dep
|
||||||
# lsmod
|
# lsmod
|
||||||
# Both dep and dep2 were loaded.
|
# Both dep and dep2 were loaded.
|
||||||
|
|
||||||
TODO: at what point does buildroot / busybox generate that file?
|
TODO: at what point does buildroot / busybox generate that file?
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ static int myinit(void)
|
|||||||
printk(KERN_ALERT "debugfs_create_dir failed");
|
printk(KERN_ALERT "debugfs_create_dir failed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
file = debugfs_create_file("f", 0666, dir, NULL, &fops);
|
file = debugfs_create_file("f", 0666, dir, NULL, &fops);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
printk(KERN_ALERT "debugfs_create_file failed");
|
printk(KERN_ALERT "debugfs_create_file failed");
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ MODULE_LICENSE("GPL");
|
|||||||
* Return value from kernel docs:*
|
* Return value from kernel docs:*
|
||||||
*
|
*
|
||||||
* enum irqreturn
|
* enum irqreturn
|
||||||
* @IRQ_NON interrupt was not from this device or was not handled
|
* @IRQ_NON interrupt was not from this device or was not handled
|
||||||
* @IRQ_HANDLED interrupt was handled by this device
|
* @IRQ_HANDLED interrupt was handled by this device
|
||||||
* @IRQ_WAKE_THREAD handler requests to wake the handler thread
|
* @IRQ_WAKE_THREAD handler requests to wake the handler thread
|
||||||
*/
|
*/
|
||||||
static irqreturn_t handler(int i, void *v)
|
static irqreturn_t handler(int i, void *v)
|
||||||
|
|||||||
@@ -12,23 +12,23 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
size_t image_size;
|
size_t image_size;
|
||||||
void *image;
|
void *image;
|
||||||
int fd;
|
int fd;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
puts("Usage ./prog mymodule.ko");
|
puts("Usage ./prog mymodule.ko");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
fd = open(argv[1], O_RDONLY);
|
fd = open(argv[1], O_RDONLY);
|
||||||
fstat(fd, &st);
|
fstat(fd, &st);
|
||||||
image_size = st.st_size;
|
image_size = st.st_size;
|
||||||
image = malloc(image_size);
|
image = malloc(image_size);
|
||||||
read(fd, image, image_size);
|
read(fd, image, image_size);
|
||||||
close(fd);
|
close(fd);
|
||||||
if (init_module(image, image_size, "") != 0) {
|
if (init_module(image, image_size, "") != 0) {
|
||||||
perror("init_module");
|
perror("init_module");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
free(image);
|
free(image);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ int main(int argc, char **argv) {
|
|||||||
puts("Usage ./prog mymodule");
|
puts("Usage ./prog mymodule");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (delete_module(argv[1], O_NONBLOCK) != 0) {
|
if (delete_module(argv[1], O_NONBLOCK) != 0) {
|
||||||
perror("delete_module");
|
perror("delete_module");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,29 +6,29 @@
|
|||||||
#include <unistd.h> /* read */
|
#include <unistd.h> /* read */
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
char buf[1024], path[1024];
|
char buf[1024], path[1024];
|
||||||
int fd, i, n;
|
int fd, i, n;
|
||||||
short revents;
|
short revents;
|
||||||
struct pollfd pfd;
|
struct pollfd pfd;
|
||||||
|
|
||||||
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
|
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("open");
|
perror("open");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pfd.fd = fd;
|
pfd.fd = fd;
|
||||||
pfd.events = POLLIN;
|
pfd.events = POLLIN;
|
||||||
while (1) {
|
while (1) {
|
||||||
puts("loop");
|
puts("loop");
|
||||||
i = poll(&pfd, 1, -1);
|
i = poll(&pfd, 1, -1);
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
perror("poll");
|
perror("poll");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
revents = pfd.revents;
|
revents = pfd.revents;
|
||||||
if (revents & POLLIN) {
|
if (revents & POLLIN) {
|
||||||
n = read(pfd.fd, buf, sizeof(buf));
|
n = read(pfd.fd, buf, sizeof(buf));
|
||||||
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
|
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user