xref: /onnv-gate/usr/src/uts/common/io/sysevent.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Sysevent Driver for GPEC
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/param.h>
35*0Sstevel@tonic-gate #include <sys/cred.h>
36*0Sstevel@tonic-gate #include <sys/file.h>
37*0Sstevel@tonic-gate #include <sys/stat.h>
38*0Sstevel@tonic-gate #include <sys/conf.h>
39*0Sstevel@tonic-gate #include <sys/ddi.h>
40*0Sstevel@tonic-gate #include <sys/sunddi.h>
41*0Sstevel@tonic-gate #include <sys/modctl.h>
42*0Sstevel@tonic-gate #include <sys/open.h>		/* OTYP_CHR definition */
43*0Sstevel@tonic-gate #include <sys/sysmacros.h>	/* L_BITSMINOR definition */
44*0Sstevel@tonic-gate #include <sys/bitmap.h>
45*0Sstevel@tonic-gate #include <sys/sysevent.h>
46*0Sstevel@tonic-gate #include <sys/sysevent_impl.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static dev_info_t *sysevent_devi;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* Definitions for binding handle array */
51*0Sstevel@tonic-gate static ulong_t sysevent_bitmap_initial = 1;	/* index 0 indicates error */
52*0Sstevel@tonic-gate static ulong_t *sysevent_minor_bitmap = &sysevent_bitmap_initial;
53*0Sstevel@tonic-gate static size_t sysevent_minor_bits = BT_NBIPUL;
54*0Sstevel@tonic-gate static kmutex_t sysevent_minor_mutex;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * evchan_ctl acts as a container for the binding handle
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate typedef struct evchan_ctl {
60*0Sstevel@tonic-gate 	evchan_t *chp;
61*0Sstevel@tonic-gate } evchan_ctl_t;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static void *evchan_ctlp;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Check if it's a null terminated array - to avoid DoS attack
67*0Sstevel@tonic-gate  * It is supposed that string points to an array with
68*0Sstevel@tonic-gate  * a minimum length of len. len must be strlen + 1.
69*0Sstevel@tonic-gate  * Checks for printable characters are already done in library.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static int
72*0Sstevel@tonic-gate sysevent_isstrend(char *string, size_t len)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	/* Return 0 if string has length of zero */
75*0Sstevel@tonic-gate 	if (len > 0) {
76*0Sstevel@tonic-gate 		return (string[len - 1] == '\0' ? 1 : 0);
77*0Sstevel@tonic-gate 	} else {
78*0Sstevel@tonic-gate 		return (0);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * Following sysevent_minor_* routines map
84*0Sstevel@tonic-gate  * a binding handle (evchan_t *) to a minor number
85*0Sstevel@tonic-gate  * Has to be called w/ locks held.
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate static ulong_t *
88*0Sstevel@tonic-gate sysevent_minor_alloc(void)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	ulong_t *bhst = sysevent_minor_bitmap;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* Increase bitmap by one BT_NBIPUL */
93*0Sstevel@tonic-gate 	if (sysevent_minor_bits + BT_NBIPUL > SYSEVENT_MINOR_MAX) {
94*0Sstevel@tonic-gate 		return ((ulong_t *)NULL);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	sysevent_minor_bitmap = kmem_zalloc(
97*0Sstevel@tonic-gate 	    BT_SIZEOFMAP(sysevent_minor_bits + BT_NBIPUL), KM_SLEEP);
98*0Sstevel@tonic-gate 	bcopy(bhst, sysevent_minor_bitmap, BT_SIZEOFMAP(sysevent_minor_bits));
99*0Sstevel@tonic-gate 	if (bhst != &sysevent_bitmap_initial)
100*0Sstevel@tonic-gate 		kmem_free(bhst, BT_SIZEOFMAP(sysevent_minor_bits));
101*0Sstevel@tonic-gate 	sysevent_minor_bits += BT_NBIPUL;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	return (sysevent_minor_bitmap);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static void
107*0Sstevel@tonic-gate sysevent_minor_free(ulong_t *bitmap)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	if (bitmap != &sysevent_bitmap_initial)
110*0Sstevel@tonic-gate 		kmem_free(bitmap, BT_SIZEOFMAP(sysevent_minor_bits));
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static index_t
114*0Sstevel@tonic-gate sysevent_minor_get(void)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	index_t idx;
117*0Sstevel@tonic-gate 	ulong_t *bhst;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* Search for an available index */
120*0Sstevel@tonic-gate 	mutex_enter(&sysevent_minor_mutex);
121*0Sstevel@tonic-gate 	if ((idx = bt_availbit(sysevent_minor_bitmap,
122*0Sstevel@tonic-gate 	    sysevent_minor_bits)) == -1) {
123*0Sstevel@tonic-gate 		/* All busy - allocate additional binding handle bitmap space */
124*0Sstevel@tonic-gate 		if ((bhst = sysevent_minor_alloc()) == NULL) {
125*0Sstevel@tonic-gate 			/* Reached our maximum of id's == SHRT_MAX */
126*0Sstevel@tonic-gate 			mutex_exit(&sysevent_minor_mutex);
127*0Sstevel@tonic-gate 			return (0);
128*0Sstevel@tonic-gate 		} else {
129*0Sstevel@tonic-gate 			sysevent_minor_bitmap = bhst;
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		idx = bt_availbit(sysevent_minor_bitmap, sysevent_minor_bits);
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	BT_SET(sysevent_minor_bitmap, idx);
134*0Sstevel@tonic-gate 	mutex_exit(&sysevent_minor_mutex);
135*0Sstevel@tonic-gate 	return (idx);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static void
139*0Sstevel@tonic-gate sysevent_minor_rele(index_t idx)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	mutex_enter(&sysevent_minor_mutex);
142*0Sstevel@tonic-gate 	ASSERT(BT_TEST(sysevent_minor_bitmap, idx) == 1);
143*0Sstevel@tonic-gate 	BT_CLEAR(sysevent_minor_bitmap, idx);
144*0Sstevel@tonic-gate 	mutex_exit(&sysevent_minor_mutex);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate static void
148*0Sstevel@tonic-gate sysevent_minor_init(void)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	mutex_init(&sysevent_minor_mutex, NULL, MUTEX_DEFAULT, NULL);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /* ARGSUSED */
154*0Sstevel@tonic-gate static int
155*0Sstevel@tonic-gate sysevent_publish(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	int km_flags;
158*0Sstevel@tonic-gate 	sev_publish_args_t uargs;
159*0Sstevel@tonic-gate 	sysevent_impl_t *ev;
160*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
163*0Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
164*0Sstevel@tonic-gate 		return (ENXIO);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_publish_args_t)) != 0)
167*0Sstevel@tonic-gate 		return (EFAULT);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/*
170*0Sstevel@tonic-gate 	 * This limits the size of an event
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 	if (uargs.ev.len > MAX_EV_SIZE_LEN)
173*0Sstevel@tonic-gate 		return (EOVERFLOW);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/*
176*0Sstevel@tonic-gate 	 * Check for valid uargs.flags
177*0Sstevel@tonic-gate 	 */
178*0Sstevel@tonic-gate 	if (uargs.flags & ~(EVCH_NOSLEEP | EVCH_SLEEP | EVCH_QWAIT))
179*0Sstevel@tonic-gate 		return (EINVAL);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * Check that at least one of EVCH_NOSLEEP or EVCH_SLEEP is
183*0Sstevel@tonic-gate 	 * specified
184*0Sstevel@tonic-gate 	 */
185*0Sstevel@tonic-gate 	km_flags = uargs.flags & (EVCH_NOSLEEP | EVCH_SLEEP);
186*0Sstevel@tonic-gate 	if (km_flags != EVCH_NOSLEEP && km_flags != EVCH_SLEEP)
187*0Sstevel@tonic-gate 		return (EINVAL);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	ev = evch_usrallocev(uargs.ev.len, uargs.flags);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.ev.name, ev, uargs.ev.len) != 0) {
192*0Sstevel@tonic-gate 		evch_usrfreeev(ev);
193*0Sstevel@tonic-gate 		return (EFAULT);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	return (evch_usrpostevent(ctl->chp, ev, uargs.flags));
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/* Event will be freed internally */
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * sysevent_chan_open - used to open a channel in the GPEC channel layer
203*0Sstevel@tonic-gate  */
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /* ARGSUSED */
206*0Sstevel@tonic-gate static int
207*0Sstevel@tonic-gate sysevent_chan_open(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	sev_bind_args_t uargs;
210*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
211*0Sstevel@tonic-gate 	char *chan_name;
212*0Sstevel@tonic-gate 	int ec;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
215*0Sstevel@tonic-gate 	if (ctl == NULL) {
216*0Sstevel@tonic-gate 		return (ENXIO);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_bind_args_t)) != 0)
220*0Sstevel@tonic-gate 		return (EFAULT);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if (uargs.chan_name.len > MAX_CHNAME_LEN)
223*0Sstevel@tonic-gate 		return (EINVAL);
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	chan_name = kmem_alloc(uargs.chan_name.len, KM_SLEEP);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.chan_name.name, chan_name,
228*0Sstevel@tonic-gate 	    uargs.chan_name.len) != 0) {
229*0Sstevel@tonic-gate 		kmem_free(chan_name, uargs.chan_name.len);
230*0Sstevel@tonic-gate 		return (EFAULT);
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (!sysevent_isstrend(chan_name, uargs.chan_name.len)) {
234*0Sstevel@tonic-gate 		kmem_free(chan_name, uargs.chan_name.len);
235*0Sstevel@tonic-gate 		return (EINVAL);
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	/*
239*0Sstevel@tonic-gate 	 * Check of uargs.flags and uargs.perms just to avoid DoS attacks.
240*0Sstevel@tonic-gate 	 * libsysevent does this carefully
241*0Sstevel@tonic-gate 	 */
242*0Sstevel@tonic-gate 	ctl->chp = evch_usrchanopen((const char *)chan_name,
243*0Sstevel@tonic-gate 	    uargs.flags & EVCH_B_FLAGS, &ec);
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	kmem_free(chan_name, uargs.chan_name.len);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (ec != 0) {
248*0Sstevel@tonic-gate 		return (ec);
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	return (0);
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate /* ARGSUSED */
255*0Sstevel@tonic-gate static int
256*0Sstevel@tonic-gate sysevent_chan_control(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	sev_control_args_t uargs;
259*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
260*0Sstevel@tonic-gate 	int rc;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
263*0Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
264*0Sstevel@tonic-gate 		return (ENXIO);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_control_args_t)) != 0)
267*0Sstevel@tonic-gate 		return (EFAULT);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	switch (uargs.cmd) {
270*0Sstevel@tonic-gate 	case EVCH_GET_CHAN_LEN:
271*0Sstevel@tonic-gate 	case EVCH_GET_CHAN_LEN_MAX:
272*0Sstevel@tonic-gate 		rc = evch_usrcontrol_get(ctl->chp, uargs.cmd, &uargs.value);
273*0Sstevel@tonic-gate 		if (rc == 0) {
274*0Sstevel@tonic-gate 			if (copyout((void *)&uargs, arg,
275*0Sstevel@tonic-gate 			    sizeof (sev_control_args_t)) != 0) {
276*0Sstevel@tonic-gate 				rc = EFAULT;
277*0Sstevel@tonic-gate 			}
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 		break;
280*0Sstevel@tonic-gate 	case EVCH_SET_CHAN_LEN:
281*0Sstevel@tonic-gate 		rc = evch_usrcontrol_set(ctl->chp, uargs.cmd, uargs.value);
282*0Sstevel@tonic-gate 		break;
283*0Sstevel@tonic-gate 	default:
284*0Sstevel@tonic-gate 		rc = EINVAL;
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 	return (rc);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate /* ARGSUSED */
290*0Sstevel@tonic-gate static int
291*0Sstevel@tonic-gate sysevent_subscribe(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	sev_subscribe_args_t uargs;
294*0Sstevel@tonic-gate 	char *sid;
295*0Sstevel@tonic-gate 	char *class_info = NULL;
296*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
297*0Sstevel@tonic-gate 	int rc;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
300*0Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
301*0Sstevel@tonic-gate 		return (ENXIO);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_subscribe_args_t)) != 0)
304*0Sstevel@tonic-gate 		return (EFAULT);
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if (uargs.sid.len > MAX_SUBID_LEN ||
307*0Sstevel@tonic-gate 	    uargs.class_info.len > MAX_CLASS_LEN)
308*0Sstevel@tonic-gate 		return (EINVAL);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	sid = kmem_alloc(uargs.sid.len, KM_SLEEP);
311*0Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.sid.name,
312*0Sstevel@tonic-gate 	    sid, uargs.sid.len) != 0) {
313*0Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
314*0Sstevel@tonic-gate 		return (EFAULT);
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 	if (!sysevent_isstrend(sid, uargs.sid.len)) {
317*0Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
318*0Sstevel@tonic-gate 		return (EINVAL);
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	/* If class string empty then class EC_ALL is assumed */
322*0Sstevel@tonic-gate 	if (uargs.class_info.len != 0) {
323*0Sstevel@tonic-gate 		class_info = kmem_alloc(uargs.class_info.len, KM_SLEEP);
324*0Sstevel@tonic-gate 		if (copyin((void *)(uintptr_t)uargs.class_info.name, class_info,
325*0Sstevel@tonic-gate 		    uargs.class_info.len) != 0) {
326*0Sstevel@tonic-gate 			kmem_free(class_info, uargs.class_info.len);
327*0Sstevel@tonic-gate 			kmem_free(sid, uargs.sid.len);
328*0Sstevel@tonic-gate 			return (EFAULT);
329*0Sstevel@tonic-gate 		}
330*0Sstevel@tonic-gate 		if (!sysevent_isstrend(class_info, uargs.class_info.len)) {
331*0Sstevel@tonic-gate 			kmem_free(class_info, uargs.class_info.len);
332*0Sstevel@tonic-gate 			kmem_free(sid, uargs.sid.len);
333*0Sstevel@tonic-gate 			return (EINVAL);
334*0Sstevel@tonic-gate 		}
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/*
338*0Sstevel@tonic-gate 	 * Check of uargs.flags just to avoid DoS attacks
339*0Sstevel@tonic-gate 	 * libsysevent does this carefully.
340*0Sstevel@tonic-gate 	 */
341*0Sstevel@tonic-gate 	rc = evch_usrsubscribe(ctl->chp, sid, class_info,
342*0Sstevel@tonic-gate 	    (int)uargs.door_desc, uargs.flags);
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	kmem_free(class_info, uargs.class_info.len);
345*0Sstevel@tonic-gate 	kmem_free(sid, uargs.sid.len);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	return (rc);
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate /* ARGSUSED */
351*0Sstevel@tonic-gate static int
352*0Sstevel@tonic-gate sysevent_unsubscribe(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	sev_unsubscribe_args_t uargs;
355*0Sstevel@tonic-gate 	char *sid;
356*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_unsubscribe_args_t)) != 0)
359*0Sstevel@tonic-gate 		return (EFAULT);
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
362*0Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
363*0Sstevel@tonic-gate 		return (ENXIO);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	if (uargs.sid.len > MAX_SUBID_LEN)
366*0Sstevel@tonic-gate 		return (EINVAL);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/* Unsubscribe for all */
369*0Sstevel@tonic-gate 	if (uargs.sid.len == 0) {
370*0Sstevel@tonic-gate 		evch_usrunsubscribe(ctl->chp, NULL, 0);
371*0Sstevel@tonic-gate 		return (0);
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	sid = kmem_alloc(uargs.sid.len, KM_SLEEP);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.sid.name,
377*0Sstevel@tonic-gate 	    sid, uargs.sid.len) != 0) {
378*0Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
379*0Sstevel@tonic-gate 		return (EFAULT);
380*0Sstevel@tonic-gate 	}
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	evch_usrunsubscribe(ctl->chp, sid, 0);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	kmem_free(sid, uargs.sid.len);
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	return (0);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate /* ARGSUSED */
390*0Sstevel@tonic-gate static int
391*0Sstevel@tonic-gate sysevent_channames(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	sev_chandata_args_t uargs;
394*0Sstevel@tonic-gate 	char *buf;
395*0Sstevel@tonic-gate 	int len;
396*0Sstevel@tonic-gate 	int rc = 0;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_chandata_args_t)) != 0)
399*0Sstevel@tonic-gate 		return (EFAULT);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (uargs.out_data.len == 0 || uargs.out_data.len > EVCH_MAX_DATA_SIZE)
402*0Sstevel@tonic-gate 		return (EINVAL);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	buf = kmem_alloc(uargs.out_data.len, KM_SLEEP);
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	if ((len = evch_usrgetchnames(buf, uargs.out_data.len)) == -1) {
407*0Sstevel@tonic-gate 		rc = EOVERFLOW;
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	if (rc == 0) {
411*0Sstevel@tonic-gate 		ASSERT(len <= uargs.out_data.len);
412*0Sstevel@tonic-gate 		if (copyout(buf,
413*0Sstevel@tonic-gate 		    (void *)(uintptr_t)uargs.out_data.name, len) != 0) {
414*0Sstevel@tonic-gate 			rc = EFAULT;
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	kmem_free(buf, uargs.out_data.len);
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	return (rc);
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate /* ARGSUSED */
424*0Sstevel@tonic-gate static int
425*0Sstevel@tonic-gate sysevent_chandata(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
426*0Sstevel@tonic-gate {
427*0Sstevel@tonic-gate 	sev_chandata_args_t uargs;
428*0Sstevel@tonic-gate 	char *channel;
429*0Sstevel@tonic-gate 	char *buf;
430*0Sstevel@tonic-gate 	int len;
431*0Sstevel@tonic-gate 	int rc = 0;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_chandata_args_t)) != 0)
434*0Sstevel@tonic-gate 		return (EFAULT);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (uargs.in_data.len > MAX_CHNAME_LEN ||
437*0Sstevel@tonic-gate 	    uargs.out_data.len > EVCH_MAX_DATA_SIZE)
438*0Sstevel@tonic-gate 		return (EINVAL);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	channel = kmem_alloc(uargs.in_data.len, KM_SLEEP);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.in_data.name, channel,
443*0Sstevel@tonic-gate 	    uargs.in_data.len) != 0) {
444*0Sstevel@tonic-gate 		kmem_free(channel, uargs.in_data.len);
445*0Sstevel@tonic-gate 		return (EFAULT);
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	if (!sysevent_isstrend(channel, uargs.in_data.len)) {
449*0Sstevel@tonic-gate 		kmem_free(channel, uargs.in_data.len);
450*0Sstevel@tonic-gate 		return (EINVAL);
451*0Sstevel@tonic-gate 	}
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	buf = kmem_alloc(uargs.out_data.len, KM_SLEEP);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	len = evch_usrgetchdata(channel, buf, uargs.out_data.len);
456*0Sstevel@tonic-gate 	if (len == 0) {
457*0Sstevel@tonic-gate 		rc = EOVERFLOW;
458*0Sstevel@tonic-gate 	} else if (len == -1) {
459*0Sstevel@tonic-gate 		rc = ENOENT;
460*0Sstevel@tonic-gate 	}
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	if (rc == 0) {
463*0Sstevel@tonic-gate 		ASSERT(len <= uargs.out_data.len);
464*0Sstevel@tonic-gate 		if (copyout(buf,
465*0Sstevel@tonic-gate 		    (void *)(uintptr_t)uargs.out_data.name, len) != 0) {
466*0Sstevel@tonic-gate 			rc = EFAULT;
467*0Sstevel@tonic-gate 		}
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	kmem_free(buf, uargs.out_data.len);
471*0Sstevel@tonic-gate 	kmem_free(channel, uargs.in_data.len);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	return (rc);
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate /*ARGSUSED*/
477*0Sstevel@tonic-gate static int
478*0Sstevel@tonic-gate sysevent_ioctl(dev_t dev, int cmd, intptr_t arg,
479*0Sstevel@tonic-gate     int flag, cred_t *cr, int *rvalp)
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate 	int rc;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	switch (cmd) {
484*0Sstevel@tonic-gate 	case SEV_PUBLISH:
485*0Sstevel@tonic-gate 		rc = sysevent_publish(dev, rvalp, (void *)arg, flag, cr);
486*0Sstevel@tonic-gate 		break;
487*0Sstevel@tonic-gate 	case SEV_CHAN_OPEN:
488*0Sstevel@tonic-gate 		rc = sysevent_chan_open(dev, rvalp, (void *)arg, flag, cr);
489*0Sstevel@tonic-gate 		break;
490*0Sstevel@tonic-gate 	case SEV_CHAN_CONTROL:
491*0Sstevel@tonic-gate 		rc = sysevent_chan_control(dev, rvalp, (void *)arg, flag, cr);
492*0Sstevel@tonic-gate 		break;
493*0Sstevel@tonic-gate 	case SEV_SUBSCRIBE:
494*0Sstevel@tonic-gate 		rc = sysevent_subscribe(dev, rvalp, (void *)arg, flag, cr);
495*0Sstevel@tonic-gate 		break;
496*0Sstevel@tonic-gate 	case SEV_UNSUBSCRIBE:
497*0Sstevel@tonic-gate 		rc = sysevent_unsubscribe(dev, rvalp, (void *)arg, flag, cr);
498*0Sstevel@tonic-gate 		break;
499*0Sstevel@tonic-gate 	case SEV_CHANNAMES:
500*0Sstevel@tonic-gate 		rc = sysevent_channames(dev, rvalp, (void *)arg, flag, cr);
501*0Sstevel@tonic-gate 		break;
502*0Sstevel@tonic-gate 	case SEV_CHANDATA:
503*0Sstevel@tonic-gate 		rc = sysevent_chandata(dev, rvalp, (void *)arg, flag, cr);
504*0Sstevel@tonic-gate 		break;
505*0Sstevel@tonic-gate 	default:
506*0Sstevel@tonic-gate 		rc = EINVAL;
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	return (rc);
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate /*ARGSUSED*/
513*0Sstevel@tonic-gate static int
514*0Sstevel@tonic-gate sysevent_open(dev_t *devp, int flag, int otyp, cred_t *cr)
515*0Sstevel@tonic-gate {
516*0Sstevel@tonic-gate 	int minor;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
519*0Sstevel@tonic-gate 		return (EINVAL);
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	if (getminor(*devp) != 0)
522*0Sstevel@tonic-gate 		return (ENXIO);
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 	minor = sysevent_minor_get();
525*0Sstevel@tonic-gate 	if (minor == 0)
526*0Sstevel@tonic-gate 		/* All minors are busy */
527*0Sstevel@tonic-gate 		return (EBUSY);
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(evchan_ctlp, minor)
530*0Sstevel@tonic-gate 	    != DDI_SUCCESS) {
531*0Sstevel@tonic-gate 		sysevent_minor_rele(minor);
532*0Sstevel@tonic-gate 		return (ENOMEM);
533*0Sstevel@tonic-gate 	}
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), minor);
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	return (0);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*ARGSUSED*/
541*0Sstevel@tonic-gate static int
542*0Sstevel@tonic-gate sysevent_close(dev_t dev, int flag, int otyp, cred_t *cr)
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	int minor = (int)getminor(dev);
545*0Sstevel@tonic-gate 	evchan_ctl_t *ctl;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
548*0Sstevel@tonic-gate 		return (EINVAL);
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, minor);
551*0Sstevel@tonic-gate 	if (ctl == NULL) {
552*0Sstevel@tonic-gate 		return (ENXIO);
553*0Sstevel@tonic-gate 	}
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	if (ctl->chp) {
556*0Sstevel@tonic-gate 		/* Release all non-persistant subscriptions */
557*0Sstevel@tonic-gate 		evch_usrunsubscribe(ctl->chp, NULL, EVCH_SUB_KEEP);
558*0Sstevel@tonic-gate 		evch_usrchanclose(ctl->chp);
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	ddi_soft_state_free(evchan_ctlp, minor);
562*0Sstevel@tonic-gate 	sysevent_minor_rele(minor);
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	return (0);
565*0Sstevel@tonic-gate }
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate /* ARGSUSED */
568*0Sstevel@tonic-gate static int
569*0Sstevel@tonic-gate sysevent_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
570*0Sstevel@tonic-gate     void *arg, void **result)
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate 	switch (infocmd) {
573*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
574*0Sstevel@tonic-gate 		*result = sysevent_devi;
575*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
576*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
577*0Sstevel@tonic-gate 		*result = 0;
578*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
579*0Sstevel@tonic-gate 	}
580*0Sstevel@tonic-gate 	return (DDI_FAILURE);
581*0Sstevel@tonic-gate }
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate /* ARGSUSED */
584*0Sstevel@tonic-gate static int
585*0Sstevel@tonic-gate sysevent_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
586*0Sstevel@tonic-gate {
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
589*0Sstevel@tonic-gate 		return (DDI_FAILURE);
590*0Sstevel@tonic-gate 	}
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "sysevent", S_IFCHR,
593*0Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
594*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
595*0Sstevel@tonic-gate 		return (DDI_FAILURE);
596*0Sstevel@tonic-gate 	}
597*0Sstevel@tonic-gate 	sysevent_devi = devi;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	sysevent_minor_init();
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
602*0Sstevel@tonic-gate }
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate static int
605*0Sstevel@tonic-gate sysevent_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate 	if (cmd != DDI_DETACH) {
608*0Sstevel@tonic-gate 		return (DDI_FAILURE);
609*0Sstevel@tonic-gate 	}
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	sysevent_minor_free(sysevent_minor_bitmap);
612*0Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
613*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
614*0Sstevel@tonic-gate }
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate static struct cb_ops sysevent_cb_ops = {
617*0Sstevel@tonic-gate 	sysevent_open,		/* open */
618*0Sstevel@tonic-gate 	sysevent_close,		/* close */
619*0Sstevel@tonic-gate 	nodev,			/* strategy */
620*0Sstevel@tonic-gate 	nodev,			/* print */
621*0Sstevel@tonic-gate 	nodev,			/* dump */
622*0Sstevel@tonic-gate 	nodev,			/* read */
623*0Sstevel@tonic-gate 	nodev,			/* write */
624*0Sstevel@tonic-gate 	sysevent_ioctl,		/* ioctl */
625*0Sstevel@tonic-gate 	nodev,			/* devmap */
626*0Sstevel@tonic-gate 	nodev,			/* mmap */
627*0Sstevel@tonic-gate 	nodev,			/* segmap */
628*0Sstevel@tonic-gate 	nochpoll,		/* poll */
629*0Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
630*0Sstevel@tonic-gate 	0,			/* streamtab  */
631*0Sstevel@tonic-gate 	D_NEW|D_MP,		/* flag */
632*0Sstevel@tonic-gate 	NULL,			/* aread */
633*0Sstevel@tonic-gate 	NULL			/* awrite */
634*0Sstevel@tonic-gate };
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate static struct dev_ops sysevent_ops = {
637*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
638*0Sstevel@tonic-gate 	0,			/* refcnt  */
639*0Sstevel@tonic-gate 	sysevent_info,		/* info */
640*0Sstevel@tonic-gate 	nulldev,		/* identify */
641*0Sstevel@tonic-gate 	nulldev,		/* probe */
642*0Sstevel@tonic-gate 	sysevent_attach,	/* attach */
643*0Sstevel@tonic-gate 	sysevent_detach,	/* detach */
644*0Sstevel@tonic-gate 	nodev,			/* reset */
645*0Sstevel@tonic-gate 	&sysevent_cb_ops,	/* driver operations */
646*0Sstevel@tonic-gate 	(struct bus_ops *)0,	/* no bus operations */
647*0Sstevel@tonic-gate 	nulldev			/* power */
648*0Sstevel@tonic-gate };
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate static struct modldrv modldrv = {
651*0Sstevel@tonic-gate 	&mod_driverops, "sysevent driver %I%", &sysevent_ops
652*0Sstevel@tonic-gate };
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
655*0Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
656*0Sstevel@tonic-gate };
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate int
659*0Sstevel@tonic-gate _init(void)
660*0Sstevel@tonic-gate {
661*0Sstevel@tonic-gate 	int s;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	s = ddi_soft_state_init(&evchan_ctlp, sizeof (evchan_ctl_t), 1);
664*0Sstevel@tonic-gate 	if (s != 0)
665*0Sstevel@tonic-gate 		return (s);
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 	if ((s = mod_install(&modlinkage)) != 0)
668*0Sstevel@tonic-gate 		ddi_soft_state_fini(&evchan_ctlp);
669*0Sstevel@tonic-gate 	return (s);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate int
673*0Sstevel@tonic-gate _fini(void)
674*0Sstevel@tonic-gate {
675*0Sstevel@tonic-gate 	int s;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	if ((s = mod_remove(&modlinkage)) != 0)
678*0Sstevel@tonic-gate 		return (s);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	ddi_soft_state_fini(&evchan_ctlp);
681*0Sstevel@tonic-gate 	return (s);
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate int
685*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
686*0Sstevel@tonic-gate {
687*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
688*0Sstevel@tonic-gate }
689