kobject_uevent.c 7.8 KB

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