smsgiucv_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Deliver z/VM CP special messages (SMSG) as uevents.
  3. *
  4. * The driver registers for z/VM CP special messages with the
  5. * "APP" prefix. Incoming messages are delivered to user space
  6. * as uevents.
  7. *
  8. * Copyright IBM Corp. 2010
  9. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  10. *
  11. */
  12. #define KMSG_COMPONENT "smsgiucv_app"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/ctype.h>
  15. #include <linux/err.h>
  16. #include <linux/device.h>
  17. #include <linux/list.h>
  18. #include <linux/kobject.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include <net/iucv/iucv.h>
  24. #include "smsgiucv.h"
  25. /* prefix used for SMSG registration */
  26. #define SMSG_PREFIX "APP"
  27. /* SMSG related uevent environment variables */
  28. #define ENV_SENDER_STR "SMSG_SENDER="
  29. #define ENV_SENDER_LEN (strlen(ENV_SENDER_STR) + 8 + 1)
  30. #define ENV_PREFIX_STR "SMSG_ID="
  31. #define ENV_PREFIX_LEN (strlen(ENV_PREFIX_STR) + \
  32. strlen(SMSG_PREFIX) + 1)
  33. #define ENV_TEXT_STR "SMSG_TEXT="
  34. #define ENV_TEXT_LEN(msg) (strlen(ENV_TEXT_STR) + strlen((msg)) + 1)
  35. /* z/VM user ID which is permitted to send SMSGs
  36. * If the value is undefined or empty (""), special messages are
  37. * accepted from any z/VM user ID. */
  38. static char *sender;
  39. module_param(sender, charp, 0400);
  40. MODULE_PARM_DESC(sender, "z/VM user ID from which CP SMSGs are accepted");
  41. /* SMSG device representation */
  42. static struct device *smsg_app_dev;
  43. /* list element for queuing received messages for delivery */
  44. struct smsg_app_event {
  45. struct list_head list;
  46. char *buf;
  47. char *envp[4];
  48. };
  49. /* queue for outgoing uevents */
  50. static LIST_HEAD(smsg_event_queue);
  51. static DEFINE_SPINLOCK(smsg_event_queue_lock);
  52. static void smsg_app_event_free(struct smsg_app_event *ev)
  53. {
  54. kfree(ev->buf);
  55. kfree(ev);
  56. }
  57. static struct smsg_app_event *smsg_app_event_alloc(const char *from,
  58. const char *msg)
  59. {
  60. struct smsg_app_event *ev;
  61. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  62. if (!ev)
  63. return NULL;
  64. ev->buf = kzalloc(ENV_SENDER_LEN + ENV_PREFIX_LEN +
  65. ENV_TEXT_LEN(msg), GFP_ATOMIC);
  66. if (!ev->buf) {
  67. kfree(ev);
  68. return NULL;
  69. }
  70. /* setting up environment pointers into buf */
  71. ev->envp[0] = ev->buf;
  72. ev->envp[1] = ev->envp[0] + ENV_SENDER_LEN;
  73. ev->envp[2] = ev->envp[1] + ENV_PREFIX_LEN;
  74. ev->envp[3] = NULL;
  75. /* setting up environment: sender, prefix name, and message text */
  76. snprintf(ev->envp[0], ENV_SENDER_LEN, ENV_SENDER_STR "%s", from);
  77. snprintf(ev->envp[1], ENV_PREFIX_LEN, ENV_PREFIX_STR "%s", SMSG_PREFIX);
  78. snprintf(ev->envp[2], ENV_TEXT_LEN(msg), ENV_TEXT_STR "%s", msg);
  79. return ev;
  80. }
  81. static void smsg_event_work_fn(struct work_struct *work)
  82. {
  83. LIST_HEAD(event_queue);
  84. struct smsg_app_event *p, *n;
  85. struct device *dev;
  86. dev = get_device(smsg_app_dev);
  87. if (!dev)
  88. return;
  89. spin_lock_bh(&smsg_event_queue_lock);
  90. list_splice_init(&smsg_event_queue, &event_queue);
  91. spin_unlock_bh(&smsg_event_queue_lock);
  92. list_for_each_entry_safe(p, n, &event_queue, list) {
  93. list_del(&p->list);
  94. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, p->envp);
  95. smsg_app_event_free(p);
  96. }
  97. put_device(dev);
  98. }
  99. static DECLARE_WORK(smsg_event_work, smsg_event_work_fn);
  100. static void smsg_app_callback(const char *from, char *msg)
  101. {
  102. struct smsg_app_event *se;
  103. /* check if the originating z/VM user ID matches
  104. * the configured sender. */
  105. if (sender && strlen(sender) > 0 && strcmp(from, sender) != 0)
  106. return;
  107. /* get start of message text (skip prefix and leading blanks) */
  108. msg += strlen(SMSG_PREFIX);
  109. while (*msg && isspace(*msg))
  110. msg++;
  111. if (*msg == '\0')
  112. return;
  113. /* allocate event list element and its environment */
  114. se = smsg_app_event_alloc(from, msg);
  115. if (!se)
  116. return;
  117. /* queue event and schedule work function */
  118. spin_lock(&smsg_event_queue_lock);
  119. list_add_tail(&se->list, &smsg_event_queue);
  120. spin_unlock(&smsg_event_queue_lock);
  121. schedule_work(&smsg_event_work);
  122. return;
  123. }
  124. static int __init smsgiucv_app_init(void)
  125. {
  126. struct device_driver *smsgiucv_drv;
  127. int rc;
  128. if (!MACHINE_IS_VM)
  129. return -ENODEV;
  130. smsg_app_dev = kzalloc(sizeof(*smsg_app_dev), GFP_KERNEL);
  131. if (!smsg_app_dev)
  132. return -ENOMEM;
  133. smsgiucv_drv = driver_find(SMSGIUCV_DRV_NAME, &iucv_bus);
  134. if (!smsgiucv_drv) {
  135. kfree(smsg_app_dev);
  136. return -ENODEV;
  137. }
  138. rc = dev_set_name(smsg_app_dev, KMSG_COMPONENT);
  139. if (rc) {
  140. kfree(smsg_app_dev);
  141. goto fail;
  142. }
  143. smsg_app_dev->bus = &iucv_bus;
  144. smsg_app_dev->parent = iucv_root;
  145. smsg_app_dev->release = (void (*)(struct device *)) kfree;
  146. smsg_app_dev->driver = smsgiucv_drv;
  147. rc = device_register(smsg_app_dev);
  148. if (rc) {
  149. put_device(smsg_app_dev);
  150. goto fail;
  151. }
  152. /* convert sender to uppercase characters */
  153. if (sender) {
  154. int len = strlen(sender);
  155. while (len--)
  156. sender[len] = toupper(sender[len]);
  157. }
  158. /* register with the smsgiucv device driver */
  159. rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback);
  160. if (rc) {
  161. device_unregister(smsg_app_dev);
  162. goto fail;
  163. }
  164. rc = 0;
  165. fail:
  166. return rc;
  167. }
  168. module_init(smsgiucv_app_init);
  169. static void __exit smsgiucv_app_exit(void)
  170. {
  171. /* unregister callback */
  172. smsg_unregister_callback(SMSG_PREFIX, smsg_app_callback);
  173. /* cancel pending work and flush any queued event work */
  174. cancel_work_sync(&smsg_event_work);
  175. smsg_event_work_fn(&smsg_event_work);
  176. device_unregister(smsg_app_dev);
  177. }
  178. module_exit(smsgiucv_app_exit);
  179. MODULE_LICENSE("GPL v2");
  180. MODULE_DESCRIPTION("Deliver z/VM CP SMSG as uevents");
  181. MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");