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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <door.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <stddef.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <synch.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <librcm_impl.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "librcm_event.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define dprint if (debug) (void) printf
450Sstevel@tonic-gate static int debug = 1;
460Sstevel@tonic-gate
470Sstevel@tonic-gate #define BUF_THRESHOLD 1024 /* larger bufs require a free */
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * Lookup seq_num. We can not use the standard nvlist_lookup functions since
510Sstevel@tonic-gate * the nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static int
lookup_seq_num(nvlist_t * nvl,uint64_t * seq_num)540Sstevel@tonic-gate lookup_seq_num(nvlist_t *nvl, uint64_t *seq_num)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate nvpair_t *nvp = NULL;
570Sstevel@tonic-gate
580Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
590Sstevel@tonic-gate if (strcmp(nvpair_name(nvp), RCM_SEQ_NUM) == 0 &&
600Sstevel@tonic-gate nvpair_type(nvp) == DATA_TYPE_UINT64)
610Sstevel@tonic-gate return (nvpair_value_uint64(nvp, seq_num));
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate return (ENOENT);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * Get event service from a named door.
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * This is similar to sysevent_post_event(), except that it deals with
710Sstevel@tonic-gate * the "return buffer problem":
720Sstevel@tonic-gate * Typically, the door service places the return buffer on the stack
730Sstevel@tonic-gate * when calling door_return(). This places an artificial limit on the
740Sstevel@tonic-gate * size of the return buffer.
750Sstevel@tonic-gate * This problem is solved by placing large buffers on the heap, referenced
760Sstevel@tonic-gate * through door_info. When client detects a large buffer, it will make a
770Sstevel@tonic-gate * second door_call() to free the buffer. The client and the server agrees
780Sstevel@tonic-gate * on a size, which is defined as BUF_THRESHOLD.
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * Returns -1 if message not delivered. With errno set to cause of error.
810Sstevel@tonic-gate * Returns 0 for success with the results returned in posting buffer.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate int
get_event_service(char * door_name,void * data,size_t datalen,void ** result,size_t * rlen)840Sstevel@tonic-gate get_event_service(char *door_name, void *data, size_t datalen,
850Sstevel@tonic-gate void **result, size_t *rlen)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate int service_door, error;
880Sstevel@tonic-gate door_arg_t door_arg;
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Open the service door
920Sstevel@tonic-gate */
930Sstevel@tonic-gate if ((service_door = open(door_name, O_RDONLY, 0)) == -1) {
940Sstevel@tonic-gate errno = ESRCH;
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate retry1:
990Sstevel@tonic-gate door_arg.rbuf = NULL; /* doorfs will provide return buf */
1000Sstevel@tonic-gate door_arg.rsize = 0;
1010Sstevel@tonic-gate door_arg.data_ptr = data;
1020Sstevel@tonic-gate door_arg.data_size = datalen;
1030Sstevel@tonic-gate door_arg.desc_ptr = NULL;
1040Sstevel@tonic-gate door_arg.desc_num = 0;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate * Make door call
1080Sstevel@tonic-gate * EAGAIN is returned when the door server is temporarily
1090Sstevel@tonic-gate * out of threads to service the door call. So retry.
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate if ((error = door_call(service_door, &door_arg)) == -1 &&
1120Sstevel@tonic-gate errno == EAGAIN) {
1130Sstevel@tonic-gate (void) sleep(1);
1140Sstevel@tonic-gate goto retry1;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate if ((error == 0) && result) {
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate uint64_t seq_num = 0;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate *result = NULL;
1220Sstevel@tonic-gate *rlen = 0;
1230Sstevel@tonic-gate if (door_arg.rbuf == NULL || door_arg.rsize == 0) {
1240Sstevel@tonic-gate dprint("bad return from door call\n");
1250Sstevel@tonic-gate (void) close(service_door);
1260Sstevel@tonic-gate errno = EFAULT;
1270Sstevel@tonic-gate return (-1);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate (void) nvlist_unpack(door_arg.rbuf, door_arg.rsize,
1310Sstevel@tonic-gate (nvlist_t **)result, 0);
1320Sstevel@tonic-gate (void) munmap(door_arg.rbuf, door_arg.rsize);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * If requiring a buf free, make another door call. There is
1360Sstevel@tonic-gate * no need to call munmap() after this door call, though.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate if (lookup_seq_num((nvlist_t *)*result, &seq_num) == 0) {
1390Sstevel@tonic-gate retry2:
1400Sstevel@tonic-gate door_arg.rbuf = NULL;
1410Sstevel@tonic-gate door_arg.rsize = 0;
1420Sstevel@tonic-gate door_arg.data_ptr = (char *)&seq_num;
1430Sstevel@tonic-gate door_arg.data_size = sizeof (seq_num);
1440Sstevel@tonic-gate door_arg.desc_ptr = NULL;
1450Sstevel@tonic-gate door_arg.desc_num = 0;
1460Sstevel@tonic-gate if (door_call(service_door, &door_arg) == -1) {
1470Sstevel@tonic-gate if (errno == EAGAIN) {
1480Sstevel@tonic-gate (void) sleep(1);
1490Sstevel@tonic-gate goto retry2;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate dprint("fail to free event buf in server\n");
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate (void) close(service_door);
1570Sstevel@tonic-gate return (error);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * Export an event service door
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate struct door_result {
1640Sstevel@tonic-gate struct door_result *next;
1650Sstevel@tonic-gate void *data;
1660Sstevel@tonic-gate uint64_t seq_num;
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate typedef struct door_cookie {
1700Sstevel@tonic-gate uint64_t seq_num;
1710Sstevel@tonic-gate mutex_t door_lock;
1720Sstevel@tonic-gate void (*door_func)(void **, size_t *);
1730Sstevel@tonic-gate struct door_result *results;
1740Sstevel@tonic-gate } door_cookie_t;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * add result to cookie, this is only invoked if result size > BUF_THRESHOLD
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate static void
add_door_result(door_cookie_t * cook,void * data,uint64_t seq_num)1800Sstevel@tonic-gate add_door_result(door_cookie_t *cook, void *data, uint64_t seq_num)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate struct door_result *result;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * Need a better way to handle memory here
1860Sstevel@tonic-gate */
1870Sstevel@tonic-gate result = malloc(sizeof (*result));
1880Sstevel@tonic-gate while (result == NULL) {
1890Sstevel@tonic-gate (void) sleep(1);
1900Sstevel@tonic-gate result = malloc(sizeof (*result));
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate result->next = NULL;
1930Sstevel@tonic-gate result->data = data;
1940Sstevel@tonic-gate result->seq_num = seq_num;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Attach current door result to the door cookie
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate (void) mutex_lock(&cook->door_lock);
2000Sstevel@tonic-gate if (cook->results == NULL) {
2010Sstevel@tonic-gate cook->results = result;
2020Sstevel@tonic-gate } else {
2030Sstevel@tonic-gate struct door_result *tmp = cook->results;
2040Sstevel@tonic-gate while (tmp->next) {
2050Sstevel@tonic-gate tmp = tmp->next;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate tmp->next = result;
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate (void) mutex_unlock(&cook->door_lock);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * free a previous door result as described by number.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate static void
free_door_result(door_cookie_t * cook,uint64_t num)2160Sstevel@tonic-gate free_door_result(door_cookie_t *cook, uint64_t num)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate struct door_result *prev = NULL, *tmp;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate (void) mutex_lock(&cook->door_lock);
2210Sstevel@tonic-gate tmp = cook->results;
2220Sstevel@tonic-gate while (tmp && tmp->seq_num != num) {
2230Sstevel@tonic-gate prev = tmp;
2240Sstevel@tonic-gate tmp = tmp->next;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (tmp == NULL) {
2280Sstevel@tonic-gate dprint("attempting to free nonexistent buf: %llu\n",
2290Sstevel@tonic-gate (unsigned long long)num);
2300Sstevel@tonic-gate (void) mutex_unlock(&cook->door_lock);
2310Sstevel@tonic-gate return;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if (prev) {
2350Sstevel@tonic-gate prev->next = tmp->next;
2360Sstevel@tonic-gate } else {
2370Sstevel@tonic-gate cook->results = tmp->next;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate (void) mutex_unlock(&cook->door_lock);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate free(tmp->data);
2420Sstevel@tonic-gate free(tmp);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*ARGSUSED*/
2460Sstevel@tonic-gate static void
door_service(void * cookie,char * args,size_t alen,door_desc_t * ddp,uint_t ndid)2470Sstevel@tonic-gate door_service(void *cookie, char *args, size_t alen,
2480Sstevel@tonic-gate door_desc_t *ddp, uint_t ndid)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate nvlist_t *nvl;
2510Sstevel@tonic-gate size_t nvl_size = 0;
2520Sstevel@tonic-gate char rbuf[BUF_THRESHOLD];
2530Sstevel@tonic-gate door_cookie_t *cook = (door_cookie_t *)cookie;
2540Sstevel@tonic-gate uint64_t seq_num = 0;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * Special case for asking to free buffer
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate if (alen == sizeof (uint64_t)) {
2600Sstevel@tonic-gate free_door_result(cookie, *(uint64_t *)(void *)args);
2610Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * door_func update args to point to return results.
2660Sstevel@tonic-gate * memory for results are dynamically allocated.
2670Sstevel@tonic-gate */
2680Sstevel@tonic-gate (*cook->door_func)((void **)&args, &alen);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * If no results, just return
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate if (args == NULL) {
2740Sstevel@tonic-gate dprint("null results returned from door_func().\n");
2750Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /* Determine the size of the packed nvlist */
2790Sstevel@tonic-gate nvl = (nvlist_t *)(void *)args;
2800Sstevel@tonic-gate args = NULL;
2810Sstevel@tonic-gate alen = 0;
2820Sstevel@tonic-gate if (errno = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE)) {
2830Sstevel@tonic-gate nvlist_free(nvl);
2840Sstevel@tonic-gate dprint("failure to sizeup door results: %s\n", strerror(errno));
2850Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * If the size of the packed nvlist would exceed the buffer threshold
2900Sstevel@tonic-gate * then get a sequence number and add it to the nvlist.
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate if (nvl_size > BUF_THRESHOLD) {
2930Sstevel@tonic-gate (void) mutex_lock(&cook->door_lock);
2940Sstevel@tonic-gate cook->seq_num++;
2950Sstevel@tonic-gate seq_num = cook->seq_num;
2960Sstevel@tonic-gate (void) mutex_unlock(&cook->door_lock);
2970Sstevel@tonic-gate (void) nvlist_add_uint64(nvl, RCM_SEQ_NUM, seq_num);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /* Refill the args with a packed version of the nvlist */
3010Sstevel@tonic-gate if (errno = nvlist_pack(nvl, &args, &alen, NV_ENCODE_NATIVE, 0)) {
3020Sstevel@tonic-gate nvlist_free(nvl);
3030Sstevel@tonic-gate dprint("failure to pack door results: %s\n", strerror(errno));
3040Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate nvlist_free(nvl);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * Based on the size of the packed nvlist, either use the local buffer
3100Sstevel@tonic-gate * or add it to the results list.
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate if (alen <= BUF_THRESHOLD) {
3130Sstevel@tonic-gate bcopy(args, rbuf, alen);
3140Sstevel@tonic-gate (void) free(args);
3150Sstevel@tonic-gate args = rbuf;
3160Sstevel@tonic-gate } else {
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate * for long data, append results to end of queue in cook
3190Sstevel@tonic-gate * and set ndid, ask client to do another door_call
3200Sstevel@tonic-gate * to free the buffer.
3210Sstevel@tonic-gate */
3220Sstevel@tonic-gate add_door_result(cook, args, seq_num);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate (void) door_return(args, alen, NULL, 0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate int
create_event_service(char * door_name,void (* func)(void ** data,size_t * datalen))3290Sstevel@tonic-gate create_event_service(char *door_name,
3300Sstevel@tonic-gate void (*func)(void **data, size_t *datalen))
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate int service_door, fd;
3330Sstevel@tonic-gate door_cookie_t *cookie;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /* create an fs file */
3360Sstevel@tonic-gate fd = open(door_name, O_EXCL|O_CREAT, S_IREAD|S_IWRITE);
3370Sstevel@tonic-gate if ((fd == -1) && (errno != EEXIST)) {
3380Sstevel@tonic-gate return (-1);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate (void) close(fd);
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /* allocate space for door cookie */
3430Sstevel@tonic-gate if ((cookie = calloc(1, sizeof (*cookie))) == NULL) {
3440Sstevel@tonic-gate return (-1);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate cookie->door_func = func;
3480Sstevel@tonic-gate if ((service_door = door_create(door_service, (void *)cookie,
3490Sstevel@tonic-gate DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
3500Sstevel@tonic-gate dprint("door create failed: %s\n", strerror(errno));
3510Sstevel@tonic-gate free(cookie);
3520Sstevel@tonic-gate return (-1);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate retry:
3560Sstevel@tonic-gate (void) fdetach(door_name);
3570Sstevel@tonic-gate if (fattach(service_door, door_name) != 0) {
3580Sstevel@tonic-gate if (errno == EBUSY) {
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate * EBUSY error may occur if anyone references the door
3610Sstevel@tonic-gate * file while we are fattach'ing. Since librcm, in the
3620Sstevel@tonic-gate * the process context of a DR initiator program, may
3630Sstevel@tonic-gate * reference the door file (via open/close/stat/
3640Sstevel@tonic-gate * door_call etc.) while we are still fattach'ing,
3650Sstevel@tonic-gate * retry on EBUSY.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate goto retry;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate dprint("door attaching failed: %s\n", strerror(errno));
3700Sstevel@tonic-gate free(cookie);
3710Sstevel@tonic-gate (void) close(service_door);
3720Sstevel@tonic-gate return (-1);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate return (service_door);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate int
revoke_event_service(int fd)3790Sstevel@tonic-gate revoke_event_service(int fd)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate struct door_info info;
3820Sstevel@tonic-gate door_cookie_t *cookie;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate if (door_info(fd, &info) == -1) {
3850Sstevel@tonic-gate return (-1);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate if (door_revoke(fd) != 0) {
3890Sstevel@tonic-gate return (-1);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate /* wait for existing door calls to finish */
3930Sstevel@tonic-gate (void) sleep(1);
3940Sstevel@tonic-gate
395*175Sjg if ((cookie = (door_cookie_t *)(uintptr_t)info.di_data) != NULL) {
3960Sstevel@tonic-gate struct door_result *tmp = cookie->results;
3970Sstevel@tonic-gate while (tmp) {
3980Sstevel@tonic-gate cookie->results = tmp->next;
3990Sstevel@tonic-gate free(tmp->data);
4000Sstevel@tonic-gate free(tmp);
4010Sstevel@tonic-gate tmp = cookie->results;
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate free(cookie);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate return (0);
4060Sstevel@tonic-gate }
407