xref: /illumos-gate/usr/src/cmd/zpool/zpool_util.c (revision dd50e0cc)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
55ad82045Snd  * Common Development and Distribution License (the "License").
65ad82045Snd  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2226fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <errno.h>
27fa9e4066Sahrens #include <libgen.h>
28fa9e4066Sahrens #include <libintl.h>
29fa9e4066Sahrens #include <stdio.h>
30fa9e4066Sahrens #include <stdlib.h>
31fa9e4066Sahrens #include <strings.h>
32*dd50e0ccSTony Hutter #include <ctype.h>
33*dd50e0ccSTony Hutter #include <sys/sysmacros.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens #include "zpool_util.h"
36fa9e4066Sahrens 
37fa9e4066Sahrens /*
38fa9e4066Sahrens  * Utility function to guarantee malloc() success.
39fa9e4066Sahrens  */
40fa9e4066Sahrens void *
safe_malloc(size_t size)41fa9e4066Sahrens safe_malloc(size_t size)
42fa9e4066Sahrens {
43fa9e4066Sahrens 	void *data;
44fa9e4066Sahrens 
45fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL) {
46fa9e4066Sahrens 		(void) fprintf(stderr, "internal error: out of memory\n");
47fa9e4066Sahrens 		exit(1);
48fa9e4066Sahrens 	}
49fa9e4066Sahrens 
50fa9e4066Sahrens 	return (data);
51fa9e4066Sahrens }
52fa9e4066Sahrens 
53fa9e4066Sahrens /*
54fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
55fa9e4066Sahrens  */
56fa9e4066Sahrens void
zpool_no_memory(void)575ad82045Snd zpool_no_memory(void)
58fa9e4066Sahrens {
59fa9e4066Sahrens 	assert(errno == ENOMEM);
60fa9e4066Sahrens 	(void) fprintf(stderr,
61fa9e4066Sahrens 	    gettext("internal error: out of memory\n"));
62fa9e4066Sahrens 	exit(1);
63fa9e4066Sahrens }
648654d025Sperrin 
658654d025Sperrin /*
668654d025Sperrin  * Return the number of logs in supplied nvlist
678654d025Sperrin  */
688654d025Sperrin uint_t
num_logs(nvlist_t * nv)698654d025Sperrin num_logs(nvlist_t *nv)
708654d025Sperrin {
718654d025Sperrin 	uint_t nlogs = 0;
728654d025Sperrin 	uint_t c, children;
738654d025Sperrin 	nvlist_t **child;
748654d025Sperrin 
758654d025Sperrin 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
768654d025Sperrin 	    &child, &children) != 0)
778654d025Sperrin 		return (0);
788654d025Sperrin 
798654d025Sperrin 	for (c = 0; c < children; c++) {
808654d025Sperrin 		uint64_t is_log = B_FALSE;
818654d025Sperrin 
828654d025Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
838654d025Sperrin 		    &is_log);
848654d025Sperrin 		if (is_log)
858654d025Sperrin 			nlogs++;
868654d025Sperrin 	}
878654d025Sperrin 	return (nlogs);
888654d025Sperrin }
89*dd50e0ccSTony Hutter 
90*dd50e0ccSTony Hutter /* Find the max element in an array of uint64_t values */
91*dd50e0ccSTony Hutter uint64_t
array64_max(uint64_t array[],unsigned int len)92*dd50e0ccSTony Hutter array64_max(uint64_t array[], unsigned int len)
93*dd50e0ccSTony Hutter {
94*dd50e0ccSTony Hutter 	uint64_t max = 0;
95*dd50e0ccSTony Hutter 	int i;
96*dd50e0ccSTony Hutter 	for (i = 0; i < len; i++)
97*dd50e0ccSTony Hutter 		max = MAX(max, array[i]);
98*dd50e0ccSTony Hutter 
99*dd50e0ccSTony Hutter 	return (max);
100*dd50e0ccSTony Hutter }
101*dd50e0ccSTony Hutter 
102*dd50e0ccSTony Hutter /*
103*dd50e0ccSTony Hutter  * Return 1 if "str" is a number string, 0 otherwise.  Works for integer and
104*dd50e0ccSTony Hutter  * floating point numbers.
105*dd50e0ccSTony Hutter  */
106*dd50e0ccSTony Hutter int
isnumber(char * str)107*dd50e0ccSTony Hutter isnumber(char *str)
108*dd50e0ccSTony Hutter {
109*dd50e0ccSTony Hutter 	for (; *str; str++)
110*dd50e0ccSTony Hutter 		if (!(isdigit(*str) || (*str == '.')))
111*dd50e0ccSTony Hutter 			return (0);
112*dd50e0ccSTony Hutter 
113*dd50e0ccSTony Hutter 	return (1);
114*dd50e0ccSTony Hutter }
115