kobject_uevent.c 6.6 KB

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