kobject_uevent.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #include <linux/spinlock.h>
  17. #include <linux/string.h>
  18. #include <linux/kobject.h>
  19. #include <linux/export.h>
  20. #include <linux/kmod.h>
  21. #include <linux/slab.h>
  22. #include <linux/socket.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netlink.h>
  25. #include <linux/uuid.h>
  26. #include <linux/ctype.h>
  27. #include <net/sock.h>
  28. #include <net/net_namespace.h>
  29. u64 uevent_seqnum;
  30. #ifdef CONFIG_UEVENT_HELPER
  31. char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
  32. #endif
  33. #ifdef CONFIG_NET
  34. struct uevent_sock {
  35. struct list_head list;
  36. struct sock *sk;
  37. };
  38. static LIST_HEAD(uevent_sock_list);
  39. #endif
  40. /* This lock protects uevent_seqnum and uevent_sock_list */
  41. static DEFINE_MUTEX(uevent_sock_mutex);
  42. /* the strings here must match the enum in include/linux/kobject.h */
  43. static const char *kobject_actions[] = {
  44. [KOBJ_ADD] = "add",
  45. [KOBJ_REMOVE] = "remove",
  46. [KOBJ_CHANGE] = "change",
  47. [KOBJ_MOVE] = "move",
  48. [KOBJ_ONLINE] = "online",
  49. [KOBJ_OFFLINE] = "offline",
  50. [KOBJ_BIND] = "bind",
  51. [KOBJ_UNBIND] = "unbind",
  52. };
  53. static int kobject_action_type(const char *buf, size_t count,
  54. enum kobject_action *type,
  55. const char **args)
  56. {
  57. enum kobject_action action;
  58. size_t count_first;
  59. const char *args_start;
  60. int ret = -EINVAL;
  61. if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
  62. count--;
  63. if (!count)
  64. goto out;
  65. args_start = strnchr(buf, count, ' ');
  66. if (args_start) {
  67. count_first = args_start - buf;
  68. args_start = args_start + 1;
  69. } else
  70. count_first = count;
  71. for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
  72. if (strncmp(kobject_actions[action], buf, count_first) != 0)
  73. continue;
  74. if (kobject_actions[action][count_first] != '\0')
  75. continue;
  76. if (args)
  77. *args = args_start;
  78. *type = action;
  79. ret = 0;
  80. break;
  81. }
  82. out:
  83. return ret;
  84. }
  85. static const char *action_arg_word_end(const char *buf, const char *buf_end,
  86. char delim)
  87. {
  88. const char *next = buf;
  89. while (next <= buf_end && *next != delim)
  90. if (!isalnum(*next++))
  91. return NULL;
  92. if (next == buf)
  93. return NULL;
  94. return next;
  95. }
  96. static int kobject_action_args(const char *buf, size_t count,
  97. struct kobj_uevent_env **ret_env)
  98. {
  99. struct kobj_uevent_env *env = NULL;
  100. const char *next, *buf_end, *key;
  101. int key_len;
  102. int r = -EINVAL;
  103. if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
  104. count--;
  105. if (!count)
  106. return -EINVAL;
  107. env = kzalloc(sizeof(*env), GFP_KERNEL);
  108. if (!env)
  109. return -ENOMEM;
  110. /* first arg is UUID */
  111. if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
  112. add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
  113. goto out;
  114. /*
  115. * the rest are custom environment variables in KEY=VALUE
  116. * format with ' ' delimiter between each KEY=VALUE pair
  117. */
  118. next = buf + UUID_STRING_LEN;
  119. buf_end = buf + count - 1;
  120. while (next <= buf_end) {
  121. if (*next != ' ')
  122. goto out;
  123. /* skip the ' ', key must follow */
  124. key = ++next;
  125. if (key > buf_end)
  126. goto out;
  127. buf = next;
  128. next = action_arg_word_end(buf, buf_end, '=');
  129. if (!next || next > buf_end || *next != '=')
  130. goto out;
  131. key_len = next - buf;
  132. /* skip the '=', value must follow */
  133. if (++next > buf_end)
  134. goto out;
  135. buf = next;
  136. next = action_arg_word_end(buf, buf_end, ' ');
  137. if (!next)
  138. goto out;
  139. if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
  140. key_len, key, (int) (next - buf), buf))
  141. goto out;
  142. }
  143. r = 0;
  144. out:
  145. if (r)
  146. kfree(env);
  147. else
  148. *ret_env = env;
  149. return r;
  150. }
  151. /**
  152. * kobject_synth_uevent - send synthetic uevent with arguments
  153. *
  154. * @kobj: struct kobject for which synthetic uevent is to be generated
  155. * @buf: buffer containing action type and action args, newline is ignored
  156. * @count: length of buffer
  157. *
  158. * Returns 0 if kobject_synthetic_uevent() is completed with success or the
  159. * corresponding error when it fails.
  160. */
  161. int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
  162. {
  163. char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
  164. enum kobject_action action;
  165. const char *action_args;
  166. struct kobj_uevent_env *env;
  167. const char *msg = NULL, *devpath;
  168. int r;
  169. r = kobject_action_type(buf, count, &action, &action_args);
  170. if (r) {
  171. msg = "unknown uevent action string\n";
  172. goto out;
  173. }
  174. if (!action_args) {
  175. r = kobject_uevent_env(kobj, action, no_uuid_envp);
  176. goto out;
  177. }
  178. r = kobject_action_args(action_args,
  179. count - (action_args - buf), &env);
  180. if (r == -EINVAL) {
  181. msg = "incorrect uevent action arguments\n";
  182. goto out;
  183. }
  184. if (r)
  185. goto out;
  186. r = kobject_uevent_env(kobj, action, env->envp);
  187. kfree(env);
  188. out:
  189. if (r) {
  190. devpath = kobject_get_path(kobj, GFP_KERNEL);
  191. printk(KERN_WARNING "synth uevent: %s: %s",
  192. devpath ?: "unknown device",
  193. msg ?: "failed to send uevent");
  194. kfree(devpath);
  195. }
  196. return r;
  197. }
  198. #ifdef CONFIG_NET
  199. static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
  200. {
  201. struct kobject *kobj = data, *ksobj;
  202. const struct kobj_ns_type_operations *ops;
  203. ops = kobj_ns_ops(kobj);
  204. if (!ops && kobj->kset) {
  205. ksobj = &kobj->kset->kobj;
  206. if (ksobj->parent != NULL)
  207. ops = kobj_ns_ops(ksobj->parent);
  208. }
  209. if (ops && ops->netlink_ns && kobj->ktype->namespace) {
  210. const void *sock_ns, *ns;
  211. ns = kobj->ktype->namespace(kobj);
  212. sock_ns = ops->netlink_ns(dsk);
  213. return sock_ns != ns;
  214. }
  215. return 0;
  216. }
  217. #endif
  218. #ifdef CONFIG_UEVENT_HELPER
  219. static int kobj_usermode_filter(struct kobject *kobj)
  220. {
  221. const struct kobj_ns_type_operations *ops;
  222. ops = kobj_ns_ops(kobj);
  223. if (ops) {
  224. const void *init_ns, *ns;
  225. ns = kobj->ktype->namespace(kobj);
  226. init_ns = ops->initial_ns();
  227. return ns != init_ns;
  228. }
  229. return 0;
  230. }
  231. static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
  232. {
  233. int len;
  234. len = strlcpy(&env->buf[env->buflen], subsystem,
  235. sizeof(env->buf) - env->buflen);
  236. if (len >= (sizeof(env->buf) - env->buflen)) {
  237. WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
  238. return -ENOMEM;
  239. }
  240. env->argv[0] = uevent_helper;
  241. env->argv[1] = &env->buf[env->buflen];
  242. env->argv[2] = NULL;
  243. env->buflen += len + 1;
  244. return 0;
  245. }
  246. static void cleanup_uevent_env(struct subprocess_info *info)
  247. {
  248. kfree(info->data);
  249. }
  250. #endif
  251. static void zap_modalias_env(struct kobj_uevent_env *env)
  252. {
  253. static const char modalias_prefix[] = "MODALIAS=";
  254. int i;
  255. for (i = 0; i < env->envp_idx;) {
  256. if (strncmp(env->envp[i], modalias_prefix,
  257. sizeof(modalias_prefix) - 1)) {
  258. i++;
  259. continue;
  260. }
  261. if (i != env->envp_idx - 1)
  262. memmove(&env->envp[i], &env->envp[i + 1],
  263. sizeof(env->envp[i]) * env->envp_idx - 1);
  264. env->envp_idx--;
  265. }
  266. }
  267. /**
  268. * kobject_uevent_env - send an uevent with environmental data
  269. *
  270. * @kobj: struct kobject that the action is happening to
  271. * @action: action that is happening
  272. * @envp_ext: pointer to environmental data
  273. *
  274. * Returns 0 if kobject_uevent_env() is completed with success or the
  275. * corresponding error when it fails.
  276. */
  277. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  278. char *envp_ext[])
  279. {
  280. struct kobj_uevent_env *env;
  281. const char *action_string = kobject_actions[action];
  282. const char *devpath = NULL;
  283. const char *subsystem;
  284. struct kobject *top_kobj;
  285. struct kset *kset;
  286. const struct kset_uevent_ops *uevent_ops;
  287. int i = 0;
  288. int retval = 0;
  289. #ifdef CONFIG_NET
  290. struct uevent_sock *ue_sk;
  291. #endif
  292. pr_debug("kobject: '%s' (%p): %s\n",
  293. kobject_name(kobj), kobj, __func__);
  294. /* search the kset we belong to */
  295. top_kobj = kobj;
  296. while (!top_kobj->kset && top_kobj->parent)
  297. top_kobj = top_kobj->parent;
  298. if (!top_kobj->kset) {
  299. pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
  300. "without kset!\n", kobject_name(kobj), kobj,
  301. __func__);
  302. return -EINVAL;
  303. }
  304. kset = top_kobj->kset;
  305. uevent_ops = kset->uevent_ops;
  306. /* skip the event, if uevent_suppress is set*/
  307. if (kobj->uevent_suppress) {
  308. pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
  309. "caused the event to drop!\n",
  310. kobject_name(kobj), kobj, __func__);
  311. return 0;
  312. }
  313. /* skip the event, if the filter returns zero. */
  314. if (uevent_ops && uevent_ops->filter)
  315. if (!uevent_ops->filter(kset, kobj)) {
  316. pr_debug("kobject: '%s' (%p): %s: filter function "
  317. "caused the event to drop!\n",
  318. kobject_name(kobj), kobj, __func__);
  319. return 0;
  320. }
  321. /* originating subsystem */
  322. if (uevent_ops && uevent_ops->name)
  323. subsystem = uevent_ops->name(kset, kobj);
  324. else
  325. subsystem = kobject_name(&kset->kobj);
  326. if (!subsystem) {
  327. pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
  328. "event to drop!\n", kobject_name(kobj), kobj,
  329. __func__);
  330. return 0;
  331. }
  332. /* environment buffer */
  333. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  334. if (!env)
  335. return -ENOMEM;
  336. /* complete object path */
  337. devpath = kobject_get_path(kobj, GFP_KERNEL);
  338. if (!devpath) {
  339. retval = -ENOENT;
  340. goto exit;
  341. }
  342. /* default keys */
  343. retval = add_uevent_var(env, "ACTION=%s", action_string);
  344. if (retval)
  345. goto exit;
  346. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  347. if (retval)
  348. goto exit;
  349. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  350. if (retval)
  351. goto exit;
  352. /* keys passed in from the caller */
  353. if (envp_ext) {
  354. for (i = 0; envp_ext[i]; i++) {
  355. retval = add_uevent_var(env, "%s", envp_ext[i]);
  356. if (retval)
  357. goto exit;
  358. }
  359. }
  360. /* let the kset specific function add its stuff */
  361. if (uevent_ops && uevent_ops->uevent) {
  362. retval = uevent_ops->uevent(kset, kobj, env);
  363. if (retval) {
  364. pr_debug("kobject: '%s' (%p): %s: uevent() returned "
  365. "%d\n", kobject_name(kobj), kobj,
  366. __func__, retval);
  367. goto exit;
  368. }
  369. }
  370. switch (action) {
  371. case KOBJ_ADD:
  372. /*
  373. * Mark "add" event so we can make sure we deliver "remove"
  374. * event to userspace during automatic cleanup. If
  375. * the object did send an "add" event, "remove" will
  376. * automatically generated by the core, if not already done
  377. * by the caller.
  378. */
  379. kobj->state_add_uevent_sent = 1;
  380. break;
  381. case KOBJ_REMOVE:
  382. kobj->state_remove_uevent_sent = 1;
  383. break;
  384. case KOBJ_UNBIND:
  385. zap_modalias_env(env);
  386. break;
  387. default:
  388. break;
  389. }
  390. mutex_lock(&uevent_sock_mutex);
  391. /* we will send an event, so request a new sequence number */
  392. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
  393. if (retval) {
  394. mutex_unlock(&uevent_sock_mutex);
  395. goto exit;
  396. }
  397. #if defined(CONFIG_NET)
  398. /* send netlink message */
  399. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  400. struct sock *uevent_sock = ue_sk->sk;
  401. struct sk_buff *skb;
  402. size_t len;
  403. if (!netlink_has_listeners(uevent_sock, 1))
  404. continue;
  405. /* allocate message with the maximum possible size */
  406. len = strlen(action_string) + strlen(devpath) + 2;
  407. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  408. if (skb) {
  409. char *scratch;
  410. /* add header */
  411. scratch = skb_put(skb, len);
  412. sprintf(scratch, "%s@%s", action_string, devpath);
  413. /* copy keys to our continuous event payload buffer */
  414. for (i = 0; i < env->envp_idx; i++) {
  415. len = strlen(env->envp[i]) + 1;
  416. scratch = skb_put(skb, len);
  417. strcpy(scratch, env->envp[i]);
  418. }
  419. NETLINK_CB(skb).dst_group = 1;
  420. retval = netlink_broadcast_filtered(uevent_sock, skb,
  421. 0, 1, GFP_KERNEL,
  422. kobj_bcast_filter,
  423. kobj);
  424. /* ENOBUFS should be handled in userspace */
  425. if (retval == -ENOBUFS || retval == -ESRCH)
  426. retval = 0;
  427. } else
  428. retval = -ENOMEM;
  429. }
  430. #endif
  431. mutex_unlock(&uevent_sock_mutex);
  432. #ifdef CONFIG_UEVENT_HELPER
  433. /* call uevent_helper, usually only enabled during early boot */
  434. if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
  435. struct subprocess_info *info;
  436. retval = add_uevent_var(env, "HOME=/");
  437. if (retval)
  438. goto exit;
  439. retval = add_uevent_var(env,
  440. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  441. if (retval)
  442. goto exit;
  443. retval = init_uevent_argv(env, subsystem);
  444. if (retval)
  445. goto exit;
  446. retval = -ENOMEM;
  447. info = call_usermodehelper_setup(env->argv[0], env->argv,
  448. env->envp, GFP_KERNEL,
  449. NULL, cleanup_uevent_env, env);
  450. if (info) {
  451. retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
  452. env = NULL; /* freed by cleanup_uevent_env */
  453. }
  454. }
  455. #endif
  456. exit:
  457. kfree(devpath);
  458. kfree(env);
  459. return retval;
  460. }
  461. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  462. /**
  463. * kobject_uevent - notify userspace by sending an uevent
  464. *
  465. * @kobj: struct kobject that the action is happening to
  466. * @action: action that is happening
  467. *
  468. * Returns 0 if kobject_uevent() is completed with success or the
  469. * corresponding error when it fails.
  470. */
  471. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  472. {
  473. return kobject_uevent_env(kobj, action, NULL);
  474. }
  475. EXPORT_SYMBOL_GPL(kobject_uevent);
  476. /**
  477. * add_uevent_var - add key value string to the environment buffer
  478. * @env: environment buffer structure
  479. * @format: printf format for the key=value pair
  480. *
  481. * Returns 0 if environment variable was added successfully or -ENOMEM
  482. * if no space was available.
  483. */
  484. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  485. {
  486. va_list args;
  487. int len;
  488. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  489. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  490. return -ENOMEM;
  491. }
  492. va_start(args, format);
  493. len = vsnprintf(&env->buf[env->buflen],
  494. sizeof(env->buf) - env->buflen,
  495. format, args);
  496. va_end(args);
  497. if (len >= (sizeof(env->buf) - env->buflen)) {
  498. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  499. return -ENOMEM;
  500. }
  501. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  502. env->buflen += len + 1;
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(add_uevent_var);
  506. #if defined(CONFIG_NET)
  507. static int uevent_net_init(struct net *net)
  508. {
  509. struct uevent_sock *ue_sk;
  510. struct netlink_kernel_cfg cfg = {
  511. .groups = 1,
  512. .flags = NL_CFG_F_NONROOT_RECV,
  513. };
  514. ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
  515. if (!ue_sk)
  516. return -ENOMEM;
  517. ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
  518. if (!ue_sk->sk) {
  519. printk(KERN_ERR
  520. "kobject_uevent: unable to create netlink socket!\n");
  521. kfree(ue_sk);
  522. return -ENODEV;
  523. }
  524. mutex_lock(&uevent_sock_mutex);
  525. list_add_tail(&ue_sk->list, &uevent_sock_list);
  526. mutex_unlock(&uevent_sock_mutex);
  527. return 0;
  528. }
  529. static void uevent_net_exit(struct net *net)
  530. {
  531. struct uevent_sock *ue_sk;
  532. mutex_lock(&uevent_sock_mutex);
  533. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  534. if (sock_net(ue_sk->sk) == net)
  535. goto found;
  536. }
  537. mutex_unlock(&uevent_sock_mutex);
  538. return;
  539. found:
  540. list_del(&ue_sk->list);
  541. mutex_unlock(&uevent_sock_mutex);
  542. netlink_kernel_release(ue_sk->sk);
  543. kfree(ue_sk);
  544. }
  545. static struct pernet_operations uevent_net_ops = {
  546. .init = uevent_net_init,
  547. .exit = uevent_net_exit,
  548. };
  549. static int __init kobject_uevent_init(void)
  550. {
  551. return register_pernet_subsys(&uevent_net_ops);
  552. }
  553. postcore_initcall(kobject_uevent_init);
  554. #endif