lzo: properly check for overruns
[linux-3.10.git] / lib / kobject_uevent.c
index b935795..52e5abb 100644 (file)
@@ -17,7 +17,8 @@
 #include <linux/spinlock.h>
 #include <linux/string.h>
 #include <linux/kobject.h>
-#include <linux/module.h>
+#include <linux/export.h>
+#include <linux/kmod.h>
 #include <linux/slab.h>
 #include <linux/user_namespace.h>
 #include <linux/socket.h>
 
 u64 uevent_seqnum;
 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
-static DEFINE_SPINLOCK(sequence_lock);
 #ifdef CONFIG_NET
 struct uevent_sock {
        struct list_head list;
        struct sock *sk;
 };
 static LIST_HEAD(uevent_sock_list);
-static DEFINE_MUTEX(uevent_sock_mutex);
 #endif
 
+/* This lock protects uevent_seqnum and uevent_sock_list */
+static DEFINE_MUTEX(uevent_sock_mutex);
+
 /* the strings here must match the enum in include/linux/kobject.h */
 static const char *kobject_actions[] = {
        [KOBJ_ADD] =            "add",
@@ -123,7 +125,7 @@ static int kobj_usermode_filter(struct kobject *kobj)
  * @kobj: struct kobject that the action is happening to
  * @envp_ext: pointer to environmental data
  *
- * Returns 0 if kobject_uevent() is completed with success or the
+ * Returns 0 if kobject_uevent_env() is completed with success or the
  * corresponding error when it fails.
  */
 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
@@ -136,7 +138,6 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
        struct kobject *top_kobj;
        struct kset *kset;
        const struct kset_uevent_ops *uevent_ops;
-       u64 seq;
        int i = 0;
        int retval = 0;
 #ifdef CONFIG_NET
@@ -243,22 +244,24 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
        else if (action == KOBJ_REMOVE)
                kobj->state_remove_uevent_sent = 1;
 
+       mutex_lock(&uevent_sock_mutex);
        /* we will send an event, so request a new sequence number */
-       spin_lock(&sequence_lock);
-       seq = ++uevent_seqnum;
-       spin_unlock(&sequence_lock);
-       retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
-       if (retval)
+       retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
+       if (retval) {
+               mutex_unlock(&uevent_sock_mutex);
                goto exit;
+       }
 
 #if defined(CONFIG_NET)
        /* send netlink message */
-       mutex_lock(&uevent_sock_mutex);
        list_for_each_entry(ue_sk, &uevent_sock_list, list) {
                struct sock *uevent_sock = ue_sk->sk;
                struct sk_buff *skb;
                size_t len;
 
+               if (!netlink_has_listeners(uevent_sock, 1))
+                       continue;
+
                /* allocate message with the maximum possible size */
                len = strlen(action_string) + strlen(devpath) + 2;
                skb = alloc_skb(len + env->buflen, GFP_KERNEL);
@@ -282,13 +285,13 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
                                                            kobj_bcast_filter,
                                                            kobj);
                        /* ENOBUFS should be handled in userspace */
-                       if (retval == -ENOBUFS)
+                       if (retval == -ENOBUFS || retval == -ESRCH)
                                retval = 0;
                } else
                        retval = -ENOMEM;
        }
-       mutex_unlock(&uevent_sock_mutex);
 #endif
+       mutex_unlock(&uevent_sock_mutex);
 
        /* call uevent_helper, usually only enabled during early boot */
        if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
@@ -317,7 +320,7 @@ exit:
 EXPORT_SYMBOL_GPL(kobject_uevent_env);
 
 /**
- * kobject_uevent - notify userspace by ending an uevent
+ * kobject_uevent - notify userspace by sending an uevent
  *
  * @action: action that is happening
  * @kobj: struct kobject that the action is happening to
@@ -370,13 +373,16 @@ EXPORT_SYMBOL_GPL(add_uevent_var);
 static int uevent_net_init(struct net *net)
 {
        struct uevent_sock *ue_sk;
+       struct netlink_kernel_cfg cfg = {
+               .groups = 1,
+               .flags  = NL_CFG_F_NONROOT_RECV,
+       };
 
        ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
        if (!ue_sk)
                return -ENOMEM;
 
-       ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT,
-                                         1, NULL, NULL, THIS_MODULE);
+       ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
        if (!ue_sk->sk) {
                printk(KERN_ERR
                       "kobject_uevent: unable to create netlink socket!\n");
@@ -416,7 +422,6 @@ static struct pernet_operations uevent_net_ops = {
 
 static int __init kobject_uevent_init(void)
 {
-       netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
        return register_pernet_subsys(&uevent_net_ops);
 }