kobject_uevent.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #define BUFFER_SIZE 2048 /* buffer for the variables */
  24. #define NUM_ENVP 32 /* number of env pointers */
  25. #if defined(CONFIG_HOTPLUG)
  26. u64 uevent_seqnum;
  27. char uevent_helper[UEVENT_HELPER_PATH_LEN] = "/sbin/hotplug";
  28. static DEFINE_SPINLOCK(sequence_lock);
  29. #if defined(CONFIG_NET)
  30. static struct sock *uevent_sock;
  31. #endif
  32. /* the strings here must match the enum in include/linux/kobject.h */
  33. const char *kobject_actions[] = {
  34. "add",
  35. "remove",
  36. "change",
  37. "move",
  38. "online",
  39. "offline",
  40. };
  41. /**
  42. * kobject_uevent_env - send an uevent with environmental data
  43. *
  44. * @action: action that is happening (usually KOBJ_MOVE)
  45. * @kobj: struct kobject that the action is happening to
  46. * @envp_ext: pointer to environmental data
  47. *
  48. * Returns 0 if kobject_uevent() is completed with success or the
  49. * corresponding error when it fails.
  50. */
  51. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  52. char *envp_ext[])
  53. {
  54. char **envp;
  55. char *buffer;
  56. char *scratch;
  57. const char *action_string;
  58. const char *devpath = NULL;
  59. const char *subsystem;
  60. struct kobject *top_kobj;
  61. struct kset *kset;
  62. struct kset_uevent_ops *uevent_ops;
  63. u64 seq;
  64. char *seq_buff;
  65. int i = 0;
  66. int retval = 0;
  67. int j;
  68. pr_debug("%s\n", __FUNCTION__);
  69. action_string = kobject_actions[action];
  70. if (!action_string) {
  71. pr_debug("kobject attempted to send uevent without action_string!\n");
  72. return -EINVAL;
  73. }
  74. /* search the kset we belong to */
  75. top_kobj = kobj;
  76. while (!top_kobj->kset && top_kobj->parent) {
  77. top_kobj = top_kobj->parent;
  78. }
  79. if (!top_kobj->kset) {
  80. pr_debug("kobject attempted to send uevent without kset!\n");
  81. return -EINVAL;
  82. }
  83. kset = top_kobj->kset;
  84. uevent_ops = kset->uevent_ops;
  85. /* skip the event, if the filter returns zero. */
  86. if (uevent_ops && uevent_ops->filter)
  87. if (!uevent_ops->filter(kset, kobj)) {
  88. pr_debug("kobject filter function caused the event to drop!\n");
  89. return 0;
  90. }
  91. /* originating subsystem */
  92. if (uevent_ops && uevent_ops->name)
  93. subsystem = uevent_ops->name(kset, kobj);
  94. else
  95. subsystem = kobject_name(&kset->kobj);
  96. if (!subsystem) {
  97. pr_debug("unset subsytem caused the event to drop!\n");
  98. return 0;
  99. }
  100. /* environment index */
  101. envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
  102. if (!envp)
  103. return -ENOMEM;
  104. /* environment values */
  105. buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  106. if (!buffer) {
  107. retval = -ENOMEM;
  108. goto exit;
  109. }
  110. /* complete object path */
  111. devpath = kobject_get_path(kobj, GFP_KERNEL);
  112. if (!devpath) {
  113. retval = -ENOENT;
  114. goto exit;
  115. }
  116. /* event environemnt for helper process only */
  117. envp[i++] = "HOME=/";
  118. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  119. /* default keys */
  120. scratch = buffer;
  121. envp [i++] = scratch;
  122. scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
  123. envp [i++] = scratch;
  124. scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
  125. envp [i++] = scratch;
  126. scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
  127. for (j = 0; envp_ext && envp_ext[j]; j++)
  128. envp[i++] = envp_ext[j];
  129. /* just reserve the space, overwrite it after kset call has returned */
  130. envp[i++] = seq_buff = scratch;
  131. scratch += strlen("SEQNUM=18446744073709551616") + 1;
  132. /* let the kset specific function add its stuff */
  133. if (uevent_ops && uevent_ops->uevent) {
  134. retval = uevent_ops->uevent(kset, kobj,
  135. &envp[i], NUM_ENVP - i, scratch,
  136. BUFFER_SIZE - (scratch - buffer));
  137. if (retval) {
  138. pr_debug ("%s - uevent() returned %d\n",
  139. __FUNCTION__, retval);
  140. goto exit;
  141. }
  142. }
  143. /* we will send an event, request a new sequence number */
  144. spin_lock(&sequence_lock);
  145. seq = ++uevent_seqnum;
  146. spin_unlock(&sequence_lock);
  147. sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
  148. #if defined(CONFIG_NET)
  149. /* send netlink message */
  150. if (uevent_sock) {
  151. struct sk_buff *skb;
  152. size_t len;
  153. /* allocate message with the maximum possible size */
  154. len = strlen(action_string) + strlen(devpath) + 2;
  155. skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL);
  156. if (skb) {
  157. /* add header */
  158. scratch = skb_put(skb, len);
  159. sprintf(scratch, "%s@%s", action_string, devpath);
  160. /* copy keys to our continuous event payload buffer */
  161. for (i = 2; envp[i]; i++) {
  162. len = strlen(envp[i]) + 1;
  163. scratch = skb_put(skb, len);
  164. strcpy(scratch, envp[i]);
  165. }
  166. NETLINK_CB(skb).dst_group = 1;
  167. netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
  168. }
  169. }
  170. #endif
  171. /* call uevent_helper, usually only enabled during early boot */
  172. if (uevent_helper[0]) {
  173. char *argv [3];
  174. argv [0] = uevent_helper;
  175. argv [1] = (char *)subsystem;
  176. argv [2] = NULL;
  177. call_usermodehelper (argv[0], argv, envp, UMH_WAIT_EXEC);
  178. }
  179. exit:
  180. kfree(devpath);
  181. kfree(buffer);
  182. kfree(envp);
  183. return retval;
  184. }
  185. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  186. /**
  187. * kobject_uevent - notify userspace by ending an uevent
  188. *
  189. * @action: action that is happening (usually KOBJ_ADD and KOBJ_REMOVE)
  190. * @kobj: struct kobject that the action is happening to
  191. *
  192. * Returns 0 if kobject_uevent() is completed with success or the
  193. * corresponding error when it fails.
  194. */
  195. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  196. {
  197. return kobject_uevent_env(kobj, action, NULL);
  198. }
  199. EXPORT_SYMBOL_GPL(kobject_uevent);
  200. /**
  201. * add_uevent_var - helper for creating event variables
  202. * @envp: Pointer to table of environment variables, as passed into
  203. * uevent() method.
  204. * @num_envp: Number of environment variable slots available, as
  205. * passed into uevent() method.
  206. * @cur_index: Pointer to current index into @envp. It should be
  207. * initialized to 0 before the first call to add_uevent_var(),
  208. * and will be incremented on success.
  209. * @buffer: Pointer to buffer for environment variables, as passed
  210. * into uevent() method.
  211. * @buffer_size: Length of @buffer, as passed into uevent() method.
  212. * @cur_len: Pointer to current length of space used in @buffer.
  213. * Should be initialized to 0 before the first call to
  214. * add_uevent_var(), and will be incremented on success.
  215. * @format: Format for creating environment variable (of the form
  216. * "XXX=%x") for snprintf().
  217. *
  218. * Returns 0 if environment variable was added successfully or -ENOMEM
  219. * if no space was available.
  220. */
  221. int add_uevent_var(char **envp, int num_envp, int *cur_index,
  222. char *buffer, int buffer_size, int *cur_len,
  223. const char *format, ...)
  224. {
  225. va_list args;
  226. /*
  227. * We check against num_envp - 1 to make sure there is at
  228. * least one slot left after we return, since kobject_uevent()
  229. * needs to set the last slot to NULL.
  230. */
  231. if (*cur_index >= num_envp - 1)
  232. return -ENOMEM;
  233. envp[*cur_index] = buffer + *cur_len;
  234. va_start(args, format);
  235. *cur_len += vsnprintf(envp[*cur_index],
  236. max(buffer_size - *cur_len, 0),
  237. format, args) + 1;
  238. va_end(args);
  239. if (*cur_len > buffer_size)
  240. return -ENOMEM;
  241. (*cur_index)++;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(add_uevent_var);
  245. #if defined(CONFIG_NET)
  246. static int __init kobject_uevent_init(void)
  247. {
  248. uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, 1, NULL,
  249. NULL, THIS_MODULE);
  250. if (!uevent_sock) {
  251. printk(KERN_ERR
  252. "kobject_uevent: unable to create netlink socket!\n");
  253. return -ENODEV;
  254. }
  255. return 0;
  256. }
  257. postcore_initcall(kobject_uevent_init);
  258. #endif
  259. #endif /* CONFIG_HOTPLUG */