xref: /titanic_44/usr/src/cmd/zpool/zpool_util.c (revision c138f478d2bc94e73ab8f6a084e323bec25e62f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <errno.h>
30 #include <libgen.h>
31 #include <libintl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 
36 #include "zpool_util.h"
37 
38 /*
39  * Utility function to guarantee malloc() success.
40  */
41 void *
42 safe_malloc(size_t size)
43 {
44 	void *data;
45 
46 	if ((data = calloc(1, size)) == NULL) {
47 		(void) fprintf(stderr, "internal error: out of memory\n");
48 		exit(1);
49 	}
50 
51 	return (data);
52 }
53 
54 /*
55  * Same as above, but for strdup()
56  */
57 char *
58 safe_strdup(const char *str)
59 {
60 	char *ret;
61 
62 	if ((ret = strdup(str)) == NULL) {
63 		(void) fprintf(stderr, "internal error: out of memory\n");
64 		exit(1);
65 	}
66 
67 	return (ret);
68 }
69 
70 /*
71  * Display an out of memory error message and abort the current program.
72  */
73 void
74 no_memory(void)
75 {
76 	assert(errno == ENOMEM);
77 	(void) fprintf(stderr,
78 	    gettext("internal error: out of memory\n"));
79 	exit(1);
80 }
81 
82 /*
83  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
84  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
85  * We also check if this is a whole disk, in which case we strip off the
86  * trailing 's0' slice name.
87  */
88 char *
89 vdev_get_name(nvlist_t *nv)
90 {
91 	char *path;
92 	uint64_t wholedisk;
93 
94 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
95 
96 		if (strncmp(path, "/dev/dsk/", 9) == 0)
97 			path += 9;
98 
99 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
100 		    &wholedisk) == 0 && wholedisk) {
101 			char *tmp = safe_strdup(path);
102 			tmp[strlen(path) - 2] = '\0';
103 			return (tmp);
104 		}
105 	} else {
106 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
107 	}
108 
109 	return (safe_strdup(path));
110 }
111