xref: /onnv-gate/usr/src/cmd/zpool/zpool_util.c (revision 10594:986cb68d2347)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
52856Snd150628  * Common Development and Distribution License (the "License").
62856Snd150628  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*10265SKrishnendu.Sadhukhan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <errno.h>
27789Sahrens #include <libgen.h>
28789Sahrens #include <libintl.h>
29789Sahrens #include <stdio.h>
30789Sahrens #include <stdlib.h>
31789Sahrens #include <strings.h>
32789Sahrens 
33789Sahrens #include "zpool_util.h"
34789Sahrens 
35789Sahrens /*
36789Sahrens  * Utility function to guarantee malloc() success.
37789Sahrens  */
38789Sahrens void *
safe_malloc(size_t size)39789Sahrens safe_malloc(size_t size)
40789Sahrens {
41789Sahrens 	void *data;
42789Sahrens 
43789Sahrens 	if ((data = calloc(1, size)) == NULL) {
44789Sahrens 		(void) fprintf(stderr, "internal error: out of memory\n");
45789Sahrens 		exit(1);
46789Sahrens 	}
47789Sahrens 
48789Sahrens 	return (data);
49789Sahrens }
50789Sahrens 
51789Sahrens /*
52789Sahrens  * Display an out of memory error message and abort the current program.
53789Sahrens  */
54789Sahrens void
zpool_no_memory(void)552856Snd150628 zpool_no_memory(void)
56789Sahrens {
57789Sahrens 	assert(errno == ENOMEM);
58789Sahrens 	(void) fprintf(stderr,
59789Sahrens 	    gettext("internal error: out of memory\n"));
60789Sahrens 	exit(1);
61789Sahrens }
624527Sperrin 
634527Sperrin /*
644527Sperrin  * Return the number of logs in supplied nvlist
654527Sperrin  */
664527Sperrin uint_t
num_logs(nvlist_t * nv)674527Sperrin num_logs(nvlist_t *nv)
684527Sperrin {
694527Sperrin 	uint_t nlogs = 0;
704527Sperrin 	uint_t c, children;
714527Sperrin 	nvlist_t **child;
724527Sperrin 
734527Sperrin 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
744527Sperrin 	    &child, &children) != 0)
754527Sperrin 		return (0);
764527Sperrin 
774527Sperrin 	for (c = 0; c < children; c++) {
784527Sperrin 		uint64_t is_log = B_FALSE;
794527Sperrin 
804527Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
814527Sperrin 		    &is_log);
824527Sperrin 		if (is_log)
834527Sperrin 			nlogs++;
844527Sperrin 	}
854527Sperrin 	return (nlogs);
864527Sperrin }
87