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 2005 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 #include <stdio.h>
30*0Sstevel@tonic-gate #include <fcntl.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <door.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <stddef.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <synch.h>
39*0Sstevel@tonic-gate #include <sys/stat.h>
40*0Sstevel@tonic-gate #include <librcm_impl.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "librcm_event.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	dprint	if (debug) (void) printf
45*0Sstevel@tonic-gate static int debug = 1;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	BUF_THRESHOLD	1024	/* larger bufs require a free */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * Lookup seq_num. We can not use the standard nvlist_lookup functions since
51*0Sstevel@tonic-gate  * the nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate static int
54*0Sstevel@tonic-gate lookup_seq_num(nvlist_t *nvl, uint64_t *seq_num)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
59*0Sstevel@tonic-gate 		if (strcmp(nvpair_name(nvp), RCM_SEQ_NUM) == 0 &&
60*0Sstevel@tonic-gate 		    nvpair_type(nvp) == DATA_TYPE_UINT64)
61*0Sstevel@tonic-gate 			return (nvpair_value_uint64(nvp, seq_num));
62*0Sstevel@tonic-gate 	}
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	return (ENOENT);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Get event service from a named door.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * This is similar to sysevent_post_event(), except that it deals with
71*0Sstevel@tonic-gate  * the "return buffer problem":
72*0Sstevel@tonic-gate  *	Typically, the door service places the return buffer on the stack
73*0Sstevel@tonic-gate  *	when calling door_return(). This places an artificial limit on the
74*0Sstevel@tonic-gate  *	size of the return buffer.
75*0Sstevel@tonic-gate  * This problem is solved by placing large buffers on the heap, referenced
76*0Sstevel@tonic-gate  * through door_info. When client detects a large buffer, it will make a
77*0Sstevel@tonic-gate  * second door_call() to free the buffer. The client and the server agrees
78*0Sstevel@tonic-gate  * on a size, which is defined as BUF_THRESHOLD.
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * Returns -1 if message not delivered. With errno set to cause of error.
81*0Sstevel@tonic-gate  * Returns 0 for success with the results returned in posting buffer.
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate int
84*0Sstevel@tonic-gate get_event_service(char *door_name, void *data, size_t datalen,
85*0Sstevel@tonic-gate     void **result, size_t *rlen)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	int service_door, error;
88*0Sstevel@tonic-gate 	door_arg_t door_arg;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*
91*0Sstevel@tonic-gate 	 * Open the service door
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	if ((service_door = open(door_name, O_RDONLY, 0)) == -1) {
94*0Sstevel@tonic-gate 		errno = ESRCH;
95*0Sstevel@tonic-gate 		return (-1);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate retry1:
99*0Sstevel@tonic-gate 	door_arg.rbuf = NULL;	/* doorfs will provide return buf */
100*0Sstevel@tonic-gate 	door_arg.rsize = 0;
101*0Sstevel@tonic-gate 	door_arg.data_ptr = data;
102*0Sstevel@tonic-gate 	door_arg.data_size = datalen;
103*0Sstevel@tonic-gate 	door_arg.desc_ptr = NULL;
104*0Sstevel@tonic-gate 	door_arg.desc_num = 0;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/*
107*0Sstevel@tonic-gate 	 * Make door call
108*0Sstevel@tonic-gate 	 * EAGAIN is returned when the door server is temporarily
109*0Sstevel@tonic-gate 	 * out of threads to service the door call. So retry.
110*0Sstevel@tonic-gate 	 */
111*0Sstevel@tonic-gate 	if ((error = door_call(service_door, &door_arg)) == -1 &&
112*0Sstevel@tonic-gate 	    errno == EAGAIN) {
113*0Sstevel@tonic-gate 		(void) sleep(1);
114*0Sstevel@tonic-gate 		goto retry1;
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if ((error == 0) && result) {
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 		uint64_t seq_num = 0;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		*result = NULL;
122*0Sstevel@tonic-gate 		*rlen = 0;
123*0Sstevel@tonic-gate 		if (door_arg.rbuf == NULL || door_arg.rsize == 0) {
124*0Sstevel@tonic-gate 			dprint("bad return from door call\n");
125*0Sstevel@tonic-gate 			(void) close(service_door);
126*0Sstevel@tonic-gate 			errno = EFAULT;
127*0Sstevel@tonic-gate 			return (-1);
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		(void) nvlist_unpack(door_arg.rbuf, door_arg.rsize,
131*0Sstevel@tonic-gate 		    (nvlist_t **)result, 0);
132*0Sstevel@tonic-gate 		(void) munmap(door_arg.rbuf, door_arg.rsize);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		/*
135*0Sstevel@tonic-gate 		 * If requiring a buf free, make another door call.  There is
136*0Sstevel@tonic-gate 		 * no need to call munmap() after this door call, though.
137*0Sstevel@tonic-gate 		 */
138*0Sstevel@tonic-gate 		if (lookup_seq_num((nvlist_t *)*result, &seq_num) == 0) {
139*0Sstevel@tonic-gate retry2:
140*0Sstevel@tonic-gate 			door_arg.rbuf = NULL;
141*0Sstevel@tonic-gate 			door_arg.rsize = 0;
142*0Sstevel@tonic-gate 			door_arg.data_ptr = (char *)&seq_num;
143*0Sstevel@tonic-gate 			door_arg.data_size = sizeof (seq_num);
144*0Sstevel@tonic-gate 			door_arg.desc_ptr = NULL;
145*0Sstevel@tonic-gate 			door_arg.desc_num = 0;
146*0Sstevel@tonic-gate 			if (door_call(service_door, &door_arg) == -1) {
147*0Sstevel@tonic-gate 				if (errno == EAGAIN) {
148*0Sstevel@tonic-gate 					(void) sleep(1);
149*0Sstevel@tonic-gate 					goto retry2;
150*0Sstevel@tonic-gate 				}
151*0Sstevel@tonic-gate 				dprint("fail to free event buf in server\n");
152*0Sstevel@tonic-gate 			}
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	(void) close(service_door);
157*0Sstevel@tonic-gate 	return (error);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * Export an event service door
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate struct door_result {
164*0Sstevel@tonic-gate 	struct door_result *next;
165*0Sstevel@tonic-gate 	void *data;
166*0Sstevel@tonic-gate 	uint64_t seq_num;
167*0Sstevel@tonic-gate };
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate typedef struct door_cookie {
170*0Sstevel@tonic-gate 	uint64_t	seq_num;
171*0Sstevel@tonic-gate 	mutex_t		door_lock;
172*0Sstevel@tonic-gate 	void		(*door_func)(void **, size_t *);
173*0Sstevel@tonic-gate 	struct door_result *results;
174*0Sstevel@tonic-gate } door_cookie_t;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate  * add result to cookie, this is only invoked if result size > BUF_THRESHOLD
178*0Sstevel@tonic-gate  */
179*0Sstevel@tonic-gate static void
180*0Sstevel@tonic-gate add_door_result(door_cookie_t *cook, void *data, uint64_t seq_num)
181*0Sstevel@tonic-gate {
182*0Sstevel@tonic-gate 	struct door_result *result;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/*
185*0Sstevel@tonic-gate 	 * Need a better way to handle memory here
186*0Sstevel@tonic-gate 	 */
187*0Sstevel@tonic-gate 	result = malloc(sizeof (*result));
188*0Sstevel@tonic-gate 	while (result == NULL) {
189*0Sstevel@tonic-gate 		(void) sleep(1);
190*0Sstevel@tonic-gate 		result = malloc(sizeof (*result));
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 	result->next = NULL;
193*0Sstevel@tonic-gate 	result->data = data;
194*0Sstevel@tonic-gate 	result->seq_num = seq_num;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	/*
197*0Sstevel@tonic-gate 	 * Attach current door result to the door cookie
198*0Sstevel@tonic-gate 	 */
199*0Sstevel@tonic-gate 	(void) mutex_lock(&cook->door_lock);
200*0Sstevel@tonic-gate 	if (cook->results == NULL) {
201*0Sstevel@tonic-gate 		cook->results = result;
202*0Sstevel@tonic-gate 	} else {
203*0Sstevel@tonic-gate 		struct door_result *tmp = cook->results;
204*0Sstevel@tonic-gate 		while (tmp->next) {
205*0Sstevel@tonic-gate 			tmp = tmp->next;
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 		tmp->next = result;
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	(void) mutex_unlock(&cook->door_lock);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate  * free a previous door result as described by number.
214*0Sstevel@tonic-gate  */
215*0Sstevel@tonic-gate static void
216*0Sstevel@tonic-gate free_door_result(door_cookie_t *cook, uint64_t num)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	struct door_result *prev = NULL, *tmp;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	(void) mutex_lock(&cook->door_lock);
221*0Sstevel@tonic-gate 	tmp = cook->results;
222*0Sstevel@tonic-gate 	while (tmp && tmp->seq_num != num) {
223*0Sstevel@tonic-gate 		prev = tmp;
224*0Sstevel@tonic-gate 		tmp = tmp->next;
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if (tmp == NULL) {
228*0Sstevel@tonic-gate 		dprint("attempting to free nonexistent buf: %llu\n",
229*0Sstevel@tonic-gate 		    (unsigned long long)num);
230*0Sstevel@tonic-gate 		(void) mutex_unlock(&cook->door_lock);
231*0Sstevel@tonic-gate 		return;
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (prev) {
235*0Sstevel@tonic-gate 		prev->next = tmp->next;
236*0Sstevel@tonic-gate 	} else {
237*0Sstevel@tonic-gate 		cook->results = tmp->next;
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	(void) mutex_unlock(&cook->door_lock);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	free(tmp->data);
242*0Sstevel@tonic-gate 	free(tmp);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate /*ARGSUSED*/
246*0Sstevel@tonic-gate static void
247*0Sstevel@tonic-gate door_service(void *cookie, char *args, size_t alen,
248*0Sstevel@tonic-gate     door_desc_t *ddp, uint_t ndid)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	nvlist_t *nvl;
251*0Sstevel@tonic-gate 	size_t nvl_size = 0;
252*0Sstevel@tonic-gate 	char rbuf[BUF_THRESHOLD];
253*0Sstevel@tonic-gate 	door_cookie_t *cook = (door_cookie_t *)cookie;
254*0Sstevel@tonic-gate 	uint64_t seq_num = 0;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/*
257*0Sstevel@tonic-gate 	 * Special case for asking to free buffer
258*0Sstevel@tonic-gate 	 */
259*0Sstevel@tonic-gate 	if (alen == sizeof (uint64_t)) {
260*0Sstevel@tonic-gate 		free_door_result(cookie, *(uint64_t *)(void *)args);
261*0Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/*
265*0Sstevel@tonic-gate 	 * door_func update args to point to return results.
266*0Sstevel@tonic-gate 	 * memory for results are dynamically allocated.
267*0Sstevel@tonic-gate 	 */
268*0Sstevel@tonic-gate 	(*cook->door_func)((void **)&args, &alen);
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * If no results, just return
272*0Sstevel@tonic-gate 	 */
273*0Sstevel@tonic-gate 	if (args == NULL) {
274*0Sstevel@tonic-gate 		dprint("null results returned from door_func().\n");
275*0Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	/* Determine the size of the packed nvlist */
279*0Sstevel@tonic-gate 	nvl = (nvlist_t *)(void *)args;
280*0Sstevel@tonic-gate 	args = NULL;
281*0Sstevel@tonic-gate 	alen = 0;
282*0Sstevel@tonic-gate 	if (errno = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE)) {
283*0Sstevel@tonic-gate 		nvlist_free(nvl);
284*0Sstevel@tonic-gate 		dprint("failure to sizeup door results: %s\n", strerror(errno));
285*0Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	/*
289*0Sstevel@tonic-gate 	 * If the size of the packed nvlist would exceed the buffer threshold
290*0Sstevel@tonic-gate 	 * then get a sequence number and add it to the nvlist.
291*0Sstevel@tonic-gate 	 */
292*0Sstevel@tonic-gate 	if (nvl_size > BUF_THRESHOLD) {
293*0Sstevel@tonic-gate 		(void) mutex_lock(&cook->door_lock);
294*0Sstevel@tonic-gate 		cook->seq_num++;
295*0Sstevel@tonic-gate 		seq_num = cook->seq_num;
296*0Sstevel@tonic-gate 		(void) mutex_unlock(&cook->door_lock);
297*0Sstevel@tonic-gate 		(void) nvlist_add_uint64(nvl, RCM_SEQ_NUM, seq_num);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/* Refill the args with a packed version of the nvlist */
301*0Sstevel@tonic-gate 	if (errno = nvlist_pack(nvl, &args, &alen, NV_ENCODE_NATIVE, 0)) {
302*0Sstevel@tonic-gate 		nvlist_free(nvl);
303*0Sstevel@tonic-gate 		dprint("failure to pack door results: %s\n", strerror(errno));
304*0Sstevel@tonic-gate 		(void) door_return(NULL, 0, NULL, 0);
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate 	nvlist_free(nvl);
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	/*
309*0Sstevel@tonic-gate 	 * Based on the size of the packed nvlist, either use the local buffer
310*0Sstevel@tonic-gate 	 * or add it to the results list.
311*0Sstevel@tonic-gate 	 */
312*0Sstevel@tonic-gate 	if (alen <= BUF_THRESHOLD) {
313*0Sstevel@tonic-gate 		bcopy(args, rbuf, alen);
314*0Sstevel@tonic-gate 		(void) free(args);
315*0Sstevel@tonic-gate 		args = rbuf;
316*0Sstevel@tonic-gate 	} else {
317*0Sstevel@tonic-gate 		/*
318*0Sstevel@tonic-gate 		 * for long data, append results to end of queue in cook
319*0Sstevel@tonic-gate 		 * and set ndid, ask client to do another door_call
320*0Sstevel@tonic-gate 		 * to free the buffer.
321*0Sstevel@tonic-gate 		 */
322*0Sstevel@tonic-gate 		add_door_result(cook, args, seq_num);
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	(void) door_return(args, alen, NULL, 0);
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate int
329*0Sstevel@tonic-gate create_event_service(char *door_name,
330*0Sstevel@tonic-gate     void (*func)(void **data, size_t *datalen))
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	int service_door, fd;
333*0Sstevel@tonic-gate 	door_cookie_t *cookie;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	/* create an fs file */
336*0Sstevel@tonic-gate 	fd = open(door_name, O_EXCL|O_CREAT, S_IREAD|S_IWRITE);
337*0Sstevel@tonic-gate 	if ((fd == -1) && (errno != EEXIST)) {
338*0Sstevel@tonic-gate 		return (-1);
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 	(void) close(fd);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	/* allocate space for door cookie */
343*0Sstevel@tonic-gate 	if ((cookie = calloc(1, sizeof (*cookie))) == NULL) {
344*0Sstevel@tonic-gate 		return (-1);
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	cookie->door_func = func;
348*0Sstevel@tonic-gate 	if ((service_door = door_create(door_service, (void *)cookie,
349*0Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
350*0Sstevel@tonic-gate 		dprint("door create failed: %s\n", strerror(errno));
351*0Sstevel@tonic-gate 		free(cookie);
352*0Sstevel@tonic-gate 		return (-1);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate retry:
356*0Sstevel@tonic-gate 	(void) fdetach(door_name);
357*0Sstevel@tonic-gate 	if (fattach(service_door, door_name) != 0) {
358*0Sstevel@tonic-gate 		if (errno == EBUSY) {
359*0Sstevel@tonic-gate 			/*
360*0Sstevel@tonic-gate 			 * EBUSY error may occur if anyone references the door
361*0Sstevel@tonic-gate 			 * file while we are fattach'ing. Since librcm, in the
362*0Sstevel@tonic-gate 			 * the process context of a DR initiator program, may
363*0Sstevel@tonic-gate 			 * reference the door file (via open/close/stat/
364*0Sstevel@tonic-gate 			 * door_call etc.) while we are still fattach'ing,
365*0Sstevel@tonic-gate 			 * retry on EBUSY.
366*0Sstevel@tonic-gate 			 */
367*0Sstevel@tonic-gate 			goto retry;
368*0Sstevel@tonic-gate 		}
369*0Sstevel@tonic-gate 		dprint("door attaching failed: %s\n", strerror(errno));
370*0Sstevel@tonic-gate 		free(cookie);
371*0Sstevel@tonic-gate 		(void) close(service_door);
372*0Sstevel@tonic-gate 		return (-1);
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	return (service_door);
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate int
379*0Sstevel@tonic-gate revoke_event_service(int fd)
380*0Sstevel@tonic-gate {
381*0Sstevel@tonic-gate 	struct door_info info;
382*0Sstevel@tonic-gate 	door_cookie_t *cookie;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	if (door_info(fd, &info) == -1) {
385*0Sstevel@tonic-gate 		return (-1);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	if (door_revoke(fd) != 0) {
389*0Sstevel@tonic-gate 		return (-1);
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	/* wait for existing door calls to finish */
393*0Sstevel@tonic-gate 	(void) sleep(1);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	if ((cookie = (door_cookie_t *)info.di_data) != NULL) {
396*0Sstevel@tonic-gate 		struct door_result *tmp = cookie->results;
397*0Sstevel@tonic-gate 		while (tmp) {
398*0Sstevel@tonic-gate 			cookie->results = tmp->next;
399*0Sstevel@tonic-gate 			free(tmp->data);
400*0Sstevel@tonic-gate 			free(tmp);
401*0Sstevel@tonic-gate 			tmp = cookie->results;
402*0Sstevel@tonic-gate 		}
403*0Sstevel@tonic-gate 		free(cookie);
404*0Sstevel@tonic-gate 	}
405*0Sstevel@tonic-gate 	return (0);
406*0Sstevel@tonic-gate }
407