kobject_uevent.c 10 KB

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