1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25168404Spjd
26168404Spjd#include <errno.h>
27168404Spjd#include <libgen.h>
28168404Spjd#include <libintl.h>
29168404Spjd#include <stdio.h>
30168404Spjd#include <stdlib.h>
31168404Spjd#include <strings.h>
32168404Spjd
33168404Spjd#include "zpool_util.h"
34168404Spjd
35168404Spjd/*
36168404Spjd * Utility function to guarantee malloc() success.
37168404Spjd */
38168404Spjdvoid *
39168404Spjdsafe_malloc(size_t size)
40168404Spjd{
41168404Spjd	void *data;
42168404Spjd
43168404Spjd	if ((data = calloc(1, size)) == NULL) {
44168404Spjd		(void) fprintf(stderr, "internal error: out of memory\n");
45168404Spjd		exit(1);
46168404Spjd	}
47168404Spjd
48168404Spjd	return (data);
49168404Spjd}
50168404Spjd
51168404Spjd/*
52168404Spjd * Display an out of memory error message and abort the current program.
53168404Spjd */
54168404Spjdvoid
55168404Spjdzpool_no_memory(void)
56168404Spjd{
57168404Spjd	assert(errno == ENOMEM);
58168404Spjd	(void) fprintf(stderr,
59168404Spjd	    gettext("internal error: out of memory\n"));
60168404Spjd	exit(1);
61168404Spjd}
62185029Spjd
63185029Spjd/*
64185029Spjd * Return the number of logs in supplied nvlist
65185029Spjd */
66185029Spjduint_t
67185029Spjdnum_logs(nvlist_t *nv)
68185029Spjd{
69185029Spjd	uint_t nlogs = 0;
70185029Spjd	uint_t c, children;
71185029Spjd	nvlist_t **child;
72185029Spjd
73185029Spjd	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
74185029Spjd	    &child, &children) != 0)
75185029Spjd		return (0);
76185029Spjd
77185029Spjd	for (c = 0; c < children; c++) {
78185029Spjd		uint64_t is_log = B_FALSE;
79185029Spjd
80185029Spjd		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
81185029Spjd		    &is_log);
82185029Spjd		if (is_log)
83185029Spjd			nlogs++;
84185029Spjd	}
85185029Spjd	return (nlogs);
86185029Spjd}
87