mirror of
https://github.com/sysprog21/lkmpg.git
synced 2024-11-22 05:41:38 +08:00
8c641a2deb
proc_remove does nothing when the argument is NULL[1]. We don't need proc_remove after proc_create failed. [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/generic.c#n789
69 lines
1.5 KiB
C
69 lines
1.5 KiB
C
/*
|
|
* procfs1.c
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/version.h>
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
|
|
#define HAVE_PROC_OPS
|
|
#endif
|
|
|
|
#define procfs_name "helloworld"
|
|
|
|
static struct proc_dir_entry *our_proc_file;
|
|
|
|
static ssize_t procfile_read(struct file *file_pointer, char __user *buffer,
|
|
size_t buffer_length, loff_t *offset)
|
|
{
|
|
char s[13] = "HelloWorld!\n";
|
|
int len = sizeof(s);
|
|
ssize_t ret = len;
|
|
|
|
if (*offset >= len || copy_to_user(buffer, s, len)) {
|
|
pr_info("copy_to_user failed\n");
|
|
ret = 0;
|
|
} else {
|
|
pr_info("procfile read %s\n", file_pointer->f_path.dentry->d_name.name);
|
|
*offset += len;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#ifdef HAVE_PROC_OPS
|
|
static const struct proc_ops proc_file_fops = {
|
|
.proc_read = procfile_read,
|
|
};
|
|
#else
|
|
static const struct file_operations proc_file_fops = {
|
|
.read = procfile_read,
|
|
};
|
|
#endif
|
|
|
|
static int __init procfs1_init(void)
|
|
{
|
|
our_proc_file = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
|
|
if (NULL == our_proc_file) {
|
|
pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
pr_info("/proc/%s created\n", procfs_name);
|
|
return 0;
|
|
}
|
|
|
|
static void __exit procfs1_exit(void)
|
|
{
|
|
proc_remove(our_proc_file);
|
|
pr_info("/proc/%s removed\n", procfs_name);
|
|
}
|
|
|
|
module_init(procfs1_init);
|
|
module_exit(procfs1_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|