XenevaOS
Loading...
Searching...
No Matches
kobject.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * kobject.h - generic kernel object infrastructure.
4 *
5 * Copyright (c) 2002-2003 Patrick Mochel
6 * Copyright (c) 2002-2003 Open Source Development Labs
7 * Copyright (c) 2006-2008 Greg Kroah-Hartman <greg@kroah.com>
8 * Copyright (c) 2006-2008 Novell Inc.
9 *
10 * Please read Documentation/core-api/kobject.rst before using the kobject
11 * interface, ESPECIALLY the parts about reference counts and object
12 * destructors.
13 */
14
15#ifndef _KOBJECT_H_
16#define _KOBJECT_H_
17
18#include <linux/types.h>
19#include <linux/list.h>
20#include <linux/sysfs.h>
21#include <linux/compiler.h>
22#include <linux/container_of.h>
23#include <linux/spinlock.h>
24#include <linux/kref.h>
25#include <linux/kobject_ns.h>
26#include <linux/wait.h>
27#include <linux/atomic.h>
28#include <linux/workqueue.h>
29#include <linux/uidgid.h>
30
31#define UEVENT_HELPER_PATH_LEN 256
32#define UEVENT_NUM_ENVP 64 /* number of env pointers */
33#define UEVENT_BUFFER_SIZE 2048 /* buffer for the variables */
34
35#ifdef CONFIG_UEVENT_HELPER
36/* path to the userspace helper executed on an event */
37extern char uevent_helper[];
38#endif
39
40/* counter to tag the uevent, read only except for the kobject core */
42
43/*
44 * The actions here must match the index to the string array
45 * in lib/kobject_uevent.c
46 *
47 * Do not add new actions here without checking with the driver-core
48 * maintainers. Action strings are not meant to express subsystem
49 * or device specific properties. In most cases you want to send a
50 * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
51 * specific variables added to the event environment.
52 */
63
64struct kobject {
65 const char *name;
67 struct kobject *parent;
68 struct kset *kset;
69 const struct kobj_type *ktype;
70 struct kernfs_node *sd; /* sysfs directory entry */
71 struct kref kref;
72
73 unsigned int state_initialized:1;
74 unsigned int state_in_sysfs:1;
75 unsigned int state_add_uevent_sent:1;
77 unsigned int uevent_suppress:1;
78
79#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
80 struct delayed_work release;
81#endif
82};
83
84__printf(2, 3) int kobject_set_name(struct kobject *kobj, const char *name, ...);
85__printf(2, 0) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list vargs);
86
87#define kobject_name(kobj) (((const struct kobject *)(const void *)(kobj))->name)
88
89void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
90__printf(3, 4) __must_check int kobject_add(struct kobject *kobj,
91 struct kobject *parent,
92 const char *fmt, ...);
93__printf(4, 5) __must_check int kobject_init_and_add(struct kobject *kobj,
94 const struct kobj_type *ktype,
95 struct kobject *parent,
96 const char *fmt, ...);
97
98void kobject_del(struct kobject *kobj);
99
101
102int __must_check kobject_rename(struct kobject *, const char *new_name);
103int __must_check kobject_move(struct kobject *, struct kobject *);
104
105struct kobject *kobject_get(struct kobject *kobj);
107void kobject_put(struct kobject *kobj);
108
109const struct ns_common *kobject_namespace(const struct kobject *kobj);
110void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid);
111char *kobject_get_path(const void *kobj, gfp_t flag);
112
113struct kobj_type {
114 void (*release)(struct kobject *kobj);
115 const struct sysfs_ops *sysfs_ops;
116 const struct attribute_group **default_groups;
117 const struct kobj_ns_type_operations *(*child_ns_type)(const struct kobject *kobj);
118 const struct ns_common *(*namespace)(const struct kobject *kobj);
119 void (*get_ownership)(const struct kobject *kobj, kuid_t *uid, kgid_t *gid);
120};
121
129
131 int (* const filter)(const struct kobject *kobj);
132 const char *(* const name)(const struct kobject *kobj);
133 int (* const uevent)(const struct kobject *kobj, struct kobj_uevent_env *env);
134};
135
138 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
139 char *buf);
140 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
141 const char *buf, size_t count);
142};
143
144extern const struct sysfs_ops kobj_sysfs_ops;
145
146struct sock;
147
171
172void kset_init(struct kset *kset);
175struct kset * __must_check kset_create_and_add(const char *name, const struct kset_uevent_ops *u,
176 struct kobject *parent_kobj);
177
178static inline struct kset *to_kset(struct kobject *kobj)
179{
180 return kobj ? container_of(kobj, struct kset, kobj) : NULL;
181}
182
183static inline struct kset *kset_get(struct kset *k)
184{
185 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
186}
187
188static inline void kset_put(struct kset *k)
189{
190 kobject_put(&k->kobj);
191}
192
193static inline const struct kobj_type *get_ktype(const struct kobject *kobj)
194{
195 return kobj->ktype;
196}
197
198struct kobject *kset_find_obj(struct kset *, const char *);
199
200/* The global /sys/kernel/ kobject for people to chain off of */
201extern struct kobject *kernel_kobj;
202/* The global /sys/kernel/mm/ kobject for people to chain off of */
203extern struct kobject *mm_kobj;
204/* The global /sys/hypervisor/ kobject for people to chain off of */
205extern struct kobject *hypervisor_kobj;
206/* The global /sys/power/ kobject for people to chain off of */
207extern struct kobject *power_kobj;
208/* The global /sys/firmware/ kobject for people to chain off of */
209extern struct kobject *firmware_kobj;
210
211/*
212 * kobject_uevent() -- a macro, deliberately.
213 *
214 * mainline has a function here, and DCL's DCL/linux_cdev_shim.c:151 still
215 * defines one (the no-op body a udev notification would need). It cannot stay
216 * a *declaration*, because virtio_console.c:1654 calls it as
217 *
218 * kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
219 *
220 * and `port->dev->kobj` is a member that does not exist: <linux/fs.h>'s
221 * struct device -- the one file this tree must not edit -- carries `kobj_name`
222 * at offset 0 (the slot mainline's kobj.name occupies) and no `kobj`. A macro
223 * whose parameters never appear in its replacement list never parses them, so
224 * the whole invocation is replaced before the parser sees `->kobj`. Same
225 * idiom as spin_lock(l) in <linux/spinlock.h>:36.
226 *
227 * The two definitions have to agree about not existing: this replaces the
228 * declaration rather than being added beside it, because a declaration that
229 * survives would still type-check `&port->dev->kobj` in any translation unit
230 * that happened to include this header before <linux/sysfs.h>. And the
231 * behaviour is unchanged -- the shim's body is empty. DCL has no udev, so the
232 * event had nowhere to go either way.
233 *
234 * kobject_uevent_env() joined it when drm_drv.c:599
235 *
236 * return kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
237 *
238 * became the first caller: same absent member, and this one uses the
239 * return value, so the macro answers (0) -- "event delivered", the answer
240 * that keeps drm_dev_wedged_event() on its success path.
241 */
242#define kobject_uevent(kobj, action) ((void)0)
243#define kobject_uevent_env(kobj, action, envp) (0)
244int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count);
245
246__printf(2, 3)
247int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);
248
249#endif /* _KOBJECT_H_ */
int64_t ssize_t
Definition stddef.h:38
char * va_list
Definition acmsvcex.h:186
#define NULL
Definition actypes.h:561
flag
Definition bytecode.h:29
#define __printf(fmt_idx, arg_idx)
Definition compiler.h:144
#define __must_check
Definition compiler.h:114
#define container_of(ptr, type, member)
Definition container_of.h:19
int __must_check kobject_move(struct kobject *, struct kobject *)
struct kobject *__must_check kobject_get_unless_zero(struct kobject *kobj)
struct kset __randomize_layout
struct kobject * kernel_kobj
#define UEVENT_BUFFER_SIZE
Definition kobject.h:33
struct kobject const char const struct kobj_type * ktype
Definition kobject.h:94
void kobject_put(struct kobject *kobj)
struct kset *__must_check kset_create_and_add(const char *name, const struct kset_uevent_ops *u, struct kobject *parent_kobj)
void kset_unregister(struct kset *kset)
struct kobject * firmware_kobj
const struct ns_common * kobject_namespace(const struct kobject *kobj)
const struct sysfs_ops kobj_sysfs_ops
kobject_action
Definition kobject.h:53
@ KOBJ_ADD
Definition kobject.h:54
@ KOBJ_BIND
Definition kobject.h:60
@ KOBJ_ONLINE
Definition kobject.h:58
@ KOBJ_OFFLINE
Definition kobject.h:59
@ KOBJ_REMOVE
Definition kobject.h:55
@ KOBJ_MOVE
Definition kobject.h:57
@ KOBJ_UNBIND
Definition kobject.h:61
@ KOBJ_CHANGE
Definition kobject.h:56
const char * format
Definition kobject.h:247
struct kobject * mm_kobj
char * kobject_get_path(const void *kobj, gfp_t flag)
const char const char * fmt
Definition kobject.h:85
struct kobject * kobject_get(struct kobject *kobj)
int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
int __must_check kset_register(struct kset *kset)
struct kobject * parent
Definition kobject.h:91
const char * name
Definition kobject.h:84
void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
int __must_check kobject_rename(struct kobject *, const char *new_name)
struct kobject const char const struct kobj_type struct kobject const char void kobject_del(struct kobject *kobj)
atomic64_t uevent_seqnum
struct kobject * power_kobj
struct kobject * hypervisor_kobj
void kset_init(struct kset *kset)
struct kobject *__must_check kobject_create_and_add(const char *name, struct kobject *parent)
struct kobject * kset_find_obj(struct kset *, const char *)
const char const char va_list vargs
Definition kobject.h:85
#define UEVENT_NUM_ENVP
Definition kobject.h:32
void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
unsigned int gfp_t
Definition kernel.h:56
int spinlock_t
Definition kernel.h:44
Definition atomic.h:45
Definition sysfs.h:33
Definition sysfs.h:23
Definition workqueue.h:229
Definition uidgid_types.h:11
Definition kobject.h:136
struct attribute attr
Definition kobject.h:137
ssize_t(* show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
Definition kobject.h:138
ssize_t(* store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
Definition kobject.h:140
Definition kobject_ns.h:40
Definition kobject.h:113
const struct sysfs_ops * sysfs_ops
Definition kobject.h:115
Definition kobject.h:122
char * argv[3]
Definition kobject.h:123
int envp_idx
Definition kobject.h:125
char * envp[UEVENT_NUM_ENVP]
Definition kobject.h:124
int buflen
Definition kobject.h:127
char buf[UEVENT_BUFFER_SIZE]
Definition kobject.h:126
Definition kobject.h:64
struct kernfs_node * sd
Definition kobject.h:70
unsigned int state_initialized
Definition kobject.h:73
struct kset * kset
Definition kobject.h:68
struct list_head entry
Definition kobject.h:66
unsigned int state_remove_uevent_sent
Definition kobject.h:76
const char * name
Definition kobject.h:65
unsigned int uevent_suppress
Definition kobject.h:77
const struct kobj_type * ktype
Definition kobject.h:69
struct kobject * parent
Definition kobject.h:67
unsigned int state_add_uevent_sent
Definition kobject.h:75
unsigned int state_in_sysfs
Definition kobject.h:74
Definition kref.h:44
Definition kobject.h:130
int(*const filter)(const struct kobject *kobj)
Definition kobject.h:131
int(*const uevent)(const struct kobject *kobj, struct kobj_uevent_env *env)
Definition kobject.h:133
const char *(*const name)(const struct kobject *kobj)
Definition kobject.h:132
Definition kobject.h:165
struct kobject kobj
Definition kobject.h:168
const struct kset_uevent_ops * uevent_ops
Definition kobject.h:169
spinlock_t list_lock
Definition kobject.h:167
struct list_head list
Definition kobject.h:166
Definition uidgid_types.h:7
Definition list.h:11