da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * The contents of this file are subject to the terms of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Common Development and Distribution License, Version 1.0 only
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * (the "License"). You may not use this file except in compliance
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin * with the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * or http://www.opensolaris.org/os/licensing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * When distributing Covered Code, include this CDDL HEADER in each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If applicable, add the following below this CDDL HEADER, with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fields enclosed by brackets "[]" replaced with your own identifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * information: Portions Copyright [yyyy] [name of copyright owner]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * Use is subject to license terms.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#pragma ident "%Z%%M% %I% %E% SMI"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This module implements the PICL Interface used by PICL clients
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * to access services of the PICL daemon
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Locking Strategy
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * A single reader/writer lock (icl_lock) protects the access to the interface
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * to the picl daemon, and the reference count, refcnt, variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * A reader lock is obtained to send a request to the daemon.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * A writer lock is obtained to initialize, reinitialize, or shutdown
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * the interface.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdio.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdlib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <unistd.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <alloca.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <fcntl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <libintl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <errno.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/mman.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <door.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/door.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/time.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <assert.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <synch.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <limits.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <picl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "picl2door.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Module variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int door_handle = -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic uint32_t refcnt = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic rwlock_t picl_lock = DEFAULTRWLOCK;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic char *picl_errmsg[] = {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "No error",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "General system failure",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Daemon not responding",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Unknown PICL service",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Session not initialized",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Invalid arguments",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Argument too big",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property not found",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Not a table property handle",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Not a node handle",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Not a property handle",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "End of property list",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property already exists",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property not writable",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Insufficient permissions",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Invalid handle",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Stale handle",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Unsupported version",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Wait timed out",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Attempting to destroy before delete",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "PICL Tree is busy",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Already has a parent",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property name is reserved",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Invalid reference value",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Continue tree walk",
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "Terminate tree walk",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Node not found",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Not enough space available",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property not readable",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "Property value unavailable"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin};
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define N_ERRORS (sizeof (picl_errmsg)/sizeof (picl_errmsg[0]))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define SEND_REQ_TRYCOUNT 1
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This function sends the client request to the daemon using a door call.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If door_handle is -1, it returns PICL_NOTINITIALIZED.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If the door_call fails, it returns PICL_NORESPONSE. Otherwise, it
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * checks the response from the daemon for error. If an error is returned
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * this function returns the error code returned and unmaps any
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * memory mapped by the door call. For successful results, the caller is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * responsible to unmap the mapped memory after retrieving the results.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This function does not attempt to reinitialize the interface if the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * initial door_call fails. It is called from handshake() , shutdown()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and trysend_req() routines.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpost_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int err;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin picl_service_t *ret;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int req_cnum;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin req_cnum = ((picl_service_t *)data_ptr)->in.cnum;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->data_ptr = data_ptr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->data_size = data_size;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->desc_ptr = desc_ptr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->desc_num = desc_num;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->rbuf = rbuf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dargp->rsize = rsize;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (door_call(door_handle, dargp) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (PICL_NORESPONSE);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*LINTED*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ret = (picl_service_t *)dargp->rbuf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (ret->in.cnum == req_cnum)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (PICL_SUCCESS);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if ((ret->in.cnum == PICL_CNUM_ERROR) &&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (ret->ret_error.in_cnum == req_cnum))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err = ret->ret_error.errnum;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err = PICL_UNKNOWNSERVICE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (dargp->rbuf != rbuf)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) munmap(dargp->rbuf, dargp->rsize);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (err);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This function posts an INIT message to the daemon to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * verify communication channel.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhandshake(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int err;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin door_arg_t darg;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin picl_reqinit_t req;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin picl_retinit_t outargs;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin req.cnum = PICL_CNUM_INIT;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin req.clrev = PICL_VERSION_1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((err = post_req(&darg, &req, sizeof (picl_reqinit_t), NULL,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin 0, &outargs, sizeof (picl_retinit_t))) != PICL_SUCCESS)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (err);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (darg.rbuf != (char *)&outargs)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) munmap(darg.rbuf, darg.rsize);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (PICL_SUCCESS);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This function calls post_req() to make door_call and reinitializes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * the interface is post_req() fails.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintrysend_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin unsigned int trycount)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int err;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int write_locked;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin write_locked = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_rdlock(&picl_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (refcnt == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_unlock(&picl_lock); /* read unlock */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (PICL_NOTINITIALIZED);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while ((err = post_req(dargp, data_ptr, data_size, desc_ptr, desc_num,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin rbuf, rsize)) == PICL_NORESPONSE) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (trycount == 0) /* no more retry */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (write_locked == 1) { /* close and open door */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) close(door_handle);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err = PICL_NORESPONSE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin --trycount;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Upgrade read to a write lock
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_unlock(&picl_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_wrlock(&picl_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * if picl_shutdown happens during lock upgrade
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (refcnt == 0) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err = PICL_NOTINITIALIZED;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin write_locked = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_unlock(&picl_lock); /* read or write unlock */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (err);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Initialize the PICL interface
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Increment the reference count.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpicl_initialize(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int err;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) rw_wrlock(&picl_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (refcnt > 0) { /* previously initialized */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin err = handshake();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (err == PICL_SUCCESS) {
++refcnt;
(void) rw_unlock(&picl_lock); /* write unlock */
return (err);
}
if (err != PICL_NORESPONSE) {
(void) rw_unlock(&picl_lock); /* write unlock */
return (err);
}
(void) close(door_handle); /* close bad door */
}
/*
* Open picld door and initialize door_handle
*/
if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
(void) rw_unlock(&picl_lock); /* write unlock */
return (PICL_NORESPONSE);
}
err = handshake();
if (err != PICL_SUCCESS)
(void) close(door_handle);
else
++refcnt;
(void) rw_unlock(&picl_lock); /* write unlock */
return (err);
}
/*
* Shutdown the PICL interface
* Decrement the reference count and close the door_handle if refcnt is zero
*/
int
picl_shutdown(void)
{
int err;
door_arg_t darg;
picl_reqfini_t req_fini;
picl_retfini_t outargs;
(void) rw_wrlock(&picl_lock); /* write lock */
if (refcnt == 0) {
(void) rw_unlock(&picl_lock); /* write unlock */
return (PICL_NOTINITIALIZED);
}
req_fini.cnum = PICL_CNUM_FINI;
err = post_req(&darg, &req_fini, sizeof (picl_reqfini_t),
NULL, 0, &outargs, sizeof (picl_retfini_t));
--refcnt;
if (refcnt == 0)
(void) close(door_handle);
(void) rw_unlock(&picl_lock); /* write unlock */
if (err != PICL_SUCCESS)
return (err);
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function waits for the specified number of seconds for a PICL
* tree refresh.
*/
int
picl_wait(unsigned int secs)
{
door_arg_t darg;
picl_reqwait_t req_wait;
picl_retwait_t outargs;
picl_service_t *ret;
int err;
req_wait.cnum = PICL_CNUM_WAIT;
req_wait.secs = secs;
err = trysend_req(&darg, &req_wait, sizeof (picl_reqwait_t),
NULL, 0, &outargs, sizeof (picl_retwait_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
err = ret->ret_wait.retcode;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}
/*
* This function copies the handle of the root node of the PICL tree into
* the buffer <rooth>
*/
int
picl_get_root(picl_nodehdl_t *rooth)
{
door_arg_t darg;
picl_reqroot_t req_root;
picl_retroot_t outargs;
picl_service_t *ret;
int err;
req_root.cnum = PICL_CNUM_GETROOT;
err = trysend_req(&darg, &req_root, sizeof (picl_reqroot_t), NULL,
0, &outargs, sizeof (picl_retroot_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*rooth = ret->ret_root.rnode;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the value of the property specified by its handle
* into the buffer <valbuf>.
*/
int
picl_get_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
{
door_arg_t darg;
picl_reqattrval_t req_attrval;
picl_service_t *ret;
picl_retattrval_t *outargs;
int err;
req_attrval.cnum = PICL_CNUM_GETATTRVAL;
req_attrval.attr = proph;
req_attrval.bufsize = (uint32_t)nbytes;
if ((size_t)req_attrval.bufsize != nbytes)
return (PICL_VALUETOOBIG);
outargs = alloca(sizeof (picl_retattrval_t) + nbytes);
err = trysend_req(&darg, &req_attrval, sizeof (picl_reqattrval_t),
NULL, 0, outargs, sizeof (picl_retattrval_t) + nbytes,
SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
if (ret->ret_attrval.nbytes > (uint32_t)nbytes)
err = PICL_VALUETOOBIG;
else
(void) memcpy(valbuf, ret->ret_attrval.ret_buf,
(size_t)ret->ret_attrval.nbytes);
if (darg.rbuf != (char *)outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}
/*
* This function copies the value of the property specified by its
* name into the buffer <valbuf>
*/
int
picl_get_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
void *valbuf, size_t nbytes)
{
door_arg_t darg;
picl_reqattrvalbyname_t req_attrvalbyname;
picl_service_t *ret;
picl_retattrvalbyname_t *outargs;
int err;
req_attrvalbyname.cnum = PICL_CNUM_GETATTRVALBYNAME;
req_attrvalbyname.nodeh = nodeh;
(void) strcpy(req_attrvalbyname.propname, propname);
req_attrvalbyname.bufsize = (uint32_t)nbytes;
if ((size_t)req_attrvalbyname.bufsize != nbytes)
return (PICL_VALUETOOBIG);
outargs = alloca(sizeof (picl_retattrvalbyname_t) + nbytes);
err = trysend_req(&darg, &req_attrvalbyname,
sizeof (picl_reqattrvalbyname_t), NULL, 0, outargs,
sizeof (picl_retattrvalbyname_t) + nbytes, SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
if (ret->ret_attrvalbyname.nbytes > (uint32_t)nbytes)
err = PICL_VALUETOOBIG;
else
(void) memcpy(valbuf, ret->ret_attrvalbyname.ret_buf,
(size_t)ret->ret_attrvalbyname.nbytes);
if (darg.rbuf != (char *)outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}
/*
* This function sets the value of the property specified by its
* handle with the value specified in <valbuf>.
*/
int
picl_set_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
{
door_arg_t darg;
picl_reqsetattrval_t ret_setattrval;
picl_reqsetattrval_t *inargs;
int err;
if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
return (PICL_VALUETOOBIG);
inargs = alloca(sizeof (picl_reqsetattrval_t) + nbytes);
inargs->cnum = PICL_CNUM_SETATTRVAL;
inargs->attr = proph;
inargs->bufsize = (uint32_t)nbytes;
if ((size_t)inargs->bufsize != nbytes)
return (PICL_VALUETOOBIG);
(void) memcpy(inargs->valbuf, valbuf, nbytes);
err = trysend_req(&darg, inargs, sizeof (picl_reqsetattrval_t) +
nbytes, NULL, 0, &ret_setattrval,
sizeof (picl_retsetattrval_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
if (darg.rbuf != (char *)&ret_setattrval)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function sets the value of the property specified by its
* name with the value given in <valbuf>
*/
int
picl_set_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
void *valbuf, size_t nbytes)
{
door_arg_t darg;
picl_retsetattrvalbyname_t ret_setattrvalbyname;
picl_reqsetattrvalbyname_t *inargs;
int err;
if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
return (PICL_VALUETOOBIG);
inargs = alloca(sizeof (picl_reqsetattrvalbyname_t) + nbytes);
inargs->cnum = PICL_CNUM_SETATTRVALBYNAME;
inargs->nodeh = nodeh;
(void) strcpy(inargs->propname, propname);
inargs->bufsize = (uint32_t)nbytes;
if ((size_t)inargs->bufsize != nbytes)
return (PICL_VALUETOOBIG);
(void) memcpy(inargs->valbuf, valbuf, nbytes);
err = trysend_req(&darg, inargs,
sizeof (picl_reqsetattrvalbyname_t) + nbytes, NULL, 0,
&ret_setattrvalbyname, sizeof (picl_retsetattrvalbyname_t),
SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
if (darg.rbuf != (char *)&ret_setattrvalbyname)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the information of the specified property
* into <pinfo>
*/
int
picl_get_propinfo(picl_prophdl_t proph, picl_propinfo_t *pinfo)
{
door_arg_t darg;
picl_reqattrinfo_t req_attrinfo;
picl_service_t *ret;
picl_retattrinfo_t outargs;
int err;
req_attrinfo.cnum = PICL_CNUM_GETATTRINFO;
req_attrinfo.attr = proph;
err = trysend_req(&darg, &req_attrinfo,
sizeof (picl_reqattrinfo_t), NULL, 0, &outargs,
sizeof (picl_retattrinfo_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
pinfo->type = ret->ret_attrinfo.type;
pinfo->accessmode = ret->ret_attrinfo.accessmode;
pinfo->size = (size_t)ret->ret_attrinfo.size;
(void) strcpy(pinfo->name, ret->ret_attrinfo.name);
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the handle of the first property of a node into
* <proph>
*/
int
picl_get_first_prop(picl_nodehdl_t nodeh, picl_prophdl_t *proph)
{
door_arg_t darg;
picl_reqfirstattr_t req_firstattr;
picl_service_t *ret;
picl_retfirstattr_t outargs;
int err;
req_firstattr.cnum = PICL_CNUM_GETFIRSTATTR;
req_firstattr.nodeh = nodeh;
err = trysend_req(&darg, &req_firstattr,
sizeof (picl_reqfirstattr_t), NULL, 0, &outargs,
sizeof (picl_retfirstattr_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*proph = ret->ret_firstattr.attr;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the handle of the next property in list
* into <nextprop>.
*/
int
picl_get_next_prop(picl_prophdl_t proph, picl_prophdl_t *nextprop)
{
door_arg_t darg;
picl_reqnextattr_t req_nextattr;
picl_service_t *ret;
picl_retnextattr_t outargs;
int err;
req_nextattr.cnum = PICL_CNUM_GETNEXTATTR;
req_nextattr.attr = proph;
err = trysend_req(&darg, &req_nextattr,
sizeof (picl_reqnextattr_t), NULL, 0, &outargs,
sizeof (picl_retnextattr_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*nextprop = ret->ret_nextattr.nextattr;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the handle of the property specified by its
* name into <proph>.
*/
int
picl_get_prop_by_name(picl_nodehdl_t nodeh, const char *name,
picl_prophdl_t *proph)
{
door_arg_t darg;
picl_reqattrbyname_t req_attrbyname;
picl_service_t *ret;
picl_retattrbyname_t outargs;
int err;
req_attrbyname.cnum = PICL_CNUM_GETATTRBYNAME;
req_attrbyname.nodeh = nodeh;
(void) strcpy(req_attrbyname.propname, name);
err = trysend_req(&darg, &req_attrbyname,
sizeof (picl_reqattrbyname_t), NULL, 0, &outargs,
sizeof (picl_retattrbyname_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*proph = ret->ret_attrbyname.attr;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the handle of the next property on the same
* row of the table into <rowproph>.
* When proph is the table handle, the handle of the property that is
* in first row and first column is copied.
*/
int
picl_get_next_by_row(picl_prophdl_t proph, picl_prophdl_t *rowproph)
{
door_arg_t darg;
picl_reqattrbyrow_t req_attrbyrow;
picl_service_t *ret;
picl_retattrbyrow_t outargs;
int err;
req_attrbyrow.cnum = PICL_CNUM_GETATTRBYROW;
req_attrbyrow.attr = proph;
err = trysend_req(&darg, &req_attrbyrow,
sizeof (picl_reqattrbyrow_t), NULL, 0, &outargs,
sizeof (picl_retattrbyrow_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*rowproph = ret->ret_attrbyrow.rowattr;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function copies the handle of the next property on the same
* column of the table into <colproph>.
* When proph is the table handle, the handle of the property that is
* in the first row and first column is copied.
*/
int
picl_get_next_by_col(picl_prophdl_t proph, picl_prophdl_t *colproph)
{
door_arg_t darg;
picl_reqattrbycol_t req_attrbycol;
picl_service_t *ret;
picl_retattrbycol_t outargs;
int err;
req_attrbycol.cnum = PICL_CNUM_GETATTRBYCOL;
req_attrbycol.attr = proph;
err = trysend_req(&darg, (char *)&req_attrbycol,
sizeof (picl_reqattrbycol_t), NULL, 0, (char *)&outargs,
sizeof (picl_retattrbycol_t), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*colproph = ret->ret_attrbycol.colattr;
if (darg.rbuf != (char *)&outargs)
(void) munmap(darg.rbuf, darg.rsize);
return (PICL_SUCCESS);
}
/*
* This function returns the picl error messages corresponding to the
* error number.
*/
char *
picl_strerror(int err)
{
if ((err < N_ERRORS) && (err >= 0)) {
return (gettext(picl_errmsg[err]));
}
return ((char *)NULL);
}
/*
* recursively visit all nodes
*/
static int
do_walk(picl_nodehdl_t rooth, const char *classname,
void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
{
int err;
picl_nodehdl_t chdh;
char classval[PICL_CLASSNAMELEN_MAX];
err = picl_get_propval_by_name(rooth, PICL_PROP_CHILD, &chdh,
sizeof (chdh));
while (err == PICL_SUCCESS) {
err = picl_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
classval, sizeof (classval));
if (err != PICL_SUCCESS)
return (err);
if ((classname == NULL) || (strcmp(classname, classval) == 0)) {
err = callback_fn(chdh, c_args);
if (err != PICL_WALK_CONTINUE)
return (err);
}
if ((err = do_walk(chdh, classname, c_args, callback_fn)) !=
PICL_WALK_CONTINUE)
return (err);
err = picl_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
sizeof (chdh));
}
if (err == PICL_PROPNOTFOUND) /* end of a branch */
return (PICL_WALK_CONTINUE);
return (err);
}
/*
* This function walks the tree by class and invokes the callback
* function on class name matches.
*/
int
picl_walk_tree_by_class(picl_nodehdl_t rooth, const char *classname,
void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
{
int err;
if (callback_fn == NULL)
return (PICL_INVALIDARG);
err = do_walk(rooth, classname, c_args, callback_fn);
if ((err == PICL_WALK_CONTINUE) || (err == PICL_WALK_TERMINATE))
return (PICL_SUCCESS);
return (err);
}
/*
* This function gets propinfo and prop handle of the named property
*/
int
picl_get_propinfo_by_name(picl_nodehdl_t nodeh, const char *prop_name,
picl_propinfo_t *pinfo, picl_prophdl_t *proph)
{
int err;
picl_prophdl_t tmpproph;
picl_propinfo_t tmppinfo;
err = picl_get_prop_by_name(nodeh, prop_name, &tmpproph);
if (err != PICL_SUCCESS)
return (err);
err = picl_get_propinfo(tmpproph, &tmppinfo);
if (err != PICL_SUCCESS)
return (err);
*proph = tmpproph;
*pinfo = tmppinfo;
return (PICL_SUCCESS);
}
int
picl_get_node_by_path(const char *piclpath, picl_nodehdl_t *nodeh)
{
door_arg_t darg;
picl_reqnodebypath_t req;
picl_retnodebypath_t out;
picl_service_t *ret;
int err;
req.cnum = PICL_CNUM_NODEBYPATH;
req.psize = PATH_MAX;
if (strlen(piclpath) >= PATH_MAX)
return (PICL_VALUETOOBIG);
(void) strncpy(req.pathbuf, piclpath, PATH_MAX);
err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
sizeof (out), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*nodeh = ret->ret_nodebypath.nodeh;
if (darg.rbuf != (char *)&out)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}
int
picl_find_node(picl_nodehdl_t rooth, char *pname, picl_prop_type_t ptype,
void *pval, size_t valsize, picl_nodehdl_t *retnodeh)
{
door_arg_t darg;
picl_reqfindnode_t *req;
picl_service_t *ret;
picl_retfindnode_t out;
int err;
req = alloca(sizeof (picl_reqfindnode_t) + valsize);
req->cnum = PICL_CNUM_FINDNODE;
req->nodeh = rooth;
if (strlen(pname) >= PICL_PROPNAMELEN_MAX)
return (PICL_VALUETOOBIG);
(void) strncpy(req->propname, pname, PICL_PROPNAMELEN_MAX);
req->ptype = ptype;
req->valsize = (uint32_t)valsize;
if ((size_t)req->valsize != valsize)
return (PICL_VALUETOOBIG);
(void) memcpy(req->valbuf, pval, valsize);
err = trysend_req(&darg, req, sizeof (picl_reqfindnode_t) + valsize,
NULL, 0, &out, sizeof (out), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*retnodeh = ret->ret_findnode.rnodeh;
if (darg.rbuf != (char *)&out)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}
int
picl_get_frutree_parent(picl_nodehdl_t devh, picl_nodehdl_t *fruh)
{
door_arg_t darg;
picl_reqfruparent_t req;
picl_retfruparent_t out;
picl_service_t *ret;
int err;
req.cnum = PICL_CNUM_FRUTREEPARENT;
req.devh = devh;
err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
sizeof (out), SEND_REQ_TRYCOUNT);
if (err != PICL_SUCCESS)
return (err);
/*LINTED*/
ret = (picl_service_t *)darg.rbuf;
*fruh = ret->ret_fruparent.fruh;
if (darg.rbuf != (char *)&out)
(void) munmap(darg.rbuf, darg.rsize);
return (err);
}