kobject_uevent.c 8.0 KB

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