~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/kobject/kobject-example.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Sample kobject implementation
  4  *
  5  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
  6  * Copyright (C) 2007 Novell Inc.
  7  */
  8 #include <linux/kobject.h>
  9 #include <linux/string.h>
 10 #include <linux/sysfs.h>
 11 #include <linux/module.h>
 12 #include <linux/init.h>
 13 
 14 /*
 15  * This module shows how to create a simple subdirectory in sysfs called
 16  * /sys/kernel/kobject-example  In that directory, 3 files are created:
 17  * "foo", "baz", and "bar".  If an integer is written to these files, it can be
 18  * later read out of it.
 19  */
 20 
 21 static int foo;
 22 static int baz;
 23 static int bar;
 24 
 25 /*
 26  * The "foo" file where a static variable is read from and written to.
 27  */
 28 static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
 29                         char *buf)
 30 {
 31         return sysfs_emit(buf, "%d\n", foo);
 32 }
 33 
 34 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
 35                          const char *buf, size_t count)
 36 {
 37         int ret;
 38 
 39         ret = kstrtoint(buf, 10, &foo);
 40         if (ret < 0)
 41                 return ret;
 42 
 43         return count;
 44 }
 45 
 46 /* Sysfs attributes cannot be world-writable. */
 47 static struct kobj_attribute foo_attribute =
 48         __ATTR(foo, 0664, foo_show, foo_store);
 49 
 50 /*
 51  * More complex function where we determine which variable is being accessed by
 52  * looking at the attribute for the "baz" and "bar" files.
 53  */
 54 static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
 55                       char *buf)
 56 {
 57         int var;
 58 
 59         if (strcmp(attr->attr.name, "baz") == 0)
 60                 var = baz;
 61         else
 62                 var = bar;
 63         return sysfs_emit(buf, "%d\n", var);
 64 }
 65 
 66 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
 67                        const char *buf, size_t count)
 68 {
 69         int var, ret;
 70 
 71         ret = kstrtoint(buf, 10, &var);
 72         if (ret < 0)
 73                 return ret;
 74 
 75         if (strcmp(attr->attr.name, "baz") == 0)
 76                 baz = var;
 77         else
 78                 bar = var;
 79         return count;
 80 }
 81 
 82 static struct kobj_attribute baz_attribute =
 83         __ATTR(baz, 0664, b_show, b_store);
 84 static struct kobj_attribute bar_attribute =
 85         __ATTR(bar, 0664, b_show, b_store);
 86 
 87 
 88 /*
 89  * Create a group of attributes so that we can create and destroy them all
 90  * at once.
 91  */
 92 static struct attribute *attrs[] = {
 93         &foo_attribute.attr,
 94         &baz_attribute.attr,
 95         &bar_attribute.attr,
 96         NULL,   /* need to NULL terminate the list of attributes */
 97 };
 98 
 99 /*
100  * An unnamed attribute group will put all of the attributes directly in
101  * the kobject directory.  If we specify a name, a subdirectory will be
102  * created for the attributes with the directory being the name of the
103  * attribute group.
104  */
105 static struct attribute_group attr_group = {
106         .attrs = attrs,
107 };
108 
109 static struct kobject *example_kobj;
110 
111 static int __init example_init(void)
112 {
113         int retval;
114 
115         /*
116          * Create a simple kobject with the name of "kobject_example",
117          * located under /sys/kernel/
118          *
119          * As this is a simple directory, no uevent will be sent to
120          * userspace.  That is why this function should not be used for
121          * any type of dynamic kobjects, where the name and number are
122          * not known ahead of time.
123          */
124         example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
125         if (!example_kobj)
126                 return -ENOMEM;
127 
128         /* Create the files associated with this kobject */
129         retval = sysfs_create_group(example_kobj, &attr_group);
130         if (retval)
131                 kobject_put(example_kobj);
132 
133         return retval;
134 }
135 
136 static void __exit example_exit(void)
137 {
138         kobject_put(example_kobj);
139 }
140 
141 module_init(example_init);
142 module_exit(example_exit);
143 MODULE_DESCRIPTION("Sample kobject implementation");
144 MODULE_LICENSE("GPL v2");
145 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
146 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php