Skip to content

Commit 39a313d

Browse files
committed
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.
1 parent 637e707 commit 39a313d

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

examples/procfs2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static ssize_t procfile_write(struct file *file, const char __user *buff,
5555
return -EFAULT;
5656

5757
procfs_buffer[procfs_buffer_size & (PROCFS_MAX_SIZE - 1)] = '\0';
58+
*off += procfs_buffer_size;
5859
pr_info("procfile write %s\n", procfs_buffer);
5960

6061
return procfs_buffer_size;

examples/procfs3.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <linux/sched.h>
99
#include <linux/uaccess.h>
1010
#include <linux/version.h>
11+
#include <linux/minmax.h>
1112

1213
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
1314
#define HAVE_PROC_OPS
1415
#endif
1516

16-
#define PROCFS_MAX_SIZE 2048
17+
#define PROCFS_MAX_SIZE 2048UL
1718
#define PROCFS_ENTRY_FILENAME "buffer2k"
1819

1920
static struct proc_dir_entry *our_proc_file;
@@ -23,30 +24,26 @@ static unsigned long procfs_buffer_size = 0;
2324
static ssize_t procfs_read(struct file *filp, char __user *buffer,
2425
size_t length, loff_t *offset)
2526
{
26-
static int finished = 0;
27-
28-
if (finished) {
27+
if (*offset || procfs_buffer_size == 0) {
2928
pr_debug("procfs_read: END\n");
30-
finished = 0;
29+
*offset = 0;
3130
return 0;
3231
}
33-
finished = 1;
34-
32+
procfs_buffer_size = min(procfs_buffer_size, length);
3533
if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
3634
return -EFAULT;
35+
*offset += procfs_buffer_size;
3736

3837
pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
3938
return procfs_buffer_size;
4039
}
4140
static ssize_t procfs_write(struct file *file, const char __user *buffer,
4241
size_t len, loff_t *off)
4342
{
44-
if (len > PROCFS_MAX_SIZE)
45-
procfs_buffer_size = PROCFS_MAX_SIZE;
46-
else
47-
procfs_buffer_size = len;
43+
procfs_buffer_size = min(PROCFS_MAX_SIZE, len);
4844
if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
4945
return -EFAULT;
46+
*off += procfs_buffer_size;
5047

5148
pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
5249
return procfs_buffer_size;

0 commit comments

Comments
 (0)