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 (usually KOBJ_MOVE)
  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. }
  97. if (!top_kobj->kset) {
  98. pr_debug("kobject attempted to send uevent without kset!\n");
  99. return -EINVAL;
  100. }
  101. kset = top_kobj->kset;
  102. uevent_ops = kset->uevent_ops;
  103. /* skip the event, if the filter returns zero. */
  104. if (uevent_ops && uevent_ops->filter)
  105. if (!uevent_ops->filter(kset, kobj)) {
  106. pr_debug("kobject filter function caused the event to drop!\n");
  107. return 0;
  108. }
  109. /* originating subsystem */
  110. if (uevent_ops && uevent_ops->name)
  111. subsystem = uevent_ops->name(kset, kobj);
  112. else
  113. subsystem = kobject_name(&kset->kobj);
  114. if (!subsystem) {
  115. pr_debug("unset subsytem caused the event to drop!\n");
  116. return 0;
  117. }
  118. /* environment buffer */
  119. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  120. if (!env)
  121. return -ENOMEM;
  122. /* complete object path */
  123. devpath = kobject_get_path(kobj, GFP_KERNEL);
  124. if (!devpath) {
  125. retval = -ENOENT;
  126. goto exit;
  127. }
  128. /* default keys */
  129. retval = add_uevent_var(env, "ACTION=%s", action_string);
  130. if (retval)
  131. goto exit;
  132. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  133. if (retval)
  134. goto exit;
  135. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  136. if (retval)
  137. goto exit;
  138. /* keys passed in from the caller */
  139. if (envp_ext) {
  140. for (i = 0; envp_ext[i]; i++) {
  141. retval = add_uevent_var(env, envp_ext[i]);
  142. if (retval)
  143. goto exit;
  144. }
  145. }
  146. /* let the kset specific function add its stuff */
  147. if (uevent_ops && uevent_ops->uevent) {
  148. retval = uevent_ops->uevent(kset, kobj, env);
  149. if (retval) {
  150. pr_debug ("%s - uevent() returned %d\n",
  151. __FUNCTION__, retval);
  152. goto exit;
  153. }
  154. }
  155. /* we will send an event, so request a new sequence number */
  156. spin_lock(&sequence_lock);
  157. seq = ++uevent_seqnum;
  158. spin_unlock(&sequence_lock);
  159. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
  160. if (retval)
  161. goto exit;
  162. #if defined(CONFIG_NET)
  163. /* send netlink message */
  164. if (uevent_sock) {
  165. struct sk_buff *skb;
  166. size_t len;
  167. /* allocate message with the maximum possible size */
  168. len = strlen(action_string) + strlen(devpath) + 2;
  169. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  170. if (skb) {
  171. char *scratch;
  172. /* add header */
  173. scratch = skb_put(skb, len);
  174. sprintf(scratch, "%s@%s", action_string, devpath);
  175. /* copy keys to our continuous event payload buffer */
  176. for (i = 0; i < env->envp_idx; i++) {
  177. len = strlen(env->envp[i]) + 1;
  178. scratch = skb_put(skb, len);
  179. strcpy(scratch, env->envp[i]);
  180. }
  181. NETLINK_CB(skb).dst_group = 1;
  182. netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
  183. }
  184. }
  185. #endif
  186. /* call uevent_helper, usually only enabled during early boot */
  187. if (uevent_helper[0]) {
  188. char *argv [3];
  189. argv [0] = uevent_helper;
  190. argv [1] = (char *)subsystem;
  191. argv [2] = NULL;
  192. retval = add_uevent_var(env, "HOME=/");
  193. if (retval)
  194. goto exit;
  195. retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  196. if (retval)
  197. goto exit;
  198. call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
  199. }
  200. exit:
  201. kfree(devpath);
  202. kfree(env);
  203. return retval;
  204. }
  205. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  206. /**
  207. * kobject_uevent - notify userspace by ending an uevent
  208. *
  209. * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
  210. * @kobj: struct kobject that the action is happening to
  211. *
  212. * Returns 0 if kobject_uevent() is completed with success or the
  213. * corresponding error when it fails.
  214. */
  215. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  216. {
  217. return kobject_uevent_env(kobj, action, NULL);
  218. }
  219. EXPORT_SYMBOL_GPL(kobject_uevent);
  220. /**
  221. * add_uevent_var - add key value string to the environment buffer
  222. * @env: environment buffer structure
  223. * @format: printf format for the key=value pair
  224. *
  225. * Returns 0 if environment variable was added successfully or -ENOMEM
  226. * if no space was available.
  227. */
  228. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  229. {
  230. va_list args;
  231. int len;
  232. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  233. printk(KERN_ERR "add_uevent_var: too many keys\n");
  234. WARN_ON(1);
  235. return -ENOMEM;
  236. }
  237. va_start(args, format);
  238. len = vsnprintf(&env->buf[env->buflen],
  239. sizeof(env->buf) - env->buflen,
  240. format, args);
  241. va_end(args);
  242. if (len >= (sizeof(env->buf) - env->buflen)) {
  243. printk(KERN_ERR "add_uevent_var: buffer size too small\n");
  244. WARN_ON(1);
  245. return -ENOMEM;
  246. }
  247. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  248. env->buflen += len + 1;
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(add_uevent_var);
  252. #if defined(CONFIG_NET)
  253. static int __init kobject_uevent_init(void)
  254. {
  255. uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
  256. 1, NULL, NULL, THIS_MODULE);
  257. if (!uevent_sock) {
  258. printk(KERN_ERR
  259. "kobject_uevent: unable to create netlink socket!\n");
  260. return -ENODEV;
  261. }
  262. return 0;
  263. }
  264. postcore_initcall(kobject_uevent_init);
  265. #endif