xref: /titanic_51/usr/src/cmd/zpool/zpool_util.c (revision c67d9675bbc8392fe45f3a7dfbda1ad4daa1eb07)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5fa9e4066Sahrens  * Common Development and Distribution License, Version 1.0 only
6fa9e4066Sahrens  * (the "License").  You may not use this file except in compliance
7fa9e4066Sahrens  * with the License.
8fa9e4066Sahrens  *
9fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
11fa9e4066Sahrens  * See the License for the specific language governing permissions
12fa9e4066Sahrens  * and limitations under the License.
13fa9e4066Sahrens  *
14fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19fa9e4066Sahrens  *
20fa9e4066Sahrens  * CDDL HEADER END
21fa9e4066Sahrens  */
22fa9e4066Sahrens /*
23*c67d9675Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <errno.h>
30fa9e4066Sahrens #include <libgen.h>
31fa9e4066Sahrens #include <libintl.h>
32fa9e4066Sahrens #include <stdio.h>
33fa9e4066Sahrens #include <stdlib.h>
34fa9e4066Sahrens #include <strings.h>
35fa9e4066Sahrens 
36fa9e4066Sahrens #include "zpool_util.h"
37fa9e4066Sahrens 
38fa9e4066Sahrens /*
39fa9e4066Sahrens  * Utility function to guarantee malloc() success.
40fa9e4066Sahrens  */
41fa9e4066Sahrens void *
42fa9e4066Sahrens safe_malloc(size_t size)
43fa9e4066Sahrens {
44fa9e4066Sahrens 	void *data;
45fa9e4066Sahrens 
46fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL) {
47fa9e4066Sahrens 		(void) fprintf(stderr, "internal error: out of memory\n");
48fa9e4066Sahrens 		exit(1);
49fa9e4066Sahrens 	}
50fa9e4066Sahrens 
51fa9e4066Sahrens 	return (data);
52fa9e4066Sahrens }
53fa9e4066Sahrens 
54fa9e4066Sahrens /*
55fa9e4066Sahrens  * Same as above, but for strdup()
56fa9e4066Sahrens  */
57fa9e4066Sahrens char *
58fa9e4066Sahrens safe_strdup(const char *str)
59fa9e4066Sahrens {
60fa9e4066Sahrens 	char *ret;
61fa9e4066Sahrens 
62fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL) {
63fa9e4066Sahrens 		(void) fprintf(stderr, "internal error: out of memory\n");
64fa9e4066Sahrens 		exit(1);
65fa9e4066Sahrens 	}
66fa9e4066Sahrens 
67fa9e4066Sahrens 	return (ret);
68fa9e4066Sahrens }
69fa9e4066Sahrens 
70fa9e4066Sahrens /*
71fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
72fa9e4066Sahrens  */
73fa9e4066Sahrens void
74fa9e4066Sahrens no_memory(void)
75fa9e4066Sahrens {
76fa9e4066Sahrens 	assert(errno == ENOMEM);
77fa9e4066Sahrens 	(void) fprintf(stderr,
78fa9e4066Sahrens 	    gettext("internal error: out of memory\n"));
79fa9e4066Sahrens 	exit(1);
80fa9e4066Sahrens }
81