kobject_uevent.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_uevent.h>
  22. #include <linux/kobject.h>
  23. #include <net/sock.h>
  24. #define BUFFER_SIZE 1024 /* buffer for the hotplug env */
  25. #define NUM_ENVP 32 /* number of env pointers */
  26. #if defined(CONFIG_KOBJECT_UEVENT) || defined(CONFIG_HOTPLUG)
  27. static char *action_to_string(enum kobject_action action)
  28. {
  29. switch (action) {
  30. case KOBJ_ADD:
  31. return "add";
  32. case KOBJ_REMOVE:
  33. return "remove";
  34. case KOBJ_CHANGE:
  35. return "change";
  36. case KOBJ_MOUNT:
  37. return "mount";
  38. case KOBJ_UMOUNT:
  39. return "umount";
  40. case KOBJ_OFFLINE:
  41. return "offline";
  42. case KOBJ_ONLINE:
  43. return "online";
  44. default:
  45. return NULL;
  46. }
  47. }
  48. #endif
  49. #ifdef CONFIG_KOBJECT_UEVENT
  50. static struct sock *uevent_sock;
  51. /**
  52. * send_uevent - notify userspace by sending event trough netlink socket
  53. *
  54. * @signal: signal name
  55. * @obj: object path (kobject)
  56. * @envp: possible hotplug environment to pass with the message
  57. * @gfp_mask:
  58. */
  59. static int send_uevent(const char *signal, const char *obj,
  60. char **envp, int gfp_mask)
  61. {
  62. struct sk_buff *skb;
  63. char *pos;
  64. int len;
  65. if (!uevent_sock)
  66. return -EIO;
  67. len = strlen(signal) + 1;
  68. len += strlen(obj) + 1;
  69. /* allocate buffer with the maximum possible message size */
  70. skb = alloc_skb(len + BUFFER_SIZE, gfp_mask);
  71. if (!skb)
  72. return -ENOMEM;
  73. pos = skb_put(skb, len);
  74. sprintf(pos, "%s@%s", signal, obj);
  75. /* copy the environment key by key to our continuous buffer */
  76. if (envp) {
  77. int i;
  78. for (i = 2; envp[i]; i++) {
  79. len = strlen(envp[i]) + 1;
  80. pos = skb_put(skb, len);
  81. strcpy(pos, envp[i]);
  82. }
  83. }
  84. return netlink_broadcast(uevent_sock, skb, 0, 1, gfp_mask);
  85. }
  86. static int do_kobject_uevent(struct kobject *kobj, enum kobject_action action,
  87. struct attribute *attr, int gfp_mask)
  88. {
  89. char *path;
  90. char *attrpath;
  91. char *signal;
  92. int len;
  93. int rc = -ENOMEM;
  94. path = kobject_get_path(kobj, gfp_mask);
  95. if (!path)
  96. return -ENOMEM;
  97. signal = action_to_string(action);
  98. if (!signal)
  99. return -EINVAL;
  100. if (attr) {
  101. len = strlen(path);
  102. len += strlen(attr->name) + 2;
  103. attrpath = kmalloc(len, gfp_mask);
  104. if (!attrpath)
  105. goto exit;
  106. sprintf(attrpath, "%s/%s", path, attr->name);
  107. rc = send_uevent(signal, attrpath, NULL, gfp_mask);
  108. kfree(attrpath);
  109. } else
  110. rc = send_uevent(signal, path, NULL, gfp_mask);
  111. exit:
  112. kfree(path);
  113. return rc;
  114. }
  115. /**
  116. * kobject_uevent - notify userspace by sending event through netlink socket
  117. *
  118. * @signal: signal name
  119. * @kobj: struct kobject that the event is happening to
  120. * @attr: optional struct attribute the event belongs to
  121. */
  122. int kobject_uevent(struct kobject *kobj, enum kobject_action action,
  123. struct attribute *attr)
  124. {
  125. return do_kobject_uevent(kobj, action, attr, GFP_KERNEL);
  126. }
  127. EXPORT_SYMBOL_GPL(kobject_uevent);
  128. int kobject_uevent_atomic(struct kobject *kobj, enum kobject_action action,
  129. struct attribute *attr)
  130. {
  131. return do_kobject_uevent(kobj, action, attr, GFP_ATOMIC);
  132. }
  133. EXPORT_SYMBOL_GPL(kobject_uevent_atomic);
  134. static int __init kobject_uevent_init(void)
  135. {
  136. uevent_sock = netlink_kernel_create(NETLINK_KOBJECT_UEVENT, NULL);
  137. if (!uevent_sock) {
  138. printk(KERN_ERR
  139. "kobject_uevent: unable to create netlink socket!\n");
  140. return -ENODEV;
  141. }
  142. return 0;
  143. }
  144. postcore_initcall(kobject_uevent_init);
  145. #else
  146. static inline int send_uevent(const char *signal, const char *obj,
  147. char **envp, int gfp_mask)
  148. {
  149. return 0;
  150. }
  151. #endif /* CONFIG_KOBJECT_UEVENT */
  152. #ifdef CONFIG_HOTPLUG
  153. char hotplug_path[HOTPLUG_PATH_LEN] = "/sbin/hotplug";
  154. u64 hotplug_seqnum;
  155. static DEFINE_SPINLOCK(sequence_lock);
  156. /**
  157. * kobject_hotplug - notify userspace by executing /sbin/hotplug
  158. *
  159. * @action: action that is happening (usually "ADD" or "REMOVE")
  160. * @kobj: struct kobject that the action is happening to
  161. */
  162. void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
  163. {
  164. char *argv [3];
  165. char **envp = NULL;
  166. char *buffer = NULL;
  167. char *seq_buff;
  168. char *scratch;
  169. int i = 0;
  170. int retval;
  171. char *kobj_path = NULL;
  172. const char *name = NULL;
  173. char *action_string;
  174. u64 seq;
  175. struct kobject *top_kobj = kobj;
  176. struct kset *kset;
  177. static struct kset_hotplug_ops null_hotplug_ops;
  178. struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
  179. /* If this kobj does not belong to a kset,
  180. try to find a parent that does. */
  181. if (!top_kobj->kset && top_kobj->parent) {
  182. do {
  183. top_kobj = top_kobj->parent;
  184. } while (!top_kobj->kset && top_kobj->parent);
  185. }
  186. if (top_kobj->kset)
  187. kset = top_kobj->kset;
  188. else
  189. return;
  190. if (kset->hotplug_ops)
  191. hotplug_ops = kset->hotplug_ops;
  192. /* If the kset has a filter operation, call it.
  193. Skip the event, if the filter returns zero. */
  194. if (hotplug_ops->filter) {
  195. if (!hotplug_ops->filter(kset, kobj))
  196. return;
  197. }
  198. pr_debug ("%s\n", __FUNCTION__);
  199. action_string = action_to_string(action);
  200. if (!action_string)
  201. return;
  202. envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
  203. if (!envp)
  204. return;
  205. memset (envp, 0x00, NUM_ENVP * sizeof (char *));
  206. buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  207. if (!buffer)
  208. goto exit;
  209. if (hotplug_ops->name)
  210. name = hotplug_ops->name(kset, kobj);
  211. if (name == NULL)
  212. name = kobject_name(&kset->kobj);
  213. argv [0] = hotplug_path;
  214. argv [1] = (char *)name; /* won't be changed but 'const' has to go */
  215. argv [2] = NULL;
  216. /* minimal command environment */
  217. envp [i++] = "HOME=/";
  218. envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  219. scratch = buffer;
  220. envp [i++] = scratch;
  221. scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
  222. kobj_path = kobject_get_path(kobj, GFP_KERNEL);
  223. if (!kobj_path)
  224. goto exit;
  225. envp [i++] = scratch;
  226. scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
  227. envp [i++] = scratch;
  228. scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
  229. /* reserve space for the sequence,
  230. * put the real one in after the hotplug call */
  231. envp[i++] = seq_buff = scratch;
  232. scratch += strlen("SEQNUM=18446744073709551616") + 1;
  233. if (hotplug_ops->hotplug) {
  234. /* have the kset specific function add its stuff */
  235. retval = hotplug_ops->hotplug (kset, kobj,
  236. &envp[i], NUM_ENVP - i, scratch,
  237. BUFFER_SIZE - (scratch - buffer));
  238. if (retval) {
  239. pr_debug ("%s - hotplug() returned %d\n",
  240. __FUNCTION__, retval);
  241. goto exit;
  242. }
  243. }
  244. spin_lock(&sequence_lock);
  245. seq = ++hotplug_seqnum;
  246. spin_unlock(&sequence_lock);
  247. sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
  248. pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
  249. __FUNCTION__, argv[0], argv[1], (unsigned long long)seq,
  250. envp[0], envp[1], envp[2], envp[3], envp[4]);
  251. send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
  252. if (!hotplug_path[0])
  253. goto exit;
  254. retval = call_usermodehelper (argv[0], argv, envp, 0);
  255. if (retval)
  256. pr_debug ("%s - call_usermodehelper returned %d\n",
  257. __FUNCTION__, retval);
  258. exit:
  259. kfree(kobj_path);
  260. kfree(buffer);
  261. kfree(envp);
  262. return;
  263. }
  264. EXPORT_SYMBOL(kobject_hotplug);
  265. /**
  266. * add_hotplug_env_var - helper for creating hotplug environment variables
  267. * @envp: Pointer to table of environment variables, as passed into
  268. * hotplug() method.
  269. * @num_envp: Number of environment variable slots available, as
  270. * passed into hotplug() method.
  271. * @cur_index: Pointer to current index into @envp. It should be
  272. * initialized to 0 before the first call to add_hotplug_env_var(),
  273. * and will be incremented on success.
  274. * @buffer: Pointer to buffer for environment variables, as passed
  275. * into hotplug() method.
  276. * @buffer_size: Length of @buffer, as passed into hotplug() method.
  277. * @cur_len: Pointer to current length of space used in @buffer.
  278. * Should be initialized to 0 before the first call to
  279. * add_hotplug_env_var(), and will be incremented on success.
  280. * @format: Format for creating environment variable (of the form
  281. * "XXX=%x") for snprintf().
  282. *
  283. * Returns 0 if environment variable was added successfully or -ENOMEM
  284. * if no space was available.
  285. */
  286. int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
  287. char *buffer, int buffer_size, int *cur_len,
  288. const char *format, ...)
  289. {
  290. va_list args;
  291. /*
  292. * We check against num_envp - 1 to make sure there is at
  293. * least one slot left after we return, since the hotplug
  294. * method needs to set the last slot to NULL.
  295. */
  296. if (*cur_index >= num_envp - 1)
  297. return -ENOMEM;
  298. envp[*cur_index] = buffer + *cur_len;
  299. va_start(args, format);
  300. *cur_len += vsnprintf(envp[*cur_index],
  301. max(buffer_size - *cur_len, 0),
  302. format, args) + 1;
  303. va_end(args);
  304. if (*cur_len > buffer_size)
  305. return -ENOMEM;
  306. (*cur_index)++;
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(add_hotplug_env_var);
  310. #endif /* CONFIG_HOTPLUG */