Root/lib/kobject_uevent.c

1/*
2 * kernel userspace event delivery
3 *
4 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 Novell, Inc. All rights reserved.
6 * Copyright (C) 2004 IBM, Inc. All rights reserved.
7 *
8 * Licensed under the GNU GPL v2.
9 *
10 * Authors:
11 * Robert Love <rml@novell.com>
12 * Kay Sievers <kay.sievers@vrfy.org>
13 * Arjan van de Ven <arjanv@redhat.com>
14 * Greg Kroah-Hartman <greg@kroah.com>
15 */
16
17#include <linux/spinlock.h>
18#include <linux/string.h>
19#include <linux/kobject.h>
20#include <linux/module.h>
21
22#include <linux/socket.h>
23#include <linux/skbuff.h>
24#include <linux/netlink.h>
25#include <net/sock.h>
26
27
28u64 uevent_seqnum;
29char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
30static DEFINE_SPINLOCK(sequence_lock);
31#if defined(CONFIG_NET)
32struct sock *uevent_sock = NULL;
33EXPORT_SYMBOL_GPL(uevent_sock);
34#endif
35
36/* the strings here must match the enum in include/linux/kobject.h */
37static const char *kobject_actions[] = {
38    [KOBJ_ADD] = "add",
39    [KOBJ_REMOVE] = "remove",
40    [KOBJ_CHANGE] = "change",
41    [KOBJ_MOVE] = "move",
42    [KOBJ_ONLINE] = "online",
43    [KOBJ_OFFLINE] = "offline",
44};
45
46u64 uevent_next_seqnum(void)
47{
48    u64 seq;
49
50    spin_lock(&sequence_lock);
51    seq = ++uevent_seqnum;
52    spin_unlock(&sequence_lock);
53
54    return seq;
55}
56EXPORT_SYMBOL_GPL(uevent_next_seqnum);
57
58/**
59 * kobject_action_type - translate action string to numeric type
60 *
61 * @buf: buffer containing the action string, newline is ignored
62 * @len: length of buffer
63 * @type: pointer to the location to store the action type
64 *
65 * Returns 0 if the action string was recognized.
66 */
67int kobject_action_type(const char *buf, size_t count,
68            enum kobject_action *type)
69{
70    enum kobject_action action;
71    int ret = -EINVAL;
72
73    if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
74        count--;
75
76    if (!count)
77        goto out;
78
79    for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
80        if (strncmp(kobject_actions[action], buf, count) != 0)
81            continue;
82        if (kobject_actions[action][count] != '\0')
83            continue;
84        *type = action;
85        ret = 0;
86        break;
87    }
88out:
89    return ret;
90}
91
92/**
93 * kobject_uevent_env - send an uevent with environmental data
94 *
95 * @action: action that is happening
96 * @kobj: struct kobject that the action is happening to
97 * @envp_ext: pointer to environmental data
98 *
99 * Returns 0 if kobject_uevent() is completed with success or the
100 * corresponding error when it fails.
101 */
102int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
103               char *envp_ext[])
104{
105    struct kobj_uevent_env *env;
106    const char *action_string = kobject_actions[action];
107    const char *devpath = NULL;
108    const char *subsystem;
109    struct kobject *top_kobj;
110    struct kset *kset;
111    struct kset_uevent_ops *uevent_ops;
112    u64 seq;
113    int i = 0;
114    int retval = 0;
115
116    pr_debug("kobject: '%s' (%p): %s\n",
117         kobject_name(kobj), kobj, __func__);
118
119    /* search the kset we belong to */
120    top_kobj = kobj;
121    while (!top_kobj->kset && top_kobj->parent)
122        top_kobj = top_kobj->parent;
123
124    if (!top_kobj->kset) {
125        pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
126             "without kset!\n", kobject_name(kobj), kobj,
127             __func__);
128        return -EINVAL;
129    }
130
131    kset = top_kobj->kset;
132    uevent_ops = kset->uevent_ops;
133
134    /* skip the event, if uevent_suppress is set*/
135    if (kobj->uevent_suppress) {
136        pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
137                 "caused the event to drop!\n",
138                 kobject_name(kobj), kobj, __func__);
139        return 0;
140    }
141    /* skip the event, if the filter returns zero. */
142    if (uevent_ops && uevent_ops->filter)
143        if (!uevent_ops->filter(kset, kobj)) {
144            pr_debug("kobject: '%s' (%p): %s: filter function "
145                 "caused the event to drop!\n",
146                 kobject_name(kobj), kobj, __func__);
147            return 0;
148        }
149
150    /* originating subsystem */
151    if (uevent_ops && uevent_ops->name)
152        subsystem = uevent_ops->name(kset, kobj);
153    else
154        subsystem = kobject_name(&kset->kobj);
155    if (!subsystem) {
156        pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
157             "event to drop!\n", kobject_name(kobj), kobj,
158             __func__);
159        return 0;
160    }
161
162    /* environment buffer */
163    env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
164    if (!env)
165        return -ENOMEM;
166
167    /* complete object path */
168    devpath = kobject_get_path(kobj, GFP_KERNEL);
169    if (!devpath) {
170        retval = -ENOENT;
171        goto exit;
172    }
173
174    /* default keys */
175    retval = add_uevent_var(env, "ACTION=%s", action_string);
176    if (retval)
177        goto exit;
178    retval = add_uevent_var(env, "DEVPATH=%s", devpath);
179    if (retval)
180        goto exit;
181    retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
182    if (retval)
183        goto exit;
184
185    /* keys passed in from the caller */
186    if (envp_ext) {
187        for (i = 0; envp_ext[i]; i++) {
188            retval = add_uevent_var(env, "%s", envp_ext[i]);
189            if (retval)
190                goto exit;
191        }
192    }
193
194    /* let the kset specific function add its stuff */
195    if (uevent_ops && uevent_ops->uevent) {
196        retval = uevent_ops->uevent(kset, kobj, env);
197        if (retval) {
198            pr_debug("kobject: '%s' (%p): %s: uevent() returned "
199                 "%d\n", kobject_name(kobj), kobj,
200                 __func__, retval);
201            goto exit;
202        }
203    }
204
205    /*
206     * Mark "add" and "remove" events in the object to ensure proper
207     * events to userspace during automatic cleanup. If the object did
208     * send an "add" event, "remove" will automatically generated by
209     * the core, if not already done by the caller.
210     */
211    if (action == KOBJ_ADD)
212        kobj->state_add_uevent_sent = 1;
213    else if (action == KOBJ_REMOVE)
214        kobj->state_remove_uevent_sent = 1;
215
216    /* we will send an event, so request a new sequence number */
217    seq = uevent_next_seqnum();
218    retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
219    if (retval)
220        goto exit;
221
222#if defined(CONFIG_NET)
223    /* send netlink message */
224    if (uevent_sock) {
225        struct sk_buff *skb;
226        size_t len;
227
228        /* allocate message with the maximum possible size */
229        len = strlen(action_string) + strlen(devpath) + 2;
230        skb = alloc_skb(len + env->buflen, GFP_KERNEL);
231        if (skb) {
232            char *scratch;
233
234            /* add header */
235            scratch = skb_put(skb, len);
236            sprintf(scratch, "%s@%s", action_string, devpath);
237
238            /* copy keys to our continuous event payload buffer */
239            for (i = 0; i < env->envp_idx; i++) {
240                len = strlen(env->envp[i]) + 1;
241                scratch = skb_put(skb, len);
242                strcpy(scratch, env->envp[i]);
243            }
244
245            NETLINK_CB(skb).dst_group = 1;
246            retval = netlink_broadcast(uevent_sock, skb, 0, 1,
247                           GFP_KERNEL);
248            /* ENOBUFS should be handled in userspace */
249            if (retval == -ENOBUFS)
250                retval = 0;
251        } else
252            retval = -ENOMEM;
253    }
254#endif
255
256    /* call uevent_helper, usually only enabled during early boot */
257    if (uevent_helper[0]) {
258        char *argv [3];
259
260        argv [0] = uevent_helper;
261        argv [1] = (char *)subsystem;
262        argv [2] = NULL;
263        retval = add_uevent_var(env, "HOME=/");
264        if (retval)
265            goto exit;
266        retval = add_uevent_var(env,
267                    "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
268        if (retval)
269            goto exit;
270
271        retval = call_usermodehelper(argv[0], argv,
272                         env->envp, UMH_WAIT_EXEC);
273    }
274
275exit:
276    kfree(devpath);
277    kfree(env);
278    return retval;
279}
280EXPORT_SYMBOL_GPL(kobject_uevent_env);
281
282/**
283 * kobject_uevent - notify userspace by ending an uevent
284 *
285 * @action: action that is happening
286 * @kobj: struct kobject that the action is happening to
287 *
288 * Returns 0 if kobject_uevent() is completed with success or the
289 * corresponding error when it fails.
290 */
291int kobject_uevent(struct kobject *kobj, enum kobject_action action)
292{
293    return kobject_uevent_env(kobj, action, NULL);
294}
295EXPORT_SYMBOL_GPL(kobject_uevent);
296
297/**
298 * add_uevent_var - add key value string to the environment buffer
299 * @env: environment buffer structure
300 * @format: printf format for the key=value pair
301 *
302 * Returns 0 if environment variable was added successfully or -ENOMEM
303 * if no space was available.
304 */
305int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
306{
307    va_list args;
308    int len;
309
310    if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
311        WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
312        return -ENOMEM;
313    }
314
315    va_start(args, format);
316    len = vsnprintf(&env->buf[env->buflen],
317            sizeof(env->buf) - env->buflen,
318            format, args);
319    va_end(args);
320
321    if (len >= (sizeof(env->buf) - env->buflen)) {
322        WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
323        return -ENOMEM;
324    }
325
326    env->envp[env->envp_idx++] = &env->buf[env->buflen];
327    env->buflen += len + 1;
328    return 0;
329}
330EXPORT_SYMBOL_GPL(add_uevent_var);
331
332#if defined(CONFIG_NET)
333static int __init kobject_uevent_init(void)
334{
335    uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
336                        1, NULL, NULL, THIS_MODULE);
337    if (!uevent_sock) {
338        printk(KERN_ERR
339               "kobject_uevent: unable to create netlink socket!\n");
340        return -ENODEV;
341    }
342    netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
343    return 0;
344}
345
346postcore_initcall(kobject_uevent_init);
347#endif
348

Archive Download this file



interactive