kobject_uevent.c 6.7 KB

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