kobject_uevent.c 7.8 KB

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