diff options
author | Arturs Artamonovs <dos21h@gmail.com> | 2023-11-25 12:02:55 +0000 |
---|---|---|
committer | Arturs Artamonovs <dos21h@gmail.com> | 2023-11-25 12:02:55 +0000 |
commit | 5fd7ebaac1e94b84b282509cbcf9c69fbd57bb90 (patch) | |
tree | a6c2862b1ea0f06c06be2a68886b73cbea82359c /md/notes/kernel/create_procfs_entry.md | |
parent | c13284051093f0876e038fe65b99aba16d2578fd (diff) | |
download | md-content-5fd7ebaac1e94b84b282509cbcf9c69fbd57bb90.tar.gz md-content-5fd7ebaac1e94b84b282509cbcf9c69fbd57bb90.zip |
Add procfs and kernel topic page
Diffstat (limited to 'md/notes/kernel/create_procfs_entry.md')
-rw-r--r-- | md/notes/kernel/create_procfs_entry.md | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/md/notes/kernel/create_procfs_entry.md b/md/notes/kernel/create_procfs_entry.md new file mode 100644 index 0000000..03d186f --- /dev/null +++ b/md/notes/kernel/create_procfs_entry.md @@ -0,0 +1,144 @@ +title:Create procfs entry +keywords:kernel,linux,threads + +# Create procfs entry + +Lets extend hello world driver. + + +__Makefile__ +```Makefile +obj-m += procfs_entry.o + +KDIR ?= /lib/modules/$(shell uname -r)/build + +all: + make -C $(KDIR) M=$(PWD) modules + +clean: + make -C $(KDIR) M=$(PWD) clean +``` + +__procfs_entry.c__ +```c +#include <linux/module.h> /* Needed by all modules */ +#include <linux/kernel.h> + +int procfs_entry_init( void ) +{ + printk(KERN_DEBUG "Hello Procfs!\n"); + return 0; +} + +void procfs_entry_exit( void ) +{ + printk(KERN_DEBUG "Exit Procfs!\n"); +} + +module_init( procfs_entry_init ); +module_exit( procfs_entry_exit ); + +MODULE_LICENSE("GPL"); +``` + + +Create proc directory with + +```c +dir = proc_mkdir(DIR_NAME, PARENT_DIR); +``` + +under the directory create file that will contain data + +```c +proc_create("file", MODE, PARENT_DIR, PROC_FOPS) +``` + +Lets create /proc/secret/file. And define basic read/write operations. That at this +stage will do nothing. + +```c +#include <linux/proc_fs.h> + +static struct proc_dir_entry *procdir, *procfile; + +static int secret_open(struct inode *inode, struct file *file) { + return 0; +} + +static int secret_release(struct inode *inode, struct file *file) { + return 0; +} + +static ssize_t secret_read(struct file *file, char __user *buffer, size_t length, loff_t *offset) { + return 0; +} + +static ssize_t secret_write(struct file *file, const char *buffer, size_t length, loff_t *offset) { + return 0; +} + +static struct proc_ops procfs_ops = { + .proc_open = secret_open, + .proc_read = secret_read, + .proc_write = secret_write, + .proc_release = secret_release +}; + + +int procfs_entry_init( void ) { +... +procdir = proc_mkdir("secret", NULL); +if (procdir == NULL) { + pr_err("Cant create secret directory\n"); + return -1; +} + +procfile = proc_create("file", 0644, procdir, &procfs_ops); +if (procfile == NULL) { + pr_err("Cant create file under the secret\n"); + return -1; +} +... +} + +void procfs_entry_exit( void ) +{ + ... + //remove entries after unloading module + proc_remove(procdir); + proc_remove(procfile); + ... +} +``` + +Next step add data to read from file entry + + + + +## List of procfs functions used + +| Function name | Note | +|---|---| +| proc_mkdir | Create procfs directory | +| proc_create | Create procfs file | +| proc_remove | Remove procfs entry | + +## Final result + +__Makefile__ +```Makefile +``` + +__procfs_entry.c__ +```c +``` + +## Links + +https://elixir.bootlin.com/linux/v6.1.21/source/include/linux/proc_fs.h + + + + |