pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


sysfs example code 1
Posted by Anonymous on Tue 15th Oct 2019 12:37
raw | new post

  1. #include <linux/module.h>
  2. #include <linux/printk.h>
  3. #include <linux/kobject.h>
  4. #include <linux/sysfs.h>
  5. #include <linux/init.h>
  6. #include <linux/fs.h>
  7. #include <linux/string.h>
  8.  
  9. static struct kobject *example_kobject;
  10. static int foo;
  11.  
  12. static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
  13.                       char *buf)
  14. {
  15.         return sprintf(buf, "%d\n", foo);
  16. }
  17.  
  18. static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
  19.                       char *buf, size_t count)
  20. {
  21.         sscanf(buf, "%du", &foo);
  22.         return count;
  23. }
  24.  
  25.  
  26. static struct kobj_attribute foo_attribute =__ATTR(foo, 0660, foo_show,
  27.                                                    foo_store);
  28.  
  29. static int __init mymodule_init (void)
  30. {
  31.         int error = 0;
  32.  
  33.         pr_debug("Module initialized successfully \n");
  34.  
  35.         example_kobject = kobject_create_and_add("kobject_example",
  36.                                                  kernel_kobj);
  37.         if(!example_kobject)
  38.                 return -ENOMEM;
  39.  
  40.         error = sysfs_create_file(example_kobject, &foo_attribute.attr);
  41.         if (error) {
  42.                 pr_debug("failed to create the foo file in /sys/kernel/kobject_example \n");
  43.         }
  44.  
  45.         return error;
  46. }
  47.  
  48. static void __exit mymodule_exit (void)
  49. {
  50.         pr_debug ("Module un initialized successfully \n");
  51.         kobject_put(example_kobject);
  52. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at