Ejemplo n.º 1
0
static struct addrinfo *
addrinfo_unpack(const nvlist_t *nvl)
{
	struct addrinfo *ai;
	const void *addr;
	size_t addrlen;
	const char *canonname;

	addr = nvlist_get_binary(nvl, "ai_addr", &addrlen);
	ai = malloc(sizeof(*ai) + addrlen);
	if (ai == NULL)
		return (NULL);
	ai->ai_flags = (int)nvlist_get_number(nvl, "ai_flags");
	ai->ai_family = (int)nvlist_get_number(nvl, "ai_family");
	ai->ai_socktype = (int)nvlist_get_number(nvl, "ai_socktype");
	ai->ai_protocol = (int)nvlist_get_number(nvl, "ai_protocol");
	ai->ai_addrlen = (socklen_t)addrlen;
	canonname = nvlist_get_string(nvl, "ai_canonname");
	if (canonname != NULL) {
		ai->ai_canonname = strdup(canonname);
		if (ai->ai_canonname == NULL) {
			free(ai);
			return (NULL);
		}
	} else {
		ai->ai_canonname = NULL;
	}
	ai->ai_addr = (void *)(ai + 1);
	bcopy(addr, ai->ai_addr, addrlen);
	ai->ai_next = NULL;

	return (ai);
}
Ejemplo n.º 2
0
static struct hostent *
hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
{
	unsigned int ii, nitems;
	char nvlname[64];
	int n;

	hostent_free(hp);

	hp->h_name = strdup(nvlist_get_string(nvl, "name"));
	if (hp->h_name == NULL)
		goto fail;
	hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
	hp->h_length = (int)nvlist_get_number(nvl, "length");

	nitems = (unsigned int)nvlist_get_number(nvl, "naliases");
	hp->h_aliases = calloc(sizeof(hp->h_aliases[0]), nitems + 1);
	if (hp->h_aliases == NULL)
		goto fail;
	for (ii = 0; ii < nitems; ii++) {
		n = snprintf(nvlname, sizeof(nvlname), "alias%u", ii);
		assert(n > 0 && n < (int)sizeof(nvlname));
		hp->h_aliases[ii] =
		    strdup(nvlist_get_string(nvl, nvlname));
		if (hp->h_aliases[ii] == NULL)
			goto fail;
	}
	hp->h_aliases[ii] = NULL;

	nitems = (unsigned int)nvlist_get_number(nvl, "naddrs");
	hp->h_addr_list = calloc(sizeof(hp->h_addr_list[0]), nitems + 1);
	if (hp->h_addr_list == NULL)
		goto fail;
	for (ii = 0; ii < nitems; ii++) {
		hp->h_addr_list[ii] = malloc(hp->h_length);
		if (hp->h_addr_list[ii] == NULL)
			goto fail;
		n = snprintf(nvlname, sizeof(nvlname), "addr%u", ii);
		assert(n > 0 && n < (int)sizeof(nvlname));
		bcopy(nvlist_get_binary(nvl, nvlname, NULL),
		    hp->h_addr_list[ii], hp->h_length);
	}
	hp->h_addr_list[ii] = NULL;

	return (hp);
fail:
	hostent_free(hp);
	h_errno = NO_RECOVERY;
	return (NULL);
}
Ejemplo n.º 3
0
int
cap_sysctlbyname(cap_channel_t *chan, const char *name, void *oldp,
    size_t *oldlenp, const void *newp, size_t newlen)
{
	nvlist_t *nvl;
	const uint8_t *retoldp;
	uint8_t operation;
	size_t oldlen;

	operation = 0;
	if (oldp != NULL)
		operation |= CAP_SYSCTL_READ;
	if (newp != NULL)
		operation |= CAP_SYSCTL_WRITE;

	nvl = nvlist_create(0);
	nvlist_add_string(nvl, "cmd", "sysctl");
	nvlist_add_string(nvl, "name", name);
	nvlist_add_number(nvl, "operation", (uint64_t)operation);
	if (oldp == NULL && oldlenp != NULL)
		nvlist_add_null(nvl, "justsize");
	else if (oldlenp != NULL)
		nvlist_add_number(nvl, "oldlen", (uint64_t)*oldlenp);
	if (newp != NULL)
		nvlist_add_binary(nvl, "newp", newp, newlen);
	nvl = cap_xfer_nvlist(chan, nvl, 0);
	if (nvl == NULL)
		return (-1);
	if (nvlist_get_number(nvl, "error") != 0) {
		errno = (int)nvlist_get_number(nvl, "error");
		nvlist_destroy(nvl);
		return (-1);
	}

	if (oldp == NULL && oldlenp != NULL) {
		*oldlenp = (size_t)nvlist_get_number(nvl, "oldlen");
	} else if (oldp != NULL) {
		retoldp = nvlist_get_binary(nvl, "oldp", &oldlen);
		memcpy(oldp, retoldp, oldlen);
		if (oldlenp != NULL)
			*oldlenp = oldlen;
	}
	nvlist_destroy(nvl);

	return (0);
}
Ejemplo n.º 4
0
int
cap_random_buf(cap_channel_t *chan, void *buf, size_t nbytes)
{
	nvlist_t *nvl;
	const void *randbuf;
	uint8_t *ptr;
	size_t left, randbufsize;

	left = nbytes;
	ptr = buf;

	while (left > 0) {
		nvl = nvlist_create(0);
		nvlist_add_string(nvl, "cmd", "generate");
		nvlist_add_number(nvl, "size",
		    (uint64_t)(left > MAXSIZE ? MAXSIZE : left));
		nvl = cap_xfer_nvlist(chan, nvl, 0);
		if (nvl == NULL)
			return (-1);
		if (nvlist_get_number(nvl, "error") != 0) {
			errno = (int)nvlist_get_number(nvl, "error");
			nvlist_destroy(nvl);
			return (-1);
		}

		randbuf = nvlist_get_binary(nvl, "data", &randbufsize);
		memcpy(ptr, randbuf, randbufsize);

		nvlist_destroy(nvl);

		ptr += randbufsize;
		assert(left >= randbufsize);
		left -= randbufsize;
	}

	return (0);
}
Ejemplo n.º 5
0
static void
print_default_value(const nvlist_t *parameter, const char *type)
{
	const uint8_t *mac;
	size_t size;

	if (strcasecmp(type, "bool") == 0)
		printf(" (default = %s)",
		    nvlist_get_bool(parameter, DEFAULT_SCHEMA_NAME) ? "true" :
		    "false");
	else if (strcasecmp(type, "string") == 0)
		printf(" (default = %s)",
		    nvlist_get_string(parameter, DEFAULT_SCHEMA_NAME));
	else if (strcasecmp(type, "uint8_t") == 0)
		printf(" (default = %ju)",
		    (uintmax_t)nvlist_get_number(parameter,
		    DEFAULT_SCHEMA_NAME));
	else if (strcasecmp(type, "uint16_t") == 0)
		printf(" (default = %ju)",
		    (uintmax_t)nvlist_get_number(parameter,
		    DEFAULT_SCHEMA_NAME));
	else if (strcasecmp(type, "uint32_t") == 0)
		printf(" (default = %ju)",
		    (uintmax_t)nvlist_get_number(parameter,
		    DEFAULT_SCHEMA_NAME));
	else if (strcasecmp(type, "uint64_t") == 0)
		printf(" (default = %ju)",
		    (uintmax_t)nvlist_get_number(parameter,
		    DEFAULT_SCHEMA_NAME));
	else if (strcasecmp(type, "unicast-mac") == 0) {
		mac = nvlist_get_binary(parameter, DEFAULT_SCHEMA_NAME, &size);
		printf(" (default = %02x:%02x:%02x:%02x:%02x:%02x)", mac[0],
		    mac[1], mac[2], mac[3], mac[4], mac[5]);
	} else
		errx(1, "Unexpected type in schema: '%s'", type);
}
Ejemplo n.º 6
0
static int
sysctl_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
    nvlist_t *nvlout)
{
	const char *name;
	const void *newp;
	void *oldp;
	uint64_t operation;
	size_t oldlen, newlen;
	size_t *oldlenp;
	int error;

	if (strcmp(cmd, "sysctl") != 0)
		return (EINVAL);
	error = sysctl_check_one(nvlin, false);
	if (error != 0)
		return (error);

	name = nvlist_get_string(nvlin, "name");
	operation = nvlist_get_number(nvlin, "operation");
	if (!sysctl_allowed(limits, name, operation))
		return (ENOTCAPABLE);

	if ((operation & CAP_SYSCTL_WRITE) != 0) {
		if (!nvlist_exists_binary(nvlin, "newp"))
			return (EINVAL);
		newp = nvlist_get_binary(nvlin, "newp", &newlen);
		assert(newp != NULL && newlen > 0);
	} else {
		newp = NULL;
		newlen = 0;
	}

	if ((operation & CAP_SYSCTL_READ) != 0) {
		if (nvlist_exists_null(nvlin, "justsize")) {
			oldp = NULL;
			oldlen = 0;
			oldlenp = &oldlen;
		} else {
			if (!nvlist_exists_number(nvlin, "oldlen"))
				return (EINVAL);
			oldlen = (size_t)nvlist_get_number(nvlin, "oldlen");
			if (oldlen == 0)
				return (EINVAL);
			oldp = calloc(1, oldlen);
			if (oldp == NULL)
				return (ENOMEM);
			oldlenp = &oldlen;
		}
	} else {
		oldp = NULL;
		oldlen = 0;
		oldlenp = NULL;
	}

	if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1) {
		error = errno;
		free(oldp);
		return (error);
	}

	if ((operation & CAP_SYSCTL_READ) != 0) {
		if (nvlist_exists_null(nvlin, "justsize"))
			nvlist_add_number(nvlout, "oldlen", (uint64_t)oldlen);
		else
			nvlist_move_binary(nvlout, "oldp", oldp, oldlen);
	}

	return (0);
}
Ejemplo n.º 7
0
static int
u_sysctl_do_sysctl(struct nvlist *nvl, int ns,
    void *oldp,
    size_t *oldlenp,
    const void *newp,
    size_t newlen)
{
	nvlist_t *nvl_resp = NULL;
	int retval = 0;
	int r_errno;
	const char *rbuf;
	size_t r_len;

	/* XXX Eventually this should be in a sysctl transaction struct */
	int shm_fd = -1;
	char *shm_mem = NULL;
	size_t shm_len = 0;
	char shm_path[128];

	/* Setup request and response buffer information */

	/*
	 * If the requested size is provided and it's greater than the
	 * maximum size allowed, we'll flip to using shm
	 */
	if (oldlenp != NULL && *oldlenp >= U_SYSCTL_MAX_REQ_BUF_LEN) {
		/* Construct a shm path */
		/* XXX should make this less guessable */
		snprintf(shm_path, 128, "/sysctl.%ld", (long) arc4random());

		/* Open it */
		shm_fd = shm_open(shm_path, O_CREAT | O_RDWR, 0640);
		if (shm_fd < 0) {
			warn("shm_open (%s)", shm_path);
			retval = -1;
			goto done;
		}

		/*
		 * Calculate a mmap size that's a multiple of
		 * the system page length.
		 */
		shm_len = round_page(*oldlenp);

		/* make it that big! */
		if (ftruncate(shm_fd, shm_len) < 0) {
			warn("ftruncate");
			goto done;
		}

		/* mmap it */
		shm_mem = mmap(NULL, shm_len, PROT_READ | PROT_WRITE,
		    0, shm_fd, 0);
		if (shm_mem == NULL) {
			warn("mmap");
			goto done;
		}

		/* add the shm path to the outbound request */
		nvlist_add_string(nvl, "sysctl_respbuf_shm_path", shm_path);
		nvlist_add_number(nvl, "sysctl_respbuf_shm_len", shm_len);
	}

	/*
	 * Writing a value may pass in a NULL oldlenp, so only conditionally
	 * send it.
	 */
	if (oldlenp != NULL)
		nvlist_add_number(nvl, "sysctl_respbuf_len", *oldlenp);

	if (newlen > 0) {
		nvlist_add_binary(nvl, "sysctl_reqbuf", newp, newlen);
	}

	/* Send command */
	if (nvlist_send(ns, nvl) < 0) {
		warn("nvlist_send");
		retval = -1;
		goto done;
	}

	/* Read response */
	nvl_resp = nvlist_recv(ns);
	if (nvl_resp == NULL) {
		warn("nvlist_recv");
		retval = -1;
		goto done;
	}

	if (! nvlist_exists_number(nvl_resp, "sysctl_errno")) {
		fprintf(stderr, "response: no errno?\n");
		goto done;
	}
	r_errno = (int) nvlist_get_number(nvl_resp, "sysctl_errno");

	/* XXX validate r_len versus oldlenp */
	if (nvlist_exists_binary(nvl_resp, "sysctl_respbuf")) {
		rbuf = nvlist_get_binary(nvl_resp, "sysctl_respbuf", &r_len);
		memcpy(oldp, rbuf, r_len);
	} else if (shm_mem != NULL) {
		memcpy(oldp, shm_mem, r_len);
		r_len = nvlist_get_number(nvl_resp, "sysctl_respbuf_shm_len");
	} else if (nvlist_exists_number(nvl_resp, "sysctl_respbuf_len")) {
		r_len = nvlist_get_number(nvl_resp, "sysctl_respbuf_len");
	} else {
		r_len = 0;
	}

	if (oldlenp != NULL)
		*oldlenp = r_len;

	if (r_errno == 0) {
		retval = 0;
	} else {
		retval = -1;
		errno = r_errno;
	}

done:
	if (shm_mem != NULL)
		munmap(shm_mem, shm_len);
	if (shm_fd != -1) {
		close(shm_fd);
		shm_unlink(shm_path);
	}
	if (nvl_resp)
		nvlist_destroy(nvl_resp);
	return (retval);
}
Ejemplo n.º 8
0
static void
parent(int sock)
{
	nvlist_t *nvl;
	const nvlist_t *cnvl;
	const char *name, *cname;
	void *cookie, *ccookie;
	int type, ctype;
	size_t size;

	nvl = nvlist_recv(sock);
	CHECK(nvlist_error(nvl) == 0);
	if (nvlist_error(nvl) != 0)
		err(1, "nvlist_recv() failed");

	cookie = NULL;

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_BOOL);
	CHECK(strcmp(name, "nvlist/bool/true") == 0);
	CHECK(nvlist_get_bool(nvl, name) == true);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_BOOL);
	CHECK(strcmp(name, "nvlist/bool/false") == 0);
	CHECK(nvlist_get_bool(nvl, name) == false);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/0") == 0);
	CHECK(nvlist_get_number(nvl, name) == 0);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/1") == 0);
	CHECK(nvlist_get_number(nvl, name) == 1);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/-1") == 0);
	CHECK((int)nvlist_get_number(nvl, name) == -1);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/UINT64_MAX") == 0);
	CHECK(nvlist_get_number(nvl, name) == UINT64_MAX);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/INT64_MIN") == 0);
	CHECK((int64_t)nvlist_get_number(nvl, name) == INT64_MIN);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NUMBER);
	CHECK(strcmp(name, "nvlist/number/INT64_MAX") == 0);
	CHECK((int64_t)nvlist_get_number(nvl, name) == INT64_MAX);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_STRING);
	CHECK(strcmp(name, "nvlist/string/") == 0);
	CHECK(strcmp(nvlist_get_string(nvl, name), "") == 0);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_STRING);
	CHECK(strcmp(name, "nvlist/string/x") == 0);
	CHECK(strcmp(nvlist_get_string(nvl, name), "x") == 0);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_STRING);
	CHECK(strcmp(name, "nvlist/string/abcdefghijklmnopqrstuvwxyz") == 0);
	CHECK(strcmp(nvlist_get_string(nvl, name), "abcdefghijklmnopqrstuvwxyz") == 0);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_DESCRIPTOR);
	CHECK(strcmp(name, "nvlist/descriptor/STDERR_FILENO") == 0);
	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, name)));

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_BINARY);
	CHECK(strcmp(name, "nvlist/binary/x") == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, name, NULL), "x", 1) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, name, &size), "x", 1) == 0);
	CHECK(size == 1);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_BINARY);
	CHECK(strcmp(name, "nvlist/binary/abcdefghijklmnopqrstuvwxyz") == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, name, NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, name, &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name != NULL);
	CHECK(type == NV_TYPE_NVLIST);
	CHECK(strcmp(name, "nvlist/nvlist") == 0);
	cnvl = nvlist_get_nvlist(nvl, name);

	ccookie = NULL;

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_BOOL);
	CHECK(strcmp(cname, "nvlist/bool/true") == 0);
	CHECK(nvlist_get_bool(cnvl, cname) == true);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_BOOL);
	CHECK(strcmp(cname, "nvlist/bool/false") == 0);
	CHECK(nvlist_get_bool(cnvl, cname) == false);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/0") == 0);
	CHECK(nvlist_get_number(cnvl, cname) == 0);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/1") == 0);
	CHECK(nvlist_get_number(cnvl, cname) == 1);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/-1") == 0);
	CHECK((int)nvlist_get_number(cnvl, cname) == -1);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/UINT64_MAX") == 0);
	CHECK(nvlist_get_number(cnvl, cname) == UINT64_MAX);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/INT64_MIN") == 0);
	CHECK((int64_t)nvlist_get_number(cnvl, cname) == INT64_MIN);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_NUMBER);
	CHECK(strcmp(cname, "nvlist/number/INT64_MAX") == 0);
	CHECK((int64_t)nvlist_get_number(cnvl, cname) == INT64_MAX);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_STRING);
	CHECK(strcmp(cname, "nvlist/string/") == 0);
	CHECK(strcmp(nvlist_get_string(cnvl, cname), "") == 0);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_STRING);
	CHECK(strcmp(cname, "nvlist/string/x") == 0);
	CHECK(strcmp(nvlist_get_string(cnvl, cname), "x") == 0);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_STRING);
	CHECK(strcmp(cname, "nvlist/string/abcdefghijklmnopqrstuvwxyz") == 0);
	CHECK(strcmp(nvlist_get_string(cnvl, cname), "abcdefghijklmnopqrstuvwxyz") == 0);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_DESCRIPTOR);
	CHECK(strcmp(cname, "nvlist/descriptor/STDERR_FILENO") == 0);
	CHECK(fd_is_valid(nvlist_get_descriptor(cnvl, cname)));

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_BINARY);
	CHECK(strcmp(cname, "nvlist/binary/x") == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, cname, NULL), "x", 1) == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, cname, &size), "x", 1) == 0);
	CHECK(size == 1);

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname != NULL);
	CHECK(ctype == NV_TYPE_BINARY);
	CHECK(strcmp(cname, "nvlist/binary/abcdefghijklmnopqrstuvwxyz") == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, cname, NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, cname, &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));

	cname = nvlist_next(cnvl, &ctype, &ccookie);
	CHECK(cname == NULL);

	name = nvlist_next(nvl, &type, &cookie);
	CHECK(name == NULL);
}
Ejemplo n.º 9
0
int
main(void)
{
	const nvlist_t *cnvl;
	nvlist_t *nvl;
	size_t size;

	printf("1..83\n");

	nvl = nvlist_create(0);

	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true"));
	nvlist_add_bool(nvl, "nvlist/bool/true", true);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);

	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false"));
	nvlist_add_bool(nvl, "nvlist/bool/false", false);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/0"));
	nvlist_add_number(nvl, "nvlist/number/0", 0);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/1"));
	nvlist_add_number(nvl, "nvlist/number/1", 1);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1"));
	nvlist_add_number(nvl, "nvlist/number/-1", -1);
	CHECK(nvlist_error(nvl) == 0);
	CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
	nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
	nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
	CHECK(nvlist_error(nvl) == 0);
	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);

	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
	nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
	CHECK(nvlist_error(nvl) == 0);
	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);

	CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
	nvlist_add_string(nvl, "nvlist/string/", "");
	CHECK(nvlist_error(nvl) == 0);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);

	CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
	nvlist_add_string(nvl, "nvlist/string/x", "x");
	CHECK(nvlist_error(nvl) == 0);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);

	CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
	nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
	CHECK(nvlist_error(nvl) == 0);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);

	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));

	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
	CHECK(nvlist_error(nvl) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
	CHECK(size == 1);

	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
	CHECK(nvlist_error(nvl) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));

	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
	CHECK(nvlist_error(nvl) == 0);
	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/true") == true);
	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/false") == false);
	CHECK(nvlist_get_number(cnvl, "nvlist/number/0") == 0);
	CHECK(nvlist_get_number(cnvl, "nvlist/number/1") == 1);
	CHECK((int)nvlist_get_number(cnvl, "nvlist/number/-1") == -1);
	CHECK(nvlist_get_number(cnvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MIN") == INT64_MIN);
	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MAX") == INT64_MAX);
	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/"), "") == 0);
	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/x"), "x") == 0);
	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
	/* TODO */
	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", NULL), "x", 1) == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", &size), "x", 1) == 0);
	CHECK(size == 1);
	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));

	CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
	CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
	CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
	CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
	CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
	CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
	CHECK(size == 1);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));

	nvlist_destroy(nvl);

	return (0);
}