kobject_uevent.c 7.9 KB

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