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