10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
511102SGavin.Maltby@Sun.COM * Common Development and Distribution License (the "License").
611102SGavin.Maltby@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12967Sgavin.maltby@oracle.com * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <stdio.h>
260Sstevel@tonic-gate #include <ctype.h>
270Sstevel@tonic-gate #include <fcntl.h>
280Sstevel@tonic-gate #include <errno.h>
290Sstevel@tonic-gate #include <door.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <stddef.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <strings.h>
3411102SGavin.Maltby@Sun.COM #include <pthread.h>
3511102SGavin.Maltby@Sun.COM #include <atomic.h>
3611102SGavin.Maltby@Sun.COM #include <signal.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/varargs.h>
390Sstevel@tonic-gate #include <sys/sysevent.h>
400Sstevel@tonic-gate #include <sys/sysevent_impl.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "libsysevent.h"
430Sstevel@tonic-gate #include "libsysevent_impl.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * The functions below deal with the General Purpose Event Handling framework
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * sysevent_evc_bind - create/bind application to named channel
490Sstevel@tonic-gate * sysevent_evc_unbind - unbind from previously bound/created channel
500Sstevel@tonic-gate * sysevent_evc_subscribe - subscribe to existing event channel
510Sstevel@tonic-gate * sysevent_evc_unsubscribe - unsubscribe from existing event channel
520Sstevel@tonic-gate * sysevent_evc_publish - generate a system event via an event channel
530Sstevel@tonic-gate * sysevent_evc_control - various channel based control operation
540Sstevel@tonic-gate */
550Sstevel@tonic-gate
5611102SGavin.Maltby@Sun.COM static void kill_door_servers(evchan_subscr_t *);
5711102SGavin.Maltby@Sun.COM
580Sstevel@tonic-gate #define misaligned(p) ((uintptr_t)(p) & 3) /* 4-byte alignment required */
590Sstevel@tonic-gate
6011102SGavin.Maltby@Sun.COM static pthread_key_t nrkey = PTHREAD_ONCE_KEY_NP;
6111102SGavin.Maltby@Sun.COM
6211102SGavin.Maltby@Sun.COM /*
6311102SGavin.Maltby@Sun.COM * If the current thread is a door server thread servicing a door created
6411102SGavin.Maltby@Sun.COM * for us in sysevent_evc_xsubscribe, then an attempt to unsubscribe from
6511102SGavin.Maltby@Sun.COM * within door invocation context on the same channel will deadlock in the
6611102SGavin.Maltby@Sun.COM * kernel waiting for our own invocation to complete. Such calls are
6711102SGavin.Maltby@Sun.COM * forbidden, and we abort if they are encountered (better than hanging
6811102SGavin.Maltby@Sun.COM * unkillably).
6911102SGavin.Maltby@Sun.COM *
7011102SGavin.Maltby@Sun.COM * We'd like to offer this detection to subscriptions established with
7111102SGavin.Maltby@Sun.COM * sysevent_evc_subscribe, but we don't have control over the door service
7211102SGavin.Maltby@Sun.COM * threads in that case. Perhaps the fix is to always use door_xcreate
7311102SGavin.Maltby@Sun.COM * even for sysevent_evc_subscribe?
7411102SGavin.Maltby@Sun.COM */
7511102SGavin.Maltby@Sun.COM static boolean_t
will_deadlock(evchan_t * scp)7611102SGavin.Maltby@Sun.COM will_deadlock(evchan_t *scp)
7711102SGavin.Maltby@Sun.COM {
7811102SGavin.Maltby@Sun.COM evchan_subscr_t *subp = pthread_getspecific(nrkey);
7911102SGavin.Maltby@Sun.COM evchan_impl_hdl_t *hdl = EVCHAN_IMPL_HNDL(scp);
8011102SGavin.Maltby@Sun.COM
8111102SGavin.Maltby@Sun.COM return (subp != NULL && subp->ev_subhead == hdl ? B_TRUE : B_FALSE);
8211102SGavin.Maltby@Sun.COM }
8311102SGavin.Maltby@Sun.COM
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * Check syntax of a channel name
860Sstevel@tonic-gate */
870Sstevel@tonic-gate static int
sysevent_is_chan_name(const char * str)880Sstevel@tonic-gate sysevent_is_chan_name(const char *str)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate for (; *str != '\0'; str++) {
910Sstevel@tonic-gate if (!EVCH_ISCHANCHAR(*str))
920Sstevel@tonic-gate return (0);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate return (1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * Check for printable characters
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate static int
strisprint(const char * s)1020Sstevel@tonic-gate strisprint(const char *s)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate for (; *s != '\0'; s++) {
1050Sstevel@tonic-gate if (*s < ' ' || *s > '~')
1060Sstevel@tonic-gate return (0);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate return (1);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * sysevent_evc_bind - Create/bind application to named channel
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate int
sysevent_evc_bind(const char * channel,evchan_t ** scpp,uint32_t flags)1160Sstevel@tonic-gate sysevent_evc_bind(const char *channel, evchan_t **scpp, uint32_t flags)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate int chanlen;
1190Sstevel@tonic-gate evchan_t *scp;
1200Sstevel@tonic-gate sev_bind_args_t uargs;
1210Sstevel@tonic-gate int ec;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (scpp == NULL || misaligned(scpp)) {
1240Sstevel@tonic-gate return (errno = EINVAL);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* Provide useful value in error case */
1280Sstevel@tonic-gate *scpp = NULL;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (channel == NULL ||
1310Sstevel@tonic-gate (chanlen = strlen(channel) + 1) > MAX_CHNAME_LEN) {
1320Sstevel@tonic-gate return (errno = EINVAL);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /* Check channel syntax */
1360Sstevel@tonic-gate if (!sysevent_is_chan_name(channel)) {
1370Sstevel@tonic-gate return (errno = EINVAL);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if (flags & ~EVCH_B_FLAGS) {
1410Sstevel@tonic-gate return (errno = EINVAL);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate scp = calloc(1, sizeof (evchan_impl_hdl_t));
1450Sstevel@tonic-gate if (scp == NULL) {
1460Sstevel@tonic-gate return (errno = ENOMEM);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * Enable sysevent driver. Fallback if the device link doesn't exist;
1510Sstevel@tonic-gate * this situation can arise if a channel is bound early in system
1520Sstevel@tonic-gate * startup, prior to devfsadm(1M) being invoked.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate EV_FD(scp) = open(DEVSYSEVENT, O_RDWR);
1550Sstevel@tonic-gate if (EV_FD(scp) == -1) {
1560Sstevel@tonic-gate if (errno != ENOENT) {
1570Sstevel@tonic-gate ec = errno == EACCES ? EPERM : errno;
1580Sstevel@tonic-gate free(scp);
1590Sstevel@tonic-gate return (errno = ec);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate EV_FD(scp) = open(DEVICESYSEVENT, O_RDWR);
1630Sstevel@tonic-gate if (EV_FD(scp) == -1) {
1640Sstevel@tonic-gate ec = errno == EACCES ? EPERM : errno;
1650Sstevel@tonic-gate free(scp);
1660Sstevel@tonic-gate return (errno = ec);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Force to close the fd's when process is doing exec.
1720Sstevel@tonic-gate * The driver will then release stale binding handles.
1730Sstevel@tonic-gate * The driver will release also the associated subscriptions
1740Sstevel@tonic-gate * if EVCH_SUB_KEEP flag was not set.
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate (void) fcntl(EV_FD(scp), F_SETFD, FD_CLOEXEC);
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate uargs.chan_name.name = (uintptr_t)channel;
1790Sstevel@tonic-gate uargs.chan_name.len = chanlen;
1800Sstevel@tonic-gate uargs.flags = flags;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (ioctl(EV_FD(scp), SEV_CHAN_OPEN, &uargs) != 0) {
1830Sstevel@tonic-gate ec = errno;
1840Sstevel@tonic-gate (void) close(EV_FD(scp));
1850Sstevel@tonic-gate free(scp);
1860Sstevel@tonic-gate return (errno = ec);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /* Needed to detect a fork() */
1900Sstevel@tonic-gate EV_PID(scp) = getpid();
1910Sstevel@tonic-gate (void) mutex_init(EV_LOCK(scp), USYNC_THREAD, NULL);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate *scpp = scp;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate return (0);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * sysevent_evc_unbind - Unbind from previously bound/created channel
2000Sstevel@tonic-gate */
20111102SGavin.Maltby@Sun.COM int
sysevent_evc_unbind(evchan_t * scp)2020Sstevel@tonic-gate sysevent_evc_unbind(evchan_t *scp)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate sev_unsubscribe_args_t uargs;
20511102SGavin.Maltby@Sun.COM evchan_subscr_t *subp;
20611102SGavin.Maltby@Sun.COM int errcp;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (scp == NULL || misaligned(scp))
20911102SGavin.Maltby@Sun.COM return (errno = EINVAL);
21011102SGavin.Maltby@Sun.COM
21111102SGavin.Maltby@Sun.COM if (will_deadlock(scp))
21211102SGavin.Maltby@Sun.COM return (errno = EDEADLK);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate (void) mutex_lock(EV_LOCK(scp));
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * Unsubscribe, if we are in the process which did the bind.
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate if (EV_PID(scp) == getpid()) {
2200Sstevel@tonic-gate uargs.sid.name = NULL;
2210Sstevel@tonic-gate uargs.sid.len = 0;
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * The unsubscribe ioctl will block until all door upcalls have
2240Sstevel@tonic-gate * drained.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate if (ioctl(EV_FD(scp), SEV_UNSUBSCRIBE, (intptr_t)&uargs) != 0) {
22711102SGavin.Maltby@Sun.COM errcp = errno;
2280Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
22911102SGavin.Maltby@Sun.COM return (errno = errcp);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
23311102SGavin.Maltby@Sun.COM while ((subp = EV_SUB_NEXT(scp)) != NULL) {
23411102SGavin.Maltby@Sun.COM EV_SUB_NEXT(scp) = subp->evsub_next;
23511102SGavin.Maltby@Sun.COM
23611102SGavin.Maltby@Sun.COM /* If door_xcreate was applied we can clean up */
23711102SGavin.Maltby@Sun.COM if (subp->evsub_attr)
23811102SGavin.Maltby@Sun.COM kill_door_servers(subp);
23911102SGavin.Maltby@Sun.COM
24011102SGavin.Maltby@Sun.COM if (door_revoke(subp->evsub_door_desc) != 0 && errno == EPERM)
24111102SGavin.Maltby@Sun.COM (void) close(subp->evsub_door_desc);
24211102SGavin.Maltby@Sun.COM
24311102SGavin.Maltby@Sun.COM free(subp->evsub_sid);
24411102SGavin.Maltby@Sun.COM free(subp);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * The close of the driver will do the unsubscribe if a) it is the last
2510Sstevel@tonic-gate * close and b) we are in a child which inherited subscriptions.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate (void) close(EV_FD(scp));
2540Sstevel@tonic-gate (void) mutex_destroy(EV_LOCK(scp));
2550Sstevel@tonic-gate free(scp);
25611102SGavin.Maltby@Sun.COM
25711102SGavin.Maltby@Sun.COM return (0);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate * sysevent_evc_publish - Generate a system event via an event channel
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate int
sysevent_evc_publish(evchan_t * scp,const char * class,const char * subclass,const char * vendor,const char * pub_name,nvlist_t * attr_list,uint32_t flags)2640Sstevel@tonic-gate sysevent_evc_publish(evchan_t *scp, const char *class,
2650Sstevel@tonic-gate const char *subclass, const char *vendor,
2660Sstevel@tonic-gate const char *pub_name, nvlist_t *attr_list,
2670Sstevel@tonic-gate uint32_t flags)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate sysevent_t *ev;
2700Sstevel@tonic-gate sev_publish_args_t uargs;
2710Sstevel@tonic-gate int rc;
2720Sstevel@tonic-gate int ec;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (scp == NULL || misaligned(scp)) {
2750Sstevel@tonic-gate return (errno = EINVAL);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /* No inheritance of binding handles via fork() */
2790Sstevel@tonic-gate if (EV_PID(scp) != getpid()) {
2800Sstevel@tonic-gate return (errno = EINVAL);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate ev = sysevent_alloc_event((char *)class, (char *)subclass,
2840Sstevel@tonic-gate (char *)vendor, (char *)pub_name, attr_list);
2850Sstevel@tonic-gate if (ev == NULL) {
2860Sstevel@tonic-gate return (errno);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate uargs.ev.name = (uintptr_t)ev;
2900Sstevel@tonic-gate uargs.ev.len = SE_SIZE(ev);
2910Sstevel@tonic-gate uargs.flags = flags;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate (void) mutex_lock(EV_LOCK(scp));
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate rc = ioctl(EV_FD(scp), SEV_PUBLISH, (intptr_t)&uargs);
2960Sstevel@tonic-gate ec = errno;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate sysevent_free(ev);
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate if (rc != 0) {
3030Sstevel@tonic-gate return (ec);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate return (0);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * Generic callback which catches events from the kernel and calls
3100Sstevel@tonic-gate * subscribers call back routine.
3110Sstevel@tonic-gate *
3120Sstevel@tonic-gate * Kernel guarantees that door_upcalls are disabled when unsubscription
3130Sstevel@tonic-gate * was issued that's why cookie points always to a valid evchan_subscr_t *.
3140Sstevel@tonic-gate *
3150Sstevel@tonic-gate * Furthermore it's not necessary to lock subp because the sysevent
3160Sstevel@tonic-gate * framework guarantees no unsubscription until door_return.
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate /*ARGSUSED3*/
3190Sstevel@tonic-gate static void
door_upcall(void * cookie,char * args,size_t alen,door_desc_t * ddp,uint_t ndid)3200Sstevel@tonic-gate door_upcall(void *cookie, char *args, size_t alen,
3210Sstevel@tonic-gate door_desc_t *ddp, uint_t ndid)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
3240Sstevel@tonic-gate int rval = 0;
3250Sstevel@tonic-gate
32611102SGavin.Maltby@Sun.COM /*
32711102SGavin.Maltby@Sun.COM * If we've been invoked simply to kill the thread then
32811102SGavin.Maltby@Sun.COM * exit now.
32911102SGavin.Maltby@Sun.COM */
33011102SGavin.Maltby@Sun.COM if (subp->evsub_state == EVCHAN_SUB_STATE_CLOSING)
33111102SGavin.Maltby@Sun.COM pthread_exit(NULL);
33211102SGavin.Maltby@Sun.COM
3330Sstevel@tonic-gate if (args == NULL || alen <= (size_t)0) {
3340Sstevel@tonic-gate /* Skip callback execution */
3350Sstevel@tonic-gate rval = EINVAL;
3360Sstevel@tonic-gate } else {
3370Sstevel@tonic-gate rval = subp->evsub_func((sysevent_t *)(void *)args,
3380Sstevel@tonic-gate subp->evsub_cookie);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate * Fill in return values for door_return
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate alen = sizeof (rval);
3450Sstevel@tonic-gate bcopy(&rval, args, alen);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate (void) door_return(args, alen, NULL, 0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
35011102SGavin.Maltby@Sun.COM static pthread_once_t xsub_thrattr_once = PTHREAD_ONCE_INIT;
35111102SGavin.Maltby@Sun.COM static pthread_attr_t xsub_thrattr;
35211102SGavin.Maltby@Sun.COM
35311102SGavin.Maltby@Sun.COM static void
xsub_thrattr_init(void)35411102SGavin.Maltby@Sun.COM xsub_thrattr_init(void)
35511102SGavin.Maltby@Sun.COM {
35611102SGavin.Maltby@Sun.COM (void) pthread_attr_init(&xsub_thrattr);
35711102SGavin.Maltby@Sun.COM (void) pthread_attr_setdetachstate(&xsub_thrattr,
35811102SGavin.Maltby@Sun.COM PTHREAD_CREATE_DETACHED);
35911102SGavin.Maltby@Sun.COM (void) pthread_attr_setscope(&xsub_thrattr, PTHREAD_SCOPE_SYSTEM);
36011102SGavin.Maltby@Sun.COM }
36111102SGavin.Maltby@Sun.COM
3620Sstevel@tonic-gate /*
36311102SGavin.Maltby@Sun.COM * Our door server create function is only called during initial
36411102SGavin.Maltby@Sun.COM * door_xcreate since we specify DOOR_NO_DEPLETION_CB.
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate int
xsub_door_server_create(door_info_t * dip,void * (* startf)(void *),void * startfarg,void * cookie)36711102SGavin.Maltby@Sun.COM xsub_door_server_create(door_info_t *dip, void *(*startf)(void *),
36811102SGavin.Maltby@Sun.COM void *startfarg, void *cookie)
36911102SGavin.Maltby@Sun.COM {
37011102SGavin.Maltby@Sun.COM evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
37111102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa = subp->evsub_attr;
37211102SGavin.Maltby@Sun.COM pthread_attr_t *thrattr;
37311102SGavin.Maltby@Sun.COM sigset_t oset;
37411102SGavin.Maltby@Sun.COM int err;
37511102SGavin.Maltby@Sun.COM
37611102SGavin.Maltby@Sun.COM if (subp->evsub_state == EVCHAN_SUB_STATE_CLOSING)
37711102SGavin.Maltby@Sun.COM return (0); /* shouldn't happen, but just in case */
37811102SGavin.Maltby@Sun.COM
37911102SGavin.Maltby@Sun.COM /*
38011102SGavin.Maltby@Sun.COM * If sysevent_evc_xsubscribe was called electing to use a
38111102SGavin.Maltby@Sun.COM * different door server create function then let it take it
38211102SGavin.Maltby@Sun.COM * from here.
38311102SGavin.Maltby@Sun.COM */
38411102SGavin.Maltby@Sun.COM if (xsa->xs_thrcreate) {
38511102SGavin.Maltby@Sun.COM return (xsa->xs_thrcreate(dip, startf, startfarg,
38611102SGavin.Maltby@Sun.COM xsa->xs_thrcreate_cookie));
38711102SGavin.Maltby@Sun.COM }
38811102SGavin.Maltby@Sun.COM
38911102SGavin.Maltby@Sun.COM if (xsa->xs_thrattr == NULL) {
39011102SGavin.Maltby@Sun.COM (void) pthread_once(&xsub_thrattr_once, xsub_thrattr_init);
39111102SGavin.Maltby@Sun.COM thrattr = &xsub_thrattr;
39211102SGavin.Maltby@Sun.COM } else {
39311102SGavin.Maltby@Sun.COM thrattr = xsa->xs_thrattr;
39411102SGavin.Maltby@Sun.COM }
39511102SGavin.Maltby@Sun.COM
39611102SGavin.Maltby@Sun.COM (void) pthread_sigmask(SIG_SETMASK, &xsa->xs_sigmask, &oset);
39711102SGavin.Maltby@Sun.COM err = pthread_create(NULL, thrattr, startf, startfarg);
39811102SGavin.Maltby@Sun.COM (void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
39911102SGavin.Maltby@Sun.COM
40011102SGavin.Maltby@Sun.COM return (err == 0 ? 1 : -1);
40111102SGavin.Maltby@Sun.COM }
40211102SGavin.Maltby@Sun.COM
40311102SGavin.Maltby@Sun.COM void
xsub_door_server_setup(void * cookie)40411102SGavin.Maltby@Sun.COM xsub_door_server_setup(void *cookie)
40511102SGavin.Maltby@Sun.COM {
40611102SGavin.Maltby@Sun.COM evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
40711102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa = subp->evsub_attr;
40811102SGavin.Maltby@Sun.COM
40911102SGavin.Maltby@Sun.COM if (xsa->xs_thrsetup == NULL) {
41011102SGavin.Maltby@Sun.COM (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
41111102SGavin.Maltby@Sun.COM (void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
41211102SGavin.Maltby@Sun.COM }
41311102SGavin.Maltby@Sun.COM
41411102SGavin.Maltby@Sun.COM (void) pthread_setspecific(nrkey, (void *)subp);
41511102SGavin.Maltby@Sun.COM
41611102SGavin.Maltby@Sun.COM if (xsa->xs_thrsetup)
41711102SGavin.Maltby@Sun.COM xsa->xs_thrsetup(xsa->xs_thrsetup_cookie);
41811102SGavin.Maltby@Sun.COM }
41911102SGavin.Maltby@Sun.COM
42011102SGavin.Maltby@Sun.COM /*
42111102SGavin.Maltby@Sun.COM * Cause private door server threads to exit. We have already performed the
42211102SGavin.Maltby@Sun.COM * unsubscribe ioctl which stops new invocations and waits until all
42311102SGavin.Maltby@Sun.COM * existing invocations are complete. So all server threads should be
42411102SGavin.Maltby@Sun.COM * blocked in door_return. The door has not yet been revoked. We will
42511102SGavin.Maltby@Sun.COM * invoke repeatedly after setting the evsub_state to be noticed on
42611102SGavin.Maltby@Sun.COM * wakeup; each invocation will result in the death of one server thread.
42711102SGavin.Maltby@Sun.COM *
42811102SGavin.Maltby@Sun.COM * You'd think it would be easier to kill these threads, such as through
42911102SGavin.Maltby@Sun.COM * pthread_cancel. Unfortunately door_return is not a cancellation point,
43011102SGavin.Maltby@Sun.COM * and if you do cancel a thread blocked in door_return the EINTR check in
43111102SGavin.Maltby@Sun.COM * the door_return assembly logic causes us to loop with EINTR forever!
43211102SGavin.Maltby@Sun.COM */
43311102SGavin.Maltby@Sun.COM static void
kill_door_servers(evchan_subscr_t * subp)43411102SGavin.Maltby@Sun.COM kill_door_servers(evchan_subscr_t *subp)
43511102SGavin.Maltby@Sun.COM {
43611102SGavin.Maltby@Sun.COM door_arg_t da;
43711102SGavin.Maltby@Sun.COM
43811102SGavin.Maltby@Sun.COM bzero(&da, sizeof (da));
43911102SGavin.Maltby@Sun.COM subp->evsub_state = EVCHAN_SUB_STATE_CLOSING;
44011102SGavin.Maltby@Sun.COM membar_producer();
44111102SGavin.Maltby@Sun.COM
44211102SGavin.Maltby@Sun.COM (void) door_call(subp->evsub_door_desc, &da);
44311102SGavin.Maltby@Sun.COM }
44411102SGavin.Maltby@Sun.COM
44511102SGavin.Maltby@Sun.COM static int
sysevent_evc_subscribe_cmn(evchan_t * scp,const char * sid,const char * class,int (* event_handler)(sysevent_t * ev,void * cookie),void * cookie,uint32_t flags,struct sysevent_subattr_impl * xsa)44611102SGavin.Maltby@Sun.COM sysevent_evc_subscribe_cmn(evchan_t *scp, const char *sid, const char *class,
4470Sstevel@tonic-gate int (*event_handler)(sysevent_t *ev, void *cookie),
44811102SGavin.Maltby@Sun.COM void *cookie, uint32_t flags, struct sysevent_subattr_impl *xsa)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate evchan_subscr_t *subp;
4510Sstevel@tonic-gate int upcall_door;
4520Sstevel@tonic-gate sev_subscribe_args_t uargs;
4530Sstevel@tonic-gate uint32_t sid_len;
4540Sstevel@tonic-gate uint32_t class_len;
4550Sstevel@tonic-gate int ec;
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate if (scp == NULL || misaligned(scp) || sid == NULL || class == NULL) {
4580Sstevel@tonic-gate return (errno = EINVAL);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate /* No inheritance of binding handles via fork() */
4620Sstevel@tonic-gate if (EV_PID(scp) != getpid()) {
4630Sstevel@tonic-gate return (errno = EINVAL);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate if ((sid_len = strlen(sid) + 1) > MAX_SUBID_LEN || sid_len == 1 ||
4670Sstevel@tonic-gate (class_len = strlen(class) + 1) > MAX_CLASS_LEN) {
4680Sstevel@tonic-gate return (errno = EINVAL);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /* Check for printable characters */
4720Sstevel@tonic-gate if (!strisprint(sid)) {
4730Sstevel@tonic-gate return (errno = EINVAL);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate if (event_handler == NULL) {
4770Sstevel@tonic-gate return (errno = EINVAL);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
48011102SGavin.Maltby@Sun.COM if (pthread_key_create_once_np(&nrkey, NULL) != 0)
48111102SGavin.Maltby@Sun.COM return (errno); /* ENOMEM or EAGAIN */
48211102SGavin.Maltby@Sun.COM
4830Sstevel@tonic-gate /* Create subscriber data */
4840Sstevel@tonic-gate if ((subp = calloc(1, sizeof (evchan_subscr_t))) == NULL) {
4850Sstevel@tonic-gate return (errno);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate if ((subp->evsub_sid = strdup(sid)) == NULL) {
4890Sstevel@tonic-gate ec = errno;
4900Sstevel@tonic-gate free(subp);
4910Sstevel@tonic-gate return (ec);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate * EC_ALL string will not be copied to kernel - NULL is assumed
4960Sstevel@tonic-gate */
4970Sstevel@tonic-gate if (strcmp(class, EC_ALL) == 0) {
4980Sstevel@tonic-gate class = NULL;
4990Sstevel@tonic-gate class_len = 0;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate
50211102SGavin.Maltby@Sun.COM /*
50311102SGavin.Maltby@Sun.COM * Fill this in now for the xsub_door_server_setup dance
50411102SGavin.Maltby@Sun.COM */
50511102SGavin.Maltby@Sun.COM subp->ev_subhead = EVCHAN_IMPL_HNDL(scp);
50611102SGavin.Maltby@Sun.COM subp->evsub_state = EVCHAN_SUB_STATE_ACTIVE;
50711102SGavin.Maltby@Sun.COM
50811102SGavin.Maltby@Sun.COM if (xsa == NULL) {
50911102SGavin.Maltby@Sun.COM upcall_door = door_create(door_upcall, (void *)subp,
51011102SGavin.Maltby@Sun.COM DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
51111102SGavin.Maltby@Sun.COM } else {
51211102SGavin.Maltby@Sun.COM subp->evsub_attr = xsa;
51311102SGavin.Maltby@Sun.COM
51411102SGavin.Maltby@Sun.COM /*
51511102SGavin.Maltby@Sun.COM * Create a private door with exactly one thread to
51611102SGavin.Maltby@Sun.COM * service the callbacks (the GPEC kernel implementation
51711102SGavin.Maltby@Sun.COM * serializes deliveries for each subscriber id).
51811102SGavin.Maltby@Sun.COM */
51911102SGavin.Maltby@Sun.COM upcall_door = door_xcreate(door_upcall, (void *)subp,
52011102SGavin.Maltby@Sun.COM DOOR_REFUSE_DESC | DOOR_NO_CANCEL | DOOR_NO_DEPLETION_CB,
52111102SGavin.Maltby@Sun.COM xsub_door_server_create, xsub_door_server_setup,
52211102SGavin.Maltby@Sun.COM (void *)subp, 1);
52311102SGavin.Maltby@Sun.COM }
52411102SGavin.Maltby@Sun.COM
5250Sstevel@tonic-gate if (upcall_door == -1) {
5260Sstevel@tonic-gate ec = errno;
5270Sstevel@tonic-gate free(subp->evsub_sid);
5280Sstevel@tonic-gate free(subp);
5290Sstevel@tonic-gate return (ec);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /* Complete subscriber information */
5330Sstevel@tonic-gate subp->evsub_door_desc = upcall_door;
5340Sstevel@tonic-gate subp->evsub_func = event_handler;
5350Sstevel@tonic-gate subp->evsub_cookie = cookie;
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate (void) mutex_lock(EV_LOCK(scp));
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate uargs.sid.name = (uintptr_t)sid;
5400Sstevel@tonic-gate uargs.sid.len = sid_len;
5410Sstevel@tonic-gate uargs.class_info.name = (uintptr_t)class;
5420Sstevel@tonic-gate uargs.class_info.len = class_len;
5430Sstevel@tonic-gate uargs.door_desc = subp->evsub_door_desc;
5440Sstevel@tonic-gate uargs.flags = flags;
5450Sstevel@tonic-gate if (ioctl(EV_FD(scp), SEV_SUBSCRIBE, (intptr_t)&uargs) != 0) {
5460Sstevel@tonic-gate ec = errno;
5470Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
54811102SGavin.Maltby@Sun.COM if (xsa)
54911102SGavin.Maltby@Sun.COM kill_door_servers(subp);
5500Sstevel@tonic-gate (void) door_revoke(upcall_door);
5510Sstevel@tonic-gate free(subp->evsub_sid);
5520Sstevel@tonic-gate free(subp);
5530Sstevel@tonic-gate return (ec);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /* Attach to subscriber list */
5570Sstevel@tonic-gate subp->evsub_next = EV_SUB_NEXT(scp);
5580Sstevel@tonic-gate EV_SUB_NEXT(scp) = subp;
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate return (0);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
56611102SGavin.Maltby@Sun.COM * sysevent_evc_subscribe - subscribe to an existing event channel
56711102SGavin.Maltby@Sun.COM * using a non-private door (which will create as many server threads
56811102SGavin.Maltby@Sun.COM * as the apparent maximum concurrency requirements suggest).
56911102SGavin.Maltby@Sun.COM */
57011102SGavin.Maltby@Sun.COM int
sysevent_evc_subscribe(evchan_t * scp,const char * sid,const char * class,int (* event_handler)(sysevent_t * ev,void * cookie),void * cookie,uint32_t flags)57111102SGavin.Maltby@Sun.COM sysevent_evc_subscribe(evchan_t *scp, const char *sid, const char *class,
57211102SGavin.Maltby@Sun.COM int (*event_handler)(sysevent_t *ev, void *cookie),
57311102SGavin.Maltby@Sun.COM void *cookie, uint32_t flags)
57411102SGavin.Maltby@Sun.COM {
57511102SGavin.Maltby@Sun.COM return (sysevent_evc_subscribe_cmn(scp, sid, class, event_handler,
57611102SGavin.Maltby@Sun.COM cookie, flags, NULL));
57711102SGavin.Maltby@Sun.COM }
57811102SGavin.Maltby@Sun.COM
57911102SGavin.Maltby@Sun.COM static void
subattr_dfltinit(struct sysevent_subattr_impl * xsa)58011102SGavin.Maltby@Sun.COM subattr_dfltinit(struct sysevent_subattr_impl *xsa)
58111102SGavin.Maltby@Sun.COM {
58211102SGavin.Maltby@Sun.COM (void) sigfillset(&xsa->xs_sigmask);
58311102SGavin.Maltby@Sun.COM (void) sigdelset(&xsa->xs_sigmask, SIGABRT);
58411102SGavin.Maltby@Sun.COM }
58511102SGavin.Maltby@Sun.COM
58611102SGavin.Maltby@Sun.COM static struct sysevent_subattr_impl dfltsa;
58711102SGavin.Maltby@Sun.COM pthread_once_t dfltsa_inited = PTHREAD_ONCE_INIT;
58811102SGavin.Maltby@Sun.COM
58911102SGavin.Maltby@Sun.COM static void
init_dfltsa(void)59011102SGavin.Maltby@Sun.COM init_dfltsa(void)
59111102SGavin.Maltby@Sun.COM {
59211102SGavin.Maltby@Sun.COM subattr_dfltinit(&dfltsa);
59311102SGavin.Maltby@Sun.COM }
59411102SGavin.Maltby@Sun.COM
59511102SGavin.Maltby@Sun.COM /*
59611102SGavin.Maltby@Sun.COM * sysevent_evc_subscribe - subscribe to an existing event channel
59711102SGavin.Maltby@Sun.COM * using a private door with control over thread creation.
59811102SGavin.Maltby@Sun.COM */
59911102SGavin.Maltby@Sun.COM int
sysevent_evc_xsubscribe(evchan_t * scp,const char * sid,const char * class,int (* event_handler)(sysevent_t * ev,void * cookie),void * cookie,uint32_t flags,sysevent_subattr_t * attr)60011102SGavin.Maltby@Sun.COM sysevent_evc_xsubscribe(evchan_t *scp, const char *sid, const char *class,
60111102SGavin.Maltby@Sun.COM int (*event_handler)(sysevent_t *ev, void *cookie),
60211102SGavin.Maltby@Sun.COM void *cookie, uint32_t flags, sysevent_subattr_t *attr)
60311102SGavin.Maltby@Sun.COM {
60411102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa;
60511102SGavin.Maltby@Sun.COM
60611102SGavin.Maltby@Sun.COM if (attr != NULL) {
60711102SGavin.Maltby@Sun.COM xsa = (struct sysevent_subattr_impl *)attr;
60811102SGavin.Maltby@Sun.COM } else {
60911102SGavin.Maltby@Sun.COM xsa = &dfltsa;
61011102SGavin.Maltby@Sun.COM (void) pthread_once(&dfltsa_inited, init_dfltsa);
61111102SGavin.Maltby@Sun.COM }
61211102SGavin.Maltby@Sun.COM
61311102SGavin.Maltby@Sun.COM return (sysevent_evc_subscribe_cmn(scp, sid, class, event_handler,
61411102SGavin.Maltby@Sun.COM cookie, flags, xsa));
61511102SGavin.Maltby@Sun.COM }
61611102SGavin.Maltby@Sun.COM
61711102SGavin.Maltby@Sun.COM sysevent_subattr_t *
sysevent_subattr_alloc(void)61811102SGavin.Maltby@Sun.COM sysevent_subattr_alloc(void)
61911102SGavin.Maltby@Sun.COM {
62011102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa = calloc(1, sizeof (*xsa));
62111102SGavin.Maltby@Sun.COM
62211102SGavin.Maltby@Sun.COM if (xsa != NULL)
62311102SGavin.Maltby@Sun.COM subattr_dfltinit(xsa);
62411102SGavin.Maltby@Sun.COM
62511102SGavin.Maltby@Sun.COM return (xsa != NULL ? (sysevent_subattr_t *)xsa : NULL);
62611102SGavin.Maltby@Sun.COM }
62711102SGavin.Maltby@Sun.COM
62811102SGavin.Maltby@Sun.COM void
sysevent_subattr_free(sysevent_subattr_t * attr)62911102SGavin.Maltby@Sun.COM sysevent_subattr_free(sysevent_subattr_t *attr)
63011102SGavin.Maltby@Sun.COM {
63111102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa =
63211102SGavin.Maltby@Sun.COM (struct sysevent_subattr_impl *)attr;
63311102SGavin.Maltby@Sun.COM
63411102SGavin.Maltby@Sun.COM free(xsa);
63511102SGavin.Maltby@Sun.COM }
63611102SGavin.Maltby@Sun.COM
63711102SGavin.Maltby@Sun.COM void
sysevent_subattr_thrcreate(sysevent_subattr_t * attr,door_xcreate_server_func_t * thrcreate,void * cookie)63811102SGavin.Maltby@Sun.COM sysevent_subattr_thrcreate(sysevent_subattr_t *attr,
63911102SGavin.Maltby@Sun.COM door_xcreate_server_func_t *thrcreate, void *cookie)
64011102SGavin.Maltby@Sun.COM {
64111102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa =
64211102SGavin.Maltby@Sun.COM (struct sysevent_subattr_impl *)attr;
64311102SGavin.Maltby@Sun.COM
64411102SGavin.Maltby@Sun.COM xsa->xs_thrcreate = thrcreate;
64511102SGavin.Maltby@Sun.COM xsa->xs_thrcreate_cookie = cookie;
64611102SGavin.Maltby@Sun.COM }
64711102SGavin.Maltby@Sun.COM
64811102SGavin.Maltby@Sun.COM void
sysevent_subattr_thrsetup(sysevent_subattr_t * attr,door_xcreate_thrsetup_func_t * thrsetup,void * cookie)64911102SGavin.Maltby@Sun.COM sysevent_subattr_thrsetup(sysevent_subattr_t *attr,
65011102SGavin.Maltby@Sun.COM door_xcreate_thrsetup_func_t *thrsetup, void *cookie)
65111102SGavin.Maltby@Sun.COM {
65211102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa =
65311102SGavin.Maltby@Sun.COM (struct sysevent_subattr_impl *)attr;
65411102SGavin.Maltby@Sun.COM
65511102SGavin.Maltby@Sun.COM xsa->xs_thrsetup = thrsetup;
65611102SGavin.Maltby@Sun.COM xsa->xs_thrsetup_cookie = cookie;
65711102SGavin.Maltby@Sun.COM }
65811102SGavin.Maltby@Sun.COM
65911102SGavin.Maltby@Sun.COM void
sysevent_subattr_sigmask(sysevent_subattr_t * attr,sigset_t * set)66011102SGavin.Maltby@Sun.COM sysevent_subattr_sigmask(sysevent_subattr_t *attr, sigset_t *set)
66111102SGavin.Maltby@Sun.COM {
66211102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa =
66311102SGavin.Maltby@Sun.COM (struct sysevent_subattr_impl *)attr;
66411102SGavin.Maltby@Sun.COM
66511102SGavin.Maltby@Sun.COM if (set) {
66611102SGavin.Maltby@Sun.COM xsa->xs_sigmask = *set;
66711102SGavin.Maltby@Sun.COM } else {
66811102SGavin.Maltby@Sun.COM (void) sigfillset(&xsa->xs_sigmask);
66911102SGavin.Maltby@Sun.COM (void) sigdelset(&xsa->xs_sigmask, SIGABRT);
67011102SGavin.Maltby@Sun.COM }
67111102SGavin.Maltby@Sun.COM }
67211102SGavin.Maltby@Sun.COM
67311102SGavin.Maltby@Sun.COM void
sysevent_subattr_thrattr(sysevent_subattr_t * attr,pthread_attr_t * thrattr)67411102SGavin.Maltby@Sun.COM sysevent_subattr_thrattr(sysevent_subattr_t *attr, pthread_attr_t *thrattr)
67511102SGavin.Maltby@Sun.COM {
67611102SGavin.Maltby@Sun.COM struct sysevent_subattr_impl *xsa =
67711102SGavin.Maltby@Sun.COM (struct sysevent_subattr_impl *)attr;
67811102SGavin.Maltby@Sun.COM
67911102SGavin.Maltby@Sun.COM xsa->xs_thrattr = thrattr;
68011102SGavin.Maltby@Sun.COM }
68111102SGavin.Maltby@Sun.COM
68211102SGavin.Maltby@Sun.COM /*
6830Sstevel@tonic-gate * sysevent_evc_unsubscribe - Unsubscribe from an existing event channel
6840Sstevel@tonic-gate */
68511102SGavin.Maltby@Sun.COM int
sysevent_evc_unsubscribe(evchan_t * scp,const char * sid)6860Sstevel@tonic-gate sysevent_evc_unsubscribe(evchan_t *scp, const char *sid)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate int all_subscribers = 0;
6890Sstevel@tonic-gate sev_unsubscribe_args_t uargs;
69011102SGavin.Maltby@Sun.COM evchan_subscr_t *subp, *prevsubp, *tofree;
69111102SGavin.Maltby@Sun.COM int errcp;
6920Sstevel@tonic-gate int rc;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate if (scp == NULL || misaligned(scp))
69511102SGavin.Maltby@Sun.COM return (errno = EINVAL);
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate if (sid == NULL || strlen(sid) == 0 ||
6980Sstevel@tonic-gate (strlen(sid) >= MAX_SUBID_LEN))
69911102SGavin.Maltby@Sun.COM return (errno = EINVAL);
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate /* No inheritance of binding handles via fork() */
70211102SGavin.Maltby@Sun.COM if (EV_PID(scp) != getpid())
70311102SGavin.Maltby@Sun.COM return (errno = EINVAL);
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate if (strcmp(sid, EVCH_ALLSUB) == 0) {
7060Sstevel@tonic-gate all_subscribers++;
7070Sstevel@tonic-gate /* Indicates all subscriber id's for this channel */
7080Sstevel@tonic-gate uargs.sid.name = NULL;
7090Sstevel@tonic-gate uargs.sid.len = 0;
7100Sstevel@tonic-gate } else {
7110Sstevel@tonic-gate uargs.sid.name = (uintptr_t)sid;
7120Sstevel@tonic-gate uargs.sid.len = strlen(sid) + 1;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
71511102SGavin.Maltby@Sun.COM if (will_deadlock(scp))
71611102SGavin.Maltby@Sun.COM return (errno = EDEADLK);
71711102SGavin.Maltby@Sun.COM
7180Sstevel@tonic-gate (void) mutex_lock(EV_LOCK(scp));
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate * The unsubscribe ioctl will block until all door upcalls have drained.
7220Sstevel@tonic-gate */
7230Sstevel@tonic-gate rc = ioctl(EV_FD(scp), SEV_UNSUBSCRIBE, (intptr_t)&uargs);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate if (rc != 0) {
72611102SGavin.Maltby@Sun.COM errcp = errno;
7270Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
72811102SGavin.Maltby@Sun.COM return (errno = errcp); /* EFAULT, ENXIO, EINVAL possible */
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate
73211102SGavin.Maltby@Sun.COM /*
73311102SGavin.Maltby@Sun.COM * Search for the matching subscriber. If EVCH_ALLSUB was specified
73411102SGavin.Maltby@Sun.COM * then the ioctl above will have returned 0 even if there are
73511102SGavin.Maltby@Sun.COM * no subscriptions, so the initial EV_SUB_NEXT can be NULL.
73611102SGavin.Maltby@Sun.COM */
73711102SGavin.Maltby@Sun.COM prevsubp = NULL;
73811102SGavin.Maltby@Sun.COM subp = EV_SUB_NEXT(scp);
73911102SGavin.Maltby@Sun.COM while (subp != NULL) {
74011102SGavin.Maltby@Sun.COM if (all_subscribers || strcmp(subp->evsub_sid, sid) == 0) {
74111102SGavin.Maltby@Sun.COM if (prevsubp == NULL) {
74211102SGavin.Maltby@Sun.COM EV_SUB_NEXT(scp) = subp->evsub_next;
74311102SGavin.Maltby@Sun.COM } else {
74411102SGavin.Maltby@Sun.COM prevsubp->evsub_next = subp->evsub_next;
74511102SGavin.Maltby@Sun.COM }
7460Sstevel@tonic-gate
74711102SGavin.Maltby@Sun.COM tofree = subp;
74811102SGavin.Maltby@Sun.COM subp = subp->evsub_next;
74911102SGavin.Maltby@Sun.COM
75011102SGavin.Maltby@Sun.COM /* If door_xcreate was applied we can clean up */
75111102SGavin.Maltby@Sun.COM if (tofree->evsub_attr)
75211102SGavin.Maltby@Sun.COM kill_door_servers(tofree);
75311102SGavin.Maltby@Sun.COM
7540Sstevel@tonic-gate (void) door_revoke(tofree->evsub_door_desc);
7550Sstevel@tonic-gate free(tofree->evsub_sid);
7560Sstevel@tonic-gate free(tofree);
75711102SGavin.Maltby@Sun.COM
75811102SGavin.Maltby@Sun.COM /* Freed single subscriber already? */
75911102SGavin.Maltby@Sun.COM if (all_subscribers == 0)
7600Sstevel@tonic-gate break;
76111102SGavin.Maltby@Sun.COM } else {
76211102SGavin.Maltby@Sun.COM prevsubp = subp;
7630Sstevel@tonic-gate subp = subp->evsub_next;
76411102SGavin.Maltby@Sun.COM }
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
76811102SGavin.Maltby@Sun.COM
76911102SGavin.Maltby@Sun.COM return (0);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate * sysevent_evc_control - Various channel based control operation
7740Sstevel@tonic-gate */
7750Sstevel@tonic-gate int
sysevent_evc_control(evchan_t * scp,int cmd,...)7760Sstevel@tonic-gate sysevent_evc_control(evchan_t *scp, int cmd, /* arg */ ...)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate va_list ap;
7790Sstevel@tonic-gate uint32_t *chlenp;
7800Sstevel@tonic-gate sev_control_args_t uargs;
7810Sstevel@tonic-gate int rc = 0;
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate if (scp == NULL || misaligned(scp)) {
7840Sstevel@tonic-gate return (errno = EINVAL);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate /* No inheritance of binding handles via fork() */
7880Sstevel@tonic-gate if (EV_PID(scp) != getpid()) {
7890Sstevel@tonic-gate return (errno = EINVAL);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate va_start(ap, cmd);
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate uargs.cmd = cmd;
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate (void) mutex_lock(EV_LOCK(scp));
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate switch (cmd) {
7990Sstevel@tonic-gate case EVCH_GET_CHAN_LEN:
8000Sstevel@tonic-gate case EVCH_GET_CHAN_LEN_MAX:
8010Sstevel@tonic-gate chlenp = va_arg(ap, uint32_t *);
8020Sstevel@tonic-gate if (chlenp == NULL || misaligned(chlenp)) {
8030Sstevel@tonic-gate rc = EINVAL;
8040Sstevel@tonic-gate break;
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate rc = ioctl(EV_FD(scp), SEV_CHAN_CONTROL, (intptr_t)&uargs);
8070Sstevel@tonic-gate *chlenp = uargs.value;
8080Sstevel@tonic-gate break;
809*12967Sgavin.maltby@oracle.com
8100Sstevel@tonic-gate case EVCH_SET_CHAN_LEN:
8110Sstevel@tonic-gate /* Range change will be handled in framework */
8120Sstevel@tonic-gate uargs.value = va_arg(ap, uint32_t);
8130Sstevel@tonic-gate rc = ioctl(EV_FD(scp), SEV_CHAN_CONTROL, (intptr_t)&uargs);
8140Sstevel@tonic-gate break;
815*12967Sgavin.maltby@oracle.com
8160Sstevel@tonic-gate default:
8170Sstevel@tonic-gate rc = EINVAL;
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate (void) mutex_unlock(EV_LOCK(scp));
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate if (rc == -1) {
8230Sstevel@tonic-gate rc = errno;
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate va_end(ap);
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate return (errno = rc);
8290Sstevel@tonic-gate }
830*12967Sgavin.maltby@oracle.com
831*12967Sgavin.maltby@oracle.com int
sysevent_evc_setpropnvl(evchan_t * scp,nvlist_t * nvl)832*12967Sgavin.maltby@oracle.com sysevent_evc_setpropnvl(evchan_t *scp, nvlist_t *nvl)
833*12967Sgavin.maltby@oracle.com {
834*12967Sgavin.maltby@oracle.com sev_propnvl_args_t uargs;
835*12967Sgavin.maltby@oracle.com char *buf = NULL;
836*12967Sgavin.maltby@oracle.com size_t nvlsz = 0;
837*12967Sgavin.maltby@oracle.com int rc;
838*12967Sgavin.maltby@oracle.com
839*12967Sgavin.maltby@oracle.com if (scp == NULL || misaligned(scp))
840*12967Sgavin.maltby@oracle.com return (errno = EINVAL);
841*12967Sgavin.maltby@oracle.com
842*12967Sgavin.maltby@oracle.com if (nvl != NULL &&
843*12967Sgavin.maltby@oracle.com nvlist_pack(nvl, &buf, &nvlsz, NV_ENCODE_NATIVE, 0) != 0)
844*12967Sgavin.maltby@oracle.com return (errno);
845*12967Sgavin.maltby@oracle.com
846*12967Sgavin.maltby@oracle.com uargs.packednvl.name = (uint64_t)(uintptr_t)buf;
847*12967Sgavin.maltby@oracle.com uargs.packednvl.len = (uint32_t)nvlsz;
848*12967Sgavin.maltby@oracle.com
849*12967Sgavin.maltby@oracle.com rc = ioctl(EV_FD(scp), SEV_SETPROPNVL, (intptr_t)&uargs);
850*12967Sgavin.maltby@oracle.com
851*12967Sgavin.maltby@oracle.com if (buf)
852*12967Sgavin.maltby@oracle.com free(buf);
853*12967Sgavin.maltby@oracle.com
854*12967Sgavin.maltby@oracle.com return (rc);
855*12967Sgavin.maltby@oracle.com }
856*12967Sgavin.maltby@oracle.com
857*12967Sgavin.maltby@oracle.com int
sysevent_evc_getpropnvl(evchan_t * scp,nvlist_t ** nvlp)858*12967Sgavin.maltby@oracle.com sysevent_evc_getpropnvl(evchan_t *scp, nvlist_t **nvlp)
859*12967Sgavin.maltby@oracle.com {
860*12967Sgavin.maltby@oracle.com sev_propnvl_args_t uargs;
861*12967Sgavin.maltby@oracle.com char buf[1024], *bufp = buf; /* stack buffer */
862*12967Sgavin.maltby@oracle.com size_t sz = sizeof (buf);
863*12967Sgavin.maltby@oracle.com char *buf2 = NULL; /* allocated if stack buf too small */
864*12967Sgavin.maltby@oracle.com int64_t expgen = -1;
865*12967Sgavin.maltby@oracle.com int rc;
866*12967Sgavin.maltby@oracle.com
867*12967Sgavin.maltby@oracle.com if (scp == NULL || misaligned(scp) || nvlp == NULL)
868*12967Sgavin.maltby@oracle.com return (errno = EINVAL);
869*12967Sgavin.maltby@oracle.com
870*12967Sgavin.maltby@oracle.com *nvlp = NULL;
871*12967Sgavin.maltby@oracle.com
872*12967Sgavin.maltby@oracle.com again:
873*12967Sgavin.maltby@oracle.com uargs.packednvl.name = (uint64_t)(uintptr_t)bufp;
874*12967Sgavin.maltby@oracle.com uargs.packednvl.len = (uint32_t)sz;
875*12967Sgavin.maltby@oracle.com
876*12967Sgavin.maltby@oracle.com rc = ioctl(EV_FD(scp), SEV_GETPROPNVL, (intptr_t)&uargs);
877*12967Sgavin.maltby@oracle.com
878*12967Sgavin.maltby@oracle.com if (rc == E2BIG)
879*12967Sgavin.maltby@oracle.com return (errno = E2BIG); /* driver refuses to copyout */
880*12967Sgavin.maltby@oracle.com
881*12967Sgavin.maltby@oracle.com /*
882*12967Sgavin.maltby@oracle.com * If the packed nvlist is too big for the buffer size we offered
883*12967Sgavin.maltby@oracle.com * then the ioctl returns EOVERFLOW and indicates in the 'len'
884*12967Sgavin.maltby@oracle.com * the size required for the current property nvlist generation
885*12967Sgavin.maltby@oracle.com * (itself returned in the generation member).
886*12967Sgavin.maltby@oracle.com */
887*12967Sgavin.maltby@oracle.com if (rc == EOVERFLOW &&
888*12967Sgavin.maltby@oracle.com (buf2 == NULL || uargs.generation != expgen)) {
889*12967Sgavin.maltby@oracle.com if (buf2 != NULL)
890*12967Sgavin.maltby@oracle.com free(buf2);
891*12967Sgavin.maltby@oracle.com
892*12967Sgavin.maltby@oracle.com if ((sz = uargs.packednvl.len) > 1024 * 1024)
893*12967Sgavin.maltby@oracle.com return (E2BIG);
894*12967Sgavin.maltby@oracle.com
895*12967Sgavin.maltby@oracle.com bufp = buf2 = malloc(sz);
896*12967Sgavin.maltby@oracle.com
897*12967Sgavin.maltby@oracle.com if (buf2 == NULL)
898*12967Sgavin.maltby@oracle.com return (errno = ENOMEM);
899*12967Sgavin.maltby@oracle.com
900*12967Sgavin.maltby@oracle.com expgen = uargs.generation;
901*12967Sgavin.maltby@oracle.com goto again;
902*12967Sgavin.maltby@oracle.com }
903*12967Sgavin.maltby@oracle.com
904*12967Sgavin.maltby@oracle.com /*
905*12967Sgavin.maltby@oracle.com * The chan prop nvlist can be absent, in which case the ioctl
906*12967Sgavin.maltby@oracle.com * returns success and uargs.packednvl.len of 0; we have already
907*12967Sgavin.maltby@oracle.com * set *nvlp to NULL. Otherwise we must unpack the nvl.
908*12967Sgavin.maltby@oracle.com */
909*12967Sgavin.maltby@oracle.com if (rc == 0 && uargs.packednvl.len != 0 &&
910*12967Sgavin.maltby@oracle.com nvlist_unpack(bufp, uargs.packednvl.len, nvlp, 0) != 0)
911*12967Sgavin.maltby@oracle.com rc = EINVAL;
912*12967Sgavin.maltby@oracle.com
913*12967Sgavin.maltby@oracle.com if (buf2 != NULL)
914*12967Sgavin.maltby@oracle.com free(buf2);
915*12967Sgavin.maltby@oracle.com
916*12967Sgavin.maltby@oracle.com return (rc ? errno = rc : 0);
917*12967Sgavin.maltby@oracle.com }
918