提交 31b9025a 编写于 作者: G Greg Kroah-Hartman

Kobject: make kobject apis more robust in handling NULL pointers

It should be ok to pass in NULL for some kobject functions, so add error
checking for all exported kobject functions to be more robust.

Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@suse.de>
上级 5331be09
...@@ -126,6 +126,8 @@ EXPORT_SYMBOL_GPL(kobject_get_path); ...@@ -126,6 +126,8 @@ EXPORT_SYMBOL_GPL(kobject_get_path);
*/ */
void kobject_init(struct kobject * kobj) void kobject_init(struct kobject * kobj)
{ {
if (!kobj)
return;
kref_init(&kobj->kref); kref_init(&kobj->kref);
INIT_LIST_HEAD(&kobj->entry); INIT_LIST_HEAD(&kobj->entry);
init_waitqueue_head(&kobj->poll); init_waitqueue_head(&kobj->poll);
...@@ -366,6 +368,8 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent) ...@@ -366,6 +368,8 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent)
void kobject_del(struct kobject * kobj) void kobject_del(struct kobject * kobj)
{ {
if (!kobj)
return;
sysfs_remove_dir(kobj); sysfs_remove_dir(kobj);
unlink(kobj); unlink(kobj);
} }
...@@ -377,6 +381,8 @@ void kobject_del(struct kobject * kobj) ...@@ -377,6 +381,8 @@ void kobject_del(struct kobject * kobj)
void kobject_unregister(struct kobject * kobj) void kobject_unregister(struct kobject * kobj)
{ {
if (!kobj)
return;
pr_debug("kobject %s: unregistering\n",kobject_name(kobj)); pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
kobject_uevent(kobj, KOBJ_REMOVE); kobject_uevent(kobj, KOBJ_REMOVE);
kobject_del(kobj); kobject_del(kobj);
...@@ -523,6 +529,8 @@ int kset_add(struct kset * k) ...@@ -523,6 +529,8 @@ int kset_add(struct kset * k)
int kset_register(struct kset * k) int kset_register(struct kset * k)
{ {
if (!k)
return -EINVAL;
kset_init(k); kset_init(k);
return kset_add(k); return kset_add(k);
} }
...@@ -535,6 +543,8 @@ int kset_register(struct kset * k) ...@@ -535,6 +543,8 @@ int kset_register(struct kset * k)
void kset_unregister(struct kset * k) void kset_unregister(struct kset * k)
{ {
if (!k)
return;
kobject_unregister(&k->kobj); kobject_unregister(&k->kobj);
} }
...@@ -586,6 +596,9 @@ int subsystem_register(struct subsystem * s) ...@@ -586,6 +596,9 @@ int subsystem_register(struct subsystem * s)
{ {
int error; int error;
if (!s)
return -EINVAL;
subsystem_init(s); subsystem_init(s);
pr_debug("subsystem %s: registering\n",s->kset.kobj.name); pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
...@@ -598,6 +611,8 @@ int subsystem_register(struct subsystem * s) ...@@ -598,6 +611,8 @@ int subsystem_register(struct subsystem * s)
void subsystem_unregister(struct subsystem * s) void subsystem_unregister(struct subsystem * s)
{ {
if (!s)
return;
pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name); pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
kset_unregister(&s->kset); kset_unregister(&s->kset);
} }
...@@ -612,6 +627,10 @@ void subsystem_unregister(struct subsystem * s) ...@@ -612,6 +627,10 @@ void subsystem_unregister(struct subsystem * s)
int subsys_create_file(struct subsystem * s, struct subsys_attribute * a) int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
{ {
int error = 0; int error = 0;
if (!s || !a)
return -EINVAL;
if (subsys_get(s)) { if (subsys_get(s)) {
error = sysfs_create_file(&s->kset.kobj,&a->attr); error = sysfs_create_file(&s->kset.kobj,&a->attr);
subsys_put(s); subsys_put(s);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册