Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Created January 8, 2024 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maurorappa/b6107f032c9af67b935aa9e7c4e03d70 to your computer and use it in GitHub Desktop.
Save maurorappa/b6107f032c9af67b935aa9e7c4e03d70 to your computer and use it in GitHub Desktop.
create a /proc file
ifneq ($(KERNELRELEASE),)
obj-m := proc.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
KBUILD_CFLAGS += $(call cc-option,-Wno-error,)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int hello_proc_show(struct seq_file *m, void *v) {
const int val = 13;
//seq_printf(m, "Hello proc!\n");
seq_printf(m, "%d!\n", val);
return 0;
}
static int hello_proc_open(struct inode *inode, struct file *file) {
return single_open(file, hello_proc_show, NULL);
}
static const struct proc_ops hello_proc_fops = {
.proc_open = hello_proc_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
static int __init hello_proc_init(void) {
proc_create("hello_proc", 0, NULL, &hello_proc_fops);
return 0;
}
static void __exit hello_proc_exit(void) {
remove_proc_entry("hello_proc", NULL);
}
MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);
@maurorappa
Copy link
Author

for recent kernels >5.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment