Here's a kernel module you can try: `my_critical_proc.c`You'll also need to add this `Makefile` to the same directory as the above C file.1. Run `make`
2. Run `sudo insmod my_critical_proc.ko` to load the module
3. Run `sudo sh -c "echo 1 > /sys/kernel/my_critical_proc/initiate"` to initiate the procedure
4. Run `sudo rmmod my_critical_proc` to remove the module
Use `dmesg` to check how things are going.
Let me know how it works for you.
Code:
#include <linux/kernel.h>#include <linux/kobject.h>#include <linux/module.h>static struct kobject *the_kobj;static ssize_t sysfs_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf){ const char* message = "just a useless test of the read function"; return sprintf(buf, "%s\n", message);}static ssize_t sysfs_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){ printk(KERN_INFO "my_critical_proc: runing the critical procedure\n"); local_irq_disable(); // do your stuff local_irq_enable(); return count;}static struct kobj_attribute sysfs_attribute = __ATTR( initiate, 0660, sysfs_read, sysfs_write);static int my_critical_proc_init(void){ printk(KERN_INFO "my_critical_proc: init\n"); the_kobj = kobject_create_and_add("my_critical_proc", kernel_kobj); if(!the_kobj) { printk(KERN_ALERT "my_critical_proc: kobject_create_and_add failed\n"); return -ENOMEM; } if(sysfs_create_file(the_kobj, &sysfs_attribute.attr)) { printk(KERN_ALERT "my_critical_proc: failed to create the sys file\n"); return -EFAULT; } return 0;}static void my_critical_proc_exit(void){ kobject_put(the_kobj); printk(KERN_INFO "my_critical_proc: exit\n");}module_init(my_critical_proc_init);module_exit(my_critical_proc_exit);MODULE_DESCRIPTION("Description");MODULE_AUTHOR("Copyright");MODULE_LICENSE("GPL v2");
Code:
obj-m += my_critical_proc.oall:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
2. Run `sudo insmod my_critical_proc.ko` to load the module
3. Run `sudo sh -c "echo 1 > /sys/kernel/my_critical_proc/initiate"` to initiate the procedure
4. Run `sudo rmmod my_critical_proc` to remove the module
Use `dmesg` to check how things are going.
Let me know how it works for you.
Statistics: Posted by JinShil — Fri Jan 26, 2024 5:59 am