kobject_uevent.c 7.3 KB

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