Skip to content

Instantly share code, notes, and snippets.

@anryko
Created March 29, 2016 20:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anryko/c8c8788ccf7d553a140a03aba22cab88 to your computer and use it in GitHub Desktop.
Save anryko/c8c8788ccf7d553a140a03aba22cab88 to your computer and use it in GitHub Desktop.
Simple kernel module example. Lists process list and count.
#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // KERN_INFO
#include <linux/sched.h> // for_each_process, pr_info
void procs_info_print(void)
{
struct task_struct* task_list;
size_t process_counter = 0;
for_each_process(task_list) {
pr_info("== %s [%d]\n", task_list->comm, task_list->pid);
++process_counter;
}
printk(KERN_INFO "== Number of process: %zu\n", process_counter);
}
int init_module(void)
{
printk(KERN_INFO "[ INIT ==\n");
procs_info_print();
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "== CLEANUP ]\n");
}
MODULE_LICENSE("MIT");
obj-m += lkm_hello1.o
KDIR ?= /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment