kobject_uevent.c 6.8 KB

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