kobject_uevent.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/socket.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/netlink.h>
  20. #include <linux/string.h>
  21. #include <linux/kobject.h>
  22. #include <net/sock.h>
  23. /* the strings here must match the enum in include/linux/kobject.h */
  24. const char *kobject_actions[] = {
  25. "add",
  26. "remove",
  27. "change",
  28. "move",
  29. "online",
  30. "offline",
  31. };
  32. #if defined(CONFIG_HOTPLUG)
  33. u64 uevent_seqnum;
  34. char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
  35. static DEFINE_SPINLOCK(sequence_lock);
  36. #if defined(CONFIG_NET)
  37. static struct sock *uevent_sock;
  38. #endif
  39. /**
  40. * kobject_uevent_env - send an uevent with environmental data
  41. *
  42. * @action: action that is happening (usually KOBJ_MOVE)
  43. * @kobj: struct kobject that the action is happening to
  44. * @envp_ext: pointer to environmental data
  45. *
  46. * Returns 0 if kobject_uevent() is completed with success or the
  47. * corresponding error when it fails.
  48. */
  49. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  50. char *envp_ext[])
  51. {
  52. struct kobj_uevent_env *env;
  53. const char *action_string = kobject_actions[action];
  54. const char *devpath = NULL;
  55. const char *subsystem;
  56. struct kobject *top_kobj;
  57. struct kset *kset;
  58. struct kset_uevent_ops *uevent_ops;
  59. u64 seq;
  60. int i = 0;
  61. int retval = 0;
  62. pr_debug("%s\n", __FUNCTION__);
  63. /* search the kset we belong to */
  64. top_kobj = kobj;
  65. while (!top_kobj->kset && top_kobj->parent) {
  66. top_kobj = top_kobj->parent;
  67. }
  68. if (!top_kobj->kset) {
  69. pr_debug("kobject attempted to send uevent without kset!\n");
  70. return -EINVAL;
  71. }
  72. kset = top_kobj->kset;
  73. uevent_ops = kset->uevent_ops;
  74. /* skip the event, if the filter returns zero. */
  75. if (uevent_ops && uevent_ops->filter)
  76. if (!uevent_ops->filter(kset, kobj)) {
  77. pr_debug("kobject filter function caused the event to drop!\n");
  78. return 0;
  79. }
  80. /* originating subsystem */
  81. if (uevent_ops && uevent_ops->name)
  82. subsystem = uevent_ops->name(kset, kobj);
  83. else
  84. subsystem = kobject_name(&kset->kobj);
  85. if (!subsystem) {
  86. pr_debug("unset subsytem caused the event to drop!\n");
  87. return 0;
  88. }
  89. /* environment buffer */
  90. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  91. if (!env)
  92. return -ENOMEM;
  93. /* complete object path */
  94. devpath = kobject_get_path(kobj, GFP_KERNEL);
  95. if (!devpath) {
  96. retval = -ENOENT;
  97. goto exit;
  98. }
  99. /* default keys */
  100. retval = add_uevent_var(env, "ACTION=%s", action_string);
  101. if (retval)
  102. goto exit;
  103. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  104. if (retval)
  105. goto exit;
  106. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  107. if (retval)
  108. goto exit;
  109. /* keys passed in from the caller */
  110. if (envp_ext) {
  111. for (i = 0; envp_ext[i]; i++) {
  112. retval = add_uevent_var(env, envp_ext[i]);
  113. if (retval)
  114. goto exit;
  115. }
  116. }
  117. /* let the kset specific function add its stuff */
  118. if (uevent_ops && uevent_ops->uevent) {
  119. retval = uevent_ops->uevent(kset, kobj, env);
  120. if (retval) {
  121. pr_debug ("%s - uevent() returned %d\n",
  122. __FUNCTION__, retval);
  123. goto exit;
  124. }
  125. }
  126. /* we will send an event, so request a new sequence number */
  127. spin_lock(&sequence_lock);
  128. seq = ++uevent_seqnum;
  129. spin_unlock(&sequence_lock);
  130. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
  131. if (retval)
  132. goto exit;
  133. #if defined(CONFIG_NET)
  134. /* send netlink message */
  135. if (uevent_sock) {
  136. struct sk_buff *skb;
  137. size_t len;
  138. /* allocate message with the maximum possible size */
  139. len = strlen(action_string) + strlen(devpath) + 2;
  140. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  141. if (skb) {
  142. char *scratch;
  143. /* add header */
  144. scratch = skb_put(skb, len);
  145. sprintf(scratch, "%s@%s", action_string, devpath);
  146. /* copy keys to our continuous event payload buffer */
  147. for (i = 0; i < env->envp_idx; i++) {
  148. len = strlen(env->envp[i]) + 1;
  149. scratch = skb_put(skb, len);
  150. strcpy(scratch, env->envp[i]);
  151. }
  152. NETLINK_CB(skb).dst_group = 1;
  153. netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
  154. }
  155. }
  156. #endif
  157. /* call uevent_helper, usually only enabled during early boot */
  158. if (uevent_helper[0]) {
  159. char *argv [3];
  160. argv [0] = uevent_helper;
  161. argv [1] = (char *)subsystem;
  162. argv [2] = NULL;
  163. retval = add_uevent_var(env, "HOME=/");
  164. if (retval)
  165. goto exit;
  166. retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  167. if (retval)
  168. goto exit;
  169. call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
  170. }
  171. exit:
  172. kfree(devpath);
  173. kfree(env);
  174. return retval;
  175. }
  176. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  177. /**
  178. * kobject_uevent - notify userspace by ending an uevent
  179. *
  180. * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
  181. * @kobj: struct kobject that the action is happening to
  182. *
  183. * Returns 0 if kobject_uevent() is completed with success or the
  184. * corresponding error when it fails.
  185. */
  186. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  187. {
  188. return kobject_uevent_env(kobj, action, NULL);
  189. }
  190. EXPORT_SYMBOL_GPL(kobject_uevent);
  191. /**
  192. * add_uevent_var - add key value string to the environment buffer
  193. * @env: environment buffer structure
  194. * @format: printf format for the key=value pair
  195. *
  196. * Returns 0 if environment variable was added successfully or -ENOMEM
  197. * if no space was available.
  198. */
  199. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  200. {
  201. va_list args;
  202. int len;
  203. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  204. printk(KERN_ERR "add_uevent_var: too many keys\n");
  205. WARN_ON(1);
  206. return -ENOMEM;
  207. }
  208. va_start(args, format);
  209. len = vsnprintf(&env->buf[env->buflen],
  210. sizeof(env->buf) - env->buflen,
  211. format, args);
  212. va_end(args);
  213. if (len >= (sizeof(env->buf) - env->buflen)) {
  214. printk(KERN_ERR "add_uevent_var: buffer size too small\n");
  215. WARN_ON(1);
  216. return -ENOMEM;
  217. }
  218. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  219. env->buflen += len + 1;
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(add_uevent_var);
  223. #if defined(CONFIG_NET)
  224. static int __init kobject_uevent_init(void)
  225. {
  226. uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
  227. 1, NULL, NULL, THIS_MODULE);
  228. if (!uevent_sock) {
  229. printk(KERN_ERR
  230. "kobject_uevent: unable to create netlink socket!\n");
  231. return -ENODEV;
  232. }
  233. return 0;
  234. }
  235. postcore_initcall(kobject_uevent_init);
  236. #endif
  237. #endif /* CONFIG_HOTPLUG */