2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <door.h>
2N/A#include <unistd.h>
2N/A#include <stddef.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <synch.h>
2N/A#include <sys/stat.h>
2N/A#include <librcm_impl.h>
2N/A
2N/A#include "librcm_event.h"
2N/A
2N/A#define dprint if (debug) (void) printf
2N/Astatic int debug = 1;
2N/A
2N/A#define BUF_THRESHOLD 1024 /* larger bufs require a free */
2N/A
2N/A/*
2N/A * Lookup seq_num. We can not use the standard nvlist_lookup functions since
2N/A * the nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE.
2N/A */
2N/Astatic int
2N/Alookup_seq_num(nvlist_t *nvl, uint64_t *seq_num)
2N/A{
2N/A nvpair_t *nvp = NULL;
2N/A
2N/A while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2N/A if (strcmp(nvpair_name(nvp), RCM_SEQ_NUM) == 0 &&
2N/A nvpair_type(nvp) == DATA_TYPE_UINT64)
2N/A return (nvpair_value_uint64(nvp, seq_num));
2N/A }
2N/A
2N/A return (ENOENT);
2N/A}
2N/A
2N/A/*
2N/A * Get event service from a named door.
2N/A *
2N/A * This is similar to sysevent_post_event(), except that it deals with
2N/A * the "return buffer problem":
2N/A * Typically, the door service places the return buffer on the stack
2N/A * when calling door_return(). This places an artificial limit on the
2N/A * size of the return buffer.
2N/A * This problem is solved by placing large buffers on the heap, referenced
2N/A * through door_info. When client detects a large buffer, it will make a
2N/A * second door_call() to free the buffer. The client and the server agrees
2N/A * on a size, which is defined as BUF_THRESHOLD.
2N/A *
2N/A * Returns -1 if message not delivered. With errno set to cause of error.
2N/A * Returns 0 for success with the results returned in posting buffer.
2N/A */
2N/Aint
2N/Aget_event_service(char *door_name, void *data, size_t datalen,
2N/A void **result, size_t *rlen)
2N/A{
2N/A int service_door, error;
2N/A door_arg_t door_arg;
2N/A
2N/A /*
2N/A * Open the service door
2N/A */
2N/A if ((service_door = open(door_name, O_RDONLY, 0)) == -1) {
2N/A errno = ESRCH;
2N/A return (-1);
2N/A }
2N/A
2N/Aretry1:
2N/A door_arg.rbuf = NULL; /* doorfs will provide return buf */
2N/A door_arg.rsize = 0;
2N/A door_arg.data_ptr = data;
2N/A door_arg.data_size = datalen;
2N/A door_arg.desc_ptr = NULL;
2N/A door_arg.desc_num = 0;
2N/A
2N/A /*
2N/A * Make door call
2N/A * EAGAIN is returned when the door server is temporarily
2N/A * out of threads to service the door call. So retry.
2N/A */
2N/A if ((error = door_call(service_door, &door_arg)) == -1 &&
2N/A errno == EAGAIN) {
2N/A (void) sleep(1);
2N/A goto retry1;
2N/A }
2N/A
2N/A if ((error == 0) && result) {
2N/A
2N/A uint64_t seq_num = 0;
2N/A
2N/A *result = NULL;
2N/A *rlen = 0;
2N/A if (door_arg.rbuf == NULL || door_arg.rsize == 0) {
2N/A dprint("bad return from door call\n");
2N/A (void) close(service_door);
2N/A errno = EFAULT;
2N/A return (-1);
2N/A }
2N/A
2N/A (void) nvlist_unpack(door_arg.rbuf, door_arg.rsize,
2N/A (nvlist_t **)result, 0);
2N/A (void) munmap(door_arg.rbuf, door_arg.rsize);
2N/A
2N/A /*
2N/A * If requiring a buf free, make another door call. There is
2N/A * no need to call munmap() after this door call, though.
2N/A */
2N/A if (lookup_seq_num((nvlist_t *)*result, &seq_num) == 0) {
2N/Aretry2:
2N/A door_arg.rbuf = NULL;
2N/A door_arg.rsize = 0;
2N/A door_arg.data_ptr = (char *)&seq_num;
2N/A door_arg.data_size = sizeof (seq_num);
2N/A door_arg.desc_ptr = NULL;
2N/A door_arg.desc_num = 0;
2N/A if (door_call(service_door, &door_arg) == -1) {
2N/A if (errno == EAGAIN) {
2N/A (void) sleep(1);
2N/A goto retry2;
2N/A }
2N/A dprint("fail to free event buf in server\n");
2N/A }
2N/A }
2N/A }
2N/A
2N/A (void) close(service_door);
2N/A return (error);
2N/A}
2N/A
2N/A/*
2N/A * Export an event service door
2N/A */
2N/Astruct door_result {
2N/A struct door_result *next;
2N/A void *data;
2N/A uint64_t seq_num;
2N/A};
2N/A
2N/Atypedef struct door_cookie {
2N/A uint64_t seq_num;
2N/A mutex_t door_lock;
2N/A void (*door_func)(void **, size_t *);
2N/A struct door_result *results;
2N/A} door_cookie_t;
2N/A
2N/A/*
2N/A * add result to cookie, this is only invoked if result size > BUF_THRESHOLD
2N/A */
2N/Astatic void
2N/Aadd_door_result(door_cookie_t *cook, void *data, uint64_t seq_num)
2N/A{
2N/A struct door_result *result;
2N/A
2N/A /*
2N/A * Need a better way to handle memory here
2N/A */
2N/A result = malloc(sizeof (*result));
2N/A while (result == NULL) {
2N/A (void) sleep(1);
2N/A result = malloc(sizeof (*result));
2N/A }
2N/A result->next = NULL;
2N/A result->data = data;
2N/A result->seq_num = seq_num;
2N/A
2N/A /*
2N/A * Attach current door result to the door cookie
2N/A */
2N/A (void) mutex_lock(&cook->door_lock);
2N/A if (cook->results == NULL) {
2N/A cook->results = result;
2N/A } else {
2N/A struct door_result *tmp = cook->results;
2N/A while (tmp->next) {
2N/A tmp = tmp->next;
2N/A }
2N/A tmp->next = result;
2N/A }
2N/A (void) mutex_unlock(&cook->door_lock);
2N/A}
2N/A
2N/A/*
2N/A * free a previous door result as described by number.
2N/A */
2N/Astatic void
2N/Afree_door_result(door_cookie_t *cook, uint64_t num)
2N/A{
2N/A struct door_result *prev = NULL, *tmp;
2N/A
2N/A (void) mutex_lock(&cook->door_lock);
2N/A tmp = cook->results;
2N/A while (tmp && tmp->seq_num != num) {
2N/A prev = tmp;
2N/A tmp = tmp->next;
2N/A }
2N/A
2N/A if (tmp == NULL) {
2N/A dprint("attempting to free nonexistent buf: %llu\n",
2N/A (unsigned long long)num);
2N/A (void) mutex_unlock(&cook->door_lock);
2N/A return;
2N/A }
2N/A
2N/A if (prev) {
2N/A prev->next = tmp->next;
2N/A } else {
2N/A cook->results = tmp->next;
2N/A }
2N/A (void) mutex_unlock(&cook->door_lock);
2N/A
2N/A free(tmp->data);
2N/A free(tmp);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Adoor_service(void *cookie, char *args, size_t alen,
2N/A door_desc_t *ddp, uint_t ndid)
2N/A{
2N/A nvlist_t *nvl;
2N/A size_t nvl_size = 0;
2N/A char rbuf[BUF_THRESHOLD];
2N/A door_cookie_t *cook = (door_cookie_t *)cookie;
2N/A uint64_t seq_num = 0;
2N/A
2N/A /*
2N/A * Special case for asking to free buffer
2N/A */
2N/A if (alen == sizeof (uint64_t)) {
2N/A free_door_result(cookie, *(uint64_t *)(void *)args);
2N/A (void) door_return(NULL, 0, NULL, 0);
2N/A }
2N/A
2N/A /*
2N/A * door_func update args to point to return results.
2N/A * memory for results are dynamically allocated.
2N/A */
2N/A (*cook->door_func)((void **)&args, &alen);
2N/A
2N/A /*
2N/A * If no results, just return
2N/A */
2N/A if (args == NULL) {
2N/A dprint("null results returned from door_func().\n");
2N/A (void) door_return(NULL, 0, NULL, 0);
2N/A }
2N/A
2N/A /* Determine the size of the packed nvlist */
2N/A nvl = (nvlist_t *)(void *)args;
2N/A args = NULL;
2N/A alen = 0;
2N/A if (errno = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE)) {
2N/A nvlist_free(nvl);
2N/A dprint("failure to sizeup door results: %s\n", strerror(errno));
2N/A (void) door_return(NULL, 0, NULL, 0);
2N/A }
2N/A
2N/A /*
2N/A * If the size of the packed nvlist would exceed the buffer threshold
2N/A * then get a sequence number and add it to the nvlist.
2N/A */
2N/A if (nvl_size > BUF_THRESHOLD) {
2N/A (void) mutex_lock(&cook->door_lock);
2N/A cook->seq_num++;
2N/A seq_num = cook->seq_num;
2N/A (void) mutex_unlock(&cook->door_lock);
2N/A (void) nvlist_add_uint64(nvl, RCM_SEQ_NUM, seq_num);
2N/A }
2N/A
2N/A /* Refill the args with a packed version of the nvlist */
2N/A if (errno = nvlist_pack(nvl, &args, &alen, NV_ENCODE_NATIVE, 0)) {
2N/A nvlist_free(nvl);
2N/A dprint("failure to pack door results: %s\n", strerror(errno));
2N/A (void) door_return(NULL, 0, NULL, 0);
2N/A }
2N/A nvlist_free(nvl);
2N/A
2N/A /*
2N/A * Based on the size of the packed nvlist, either use the local buffer
2N/A * or add it to the results list.
2N/A */
2N/A if (alen <= BUF_THRESHOLD) {
2N/A bcopy(args, rbuf, alen);
2N/A (void) free(args);
2N/A args = rbuf;
2N/A } else {
2N/A /*
2N/A * for long data, append results to end of queue in cook
2N/A * and set ndid, ask client to do another door_call
2N/A * to free the buffer.
2N/A */
2N/A add_door_result(cook, args, seq_num);
2N/A }
2N/A
2N/A (void) door_return(args, alen, NULL, 0);
2N/A}
2N/A
2N/Aint
2N/Acreate_event_service(char *door_name,
2N/A void (*func)(void **data, size_t *datalen))
2N/A{
2N/A int service_door, fd;
2N/A door_cookie_t *cookie;
2N/A
2N/A /* create an fs file */
2N/A fd = open(door_name, O_EXCL|O_CREAT, S_IREAD|S_IWRITE);
2N/A if ((fd == -1) && (errno != EEXIST)) {
2N/A return (-1);
2N/A }
2N/A (void) close(fd);
2N/A
2N/A /* allocate space for door cookie */
2N/A if ((cookie = calloc(1, sizeof (*cookie))) == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A cookie->door_func = func;
2N/A if ((service_door = door_create(door_service, (void *)cookie,
2N/A DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
2N/A dprint("door create failed: %s\n", strerror(errno));
2N/A free(cookie);
2N/A return (-1);
2N/A }
2N/A
2N/Aretry:
2N/A (void) fdetach(door_name);
2N/A if (fattach(service_door, door_name) != 0) {
2N/A if (errno == EBUSY) {
2N/A /*
2N/A * EBUSY error may occur if anyone references the door
2N/A * file while we are fattach'ing. Since librcm, in the
2N/A * the process context of a DR initiator program, may
2N/A * reference the door file (via open/close/stat/
2N/A * door_call etc.) while we are still fattach'ing,
2N/A * retry on EBUSY.
2N/A */
2N/A goto retry;
2N/A }
2N/A dprint("door attaching failed: %s\n", strerror(errno));
2N/A free(cookie);
2N/A (void) close(service_door);
2N/A return (-1);
2N/A }
2N/A
2N/A return (service_door);
2N/A}
2N/A
2N/Aint
2N/Arevoke_event_service(int fd)
2N/A{
2N/A struct door_info info;
2N/A door_cookie_t *cookie;
2N/A
2N/A if (door_info(fd, &info) == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A if (door_revoke(fd) != 0) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* wait for existing door calls to finish */
2N/A (void) sleep(1);
2N/A
2N/A if ((cookie = (door_cookie_t *)(uintptr_t)info.di_data) != NULL) {
2N/A struct door_result *tmp = cookie->results;
2N/A while (tmp) {
2N/A cookie->results = tmp->next;
2N/A free(tmp->data);
2N/A free(tmp);
2N/A tmp = cookie->results;
2N/A }
2N/A free(cookie);
2N/A }
2N/A return (0);
2N/A}