Issue
I'm trying to handle the writting operation on my proc
module, but the pointer function is not working.
When execute cat
on the file, dmesg
command shows the pr_info defined on my_read
function
$ cat /proc/myprocfile
$ sudo dmesg | grep 'my_read'
[ 5788.919508] myprocfile my_read handler
But when try to write on the file, dmesg
command never shows the pr_info defined on my_write
function
$ echo 1 > /proc/myprocfile
$ sudo dmesg | grep 'my_write'
My module
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
#define FILE_NAME "myprocfile"
static struct proc_dir_entry *my_proc_file;
static ssize_t my_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
{
pr_info("%s my_read handler\n", FILE_NAME);
return -1;
}
static ssize_t my_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
{
pr_info("%s my_write handler\n", FILE_NAME);
return -1;
}
static const struct proc_ops proc_file_fops = {
.proc_read = my_read,
.proc_write = my_write
};
static int __init proc_init(void)
{
my_proc_file = proc_create(
FILE_NAME,
0644,
NULL,
&proc_file_fops
);
return 0;
}
static void __exit proc_exit(void)
{
proc_remove(my_proc_file);
}
module_init(proc_init);
module_exit(proc_exit);
MODULE_LICENSE("GPL");
This is my Makefile
obj-m += proc_test.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
My kernel version is 5.11.0-40-generic
Solution
I've resolved the issue, just changed the value of permissions from 0644 to 0666 on proc_create_function
.
static int __init proc_init(void)
{
my_proc_file = proc_create(
FILE_NAME,
0666, // Updated value
NULL,
&proc_file_fops
);
return 0;
}
Answered By - carlos menjivar Answer Checked By - Mary Flores (WPSolving Volunteer)