kobject_uevent.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. retval = netlink_broadcast(uevent_sock, skb, 0, 1,
  201. GFP_KERNEL);
  202. } else
  203. retval = -ENOMEM;
  204. }
  205. #endif
  206. /* call uevent_helper, usually only enabled during early boot */
  207. if (uevent_helper[0]) {
  208. char *argv [3];
  209. argv [0] = uevent_helper;
  210. argv [1] = (char *)subsystem;
  211. argv [2] = NULL;
  212. retval = add_uevent_var(env, "HOME=/");
  213. if (retval)
  214. goto exit;
  215. retval = add_uevent_var(env,
  216. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  217. if (retval)
  218. goto exit;
  219. retval = call_usermodehelper(argv[0], argv,
  220. env->envp, UMH_WAIT_EXEC);
  221. }
  222. exit:
  223. kfree(devpath);
  224. kfree(env);
  225. return retval;
  226. }
  227. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  228. /**
  229. * kobject_uevent - notify userspace by ending an uevent
  230. *
  231. * @action: action that is happening
  232. * @kobj: struct kobject that the action is happening to
  233. *
  234. * Returns 0 if kobject_uevent() is completed with success or the
  235. * corresponding error when it fails.
  236. */
  237. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  238. {
  239. return kobject_uevent_env(kobj, action, NULL);
  240. }
  241. EXPORT_SYMBOL_GPL(kobject_uevent);
  242. /**
  243. * add_uevent_var - add key value string to the environment buffer
  244. * @env: environment buffer structure
  245. * @format: printf format for the key=value pair
  246. *
  247. * Returns 0 if environment variable was added successfully or -ENOMEM
  248. * if no space was available.
  249. */
  250. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  251. {
  252. va_list args;
  253. int len;
  254. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  255. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  256. return -ENOMEM;
  257. }
  258. va_start(args, format);
  259. len = vsnprintf(&env->buf[env->buflen],
  260. sizeof(env->buf) - env->buflen,
  261. format, args);
  262. va_end(args);
  263. if (len >= (sizeof(env->buf) - env->buflen)) {
  264. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  265. return -ENOMEM;
  266. }
  267. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  268. env->buflen += len + 1;
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(add_uevent_var);
  272. #if defined(CONFIG_NET)
  273. static int __init kobject_uevent_init(void)
  274. {
  275. uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
  276. 1, NULL, NULL, THIS_MODULE);
  277. if (!uevent_sock) {
  278. printk(KERN_ERR
  279. "kobject_uevent: unable to create netlink socket!\n");
  280. return -ENODEV;
  281. }
  282. return 0;
  283. }
  284. postcore_initcall(kobject_uevent_init);
  285. #endif