Linux Audio

Check our new training course

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