kobject_uevent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <net/sock.h>
  26. #include <net/net_namespace.h>
  27. u64 uevent_seqnum;
  28. #ifdef CONFIG_UEVENT_HELPER
  29. char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
  30. #endif
  31. #ifdef CONFIG_NET
  32. struct uevent_sock {
  33. struct list_head list;
  34. struct sock *sk;
  35. };
  36. static LIST_HEAD(uevent_sock_list);
  37. #endif
  38. /* This lock protects uevent_seqnum and uevent_sock_list */
  39. static DEFINE_MUTEX(uevent_sock_mutex);
  40. /* the strings here must match the enum in include/linux/kobject.h */
  41. static const char *kobject_actions[] = {
  42. [KOBJ_ADD] = "add",
  43. [KOBJ_REMOVE] = "remove",
  44. [KOBJ_CHANGE] = "change",
  45. [KOBJ_MOVE] = "move",
  46. [KOBJ_ONLINE] = "online",
  47. [KOBJ_OFFLINE] = "offline",
  48. };
  49. /**
  50. * kobject_action_type - translate action string to numeric type
  51. *
  52. * @buf: buffer containing the action string, newline is ignored
  53. * @len: length of buffer
  54. * @type: pointer to the location to store the action type
  55. *
  56. * Returns 0 if the action string was recognized.
  57. */
  58. int kobject_action_type(const char *buf, size_t count,
  59. enum kobject_action *type)
  60. {
  61. enum kobject_action action;
  62. int ret = -EINVAL;
  63. if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
  64. count--;
  65. if (!count)
  66. goto out;
  67. for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
  68. if (strncmp(kobject_actions[action], buf, count) != 0)
  69. continue;
  70. if (kobject_actions[action][count] != '\0')
  71. continue;
  72. *type = action;
  73. ret = 0;
  74. break;
  75. }
  76. out:
  77. return ret;
  78. }
  79. #ifdef CONFIG_NET
  80. static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
  81. {
  82. struct kobject *kobj = data, *ksobj;
  83. const struct kobj_ns_type_operations *ops;
  84. ops = kobj_ns_ops(kobj);
  85. if (!ops && kobj->kset) {
  86. ksobj = &kobj->kset->kobj;
  87. if (ksobj->parent != NULL)
  88. ops = kobj_ns_ops(ksobj->parent);
  89. }
  90. if (ops && ops->netlink_ns && kobj->ktype->namespace) {
  91. const void *sock_ns, *ns;
  92. ns = kobj->ktype->namespace(kobj);
  93. sock_ns = ops->netlink_ns(dsk);
  94. return sock_ns != ns;
  95. }
  96. return 0;
  97. }
  98. #endif
  99. #ifdef CONFIG_UEVENT_HELPER
  100. static int kobj_usermode_filter(struct kobject *kobj)
  101. {
  102. const struct kobj_ns_type_operations *ops;
  103. ops = kobj_ns_ops(kobj);
  104. if (ops) {
  105. const void *init_ns, *ns;
  106. ns = kobj->ktype->namespace(kobj);
  107. init_ns = ops->initial_ns();
  108. return ns != init_ns;
  109. }
  110. return 0;
  111. }
  112. static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
  113. {
  114. int len;
  115. len = strlcpy(&env->buf[env->buflen], subsystem,
  116. sizeof(env->buf) - env->buflen);
  117. if (len >= (sizeof(env->buf) - env->buflen)) {
  118. WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
  119. return -ENOMEM;
  120. }
  121. env->argv[0] = uevent_helper;
  122. env->argv[1] = &env->buf[env->buflen];
  123. env->argv[2] = NULL;
  124. env->buflen += len + 1;
  125. return 0;
  126. }
  127. static void cleanup_uevent_env(struct subprocess_info *info)
  128. {
  129. kfree(info->data);
  130. }
  131. #endif
  132. /**
  133. * kobject_uevent_env - send an uevent with environmental data
  134. *
  135. * @action: action that is happening
  136. * @kobj: struct kobject that the action is happening to
  137. * @envp_ext: pointer to environmental data
  138. *
  139. * Returns 0 if kobject_uevent_env() is completed with success or the
  140. * corresponding error when it fails.
  141. */
  142. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  143. char *envp_ext[])
  144. {
  145. struct kobj_uevent_env *env;
  146. const char *action_string = kobject_actions[action];
  147. const char *devpath = NULL;
  148. const char *subsystem;
  149. struct kobject *top_kobj;
  150. struct kset *kset;
  151. const struct kset_uevent_ops *uevent_ops;
  152. int i = 0;
  153. int retval = 0;
  154. #ifdef CONFIG_NET
  155. struct uevent_sock *ue_sk;
  156. #endif
  157. pr_debug("kobject: '%s' (%p): %s\n",
  158. kobject_name(kobj), kobj, __func__);
  159. /* search the kset we belong to */
  160. top_kobj = kobj;
  161. while (!top_kobj->kset && top_kobj->parent)
  162. top_kobj = top_kobj->parent;
  163. if (!top_kobj->kset) {
  164. pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
  165. "without kset!\n", kobject_name(kobj), kobj,
  166. __func__);
  167. return -EINVAL;
  168. }
  169. kset = top_kobj->kset;
  170. uevent_ops = kset->uevent_ops;
  171. /* skip the event, if uevent_suppress is set*/
  172. if (kobj->uevent_suppress) {
  173. pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
  174. "caused the event to drop!\n",
  175. kobject_name(kobj), kobj, __func__);
  176. return 0;
  177. }
  178. /* skip the event, if the filter returns zero. */
  179. if (uevent_ops && uevent_ops->filter)
  180. if (!uevent_ops->filter(kset, kobj)) {
  181. pr_debug("kobject: '%s' (%p): %s: filter function "
  182. "caused the event to drop!\n",
  183. kobject_name(kobj), kobj, __func__);
  184. return 0;
  185. }
  186. /* originating subsystem */
  187. if (uevent_ops && uevent_ops->name)
  188. subsystem = uevent_ops->name(kset, kobj);
  189. else
  190. subsystem = kobject_name(&kset->kobj);
  191. if (!subsystem) {
  192. pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
  193. "event to drop!\n", kobject_name(kobj), kobj,
  194. __func__);
  195. return 0;
  196. }
  197. /* environment buffer */
  198. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  199. if (!env)
  200. return -ENOMEM;
  201. /* complete object path */
  202. devpath = kobject_get_path(kobj, GFP_KERNEL);
  203. if (!devpath) {
  204. retval = -ENOENT;
  205. goto exit;
  206. }
  207. /* default keys */
  208. retval = add_uevent_var(env, "ACTION=%s", action_string);
  209. if (retval)
  210. goto exit;
  211. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  212. if (retval)
  213. goto exit;
  214. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  215. if (retval)
  216. goto exit;
  217. /* keys passed in from the caller */
  218. if (envp_ext) {
  219. for (i = 0; envp_ext[i]; i++) {
  220. retval = add_uevent_var(env, "%s", envp_ext[i]);
  221. if (retval)
  222. goto exit;
  223. }
  224. }
  225. /* let the kset specific function add its stuff */
  226. if (uevent_ops && uevent_ops->uevent) {
  227. retval = uevent_ops->uevent(kset, kobj, env);
  228. if (retval) {
  229. pr_debug("kobject: '%s' (%p): %s: uevent() returned "
  230. "%d\n", kobject_name(kobj), kobj,
  231. __func__, retval);
  232. goto exit;
  233. }
  234. }
  235. /*
  236. * Mark "add" and "remove" events in the object to ensure proper
  237. * events to userspace during automatic cleanup. If the object did
  238. * send an "add" event, "remove" will automatically generated by
  239. * the core, if not already done by the caller.
  240. */
  241. if (action == KOBJ_ADD)
  242. kobj->state_add_uevent_sent = 1;
  243. else if (action == KOBJ_REMOVE)
  244. kobj->state_remove_uevent_sent = 1;
  245. mutex_lock(&uevent_sock_mutex);
  246. /* we will send an event, so request a new sequence number */
  247. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
  248. if (retval) {
  249. mutex_unlock(&uevent_sock_mutex);
  250. goto exit;
  251. }
  252. #if defined(CONFIG_NET)
  253. /* send netlink message */
  254. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  255. struct sock *uevent_sock = ue_sk->sk;
  256. struct sk_buff *skb;
  257. size_t len;
  258. if (!netlink_has_listeners(uevent_sock, 1))
  259. continue;
  260. /* allocate message with the maximum possible size */
  261. len = strlen(action_string) + strlen(devpath) + 2;
  262. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  263. if (skb) {
  264. char *scratch;
  265. /* add header */
  266. scratch = skb_put(skb, len);
  267. sprintf(scratch, "%s@%s", action_string, devpath);
  268. /* copy keys to our continuous event payload buffer */
  269. for (i = 0; i < env->envp_idx; i++) {
  270. len = strlen(env->envp[i]) + 1;
  271. scratch = skb_put(skb, len);
  272. strcpy(scratch, env->envp[i]);
  273. }
  274. NETLINK_CB(skb).dst_group = 1;
  275. retval = netlink_broadcast_filtered(uevent_sock, skb,
  276. 0, 1, GFP_KERNEL,
  277. kobj_bcast_filter,
  278. kobj);
  279. /* ENOBUFS should be handled in userspace */
  280. if (retval == -ENOBUFS || retval == -ESRCH)
  281. retval = 0;
  282. } else
  283. retval = -ENOMEM;
  284. }
  285. #endif
  286. mutex_unlock(&uevent_sock_mutex);
  287. #ifdef CONFIG_UEVENT_HELPER
  288. /* call uevent_helper, usually only enabled during early boot */
  289. if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
  290. struct subprocess_info *info;
  291. retval = add_uevent_var(env, "HOME=/");
  292. if (retval)
  293. goto exit;
  294. retval = add_uevent_var(env,
  295. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  296. if (retval)
  297. goto exit;
  298. retval = init_uevent_argv(env, subsystem);
  299. if (retval)
  300. goto exit;
  301. retval = -ENOMEM;
  302. info = call_usermodehelper_setup(env->argv[0], env->argv,
  303. env->envp, GFP_KERNEL,
  304. NULL, cleanup_uevent_env, env);
  305. if (info) {
  306. retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
  307. env = NULL; /* freed by cleanup_uevent_env */
  308. }
  309. }
  310. #endif
  311. exit:
  312. kfree(devpath);
  313. kfree(env);
  314. return retval;
  315. }
  316. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  317. /**
  318. * kobject_uevent - notify userspace by sending an uevent
  319. *
  320. * @action: action that is happening
  321. * @kobj: struct kobject that the action is happening to
  322. *
  323. * Returns 0 if kobject_uevent() is completed with success or the
  324. * corresponding error when it fails.
  325. */
  326. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  327. {
  328. return kobject_uevent_env(kobj, action, NULL);
  329. }
  330. EXPORT_SYMBOL_GPL(kobject_uevent);
  331. /**
  332. * add_uevent_var - add key value string to the environment buffer
  333. * @env: environment buffer structure
  334. * @format: printf format for the key=value pair
  335. *
  336. * Returns 0 if environment variable was added successfully or -ENOMEM
  337. * if no space was available.
  338. */
  339. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  340. {
  341. va_list args;
  342. int len;
  343. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  344. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  345. return -ENOMEM;
  346. }
  347. va_start(args, format);
  348. len = vsnprintf(&env->buf[env->buflen],
  349. sizeof(env->buf) - env->buflen,
  350. format, args);
  351. va_end(args);
  352. if (len >= (sizeof(env->buf) - env->buflen)) {
  353. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  354. return -ENOMEM;
  355. }
  356. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  357. env->buflen += len + 1;
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(add_uevent_var);
  361. #if defined(CONFIG_NET)
  362. static int uevent_net_init(struct net *net)
  363. {
  364. struct uevent_sock *ue_sk;
  365. struct netlink_kernel_cfg cfg = {
  366. .groups = 1,
  367. .flags = NL_CFG_F_NONROOT_RECV,
  368. };
  369. ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
  370. if (!ue_sk)
  371. return -ENOMEM;
  372. ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
  373. if (!ue_sk->sk) {
  374. printk(KERN_ERR
  375. "kobject_uevent: unable to create netlink socket!\n");
  376. kfree(ue_sk);
  377. return -ENODEV;
  378. }
  379. mutex_lock(&uevent_sock_mutex);
  380. list_add_tail(&ue_sk->list, &uevent_sock_list);
  381. mutex_unlock(&uevent_sock_mutex);
  382. return 0;
  383. }
  384. static void uevent_net_exit(struct net *net)
  385. {
  386. struct uevent_sock *ue_sk;
  387. mutex_lock(&uevent_sock_mutex);
  388. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  389. if (sock_net(ue_sk->sk) == net)
  390. goto found;
  391. }
  392. mutex_unlock(&uevent_sock_mutex);
  393. return;
  394. found:
  395. list_del(&ue_sk->list);
  396. mutex_unlock(&uevent_sock_mutex);
  397. netlink_kernel_release(ue_sk->sk);
  398. kfree(ue_sk);
  399. }
  400. static struct pernet_operations uevent_net_ops = {
  401. .init = uevent_net_init,
  402. .exit = uevent_net_exit,
  403. };
  404. static int __init kobject_uevent_init(void)
  405. {
  406. return register_pernet_subsys(&uevent_net_ops);
  407. }
  408. postcore_initcall(kobject_uevent_init);
  409. #endif