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 (the "License").
2N/A * You may not use this file except in compliance 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/*
2N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/syscall.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <errno.h>
2N/A#include <libintl.h>
2N/A#include <libnvpair.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A
2N/A#include "libcpc.h"
2N/A#include "libcpc_impl.h"
2N/A
2N/A/*
2N/A * Pack a request set into a buffer using libnvpair. Size of buffer is returned
2N/A * in buflen.
2N/A */
2N/Achar *
2N/A__cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen)
2N/A{
2N/A cpc_request_t *req;
2N/A nvlist_t *setlist, **reqlist;
2N/A size_t packsize = 0;
2N/A char *buf = NULL;
2N/A int i;
2N/A int j;
2N/A
2N/A *buflen = 0;
2N/A
2N/A if (nvlist_alloc(&setlist, 0, 0) == ENOMEM) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((reqlist = (nvlist_t **)malloc(set->cs_nreqs * sizeof (*reqlist)))
2N/A == NULL) {
2N/A nvlist_free(setlist);
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A
2N/A bzero((void *)reqlist, set->cs_nreqs * sizeof (*reqlist));
2N/A
2N/A i = 0;
2N/A for (req = set->cs_request; req != NULL; req = req->cr_next) {
2N/A if (nvlist_alloc(&reqlist[i], 0, 0) == ENOMEM)
2N/A goto nomem;
2N/A
2N/A if (nvlist_add_string(reqlist[i], "cr_event",
2N/A req->cr_event) != 0)
2N/A goto nomem;
2N/A if (nvlist_add_uint64(reqlist[i], "cr_preset",
2N/A req->cr_preset) != 0)
2N/A goto nomem;
2N/A if (nvlist_add_uint32(reqlist[i], "cr_flags",
2N/A req->cr_flags) != 0)
2N/A goto nomem;
2N/A if (nvlist_add_uint32(reqlist[i], "cr_index",
2N/A req->cr_index) != 0)
2N/A goto nomem;
2N/A
2N/A if (req->cr_nattrs != 0) {
2N/A nvlist_t *attrs;
2N/A
2N/A if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) == ENOMEM)
2N/A goto nomem;
2N/A
2N/A for (j = 0; j < req->cr_nattrs; j++) {
2N/A if (nvlist_add_uint64(attrs,
2N/A req->cr_attr[j].ka_name,
2N/A req->cr_attr[j].ka_val) != 0) {
2N/A nvlist_free(attrs);
2N/A goto nomem;
2N/A }
2N/A }
2N/A
2N/A if (nvlist_add_nvlist(reqlist[i], "cr_attr",
2N/A attrs) != 0) {
2N/A nvlist_free(attrs);
2N/A goto nomem;
2N/A }
2N/A
2N/A nvlist_free(attrs);
2N/A }
2N/A i++;
2N/A }
2N/A
2N/A if (nvlist_add_nvlist_array(setlist, "reqs", reqlist,
2N/A set->cs_nreqs) != 0)
2N/A goto nomem;
2N/A
2N/A if (nvlist_add_uint32(setlist, "flags", flags) != 0)
2N/A goto nomem;
2N/A
2N/A if (nvlist_pack(setlist, &buf, &packsize, NV_ENCODE_NATIVE,
2N/A 0) != 0)
2N/A goto nomem;
2N/A
2N/A for (i = 0; i < set->cs_nreqs; i++)
2N/A nvlist_free(reqlist[i]);
2N/A
2N/A nvlist_free(setlist);
2N/A free(reqlist);
2N/A
2N/A *buflen = packsize;
2N/A return (buf);
2N/A
2N/Anomem:
2N/A for (i = 0; i < set->cs_nreqs; i++) {
2N/A if (reqlist[i] != 0)
2N/A nvlist_free(reqlist[i]);
2N/A }
2N/A nvlist_free(setlist);
2N/A free(reqlist);
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A}
2N/A
2N/Acpc_strhash_t *
2N/A__cpc_strhash_alloc(void)
2N/A{
2N/A cpc_strhash_t *p;
2N/A
2N/A if ((p = malloc(sizeof (cpc_strhash_t))) == NULL)
2N/A return (NULL);
2N/A
2N/A p->str = "";
2N/A p->cur = NULL;
2N/A p->next = NULL;
2N/A
2N/A return (p);
2N/A}
2N/A
2N/Avoid
2N/A__cpc_strhash_free(cpc_strhash_t *hash)
2N/A{
2N/A cpc_strhash_t *p = hash, *f;
2N/A
2N/A while (p != NULL) {
2N/A f = p;
2N/A p = p->next;
2N/A free(f);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Insert a new key into the hash table.
2N/A *
2N/A * Returns 0 if key was unique and insert successful.
2N/A *
2N/A * Returns 1 if key was already in table and no insert took place.
2N/A *
2N/A * Returns -1 if out of memory.
2N/A */
2N/Aint
2N/A__cpc_strhash_add(cpc_strhash_t *hash, char *key)
2N/A{
2N/A cpc_strhash_t *p, *tmp;
2N/A
2N/A for (p = hash; p != NULL; p = p->next) {
2N/A if (strcmp(p->str, key) == 0)
2N/A return (1);
2N/A }
2N/A
2N/A if ((p = malloc(sizeof (*p))) == NULL)
2N/A return (-1);
2N/A
2N/A p->str = key;
2N/A tmp = hash->next;
2N/A hash->next = p;
2N/A p->next = tmp;
2N/A /*
2N/A * The head node's current pointer must stay pointed at the first
2N/A * real node. We just inserted at the head.
2N/A */
2N/A hash->cur = p;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Achar *
2N/A__cpc_strhash_next(cpc_strhash_t *hash)
2N/A{
2N/A cpc_strhash_t *p;
2N/A
2N/A if (hash->cur != NULL) {
2N/A p = hash->cur;
2N/A hash->cur = hash->cur->next;
2N/A return (p->str);
2N/A }
2N/A
2N/A return (NULL);
2N/A}