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