2021-07-22 06:35:24 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* procfs3.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/uaccess.h>
|
2021-07-22 07:17:31 +08:00
|
|
|
#include <linux/version.h>
|
2022-09-21 13:53:06 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
|
2022-09-08 06:51:14 +08:00
|
|
|
#include <linux/minmax.h>
|
2022-09-21 13:53:06 +08:00
|
|
|
#endif
|
2021-07-22 07:17:31 +08:00
|
|
|
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
|
|
|
|
#define HAVE_PROC_OPS
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2022-09-08 06:51:14 +08:00
|
|
|
#define PROCFS_MAX_SIZE 2048UL
|
2021-07-22 06:58:13 +08:00
|
|
|
#define PROCFS_ENTRY_FILENAME "buffer2k"
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static struct proc_dir_entry *our_proc_file;
|
2021-07-22 06:35:24 +08:00
|
|
|
static char procfs_buffer[PROCFS_MAX_SIZE];
|
|
|
|
static unsigned long procfs_buffer_size = 0;
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static ssize_t procfs_read(struct file *filp, char __user *buffer,
|
|
|
|
size_t length, loff_t *offset)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2022-09-08 06:51:14 +08:00
|
|
|
if (*offset || procfs_buffer_size == 0) {
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_debug("procfs_read: END\n");
|
2022-09-08 06:51:14 +08:00
|
|
|
*offset = 0;
|
2021-07-22 06:35:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-09-08 06:51:14 +08:00
|
|
|
procfs_buffer_size = min(procfs_buffer_size, length);
|
2021-07-22 06:58:13 +08:00
|
|
|
if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
|
2021-07-22 06:35:24 +08:00
|
|
|
return -EFAULT;
|
2022-09-08 06:51:14 +08:00
|
|
|
*offset += procfs_buffer_size;
|
2021-09-02 15:15:07 +08:00
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
|
|
|
|
return procfs_buffer_size;
|
|
|
|
}
|
2021-09-04 17:53:29 +08:00
|
|
|
static ssize_t procfs_write(struct file *file, const char __user *buffer,
|
|
|
|
size_t len, loff_t *off)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2022-09-08 06:51:14 +08:00
|
|
|
procfs_buffer_size = min(PROCFS_MAX_SIZE, len);
|
2021-07-22 06:58:13 +08:00
|
|
|
if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
|
2021-07-22 06:35:24 +08:00
|
|
|
return -EFAULT;
|
2022-09-08 06:51:14 +08:00
|
|
|
*off += procfs_buffer_size;
|
2021-09-02 15:15:07 +08:00
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
|
|
|
|
return procfs_buffer_size;
|
|
|
|
}
|
2021-09-04 17:53:29 +08:00
|
|
|
static int procfs_open(struct inode *inode, struct file *file)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
try_module_get(THIS_MODULE);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-04 17:53:29 +08:00
|
|
|
static int procfs_close(struct inode *inode, struct file *file)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:17:31 +08:00
|
|
|
#ifdef HAVE_PROC_OPS
|
2021-09-02 15:15:07 +08:00
|
|
|
static struct proc_ops file_ops_4_our_proc_file = {
|
2021-07-22 06:58:13 +08:00
|
|
|
.proc_read = procfs_read,
|
|
|
|
.proc_write = procfs_write,
|
|
|
|
.proc_open = procfs_open,
|
|
|
|
.proc_release = procfs_close,
|
2021-07-22 06:35:24 +08:00
|
|
|
};
|
2021-07-22 07:17:31 +08:00
|
|
|
#else
|
2021-09-02 15:15:07 +08:00
|
|
|
static const struct file_operations file_ops_4_our_proc_file = {
|
2021-07-22 07:17:31 +08:00
|
|
|
.read = procfs_read,
|
|
|
|
.write = procfs_write,
|
|
|
|
.open = procfs_open,
|
|
|
|
.release = procfs_close,
|
|
|
|
};
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static int __init procfs3_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-09-02 15:15:07 +08:00
|
|
|
our_proc_file = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL,
|
|
|
|
&file_ops_4_our_proc_file);
|
|
|
|
if (our_proc_file == NULL) {
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_debug("Error: Could not initialize /proc/%s\n",
|
|
|
|
PROCFS_ENTRY_FILENAME);
|
2021-07-22 06:35:24 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2021-09-02 15:15:07 +08:00
|
|
|
proc_set_size(our_proc_file, 80);
|
|
|
|
proc_set_user(our_proc_file, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_debug("/proc/%s created\n", PROCFS_ENTRY_FILENAME);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-08 00:29:24 +08:00
|
|
|
|
|
|
|
static void __exit procfs3_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
|
|
|
|
pr_debug("/proc/%s removed\n", PROCFS_ENTRY_FILENAME);
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
module_init(procfs3_init);
|
|
|
|
module_exit(procfs3_exit);
|
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
MODULE_LICENSE("GPL");
|