|
/*
* Program to read or create an nvlist.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <libnvpair.h>
/* generate a packed nvlist */
static int
create_packed_nvlist(char **buf, uint_t *buflen, int encode)
{
uchar_t bytes[] = {0xaa, 0xbb, 0xcc, 0xdd};
int16_t int16[] = {0, 1, 2};
int32_t int32[] = {3, 4, 5};
uint64_t uint64[] = {0x100000007, 0x100000008, 0x100000009};
char *strs[] = {"child0", "child1", "child2"};
int err;
nvlist_t *nvl;
err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0); /* allocate list */
if (err) {
(void) printf("nvlist_alloc() failed\n");
return (err);
}
/* add a value of each type */
if ((nvlist_add_boolean(nvl, "bool") != 0) ||
(nvlist_add_byte(nvl, "byte", bytes[0]) != 0) ||
(nvlist_add_int16(nvl, "int16", int16[0]) != 0) ||
(nvlist_add_int32(nvl, "int32", int32[0]) != 0) ||
(nvlist_add_uint64(nvl, "uint64", uint64[0]) != 0) ||
(nvlist_add_string(nvl, "string", strs[0]) != 0) ||
(nvlist_add_byte_array(nvl, "byte_array", bytes, 4) != 0) ||
(nvlist_add_int16_array(nvl, "int16_array", int16, 3) != 0) ||
(nvlist_add_int32_array(nvl, "int32_array", int32, 3) != 0) ||
(nvlist_add_uint64_array(nvl, "uint64_array", uint64, 3) != 0) ||
(nvlist_add_string_array(nvl, "string_array", strs, 3) != 0)) {
nvlist_free(nvl);
return (-1);
}
err = nvlist_size(nvl, buflen, encode);
if (err) {
(void) printf("nvlist_size: %s\n", strerror(err));
return (err);
}
/* pack into contig. memory */
err = nvlist_pack(nvl, buf, buflen, encode, 0);
if (err)
(void) printf("nvlist_pack: %s\n", strerror(err));
/* free the original list */
nvlist_free(nvl);
return (err);
}
/* read a packed nvlist from file or create a packed nvlist */
static int
get_nvlist_buf(char *file, char **buf, size_t *buflen) {
int fd, rv;
struct stat sbuf;
if (file == NULL)
return (create_packed_nvlist(buf, buflen, NV_ENCODE_NATIVE));
/* read from file */
fd = open(file, O_RDONLY);
if (fd == -1) {
(void) printf("cannot open file %s\n", file);
return (-1);
}
(void) fstat(fd, &sbuf);
*buflen = sbuf.st_size;
*buf = malloc(*buflen);
if (*buf == NULL) {
(void) printf("out of memory\n");
return (-1);
}
rv = read(fd, *buf, *buflen);
(void) close(fd);
return (rv);
}
/* selectively print nvpairs */
static void
nvlist_lookup_and_print(nvlist_t *nvl)
{
char **str_val;
int i, int_val;
uint_t nval;
if (nvlist_lookup_int32(nvl, "int32", &int_val) == 0)
(void) printf("int32 = %d\n", int_val);
if (nvlist_lookup_string_array(nvl, "string_array", &str_val, &nval)
== 0) {
(void) printf("string_array =");
for (i = 0; i < nval; i++)
(void) printf(" %s", str_val[i]);
(void) printf("\n");
}
}
void
main(int argc, char *argv[])
{
int c, err;
char *file = NULL, *buf = NULL;
size_t buflen;
nvlist_t *nvl = NULL;
while ((c = getopt(argc, argv, "r:")) != EOF)
switch (c) {
case 'r':
file = optarg;
break;
default:
(void) printf("Usage: %s [ -r file ]", argv[0]);
return;
}
if (get_nvlist_buf(file, &buf, &buflen) != 0) {
(void) printf("cannot get packed nvlist buffer\n");
return;
}
/* unpack into an nvlist_t */
err = nvlist_unpack(buf, buflen, &nvl, 0);
if (err) {
(void) printf("nvlist_unpack(): %s\n", strerror(err));
return;
}
/* selectively print out attributes */
nvlist_lookup_and_print(nvl);
return;
}
|