diff options
author | Arturs Artamonovs <dos21h@gmail.com> | 2023-11-26 08:22:42 +0000 |
---|---|---|
committer | Arturs Artamonovs <dos21h@gmail.com> | 2023-11-26 08:22:42 +0000 |
commit | 31e22a220a9b81dfc13ead45da800e97f9de02c9 (patch) | |
tree | bf78e01a410f05a8fa2d4521b9129195a3f02271 | |
parent | 5fd7ebaac1e94b84b282509cbcf9c69fbd57bb90 (diff) | |
download | md-content-31e22a220a9b81dfc13ead45da800e97f9de02c9.tar.gz md-content-31e22a220a9b81dfc13ead45da800e97f9de02c9.zip |
Add last part of procfs notes
-rw-r--r-- | md/notes/kernel/create_procfs_entry.md | 125 |
1 files changed, 121 insertions, 4 deletions
diff --git a/md/notes/kernel/create_procfs_entry.md b/md/notes/kernel/create_procfs_entry.md index 03d186f..dcb51c8 100644 --- a/md/notes/kernel/create_procfs_entry.md +++ b/md/notes/kernel/create_procfs_entry.md @@ -112,8 +112,56 @@ void procfs_entry_exit( void ) } ``` -Next step add data to read from file entry +Now module can be run and there is one file under the /proc/secret +Next step add data to read from file entry/ + +Update the file operation functions, so its possible to read some data from /proc/secret/file + +```c +char file_data[256] = "This is data\n"; +static int read_once=1; + + +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) { + int len=0; + if (read_once) { + read_once=0; + } else { + read_once=1; + return 0; + } + len = strlen(file_data); + if (copy_to_user(buffer, &file_data, len)) { + pr_err("secreate_read: Error\n"); + } + return len; +} + +static ssize_t secret_write(struct file *file, const char *buffer, size_t length, loff_t *offset) { + return 0; +} +``` + +Now some data is exposed if module is loaded and run command + +```sh +cat /proc/secret/file +``` + +output will be + +```sh +This is data +``` @@ -127,12 +175,81 @@ Next step add data to read from file entry ## Final result -__Makefile__ -```Makefile -``` __procfs_entry.c__ ```c +#include <linux/module.h> /* Needed by all modules */ +#include <linux/kernel.h> +#include <linux/proc_fs.h> + +static struct proc_dir_entry *procdir, *procfile; +char file_data[256] = "This is data\n"; +static int read_once=1; + +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) { + int len=0; + if (read_once) { + read_once=0; + } else { + read_once=1; + return 0; + } + len = strlen(file_data); + if (copy_to_user(buffer, &file_data, len)) { + pr_err("secreate_read: Error\n"); + } + return len; +} + +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 ) +{ + printk(KERN_DEBUG "Hello Procfs!\n"); + 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; + } + + return 0; +} + +void procfs_entry_exit( void ) +{ + printk(KERN_DEBUG "Exit Procfs!\n"); + proc_remove(procdir); + proc_remove(procfile); +} + +module_init( procfs_entry_init ); +module_exit( procfs_entry_exit ); + +MODULE_LICENSE("GPL") ``` ## Links |