Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * kobject.h - generic kernel object infrastructure.
0004  *
0005  * Copyright (c) 2002-2003 Patrick Mochel
0006  * Copyright (c) 2002-2003 Open Source Development Labs
0007  * Copyright (c) 2006-2008 Greg Kroah-Hartman <greg@kroah.com>
0008  * Copyright (c) 2006-2008 Novell Inc.
0009  *
0010  * Please read Documentation/core-api/kobject.rst before using the kobject
0011  * interface, ESPECIALLY the parts about reference counts and object
0012  * destructors.
0013  */
0014 
0015 #ifndef _KOBJECT_H_
0016 #define _KOBJECT_H_
0017 
0018 #include <linux/types.h>
0019 #include <linux/list.h>
0020 #include <linux/sysfs.h>
0021 #include <linux/compiler.h>
0022 #include <linux/container_of.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/kref.h>
0025 #include <linux/kobject_ns.h>
0026 #include <linux/wait.h>
0027 #include <linux/atomic.h>
0028 #include <linux/workqueue.h>
0029 #include <linux/uidgid.h>
0030 
0031 #define UEVENT_HELPER_PATH_LEN      256
0032 #define UEVENT_NUM_ENVP         64  /* number of env pointers */
0033 #define UEVENT_BUFFER_SIZE      2048    /* buffer for the variables */
0034 
0035 #ifdef CONFIG_UEVENT_HELPER
0036 /* path to the userspace helper executed on an event */
0037 extern char uevent_helper[];
0038 #endif
0039 
0040 /* counter to tag the uevent, read only except for the kobject core */
0041 extern u64 uevent_seqnum;
0042 
0043 /*
0044  * The actions here must match the index to the string array
0045  * in lib/kobject_uevent.c
0046  *
0047  * Do not add new actions here without checking with the driver-core
0048  * maintainers. Action strings are not meant to express subsystem
0049  * or device specific properties. In most cases you want to send a
0050  * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
0051  * specific variables added to the event environment.
0052  */
0053 enum kobject_action {
0054     KOBJ_ADD,
0055     KOBJ_REMOVE,
0056     KOBJ_CHANGE,
0057     KOBJ_MOVE,
0058     KOBJ_ONLINE,
0059     KOBJ_OFFLINE,
0060     KOBJ_BIND,
0061     KOBJ_UNBIND,
0062 };
0063 
0064 struct kobject {
0065     const char      *name;
0066     struct list_head    entry;
0067     struct kobject      *parent;
0068     struct kset     *kset;
0069     const struct kobj_type  *ktype;
0070     struct kernfs_node  *sd; /* sysfs directory entry */
0071     struct kref     kref;
0072 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
0073     struct delayed_work release;
0074 #endif
0075     unsigned int state_initialized:1;
0076     unsigned int state_in_sysfs:1;
0077     unsigned int state_add_uevent_sent:1;
0078     unsigned int state_remove_uevent_sent:1;
0079     unsigned int uevent_suppress:1;
0080 };
0081 
0082 extern __printf(2, 3)
0083 int kobject_set_name(struct kobject *kobj, const char *name, ...);
0084 extern __printf(2, 0)
0085 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
0086                va_list vargs);
0087 
0088 static inline const char *kobject_name(const struct kobject *kobj)
0089 {
0090     return kobj->name;
0091 }
0092 
0093 extern void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
0094 extern __printf(3, 4) __must_check
0095 int kobject_add(struct kobject *kobj, struct kobject *parent,
0096         const char *fmt, ...);
0097 extern __printf(4, 5) __must_check
0098 int kobject_init_and_add(struct kobject *kobj,
0099              const struct kobj_type *ktype, struct kobject *parent,
0100              const char *fmt, ...);
0101 
0102 extern void kobject_del(struct kobject *kobj);
0103 
0104 extern struct kobject * __must_check kobject_create_and_add(const char *name,
0105                         struct kobject *parent);
0106 
0107 extern int __must_check kobject_rename(struct kobject *, const char *new_name);
0108 extern int __must_check kobject_move(struct kobject *, struct kobject *);
0109 
0110 extern struct kobject *kobject_get(struct kobject *kobj);
0111 extern struct kobject * __must_check kobject_get_unless_zero(
0112                         struct kobject *kobj);
0113 extern void kobject_put(struct kobject *kobj);
0114 
0115 extern const void *kobject_namespace(struct kobject *kobj);
0116 extern void kobject_get_ownership(struct kobject *kobj,
0117                   kuid_t *uid, kgid_t *gid);
0118 extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
0119 
0120 struct kobj_type {
0121     void (*release)(struct kobject *kobj);
0122     const struct sysfs_ops *sysfs_ops;
0123     const struct attribute_group **default_groups;
0124     const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
0125     const void *(*namespace)(struct kobject *kobj);
0126     void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
0127 };
0128 
0129 struct kobj_uevent_env {
0130     char *argv[3];
0131     char *envp[UEVENT_NUM_ENVP];
0132     int envp_idx;
0133     char buf[UEVENT_BUFFER_SIZE];
0134     int buflen;
0135 };
0136 
0137 struct kset_uevent_ops {
0138     int (* const filter)(struct kobject *kobj);
0139     const char *(* const name)(struct kobject *kobj);
0140     int (* const uevent)(struct kobject *kobj, struct kobj_uevent_env *env);
0141 };
0142 
0143 struct kobj_attribute {
0144     struct attribute attr;
0145     ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
0146             char *buf);
0147     ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
0148              const char *buf, size_t count);
0149 };
0150 
0151 extern const struct sysfs_ops kobj_sysfs_ops;
0152 
0153 struct sock;
0154 
0155 /**
0156  * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
0157  *
0158  * A kset defines a group of kobjects.  They can be individually
0159  * different "types" but overall these kobjects all want to be grouped
0160  * together and operated on in the same manner.  ksets are used to
0161  * define the attribute callbacks and other common events that happen to
0162  * a kobject.
0163  *
0164  * @list: the list of all kobjects for this kset
0165  * @list_lock: a lock for iterating over the kobjects
0166  * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
0167  * @uevent_ops: the set of uevent operations for this kset.  These are
0168  * called whenever a kobject has something happen to it so that the kset
0169  * can add new environment variables, or filter out the uevents if so
0170  * desired.
0171  */
0172 struct kset {
0173     struct list_head list;
0174     spinlock_t list_lock;
0175     struct kobject kobj;
0176     const struct kset_uevent_ops *uevent_ops;
0177 } __randomize_layout;
0178 
0179 extern void kset_init(struct kset *kset);
0180 extern int __must_check kset_register(struct kset *kset);
0181 extern void kset_unregister(struct kset *kset);
0182 extern struct kset * __must_check kset_create_and_add(const char *name,
0183                         const struct kset_uevent_ops *u,
0184                         struct kobject *parent_kobj);
0185 
0186 static inline struct kset *to_kset(struct kobject *kobj)
0187 {
0188     return kobj ? container_of(kobj, struct kset, kobj) : NULL;
0189 }
0190 
0191 static inline struct kset *kset_get(struct kset *k)
0192 {
0193     return k ? to_kset(kobject_get(&k->kobj)) : NULL;
0194 }
0195 
0196 static inline void kset_put(struct kset *k)
0197 {
0198     kobject_put(&k->kobj);
0199 }
0200 
0201 static inline const struct kobj_type *get_ktype(struct kobject *kobj)
0202 {
0203     return kobj->ktype;
0204 }
0205 
0206 extern struct kobject *kset_find_obj(struct kset *, const char *);
0207 
0208 /* The global /sys/kernel/ kobject for people to chain off of */
0209 extern struct kobject *kernel_kobj;
0210 /* The global /sys/kernel/mm/ kobject for people to chain off of */
0211 extern struct kobject *mm_kobj;
0212 /* The global /sys/hypervisor/ kobject for people to chain off of */
0213 extern struct kobject *hypervisor_kobj;
0214 /* The global /sys/power/ kobject for people to chain off of */
0215 extern struct kobject *power_kobj;
0216 /* The global /sys/firmware/ kobject for people to chain off of */
0217 extern struct kobject *firmware_kobj;
0218 
0219 int kobject_uevent(struct kobject *kobj, enum kobject_action action);
0220 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
0221             char *envp[]);
0222 int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count);
0223 
0224 __printf(2, 3)
0225 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);
0226 
0227 #endif /* _KOBJECT_H_ */