kobject_uevent.c 8.4 KB

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