kobject_uevent.c 6.9 KB

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