diff --git a/LKMPG-3.16.html b/LKMPG-3.16.html index 12b7062..bdf0486 100644 --- a/LKMPG-3.16.html +++ b/LKMPG-3.16.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- +-This is the complete makefile for all the examples we've seen so far. The -first five lines are nothing special, but for the last example we'll need two -lines. First we invent an object name for our combined module, second we tell -make what object files are part of that module. +This is the complete makefile for all the examples we've seen so far. The first five lines are nothing special, but for the last example we'll need two lines. First we invent an object name for our combined module, second we tell make what object files are part of that module.
@@ -1961,7 +1958,7 @@ In Linux, there is an additional mechanism for the kernel and kernel modules to-The method to use the proc file system is very similar to the one used with device drivers — a structure is created with all the information needed for the /proc file, including pointers to any handler functions (in our case there is only one, the one called when somebody attempts to read from the / proc file). Then, init_module registers the structure with the kernel and cleanup_module unregisters it. +The method to use the proc file system is very similar to the one used with device drivers — a structure is created with all the information needed for the /proc file, including pointers to any handler functions (in our case there is only one, the one called when somebody attempts to read from the /proc file). Then, init_module registers the structure with the kernel and cleanup_module unregisters it.
@@ -1986,7 +1983,7 @@ Each time, everytime the file /proc/helloworld is read, the function proc
# cat /proc/helloworld +# cat /proc/helloworld HelloWorld!
-sysfs allows you to interact with the running kernel by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for userland applications. +sysfs allows you to interact with the running kernel by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for userland applications or scripts.
An example of a hello world module which includes a variable accessible via sysfs is given below.
+TODO +/* + * hello-sysfs.c sysfs example + */ + +#include <linux/module.h> +#include <linux/printk.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> +#include <linux/init.h> +#include <linux/fs.h> +#include <linux/string.h> + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Bob Mottram"); + +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, "%du", &myvariable); + return count; +} + + +static struct kobj_attribute myvariable_attribute = + __ATTR(myvariable, 0660, myvariable_show, myvariable_store); + +static int __init mymodule_init (void) +{ + int error = 0; + + pr_debug("mymodule: initialised\n"); + + mymodule = + kobject_create_and_add("mymodule", kernel_kobj); + if (!mymodule) + return -ENOMEM; + + error = sysfs_create_file(mymodule, &myvariable_attribute.attr); + if (error) { + pr_debug("failed to create the myvariable file " \ + "in /sys/kernel/mymodule\n"); + } + + return error; +} + +static void __exit mymodule_exit (void) +{ + pr_debug ("mymodule: Exit success\n"); + kobject_put(mymodule); +} + +module_init(mymodule_init); +module_exit(mymodule_exit);
-Then to test it: +Make and install the module:
make sudo insmod hello-sysfs.ko -sudo lsmod | grep hello_sysfs ++
+Check that it exists: +
+ +sudo lsmod | grep hello_sysfs ++
+What is the current value of myvariable ? +
+ +cat /sys/kernel/mymodule/myvariable ++
+Set the value of myvariable and check that it changed. +
+ +echo "32" > /sys/kernel/mymodule/myvariable +cat /sys/kernel/mymodule/myvariable ++
+Finally, remove the test module: +
+ +sudo rmmod hello_sysfs