kobject_uevent.c 6.7 KB

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