kobject_uevent.c 9.0 KB

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