kobject_uevent.c 11 KB

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