procfs{2, 3}: Change to use offset parameter

To make sure the behavior of the read and write operations are correct
with offset, update it each time. Also, since it is using the offset,
modify the part of read for removing unnecessary variable.
This commit is contained in:
linD026 2022-09-08 06:51:14 +08:00
parent 637e707a1a
commit 39a313dc20
2 changed files with 9 additions and 11 deletions

View File

@ -55,6 +55,7 @@ static ssize_t procfile_write(struct file *file, const char __user *buff,
return -EFAULT;
procfs_buffer[procfs_buffer_size & (PROCFS_MAX_SIZE - 1)] = '\0';
*off += procfs_buffer_size;
pr_info("procfile write %s\n", procfs_buffer);
return procfs_buffer_size;

View File

@ -8,12 +8,13 @@
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/minmax.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
#define HAVE_PROC_OPS
#endif
#define PROCFS_MAX_SIZE 2048
#define PROCFS_MAX_SIZE 2048UL
#define PROCFS_ENTRY_FILENAME "buffer2k"
static struct proc_dir_entry *our_proc_file;
@ -23,17 +24,15 @@ static unsigned long procfs_buffer_size = 0;
static ssize_t procfs_read(struct file *filp, char __user *buffer,
size_t length, loff_t *offset)
{
static int finished = 0;
if (finished) {
if (*offset || procfs_buffer_size == 0) {
pr_debug("procfs_read: END\n");
finished = 0;
*offset = 0;
return 0;
}
finished = 1;
procfs_buffer_size = min(procfs_buffer_size, length);
if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
return -EFAULT;
*offset += procfs_buffer_size;
pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
return procfs_buffer_size;
@ -41,12 +40,10 @@ static ssize_t procfs_read(struct file *filp, char __user *buffer,
static ssize_t procfs_write(struct file *file, const char __user *buffer,
size_t len, loff_t *off)
{
if (len > PROCFS_MAX_SIZE)
procfs_buffer_size = PROCFS_MAX_SIZE;
else
procfs_buffer_size = len;
procfs_buffer_size = min(PROCFS_MAX_SIZE, len);
if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
return -EFAULT;
*off += procfs_buffer_size;
pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
return procfs_buffer_size;