#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <door.h>
#include <libnvpair.h>
#include <libhotplug.h>
#include <libhotplug_impl.h>
#include <sys/sunddi.h>
#include <sys/ddi_hp.h>
static void i_hp_dprintf(const char *fmt, ...);
static int i_hp_pack_branch(hp_node_t, char **, size_t *);
static int i_hp_pack_node(hp_node_t, char **, size_t *);
static int i_hp_unpack_node(char *, size_t, hp_node_t, hp_node_t *);
static int i_hp_unpack_branch(char *, size_t, hp_node_t, hp_node_t *);
static int i_hp_call_hotplugd(nvlist_t *, nvlist_t **);
static nvlist_t *i_hp_set_args(hp_cmd_t, const char *, const char *, uint_t,
const char *, int);
static int i_hp_parse_results(nvlist_t *, hp_node_t *, char **);
int libhotplug_debug = 0;
hp_node_t
hp_init(const char *path, const char *connection, uint_t flags)
{
nvlist_t *args;
nvlist_t *results;
hp_node_t root = NULL;
int rv;
i_hp_dprintf("hp_init: path=%p, connection=%p, flags=0x%x\n",
(void *)path, (void *)connection, flags);
if ((path == NULL) || !HP_INIT_FLAGS_VALID(flags)) {
i_hp_dprintf("hp_init: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if ((args = i_hp_set_args(HP_CMD_GETINFO, path, connection, flags,
NULL, 0)) == NULL) {
i_hp_dprintf("hp_init: cannot build arguments nvlist.\n");
errno = ENOMEM;
return (NULL);
}
rv = i_hp_call_hotplugd(args, &results);
nvlist_free(args);
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, &root, NULL);
nvlist_free(results);
}
if (rv != 0) {
i_hp_dprintf("hp_init: failure (%s).\n", strerror(rv));
if (root)
hp_fini(root);
errno = rv;
return (NULL);
}
if (root == NULL) {
i_hp_dprintf("hp_init: missing info snapshot.\n");
errno = EFAULT;
return (NULL);
}
return (root);
}
void
hp_fini(hp_node_t root)
{
hp_node_t node;
hp_node_t sibling;
char *basepath;
i_hp_dprintf("hp_fini: root=%p\n", (void *)root);
if (root == NULL) {
i_hp_dprintf("hp_fini: invalid arguments.\n");
return;
}
if (root->hp_basepath) {
basepath = root->hp_basepath;
for (node = root; node != NULL; node = node->hp_sibling)
node->hp_basepath = NULL;
free(basepath);
}
node = root;
while (node) {
sibling = node->hp_sibling;
if (node->hp_child)
hp_fini(node->hp_child);
if (node->hp_name)
free(node->hp_name);
if (node->hp_usage)
free(node->hp_usage);
if (node->hp_description)
free(node->hp_description);
free(node);
node = sibling;
}
}
int
hp_traverse(hp_node_t root, void *arg, int (*hp_callback)(hp_node_t, void *arg))
{
int rv;
hp_node_t node;
i_hp_dprintf("hp_traverse: root=%p, arg=%p, hp_callback=%p\n",
(void *)root, arg, (void *)hp_callback);
if ((root == NULL) || (hp_callback == NULL)) {
i_hp_dprintf("hp_traverse: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
for (node = root; node; node = node->hp_sibling) {
rv = hp_callback(node, arg);
if (rv == HP_WALK_TERMINATE) {
i_hp_dprintf("hp_traverse: walk terminated.\n");
return (HP_WALK_TERMINATE);
}
if (node->hp_child && (rv != HP_WALK_PRUNECHILD))
if (hp_traverse(node->hp_child, arg, hp_callback) ==
HP_WALK_TERMINATE) {
i_hp_dprintf("hp_traverse: walk terminated.\n");
return (HP_WALK_TERMINATE);
}
if (rv == HP_WALK_PRUNESIBLING)
break;
}
return (0);
}
int
hp_type(hp_node_t node)
{
i_hp_dprintf("hp_type: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_type: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
return (node->hp_type);
}
char *
hp_name(hp_node_t node)
{
i_hp_dprintf("hp_name: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_name: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_name == NULL) {
i_hp_dprintf("hp_name: missing name value.\n");
errno = EFAULT;
}
return (node->hp_name);
}
int
hp_state(hp_node_t node)
{
i_hp_dprintf("hp_state: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_state: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_state: operation not supported.\n");
errno = ENOTSUP;
return (-1);
}
return (node->hp_state);
}
char *
hp_usage(hp_node_t node)
{
i_hp_dprintf("hp_usage: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_usage: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_type != HP_NODE_USAGE) {
i_hp_dprintf("hp_usage: operation not supported.\n");
errno = ENOTSUP;
return (NULL);
}
if (node->hp_usage == NULL) {
i_hp_dprintf("hp_usage: missing usage value.\n");
errno = EFAULT;
}
return (node->hp_usage);
}
char *
hp_description(hp_node_t node)
{
i_hp_dprintf("hp_description: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_description: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_description: operation not supported.\n");
errno = ENOTSUP;
return (NULL);
}
if (node->hp_description == NULL) {
i_hp_dprintf("hp_description: missing description value.\n");
errno = EFAULT;
}
return (node->hp_description);
}
time_t
hp_last_change(hp_node_t node)
{
i_hp_dprintf("hp_last_change: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_last_change: invalid arguments.\n");
errno = EINVAL;
return (0);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_last_change: operation not supported.\n");
errno = ENOTSUP;
return (0);
}
return (node->hp_last_change);
}
hp_node_t
hp_parent(hp_node_t node)
{
i_hp_dprintf("hp_parent: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_parent: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_parent == NULL) {
i_hp_dprintf("hp_parent: node has no parent.\n");
errno = ENXIO;
}
return (node->hp_parent);
}
hp_node_t
hp_child(hp_node_t node)
{
i_hp_dprintf("hp_child: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_child: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_child == NULL) {
i_hp_dprintf("hp_child: node has no child.\n");
errno = ENXIO;
}
return (node->hp_child);
}
hp_node_t
hp_sibling(hp_node_t node)
{
i_hp_dprintf("hp_sibling: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_sibling: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_sibling == NULL) {
i_hp_dprintf("hp_sibling: node has no sibling.\n");
errno = ENXIO;
}
return (node->hp_sibling);
}
int
hp_path(hp_node_t node, char *path, char *connection)
{
hp_node_t root = NULL;
hp_node_t parent;
int i;
char *s;
char components[MAXPATHLEN];
i_hp_dprintf("hp_path: node=%p, path=%p, connection=%p\n", (void *)node,
(void *)path, (void *)connection);
if ((node == NULL) || (path == NULL) || (connection == NULL)) {
i_hp_dprintf("hp_path: invalid arguments.\n");
return (EINVAL);
}
(void) memset(path, 0, MAXPATHLEN);
(void) memset(connection, 0, MAXPATHLEN);
(void) memset(components, 0, MAXPATHLEN);
if ((node->hp_type == HP_NODE_CONNECTOR) ||
(node->hp_type == HP_NODE_PORT))
(void) strlcpy(connection, node->hp_name, MAXPATHLEN);
for (parent = node; parent != NULL; parent = parent->hp_parent) {
if (parent->hp_type == HP_NODE_DEVICE) {
(void) strlcat(components, "/", MAXPATHLEN);
(void) strlcat(components, parent->hp_name, MAXPATHLEN);
}
if (parent->hp_parent == NULL)
root = parent;
}
if (root->hp_basepath == NULL) {
i_hp_dprintf("hp_path: missing base pathname.\n");
return (EFAULT);
}
if (strcmp(root->hp_basepath, "/") != 0) {
(void) strlcat(path, root->hp_basepath, MAXPATHLEN);
if ((root->hp_type == HP_NODE_DEVICE) &&
((s = strrchr(path, '/')) != NULL))
*s = '\0';
}
for (i = strlen(components) - 1; i >= 0; i--) {
if (components[i] == '/') {
(void) strlcat(path, &components[i], MAXPATHLEN);
components[i] = '\0';
}
}
return (0);
}
int
hp_set_state(hp_node_t node, uint_t flags, int state, hp_node_t *resultsp)
{
hp_node_t root = NULL;
nvlist_t *args;
nvlist_t *results;
int rv;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_set_state: node=%p, flags=0x%x, state=0x%x, "
"resultsp=%p\n", (void *)node, flags, state, (void *)resultsp);
if ((node == NULL) || (resultsp == NULL) ||
!HP_SET_STATE_FLAGS_VALID(flags)) {
i_hp_dprintf("hp_set_state: invalid arguments.\n");
return (EINVAL);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_set_state: operation not supported.\n");
return (ENOTSUP);
}
switch (state) {
case DDI_HP_CN_STATE_PRESENT:
case DDI_HP_CN_STATE_POWERED:
case DDI_HP_CN_STATE_ENABLED:
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_set_state: mismatched target.\n");
return (ENOTSUP);
}
break;
case DDI_HP_CN_STATE_PORT_PRESENT:
case DDI_HP_CN_STATE_OFFLINE:
case DDI_HP_CN_STATE_ONLINE:
if (node->hp_type != HP_NODE_PORT) {
i_hp_dprintf("hp_set_state: mismatched target.\n");
return (ENOTSUP);
}
break;
default:
i_hp_dprintf("hp_set_state: invalid target state.\n");
return (EINVAL);
}
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
if ((args = i_hp_set_args(HP_CMD_CHANGESTATE, path, connection, flags,
NULL, state)) == NULL)
return (ENOMEM);
rv = i_hp_call_hotplugd(args, &results);
nvlist_free(args);
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, &root, NULL);
nvlist_free(results);
*resultsp = root;
}
return (rv);
}
int
hp_set_private(hp_node_t node, const char *options, char **resultsp)
{
int rv;
nvlist_t *args;
nvlist_t *results;
char *values = NULL;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_set_private: node=%p, options=%p, resultsp=%p\n",
(void *)node, (void *)options, (void *)resultsp);
if ((node == NULL) || (options == NULL) || (resultsp == NULL)) {
i_hp_dprintf("hp_set_private: invalid arguments.\n");
return (EINVAL);
}
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_set_private: operation not supported.\n");
return (ENOTSUP);
}
*resultsp = NULL;
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
if ((args = i_hp_set_args(HP_CMD_SETPRIVATE, path, connection, 0,
options, 0)) == NULL)
return (ENOMEM);
rv = i_hp_call_hotplugd(args, &results);
nvlist_free(args);
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, NULL, &values);
nvlist_free(results);
*resultsp = values;
}
return (rv);
}
int
hp_get_private(hp_node_t node, const char *options, char **resultsp)
{
int rv;
nvlist_t *args;
nvlist_t *results;
char *values = NULL;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_get_private: node=%p, options=%p, resultsp=%p\n",
(void *)node, (void *)options, (void *)resultsp);
if ((node == NULL) || (options == NULL) || (resultsp == NULL)) {
i_hp_dprintf("hp_get_private: invalid arguments.\n");
return (EINVAL);
}
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_get_private: operation not supported.\n");
return (ENOTSUP);
}
*resultsp = NULL;
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
if ((args = i_hp_set_args(HP_CMD_GETPRIVATE, path, connection, 0,
options, 0)) == NULL)
return (ENOMEM);
rv = i_hp_call_hotplugd(args, &results);
nvlist_free(args);
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, NULL, &values);
nvlist_free(results);
*resultsp = values;
}
return (rv);
}
int
hp_pack(hp_node_t root, char **bufp, size_t *lenp)
{
hp_node_t node;
nvlist_t *nvl;
char *buf;
size_t len;
int rv;
i_hp_dprintf("hp_pack: root=%p, bufp=%p, lenp=%p\n", (void *)root,
(void *)bufp, (void *)lenp);
if ((root == NULL) || (bufp == NULL) || (lenp == NULL)) {
i_hp_dprintf("hp_pack: invalid arguments.\n");
return (EINVAL);
}
*lenp = 0;
*bufp = NULL;
if (nvlist_alloc(&nvl, 0, 0) != 0) {
i_hp_dprintf("hp_pack: nvlist_alloc() failed (%s).\n",
strerror(errno));
return (ENOMEM);
}
if (root->hp_basepath != NULL) {
rv = nvlist_add_string(nvl, HP_INFO_BASE, root->hp_basepath);
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
for (node = root; node != NULL; node = node->hp_sibling) {
if ((rv = i_hp_pack_branch(node, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_BRANCH,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
len = 0;
buf = NULL;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) == 0) {
*lenp = len;
*bufp = buf;
}
nvlist_free(nvl);
return (rv);
}
int
hp_unpack(char *packed_buf, size_t packed_len, hp_node_t *retp)
{
hp_node_t root;
hp_node_t root_list = NULL;
hp_node_t prev_root = NULL;
nvlist_t *nvl = NULL;
nvpair_t *nvp;
char *basepath = NULL;
int rv;
i_hp_dprintf("hp_unpack: packed_buf=%p, packed_len=%u, retp=%p\n",
(void *)packed_buf, (uint32_t)packed_len, (void *)retp);
if ((packed_buf == NULL) || (packed_len == 0) || (retp == NULL)) {
i_hp_dprintf("hp_unpack: invalid arguments.\n");
return (EINVAL);
}
if ((rv = nvlist_unpack(packed_buf, packed_len, &nvl, 0)) != 0)
return (rv);
if (nvlist_next_nvpair(nvl, NULL) == NULL) {
nvlist_free(nvl);
errno = EINVAL;
return (0);
}
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
rv = EINVAL;
if (strcmp(nvpair_name(nvp), HP_INFO_BASE) == 0) {
char *val_string;
if ((rv = nvpair_value_string(nvp, &val_string)) == 0) {
if ((basepath = strdup(val_string)) == NULL)
rv = ENOMEM;
}
} else if (strcmp(nvpair_name(nvp), HP_INFO_BRANCH) == 0) {
size_t len = 0;
char *buf = NULL;
if ((rv = nvpair_value_byte_array(nvp,
(uchar_t **)&buf, (uint_t *)&len)) == 0) {
rv = i_hp_unpack_branch(buf, len, NULL, &root);
}
if (rv == 0) {
if (prev_root) {
prev_root->hp_sibling = root;
} else {
root_list = root;
}
prev_root = root;
}
}
if (rv != 0) {
if (basepath)
free(basepath);
nvlist_free(nvl);
hp_fini(root_list);
*retp = NULL;
return (rv);
}
}
if (basepath) {
for (root = root_list; root; root = root->hp_sibling)
root->hp_basepath = basepath;
}
nvlist_free(nvl);
*retp = root_list;
return (0);
}
static void
i_hp_dprintf(const char *fmt, ...)
{
va_list ap;
if (libhotplug_debug) {
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
}
}
static int
i_hp_pack_branch(hp_node_t root, char **bufp, size_t *lenp)
{
hp_node_t child;
nvlist_t *nvl;
char *buf;
size_t len;
int rv;
*lenp = 0;
*bufp = NULL;
if (nvlist_alloc(&nvl, 0, 0) != 0)
return (ENOMEM);
if ((rv = i_hp_pack_node(root, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_NODE,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
for (child = root->hp_child; child != NULL; child = child->hp_sibling) {
if ((rv = i_hp_pack_branch(child, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_BRANCH,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
len = 0;
buf = NULL;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) == 0) {
*lenp = len;
*bufp = buf;
}
nvlist_free(nvl);
return (rv);
}
static int
i_hp_pack_node(hp_node_t node, char **bufp, size_t *lenp)
{
nvlist_t *nvl;
char *buf = NULL;
size_t len = 0;
int rv;
if (nvlist_alloc(&nvl, 0, 0) != 0)
return (ENOMEM);
if ((rv = nvlist_add_uint32(nvl, HP_INFO_TYPE,
(uint32_t)node->hp_type)) != 0)
goto fail;
if ((node->hp_name) &&
((rv = nvlist_add_string(nvl, HP_INFO_NAME, node->hp_name)) != 0))
goto fail;
if ((node->hp_usage) &&
((rv = nvlist_add_string(nvl, HP_INFO_USAGE, node->hp_usage)) != 0))
goto fail;
if ((node->hp_description) &&
((rv = nvlist_add_string(nvl, HP_INFO_DESC,
node->hp_description)) != 0))
goto fail;
if ((rv = nvlist_add_uint32(nvl, HP_INFO_STATE, node->hp_state)) != 0)
goto fail;
if ((node->hp_last_change != 0) &&
((rv = nvlist_add_uint32(nvl, HP_INFO_TIME,
node->hp_last_change)) != 0))
goto fail;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) != 0)
goto fail;
*bufp = buf;
*lenp = len;
nvlist_free(nvl);
return (0);
fail:
*bufp = NULL;
*lenp = 0;
nvlist_free(nvl);
return (rv);
}
static int
i_hp_unpack_branch(char *packed_buf, size_t packed_len, hp_node_t parent,
hp_node_t *retp)
{
hp_node_t node = NULL;
hp_node_t child;
hp_node_t prev_child = NULL;
nvlist_t *nvl = NULL;
nvpair_t *nvp;
char *buf;
size_t len;
int rv;
*retp = NULL;
if ((rv = nvlist_unpack(packed_buf, packed_len, &nvl, 0)) != 0)
return (rv);
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
len = 0;
buf = NULL;
if (strcmp(nvpair_name(nvp), HP_INFO_NODE) == 0) {
if (node != NULL) {
hp_fini(node);
nvlist_free(nvl);
return (EFAULT);
}
if ((rv = nvpair_value_byte_array(nvp, (uchar_t **)&buf,
(uint_t *)&len)) == 0)
rv = i_hp_unpack_node(buf, len, parent, &node);
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
} else if (strcmp(nvpair_name(nvp), HP_INFO_BRANCH) == 0) {
if ((rv = nvpair_value_byte_array(nvp, (uchar_t **)&buf,
(uint_t *)&len)) == 0)
rv = i_hp_unpack_branch(buf, len, node, &child);
if (rv != 0) {
hp_fini(node);
nvlist_free(nvl);
return (rv);
}
if (prev_child) {
prev_child->hp_sibling = child;
} else {
node->hp_child = child;
}
prev_child = child;
}
}
nvlist_free(nvl);
*retp = node;
return (0);
}
static int
i_hp_unpack_node(char *buf, size_t len, hp_node_t parent, hp_node_t *retp)
{
hp_node_t node;
nvlist_t *nvl;
nvpair_t *nvp;
uint32_t val_uint32;
char *val_string;
int rv = 0;
*retp = NULL;
if ((nvlist_unpack(buf, len, &nvl, 0) != 0))
return (EINVAL);
if ((node = (hp_node_t)calloc(1, sizeof (struct hp_node))) == NULL) {
nvlist_free(nvl);
return (ENOMEM);
}
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
if ((strcmp(nvpair_name(nvp), HP_INFO_TYPE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_type = val_uint32;
} else if ((strcmp(nvpair_name(nvp), HP_INFO_NAME) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_name = strdup(val_string)) == NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_STATE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_state = val_uint32;
} else if ((strcmp(nvpair_name(nvp), HP_INFO_USAGE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_usage = strdup(val_string)) == NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_DESC) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_description = strdup(val_string))
== NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_TIME) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_last_change = (time_t)val_uint32;
} else {
i_hp_dprintf("i_hp_unpack_node: unrecognized: '%s'\n",
nvpair_name(nvp));
}
}
nvlist_free(nvl);
if (rv != 0) {
hp_fini(node);
return (rv);
}
node->hp_parent = parent;
*retp = node;
return (0);
}
static int
i_hp_call_hotplugd(nvlist_t *args, nvlist_t **resultsp)
{
door_arg_t door_arg;
nvlist_t *results = NULL;
char *buf = NULL;
size_t len = 0;
uint64_t seqnum;
int door_fd;
int rv;
*resultsp = NULL;
if ((door_fd = open(HOTPLUGD_DOOR, O_RDONLY)) < 0) {
i_hp_dprintf("i_hp_call_hotplugd: cannot open door (%s)\n",
strerror(errno));
return (EBADF);
}
if ((rv = nvlist_pack(args, &buf, &len, NV_ENCODE_NATIVE, 0)) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: cannot pack arguments (%s)\n",
strerror(rv));
return (rv);
}
door_arg.data_ptr = buf;
door_arg.data_size = len;
door_arg.desc_ptr = NULL;
door_arg.desc_num = 0;
door_arg.rbuf = (char *)(uintptr_t)&rv;
door_arg.rsize = sizeof (rv);
if (door_call(door_fd, &door_arg) != 0) {
rv = errno;
i_hp_dprintf("i_hp_call_hotplugd: door call failed (%s)\n",
strerror(rv));
(void) close(door_fd);
free(buf);
return (rv);
}
free(buf);
if (door_arg.rbuf != (char *)(uintptr_t)&rv) {
if ((door_arg.rbuf == NULL) ||
(door_arg.data_size < sizeof (rv))) {
i_hp_dprintf("i_hp_call_hotplugd: invalid results.\n");
rv = EFAULT;
} else if (door_arg.data_size == sizeof (rv)) {
rv = *(int *)(uintptr_t)door_arg.rbuf;
} else if ((rv = nvlist_unpack(door_arg.rbuf,
door_arg.data_size, &results, 0)) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: "
"cannot unpack results (%s).\n", strerror(rv));
results = NULL;
rv = EFAULT;
}
if (door_arg.rbuf != NULL)
(void) munmap(door_arg.rbuf, door_arg.rsize);
if ((results != NULL) &&
(nvlist_lookup_uint64(results, HPD_SEQNUM, &seqnum) == 0)) {
door_arg.data_ptr = (char *)(uintptr_t)&seqnum;
door_arg.data_size = sizeof (seqnum);
door_arg.desc_ptr = NULL;
door_arg.desc_num = 0;
door_arg.rbuf = NULL;
door_arg.rsize = 0;
(void) door_call(door_fd, &door_arg);
if (door_arg.rbuf != NULL)
(void) munmap(door_arg.rbuf, door_arg.rsize);
}
*resultsp = results;
}
(void) close(door_fd);
return (rv);
}
static nvlist_t *
i_hp_set_args(hp_cmd_t cmd, const char *path, const char *connection,
uint_t flags, const char *options, int state)
{
nvlist_t *args;
if (nvlist_alloc(&args, NV_UNIQUE_NAME_TYPE, 0) != 0)
return (NULL);
if ((nvlist_add_int32(args, HPD_CMD, cmd) != 0) ||
(nvlist_add_string(args, HPD_PATH, path) != 0)) {
nvlist_free(args);
return (NULL);
}
if ((connection != NULL) && (connection[0] != '\0') &&
(nvlist_add_string(args, HPD_CONNECTION, connection) != 0)) {
nvlist_free(args);
return (NULL);
}
if ((flags != 0) && (nvlist_add_uint32(args, HPD_FLAGS, flags) != 0)) {
nvlist_free(args);
return (NULL);
}
if ((options != NULL) &&
(nvlist_add_string(args, HPD_OPTIONS, options) != 0)) {
nvlist_free(args);
return (NULL);
}
if ((cmd == HP_CMD_CHANGESTATE) &&
(nvlist_add_int32(args, HPD_STATE, state) != 0)) {
nvlist_free(args);
return (NULL);
}
return (args);
}
static int
i_hp_parse_results(nvlist_t *results, hp_node_t *rootp, char **optionsp)
{
int rv;
if (rootp) {
char *buf = NULL;
size_t len = 0;
*rootp = NULL;
if (nvlist_lookup_byte_array(results, HPD_INFO,
(uchar_t **)&buf, (uint_t *)&len) == 0) {
if ((rv = hp_unpack(buf, len, rootp)) != 0)
return (rv);
}
}
if (optionsp) {
char *str;
*optionsp = NULL;
if ((nvlist_lookup_string(results, HPD_OPTIONS, &str) == 0) &&
((*optionsp = strdup(str)) == NULL)) {
return (ENOMEM);
}
}
if (nvlist_lookup_int32(results, HPD_STATUS, &rv) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: missing status.\n");
return (EFAULT);
}
return (rv);
}