kobject_uevent.c 7.9 KB

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