kobject_uevent.c 10 KB

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