2021-07-22 06:35:24 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* procfs1.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/uaccess.h>
|
2021-07-22 07:17:31 +08:00
|
|
|
#include <linux/version.h>
|
|
|
|
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
|
|
|
|
#define HAVE_PROC_OPS
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
#define procfs_name "helloworld"
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static struct proc_dir_entry *our_proc_file;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2023-03-24 11:36:33 +08:00
|
|
|
static ssize_t procfile_read(struct file *file_pointer, char __user *buffer,
|
2021-09-04 17:53:29 +08:00
|
|
|
size_t buffer_length, loff_t *offset)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-07-29 16:10:52 +08:00
|
|
|
char s[13] = "HelloWorld!\n";
|
|
|
|
int len = sizeof(s);
|
|
|
|
ssize_t ret = len;
|
|
|
|
|
|
|
|
if (*offset >= len || copy_to_user(buffer, s, len)) {
|
2021-08-07 23:33:37 +08:00
|
|
|
pr_info("copy_to_user failed\n");
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2023-03-24 11:36:33 +08:00
|
|
|
pr_info("procfile read %s\n", file_pointer->f_path.dentry->d_name.name);
|
2021-07-29 16:10:52 +08:00
|
|
|
*offset += len;
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
2021-07-29 16:10:52 +08:00
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:17:31 +08:00
|
|
|
#ifdef HAVE_PROC_OPS
|
2021-07-22 06:35:24 +08:00
|
|
|
static const struct proc_ops proc_file_fops = {
|
2021-07-22 06:58:13 +08:00
|
|
|
.proc_read = procfile_read,
|
2021-07-22 06:35:24 +08:00
|
|
|
};
|
2021-07-22 07:17:31 +08:00
|
|
|
#else
|
|
|
|
static const struct file_operations proc_file_fops = {
|
|
|
|
.read = procfile_read,
|
|
|
|
};
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static int __init procfs1_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-09-02 15:15:07 +08:00
|
|
|
our_proc_file = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
|
|
|
|
if (NULL == our_proc_file) {
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
|
2021-07-22 06:35:24 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("/proc/%s created\n", procfs_name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static void __exit procfs1_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-09-02 15:15:07 +08:00
|
|
|
proc_remove(our_proc_file);
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_info("/proc/%s removed\n", procfs_name);
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
module_init(procfs1_init);
|
|
|
|
module_exit(procfs1_exit);
|
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
MODULE_LICENSE("GPL");
|