kobject_uevent.c 8.4 KB

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