xref: /titanic_41/usr/src/cmd/lvm/rpc.metamhd/mhd_error.c (revision a4dd1f3517267c5e5aa5b2bb53cb388002bc688f)
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 2003 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 "mhd_local.h"
30 
31 #include <syslog.h>
32 
33 /*
34  * debug stuff
35  */
36 #ifdef	MHD_DEBUG
37 int	mhd_debug = MHD_DEBUG;
38 #endif
39 
40 /*
41  * free and clear error
42  */
43 void
44 mhd_clrerror(
45 	mhd_error_t	*mhep
46 )
47 {
48 	if (mhep->name != NULL)
49 		Free(mhep->name);
50 	(void) memset(mhep, 0, sizeof (*mhep));
51 }
52 
53 /*
54  * setup error
55  */
56 int
57 mhd_error(
58 	mhd_error_t	*mhep,
59 	int		errnum,
60 	char		*name
61 )
62 {
63 	mhd_clrerror(mhep);
64 	if (errnum != 0) {
65 		mhep->errnum = errnum;
66 		if (name != NULL)
67 			mhep->name = Strdup(name);
68 		return (-1);
69 	}
70 	return (0);
71 }
72 
73 /*
74  * mhd_error_t to string
75  */
76 static char *
77 mhd_strerror(
78 	mhd_error_t	*mhep
79 )
80 {
81 	static char	buf[1024];
82 	char		*emsg;
83 
84 	switch (mhep->errnum) {
85 	case MHD_E_MAJORITY:
86 		return ("could not get any reservations");
87 	case MHD_E_RESERVED:
88 		return ("disk is reserved");
89 	default:
90 		if ((emsg = strerror(mhep->errnum)) != NULL)
91 			return (emsg);
92 		(void) sprintf(buf, "errno %d out of range", errno);
93 		return (buf);
94 	}
95 }
96 
97 /*
98  * printf-like log
99  */
100 static void
101 mhd_vprintf(
102 	const char	*fmt,
103 	va_list		ap
104 )
105 {
106 	if (isatty(fileno(stderr))) {
107 		static mutex_t	stderr_mx = DEFAULTMUTEX;
108 
109 		mhd_mx_lock(&stderr_mx);
110 		(void) vfprintf(stderr, fmt, ap);
111 		(void) fflush(stderr);
112 		(void) fsync(fileno(stderr));
113 		mhd_mx_unlock(&stderr_mx);
114 	}
115 	vsyslog(LOG_ERR, fmt, ap);
116 }
117 
118 /*PRINTFLIKE1*/
119 void
120 mhd_eprintf(
121 	const char	*fmt,
122 	...
123 )
124 {
125 	va_list		ap;
126 
127 	va_start(ap, fmt);
128 	mhd_vprintf(fmt, ap);
129 	va_end(ap);
130 }
131 
132 /*
133  * printf-like perror() log
134  */
135 /*PRINTFLIKE2*/
136 static void
137 mhd_vperror(
138 	mhd_error_t	*mhep,
139 	const char	*fmt,
140 	va_list		ap
141 )
142 {
143 	char		buf[1024];
144 	char		*p = buf;
145 	size_t		len = sizeof (buf);
146 	int		n;
147 
148 	if ((mhep->name != NULL) && (mhep->name[0] != '\0')) {
149 		n = snprintf(p, len, "%s: ", mhep->name);
150 		p += n;
151 		len -= n;
152 	}
153 	if ((fmt != NULL) && (*fmt != '\0')) {
154 		n = vsnprintf(p, len, fmt, ap);
155 		p += n;
156 		len -= n;
157 		n = snprintf(p, len, ": ");
158 		p += n;
159 		len -= n;
160 	}
161 	(void) snprintf(p, len, "%s", mhd_strerror(mhep));
162 	mhd_eprintf("%s\n", buf);
163 }
164 
165 /*PRINTFLIKE2*/
166 void
167 mhde_perror(
168 	mhd_error_t	*mhep,
169 	const char	*fmt,
170 	...
171 )
172 {
173 	va_list		ap;
174 
175 	va_start(ap, fmt);
176 	mhd_vperror(mhep, fmt, ap);
177 	va_end(ap);
178 }
179 
180 /*PRINTFLIKE1*/
181 void
182 mhd_perror(
183 	const char	*fmt,
184 	...
185 )
186 {
187 	va_list		ap;
188 	mhd_error_t	status = mhd_null_error;
189 
190 	(void) mhd_error(&status, errno, NULL);
191 	va_start(ap, fmt);
192 	mhd_vperror(&status, fmt, ap);
193 	va_end(ap);
194 	mhd_clrerror(&status);
195 }
196