Skip to content

Commit

Permalink
uevent: add alloc_uevent_skb() helper
Browse files Browse the repository at this point in the history
This patch adds alloc_uevent_skb() in preparation for follow up patches.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Christian Brauner authored and David S. Miller committed May 1, 2018
1 parent e33200b commit 26045a7
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions lib/kobject_uevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linux/socket.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/uidgid.h>
#include <linux/uuid.h>
#include <linux/ctype.h>
#include <net/sock.h>
Expand Down Expand Up @@ -296,6 +297,38 @@ static void cleanup_uevent_env(struct subprocess_info *info)
}
#endif

#ifdef CONFIG_NET
static struct sk_buff *alloc_uevent_skb(struct kobj_uevent_env *env,
const char *action_string,
const char *devpath)
{
struct netlink_skb_parms *parms;
struct sk_buff *skb = NULL;
char *scratch;
size_t len;

/* allocate message with maximum possible size */
len = strlen(action_string) + strlen(devpath) + 2;
skb = alloc_skb(len + env->buflen, GFP_KERNEL);
if (!skb)
return NULL;

/* add header */
scratch = skb_put(skb, len);
sprintf(scratch, "%s@%s", action_string, devpath);

skb_put_data(skb, env->buf, env->buflen);

parms = &NETLINK_CB(skb);
parms->creds.uid = GLOBAL_ROOT_UID;
parms->creds.gid = GLOBAL_ROOT_GID;
parms->dst_group = 1;
parms->portid = 0;

return skb;
}
#endif

static int kobject_uevent_net_broadcast(struct kobject *kobj,
struct kobj_uevent_env *env,
const char *action_string,
Expand All @@ -314,22 +347,10 @@ static int kobject_uevent_net_broadcast(struct kobject *kobj,
continue;

if (!skb) {
/* allocate message with the maximum possible size */
size_t len = strlen(action_string) + strlen(devpath) + 2;
char *scratch;

retval = -ENOMEM;
skb = alloc_skb(len + env->buflen, GFP_KERNEL);
skb = alloc_uevent_skb(env, action_string, devpath);
if (!skb)
continue;

/* add header */
scratch = skb_put(skb, len);
sprintf(scratch, "%s@%s", action_string, devpath);

skb_put_data(skb, env->buf, env->buflen);

NETLINK_CB(skb).dst_group = 1;
}

retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
Expand Down

0 comments on commit 26045a7

Please sign in to comment.