xref: /titanic_52/usr/src/lib/librcm/librcm_event.c (revision 03859504f0d42ac2e32c1828dd62f1a80c4d2ab1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <door.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <stddef.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <synch.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <librcm_impl.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "librcm_event.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	dprint	if (debug) (void) printf
457c478bd9Sstevel@tonic-gate static int debug = 1;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	BUF_THRESHOLD	1024	/* larger bufs require a free */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Lookup seq_num. We can not use the standard nvlist_lookup functions since
517c478bd9Sstevel@tonic-gate  * the nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static int
547c478bd9Sstevel@tonic-gate lookup_seq_num(nvlist_t *nvl, uint64_t *seq_num)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
597c478bd9Sstevel@tonic-gate 		if (strcmp(nvpair_name(nvp), RCM_SEQ_NUM) == 0 &&
607c478bd9Sstevel@tonic-gate 		    nvpair_type(nvp) == DATA_TYPE_UINT64)
617c478bd9Sstevel@tonic-gate 			return (nvpair_value_uint64(nvp, seq_num));
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	return (ENOENT);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Get event service from a named door.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * This is similar to sysevent_post_event(), except that it deals with
717c478bd9Sstevel@tonic-gate  * the "return buffer problem":
727c478bd9Sstevel@tonic-gate  *	Typically, the door service places the return buffer on the stack
737c478bd9Sstevel@tonic-gate  *	when calling door_return(). This places an artificial limit on the
747c478bd9Sstevel@tonic-gate  *	size of the return buffer.
757c478bd9Sstevel@tonic-gate  * This problem is solved by placing large buffers on the heap, referenced
767c478bd9Sstevel@tonic-gate  * through door_info. When client detects a large buffer, it will make a
777c478bd9Sstevel@tonic-gate  * second door_call() to free the buffer. The client and the server agrees
787c478bd9Sstevel@tonic-gate  * on a size, which is defined as BUF_THRESHOLD.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * Returns -1 if message not delivered. With errno set to cause of error.
817c478bd9Sstevel@tonic-gate  * Returns 0 for success with the results returned in posting buffer.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate int
847c478bd9Sstevel@tonic-gate get_event_service(char *door_name, void *data, size_t datalen,
857c478bd9Sstevel@tonic-gate     void **result, size_t *rlen)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	int service_door, error;
887c478bd9Sstevel@tonic-gate 	door_arg_t door_arg;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * Open the service door
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	if ((service_door = open(door_name, O_RDONLY, 0)) == -1) {
947c478bd9Sstevel@tonic-gate 		errno = ESRCH;
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate retry1:
997c478bd9Sstevel@tonic-gate 	door_arg.rbuf = NULL;	/* doorfs will provide return buf */
1007c478bd9Sstevel@tonic-gate 	door_arg.rsize = 0;
1017c478bd9Sstevel@tonic-gate 	door_arg.data_ptr = data;
1027c478bd9Sstevel@tonic-gate 	door_arg.data_size = datalen;
1037c478bd9Sstevel@tonic-gate 	door_arg.desc_ptr = NULL;
1047c478bd9Sstevel@tonic-gate 	door_arg.desc_num = 0;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * Make door call
1087c478bd9Sstevel@tonic-gate 	 * EAGAIN is returned when the door server is temporarily
1097c478bd9Sstevel@tonic-gate 	 * out of threads to service the door call. So retry.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	if ((error = door_call(service_door, &door_arg)) == -1 &&
1127c478bd9Sstevel@tonic-gate 	    errno == EAGAIN) {
1137c478bd9Sstevel@tonic-gate 		(void) sleep(1);
1147c478bd9Sstevel@tonic-gate 		goto retry1;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((error == 0) && result) {
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		uint64_t seq_num = 0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		*result = NULL;
1227c478bd9Sstevel@tonic-gate 		*rlen = 0;
1237c478bd9Sstevel@tonic-gate 		if (door_arg.rbuf == NULL || door_arg.rsize == 0) {
1247c478bd9Sstevel@tonic-gate 			dprint("bad return from door call\n");
1257c478bd9Sstevel@tonic-gate 			(void) close(service_door);
1267c478bd9Sstevel@tonic-gate 			errno = EFAULT;
1277c478bd9Sstevel@tonic-gate 			return (-1);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		(void) nvlist_unpack(door_arg.rbuf, door_arg.rsize,
1317c478bd9Sstevel@tonic-gate 		    (nvlist_t **)result, 0);
1327c478bd9Sstevel@tonic-gate 		(void) munmap(door_arg.rbuf, door_arg.rsize);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		/*
1357c478bd9Sstevel@tonic-gate 		 * If requiring a buf free, make another door call.  There is
1367c478bd9Sstevel@tonic-gate 		 * no need to call munmap() after this door call, though.
1377c478bd9Sstevel@tonic-gate 		 */
1387c478bd9Sstevel@tonic-gate 		if (lookup_seq_num((nvlist_t *)*result, &seq_num) == 0) {
1397c478bd9Sstevel@tonic-gate retry2:
1407c478bd9Sstevel@tonic-gate 			door_arg.rbuf = NULL;
1417c478bd9Sstevel@tonic-gate 			door_arg.rsize = 0;
1427c478bd9Sstevel@tonic-gate 			door_arg.data_ptr = (char *)&seq_num;
1437c478bd9Sstevel@tonic-gate 			door_arg.data_size = sizeof (seq_num);
1447c478bd9Sstevel@tonic-gate 			door_arg.desc_ptr = NULL;
1457c478bd9Sstevel@tonic-gate 			door_arg.desc_num = 0;
1467c478bd9Sstevel@tonic-gate 			if (door_call(service_door, &door_arg) == -1) {
1477c478bd9Sstevel@tonic-gate 				if (errno == EAGAIN) {
1487c478bd9Sstevel@tonic-gate 					(void) sleep(1);
1497c478bd9Sstevel@tonic-gate 					goto retry2;
1507c478bd9Sstevel@tonic-gate 				}
1517c478bd9Sstevel@tonic-gate 				dprint("fail to free event buf in server\n");
1527c478bd9Sstevel@tonic-gate 			}
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	(void) close(service_door);
1577c478bd9Sstevel@tonic-gate 	return (error);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Export an event service door
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate struct door_result {
1647c478bd9Sstevel@tonic-gate 	struct door_result *next;
1657c478bd9Sstevel@tonic-gate 	void *data;
1667c478bd9Sstevel@tonic-gate 	uint64_t seq_num;
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate typedef struct door_cookie {
1707c478bd9Sstevel@tonic-gate 	uint64_t	seq_num;
1717c478bd9Sstevel@tonic-gate 	mutex_t		door_lock;
1727c478bd9Sstevel@tonic-gate 	void		(*door_func)(void **, size_t *);
1737c478bd9Sstevel@tonic-gate 	struct door_result *results;
1747c478bd9Sstevel@tonic-gate } door_cookie_t;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * add result to cookie, this is only invoked if result size > BUF_THRESHOLD
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static void
1807c478bd9Sstevel@tonic-gate add_door_result(door_cookie_t *cook, void *data, uint64_t seq_num)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	struct door_result *result;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * Need a better way to handle memory here
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	result = malloc(sizeof (*result));
1887c478bd9Sstevel@tonic-gate 	while (result == NULL) {
1897c478bd9Sstevel@tonic-gate 		(void) sleep(1);
1907c478bd9Sstevel@tonic-gate 		result = malloc(sizeof (*result));
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	result->next = NULL;
1937c478bd9Sstevel@tonic-gate 	result->data = data;
1947c478bd9Sstevel@tonic-gate 	result->seq_num = seq_num;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Attach current door result to the door cookie
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cook->door_lock);
2007c478bd9Sstevel@tonic-gate 	if (cook->results == NULL) {
2017c478bd9Sstevel@tonic-gate 		cook->results = result;
2027c478bd9Sstevel@tonic-gate 	} else {
2037c478bd9Sstevel@tonic-gate 		struct door_result *tmp = cook->results;
2047c478bd9Sstevel@tonic-gate 		while (tmp->next) {
2057c478bd9Sstevel@tonic-gate 			tmp = tmp->next;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		tmp->next = result;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cook->door_lock);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * free a previous door result as described by number.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate static void
2167c478bd9Sstevel@tonic-gate free_door_result(door_cookie_t *cook, uint64_t num)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	struct door_result *prev = NULL, *tmp;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cook->door_lock);
2217c478bd9Sstevel@tonic-gate 	tmp = cook->results;
2227c478bd9Sstevel@tonic-gate 	while (tmp && tmp->seq_num != num) {
2237c478bd9Sstevel@tonic-gate 		prev = tmp;
2247c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
2287c478bd9Sstevel@tonic-gate 		dprint("attempting to free nonexistent buf: %llu\n",
2297c478bd9Sstevel@tonic-gate 		    (unsigned long long)num);
2307c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cook->door_lock);
2317c478bd9Sstevel@tonic-gate 		return;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (prev) {
2357c478bd9Sstevel@tonic-gate 		prev->next = tmp->next;
2367c478bd9Sstevel@tonic-gate 	} else {
2377c478bd9Sstevel@tonic-gate 		cook->results = tmp->next;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cook->door_lock);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	free(tmp->data);
2427c478bd9Sstevel@tonic-gate 	free(tmp);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2467c478bd9Sstevel@tonic-gate static void
2477c478bd9Sstevel@tonic-gate door_service(void *cookie, char *args, size_t alen,
2487c478bd9Sstevel@tonic-gate     door_desc_t *ddp, uint_t ndid)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
2517c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
2527c478bd9Sstevel@tonic-gate 	char rbuf[BUF_THRESHOLD];
2537c478bd9Sstevel@tonic-gate 	door_cookie_t *cook = (door_cookie_t *)cookie;
2547c478bd9Sstevel@tonic-gate 	uint64_t seq_num = 0;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Special case for asking to free buffer
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	if (alen == sizeof (uint64_t)) {
2607c478bd9Sstevel@tonic-gate 		free_door_result(cookie, *(uint64_t *)(void *)args);
2617c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * door_func update args to point to return results.
2667c478bd9Sstevel@tonic-gate 	 * memory for results are dynamically allocated.
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	(*cook->door_func)((void **)&args, &alen);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * If no results, just return
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	if (args == NULL) {
2747c478bd9Sstevel@tonic-gate 		dprint("null results returned from door_func().\n");
2757c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* Determine the size of the packed nvlist */
2797c478bd9Sstevel@tonic-gate 	nvl = (nvlist_t *)(void *)args;
2807c478bd9Sstevel@tonic-gate 	args = NULL;
2817c478bd9Sstevel@tonic-gate 	alen = 0;
2827c478bd9Sstevel@tonic-gate 	if (errno = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE)) {
2837c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
2847c478bd9Sstevel@tonic-gate 		dprint("failure to sizeup door results: %s\n", strerror(errno));
2857c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * If the size of the packed nvlist would exceed the buffer threshold
2907c478bd9Sstevel@tonic-gate 	 * then get a sequence number and add it to the nvlist.
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 	if (nvl_size > BUF_THRESHOLD) {
2937c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&cook->door_lock);
2947c478bd9Sstevel@tonic-gate 		cook->seq_num++;
2957c478bd9Sstevel@tonic-gate 		seq_num = cook->seq_num;
2967c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cook->door_lock);
2977c478bd9Sstevel@tonic-gate 		(void) nvlist_add_uint64(nvl, RCM_SEQ_NUM, seq_num);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/* Refill the args with a packed version of the nvlist */
3017c478bd9Sstevel@tonic-gate 	if (errno = nvlist_pack(nvl, &args, &alen, NV_ENCODE_NATIVE, 0)) {
3027c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
3037c478bd9Sstevel@tonic-gate 		dprint("failure to pack door results: %s\n", strerror(errno));
3047c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Based on the size of the packed nvlist, either use the local buffer
3107c478bd9Sstevel@tonic-gate 	 * or add it to the results list.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if (alen <= BUF_THRESHOLD) {
3137c478bd9Sstevel@tonic-gate 		bcopy(args, rbuf, alen);
3147c478bd9Sstevel@tonic-gate 		(void) free(args);
3157c478bd9Sstevel@tonic-gate 		args = rbuf;
3167c478bd9Sstevel@tonic-gate 	} else {
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * for long data, append results to end of queue in cook
3197c478bd9Sstevel@tonic-gate 		 * and set ndid, ask client to do another door_call
3207c478bd9Sstevel@tonic-gate 		 * to free the buffer.
3217c478bd9Sstevel@tonic-gate 		 */
3227c478bd9Sstevel@tonic-gate 		add_door_result(cook, args, seq_num);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	(void) door_return(args, alen, NULL, 0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate int
3297c478bd9Sstevel@tonic-gate create_event_service(char *door_name,
3307c478bd9Sstevel@tonic-gate     void (*func)(void **data, size_t *datalen))
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	int service_door, fd;
3337c478bd9Sstevel@tonic-gate 	door_cookie_t *cookie;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* create an fs file */
3367c478bd9Sstevel@tonic-gate 	fd = open(door_name, O_EXCL|O_CREAT, S_IREAD|S_IWRITE);
3377c478bd9Sstevel@tonic-gate 	if ((fd == -1) && (errno != EEXIST)) {
3387c478bd9Sstevel@tonic-gate 		return (-1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	(void) close(fd);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* allocate space for door cookie */
3437c478bd9Sstevel@tonic-gate 	if ((cookie = calloc(1, sizeof (*cookie))) == NULL) {
3447c478bd9Sstevel@tonic-gate 		return (-1);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	cookie->door_func = func;
3487c478bd9Sstevel@tonic-gate 	if ((service_door = door_create(door_service, (void *)cookie,
3497c478bd9Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
3507c478bd9Sstevel@tonic-gate 		dprint("door create failed: %s\n", strerror(errno));
3517c478bd9Sstevel@tonic-gate 		free(cookie);
3527c478bd9Sstevel@tonic-gate 		return (-1);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate retry:
3567c478bd9Sstevel@tonic-gate 	(void) fdetach(door_name);
3577c478bd9Sstevel@tonic-gate 	if (fattach(service_door, door_name) != 0) {
3587c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
3597c478bd9Sstevel@tonic-gate 			/*
3607c478bd9Sstevel@tonic-gate 			 * EBUSY error may occur if anyone references the door
3617c478bd9Sstevel@tonic-gate 			 * file while we are fattach'ing. Since librcm, in the
3627c478bd9Sstevel@tonic-gate 			 * the process context of a DR initiator program, may
3637c478bd9Sstevel@tonic-gate 			 * reference the door file (via open/close/stat/
3647c478bd9Sstevel@tonic-gate 			 * door_call etc.) while we are still fattach'ing,
3657c478bd9Sstevel@tonic-gate 			 * retry on EBUSY.
3667c478bd9Sstevel@tonic-gate 			 */
3677c478bd9Sstevel@tonic-gate 			goto retry;
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		dprint("door attaching failed: %s\n", strerror(errno));
3707c478bd9Sstevel@tonic-gate 		free(cookie);
3717c478bd9Sstevel@tonic-gate 		(void) close(service_door);
3727c478bd9Sstevel@tonic-gate 		return (-1);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (service_door);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate int
3797c478bd9Sstevel@tonic-gate revoke_event_service(int fd)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	struct door_info info;
3827c478bd9Sstevel@tonic-gate 	door_cookie_t *cookie;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (door_info(fd, &info) == -1) {
3857c478bd9Sstevel@tonic-gate 		return (-1);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (door_revoke(fd) != 0) {
3897c478bd9Sstevel@tonic-gate 		return (-1);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* wait for existing door calls to finish */
3937c478bd9Sstevel@tonic-gate 	(void) sleep(1);
3947c478bd9Sstevel@tonic-gate 
395*03859504Sjg 	if ((cookie = (door_cookie_t *)(uintptr_t)info.di_data) != NULL) {
3967c478bd9Sstevel@tonic-gate 		struct door_result *tmp = cookie->results;
3977c478bd9Sstevel@tonic-gate 		while (tmp) {
3987c478bd9Sstevel@tonic-gate 			cookie->results = tmp->next;
3997c478bd9Sstevel@tonic-gate 			free(tmp->data);
4007c478bd9Sstevel@tonic-gate 			free(tmp);
4017c478bd9Sstevel@tonic-gate 			tmp = cookie->results;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		free(cookie);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	return (0);
4067c478bd9Sstevel@tonic-gate }
407