2 * Deliver z/VM CP special messages (SMSG) as uevents.
4 * The driver registers for z/VM CP special messages with the
5 * "APP" prefix. Incoming messages are delivered to user space
8 * Copyright IBM Corp. 2010
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
12 #define KMSG_COMPONENT "smsgiucv_app"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/ctype.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
18 #include <linux/list.h>
19 #include <linux/kobject.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/workqueue.h>
24 #include <net/iucv/iucv.h>
27 /* prefix used for SMSG registration */
28 #define SMSG_PREFIX "APP"
30 /* SMSG related uevent environment variables */
31 #define ENV_SENDER_STR "SMSG_SENDER="
32 #define ENV_SENDER_LEN (strlen(ENV_SENDER_STR) + 8 + 1)
33 #define ENV_PREFIX_STR "SMSG_ID="
34 #define ENV_PREFIX_LEN (strlen(ENV_PREFIX_STR) + \
35 strlen(SMSG_PREFIX) + 1)
36 #define ENV_TEXT_STR "SMSG_TEXT="
37 #define ENV_TEXT_LEN(msg) (strlen(ENV_TEXT_STR) + strlen((msg)) + 1)
39 /* z/VM user ID which is permitted to send SMSGs
40 * If the value is undefined or empty (""), special messages are
41 * accepted from any z/VM user ID. */
43 module_param(sender
, charp
, 0400);
44 MODULE_PARM_DESC(sender
, "z/VM user ID from which CP SMSGs are accepted");
46 /* SMSG device representation */
47 static struct device
*smsg_app_dev
;
49 /* list element for queuing received messages for delivery */
50 struct smsg_app_event
{
51 struct list_head list
;
56 /* queue for outgoing uevents */
57 static LIST_HEAD(smsg_event_queue
);
58 static DEFINE_SPINLOCK(smsg_event_queue_lock
);
60 static void smsg_app_event_free(struct smsg_app_event
*ev
)
66 static struct smsg_app_event
*smsg_app_event_alloc(const char *from
,
69 struct smsg_app_event
*ev
;
71 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
75 ev
->buf
= kzalloc(ENV_SENDER_LEN
+ ENV_PREFIX_LEN
+
76 ENV_TEXT_LEN(msg
), GFP_ATOMIC
);
82 /* setting up environment pointers into buf */
83 ev
->envp
[0] = ev
->buf
;
84 ev
->envp
[1] = ev
->envp
[0] + ENV_SENDER_LEN
;
85 ev
->envp
[2] = ev
->envp
[1] + ENV_PREFIX_LEN
;
88 /* setting up environment: sender, prefix name, and message text */
89 snprintf(ev
->envp
[0], ENV_SENDER_LEN
, ENV_SENDER_STR
"%s", from
);
90 snprintf(ev
->envp
[1], ENV_PREFIX_LEN
, ENV_PREFIX_STR
"%s", SMSG_PREFIX
);
91 snprintf(ev
->envp
[2], ENV_TEXT_LEN(msg
), ENV_TEXT_STR
"%s", msg
);
96 static void smsg_event_work_fn(struct work_struct
*work
)
98 LIST_HEAD(event_queue
);
99 struct smsg_app_event
*p
, *n
;
102 dev
= get_device(smsg_app_dev
);
106 spin_lock_bh(&smsg_event_queue_lock
);
107 list_splice_init(&smsg_event_queue
, &event_queue
);
108 spin_unlock_bh(&smsg_event_queue_lock
);
110 list_for_each_entry_safe(p
, n
, &event_queue
, list
) {
112 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, p
->envp
);
113 smsg_app_event_free(p
);
118 static DECLARE_WORK(smsg_event_work
, smsg_event_work_fn
);
120 static void smsg_app_callback(const char *from
, char *msg
)
122 struct smsg_app_event
*se
;
124 /* check if the originating z/VM user ID matches
125 * the configured sender. */
126 if (sender
&& strlen(sender
) > 0 && strcmp(from
, sender
) != 0)
129 /* get start of message text (skip prefix and leading blanks) */
130 msg
+= strlen(SMSG_PREFIX
);
131 while (*msg
&& isspace(*msg
))
136 /* allocate event list element and its environment */
137 se
= smsg_app_event_alloc(from
, msg
);
141 /* queue event and schedule work function */
142 spin_lock(&smsg_event_queue_lock
);
143 list_add_tail(&se
->list
, &smsg_event_queue
);
144 spin_unlock(&smsg_event_queue_lock
);
146 schedule_work(&smsg_event_work
);
150 static int __init
smsgiucv_app_init(void)
152 struct device_driver
*smsgiucv_drv
;
158 smsg_app_dev
= kzalloc(sizeof(*smsg_app_dev
), GFP_KERNEL
);
162 smsgiucv_drv
= driver_find(SMSGIUCV_DRV_NAME
, &iucv_bus
);
168 rc
= dev_set_name(smsg_app_dev
, KMSG_COMPONENT
);
173 smsg_app_dev
->bus
= &iucv_bus
;
174 smsg_app_dev
->parent
= iucv_root
;
175 smsg_app_dev
->release
= (void (*)(struct device
*)) kfree
;
176 smsg_app_dev
->driver
= smsgiucv_drv
;
177 rc
= device_register(smsg_app_dev
);
179 put_device(smsg_app_dev
);
183 /* convert sender to uppercase characters */
185 int len
= strlen(sender
);
187 sender
[len
] = toupper(sender
[len
]);
190 /* register with the smsgiucv device driver */
191 rc
= smsg_register_callback(SMSG_PREFIX
, smsg_app_callback
);
193 device_unregister(smsg_app_dev
);
201 module_init(smsgiucv_app_init
);
203 static void __exit
smsgiucv_app_exit(void)
205 /* unregister callback */
206 smsg_unregister_callback(SMSG_PREFIX
, smsg_app_callback
);
208 /* cancel pending work and flush any queued event work */
209 cancel_work_sync(&smsg_event_work
);
210 smsg_event_work_fn(&smsg_event_work
);
212 device_unregister(smsg_app_dev
);
214 module_exit(smsgiucv_app_exit
);
216 MODULE_LICENSE("GPL v2");
217 MODULE_DESCRIPTION("Deliver z/VM CP SMSG as uevents");
218 MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");