mirror of
https://github.com/sysprog21/lkmpg.git
synced 2024-11-25 09:41:47 +08:00
2a04a64041
As myvariable is declared as an integer, it is meaningless that putting u after %d in the format specifier of sscanf for scanning an integer. This patch removes the u in the format specifier for avoiding misleading.
62 lines
1.4 KiB
C
62 lines
1.4 KiB
C
/*
|
|
* hello-sysfs.c sysfs example
|
|
*/
|
|
#include <linux/fs.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kobject.h>
|
|
#include <linux/module.h>
|
|
#include <linux/string.h>
|
|
#include <linux/sysfs.h>
|
|
|
|
static struct kobject *mymodule;
|
|
|
|
/* the variable you want to be able to change */
|
|
static int myvariable = 0;
|
|
|
|
static ssize_t myvariable_show(struct kobject *kobj,
|
|
struct kobj_attribute *attr, char *buf)
|
|
{
|
|
return sprintf(buf, "%d\n", myvariable);
|
|
}
|
|
|
|
static ssize_t myvariable_store(struct kobject *kobj,
|
|
struct kobj_attribute *attr, char *buf,
|
|
size_t count)
|
|
{
|
|
sscanf(buf, "%d", &myvariable);
|
|
return count;
|
|
}
|
|
|
|
static struct kobj_attribute myvariable_attribute =
|
|
__ATTR(myvariable, 0660, myvariable_show, (void *)myvariable_store);
|
|
|
|
static int __init mymodule_init(void)
|
|
{
|
|
int error = 0;
|
|
|
|
pr_info("mymodule: initialized\n");
|
|
|
|
mymodule = kobject_create_and_add("mymodule", kernel_kobj);
|
|
if (!mymodule)
|
|
return -ENOMEM;
|
|
|
|
error = sysfs_create_file(mymodule, &myvariable_attribute.attr);
|
|
if (error) {
|
|
pr_info("failed to create the myvariable file "
|
|
"in /sys/kernel/mymodule\n");
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
static void __exit mymodule_exit(void)
|
|
{
|
|
pr_info("mymodule: Exit success\n");
|
|
kobject_put(mymodule);
|
|
}
|
|
|
|
module_init(mymodule_init);
|
|
module_exit(mymodule_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|