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