kobject_uevent.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 char *action_to_string(enum kobject_action action)
  30. {
  31. switch (action) {
  32. case KOBJ_ADD:
  33. return "add";
  34. case KOBJ_REMOVE:
  35. return "remove";
  36. case KOBJ_CHANGE:
  37. return "change";
  38. case KOBJ_MOUNT:
  39. return "mount";
  40. case KOBJ_UMOUNT:
  41. return "umount";
  42. case KOBJ_OFFLINE:
  43. return "offline";
  44. case KOBJ_ONLINE:
  45. return "online";
  46. default:
  47. return NULL;
  48. }
  49. }
  50. static struct sock *uevent_sock;
  51. /**
  52. * send_uevent - notify userspace by sending event through 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, gfp_t 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, gfp_t 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. /**
  148. * kobject_hotplug - notify userspace by executing /sbin/hotplug
  149. *
  150. * @action: action that is happening (usually "ADD" or "REMOVE")
  151. * @kobj: struct kobject that the action is happening to
  152. */
  153. void kobject_hotplug(struct kobject *kobj, enum kobject_action action)
  154. {
  155. char *argv [3];
  156. char **envp = NULL;
  157. char *buffer = NULL;
  158. char *seq_buff;
  159. char *scratch;
  160. int i = 0;
  161. int retval;
  162. char *kobj_path = NULL;
  163. const char *name = NULL;
  164. char *action_string;
  165. u64 seq;
  166. struct kobject *top_kobj = kobj;
  167. struct kset *kset;
  168. static struct kset_hotplug_ops null_hotplug_ops;
  169. struct kset_hotplug_ops *hotplug_ops = &null_hotplug_ops;
  170. /* If this kobj does not belong to a kset,
  171. try to find a parent that does. */
  172. if (!top_kobj->kset && top_kobj->parent) {
  173. do {
  174. top_kobj = top_kobj->parent;
  175. } while (!top_kobj->kset && top_kobj->parent);
  176. }
  177. if (top_kobj->kset)
  178. kset = top_kobj->kset;
  179. else
  180. return;
  181. if (kset->hotplug_ops)
  182. hotplug_ops = kset->hotplug_ops;
  183. /* If the kset has a filter operation, call it.
  184. Skip the event, if the filter returns zero. */
  185. if (hotplug_ops->filter) {
  186. if (!hotplug_ops->filter(kset, kobj))
  187. return;
  188. }
  189. pr_debug ("%s\n", __FUNCTION__);
  190. action_string = action_to_string(action);
  191. if (!action_string)
  192. return;
  193. envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
  194. if (!envp)
  195. return;
  196. memset (envp, 0x00, NUM_ENVP * sizeof (char *));
  197. buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  198. if (!buffer)
  199. goto exit;
  200. if (hotplug_ops->name)
  201. name = hotplug_ops->name(kset, kobj);
  202. if (name == NULL)
  203. name = kobject_name(&kset->kobj);
  204. argv [0] = hotplug_path;
  205. argv [1] = (char *)name; /* won't be changed but 'const' has to go */
  206. argv [2] = NULL;
  207. /* minimal command environment */
  208. envp [i++] = "HOME=/";
  209. envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  210. scratch = buffer;
  211. envp [i++] = scratch;
  212. scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
  213. kobj_path = kobject_get_path(kobj, GFP_KERNEL);
  214. if (!kobj_path)
  215. goto exit;
  216. envp [i++] = scratch;
  217. scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
  218. envp [i++] = scratch;
  219. scratch += sprintf(scratch, "SUBSYSTEM=%s", name) + 1;
  220. /* reserve space for the sequence,
  221. * put the real one in after the hotplug call */
  222. envp[i++] = seq_buff = scratch;
  223. scratch += strlen("SEQNUM=18446744073709551616") + 1;
  224. if (hotplug_ops->hotplug) {
  225. /* have the kset specific function add its stuff */
  226. retval = hotplug_ops->hotplug (kset, kobj,
  227. &envp[i], NUM_ENVP - i, scratch,
  228. BUFFER_SIZE - (scratch - buffer));
  229. if (retval) {
  230. pr_debug ("%s - hotplug() returned %d\n",
  231. __FUNCTION__, retval);
  232. goto exit;
  233. }
  234. }
  235. spin_lock(&sequence_lock);
  236. seq = ++hotplug_seqnum;
  237. spin_unlock(&sequence_lock);
  238. sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq);
  239. pr_debug ("%s: %s %s seq=%llu %s %s %s %s %s\n",
  240. __FUNCTION__, argv[0], argv[1], (unsigned long long)seq,
  241. envp[0], envp[1], envp[2], envp[3], envp[4]);
  242. send_uevent(action_string, kobj_path, envp, GFP_KERNEL);
  243. if (!hotplug_path[0])
  244. goto exit;
  245. retval = call_usermodehelper (argv[0], argv, envp, 0);
  246. if (retval)
  247. pr_debug ("%s - call_usermodehelper returned %d\n",
  248. __FUNCTION__, retval);
  249. exit:
  250. kfree(kobj_path);
  251. kfree(buffer);
  252. kfree(envp);
  253. return;
  254. }
  255. EXPORT_SYMBOL(kobject_hotplug);
  256. /**
  257. * add_hotplug_env_var - helper for creating hotplug environment variables
  258. * @envp: Pointer to table of environment variables, as passed into
  259. * hotplug() method.
  260. * @num_envp: Number of environment variable slots available, as
  261. * passed into hotplug() method.
  262. * @cur_index: Pointer to current index into @envp. It should be
  263. * initialized to 0 before the first call to add_hotplug_env_var(),
  264. * and will be incremented on success.
  265. * @buffer: Pointer to buffer for environment variables, as passed
  266. * into hotplug() method.
  267. * @buffer_size: Length of @buffer, as passed into hotplug() method.
  268. * @cur_len: Pointer to current length of space used in @buffer.
  269. * Should be initialized to 0 before the first call to
  270. * add_hotplug_env_var(), and will be incremented on success.
  271. * @format: Format for creating environment variable (of the form
  272. * "XXX=%x") for snprintf().
  273. *
  274. * Returns 0 if environment variable was added successfully or -ENOMEM
  275. * if no space was available.
  276. */
  277. int add_hotplug_env_var(char **envp, int num_envp, int *cur_index,
  278. char *buffer, int buffer_size, int *cur_len,
  279. const char *format, ...)
  280. {
  281. va_list args;
  282. /*
  283. * We check against num_envp - 1 to make sure there is at
  284. * least one slot left after we return, since the hotplug
  285. * method needs to set the last slot to NULL.
  286. */
  287. if (*cur_index >= num_envp - 1)
  288. return -ENOMEM;
  289. envp[*cur_index] = buffer + *cur_len;
  290. va_start(args, format);
  291. *cur_len += vsnprintf(envp[*cur_index],
  292. max(buffer_size - *cur_len, 0),
  293. format, args) + 1;
  294. va_end(args);
  295. if (*cur_len > buffer_size)
  296. return -ENOMEM;
  297. (*cur_index)++;
  298. return 0;
  299. }
  300. EXPORT_SYMBOL(add_hotplug_env_var);
  301. #endif /* CONFIG_HOTPLUG */