kobject_uevent.c 6.7 KB

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