xref: /onnv-gate/usr/src/cmd/lvm/rpc.metamhd/mhd_error.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "mhd_local.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <syslog.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * debug stuff
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate #ifdef	MHD_DEBUG
37*0Sstevel@tonic-gate int	mhd_debug = MHD_DEBUG;
38*0Sstevel@tonic-gate #endif
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * free and clear error
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate void
mhd_clrerror(mhd_error_t * mhep)44*0Sstevel@tonic-gate mhd_clrerror(
45*0Sstevel@tonic-gate 	mhd_error_t	*mhep
46*0Sstevel@tonic-gate )
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	if (mhep->name != NULL)
49*0Sstevel@tonic-gate 		Free(mhep->name);
50*0Sstevel@tonic-gate 	(void) memset(mhep, 0, sizeof (*mhep));
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * setup error
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate int
mhd_error(mhd_error_t * mhep,int errnum,char * name)57*0Sstevel@tonic-gate mhd_error(
58*0Sstevel@tonic-gate 	mhd_error_t	*mhep,
59*0Sstevel@tonic-gate 	int		errnum,
60*0Sstevel@tonic-gate 	char		*name
61*0Sstevel@tonic-gate )
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	mhd_clrerror(mhep);
64*0Sstevel@tonic-gate 	if (errnum != 0) {
65*0Sstevel@tonic-gate 		mhep->errnum = errnum;
66*0Sstevel@tonic-gate 		if (name != NULL)
67*0Sstevel@tonic-gate 			mhep->name = Strdup(name);
68*0Sstevel@tonic-gate 		return (-1);
69*0Sstevel@tonic-gate 	}
70*0Sstevel@tonic-gate 	return (0);
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * mhd_error_t to string
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate static char *
mhd_strerror(mhd_error_t * mhep)77*0Sstevel@tonic-gate mhd_strerror(
78*0Sstevel@tonic-gate 	mhd_error_t	*mhep
79*0Sstevel@tonic-gate )
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	static char	buf[1024];
82*0Sstevel@tonic-gate 	char		*emsg;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	switch (mhep->errnum) {
85*0Sstevel@tonic-gate 	case MHD_E_MAJORITY:
86*0Sstevel@tonic-gate 		return ("could not get any reservations");
87*0Sstevel@tonic-gate 	case MHD_E_RESERVED:
88*0Sstevel@tonic-gate 		return ("disk is reserved");
89*0Sstevel@tonic-gate 	default:
90*0Sstevel@tonic-gate 		if ((emsg = strerror(mhep->errnum)) != NULL)
91*0Sstevel@tonic-gate 			return (emsg);
92*0Sstevel@tonic-gate 		(void) sprintf(buf, "errno %d out of range", errno);
93*0Sstevel@tonic-gate 		return (buf);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * printf-like log
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate static void
mhd_vprintf(const char * fmt,va_list ap)101*0Sstevel@tonic-gate mhd_vprintf(
102*0Sstevel@tonic-gate 	const char	*fmt,
103*0Sstevel@tonic-gate 	va_list		ap
104*0Sstevel@tonic-gate )
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	if (isatty(fileno(stderr))) {
107*0Sstevel@tonic-gate 		static mutex_t	stderr_mx = DEFAULTMUTEX;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		mhd_mx_lock(&stderr_mx);
110*0Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
111*0Sstevel@tonic-gate 		(void) fflush(stderr);
112*0Sstevel@tonic-gate 		(void) fsync(fileno(stderr));
113*0Sstevel@tonic-gate 		mhd_mx_unlock(&stderr_mx);
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	vsyslog(LOG_ERR, fmt, ap);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*PRINTFLIKE1*/
119*0Sstevel@tonic-gate void
mhd_eprintf(const char * fmt,...)120*0Sstevel@tonic-gate mhd_eprintf(
121*0Sstevel@tonic-gate 	const char	*fmt,
122*0Sstevel@tonic-gate 	...
123*0Sstevel@tonic-gate )
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	va_list		ap;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	va_start(ap, fmt);
128*0Sstevel@tonic-gate 	mhd_vprintf(fmt, ap);
129*0Sstevel@tonic-gate 	va_end(ap);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate  * printf-like perror() log
134*0Sstevel@tonic-gate  */
135*0Sstevel@tonic-gate /*PRINTFLIKE2*/
136*0Sstevel@tonic-gate static void
mhd_vperror(mhd_error_t * mhep,const char * fmt,va_list ap)137*0Sstevel@tonic-gate mhd_vperror(
138*0Sstevel@tonic-gate 	mhd_error_t	*mhep,
139*0Sstevel@tonic-gate 	const char	*fmt,
140*0Sstevel@tonic-gate 	va_list		ap
141*0Sstevel@tonic-gate )
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	char		buf[1024];
144*0Sstevel@tonic-gate 	char		*p = buf;
145*0Sstevel@tonic-gate 	size_t		len = sizeof (buf);
146*0Sstevel@tonic-gate 	int		n;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if ((mhep->name != NULL) && (mhep->name[0] != '\0')) {
149*0Sstevel@tonic-gate 		n = snprintf(p, len, "%s: ", mhep->name);
150*0Sstevel@tonic-gate 		p += n;
151*0Sstevel@tonic-gate 		len -= n;
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 	if ((fmt != NULL) && (*fmt != '\0')) {
154*0Sstevel@tonic-gate 		n = vsnprintf(p, len, fmt, ap);
155*0Sstevel@tonic-gate 		p += n;
156*0Sstevel@tonic-gate 		len -= n;
157*0Sstevel@tonic-gate 		n = snprintf(p, len, ": ");
158*0Sstevel@tonic-gate 		p += n;
159*0Sstevel@tonic-gate 		len -= n;
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 	(void) snprintf(p, len, "%s", mhd_strerror(mhep));
162*0Sstevel@tonic-gate 	mhd_eprintf("%s\n", buf);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate /*PRINTFLIKE2*/
166*0Sstevel@tonic-gate void
mhde_perror(mhd_error_t * mhep,const char * fmt,...)167*0Sstevel@tonic-gate mhde_perror(
168*0Sstevel@tonic-gate 	mhd_error_t	*mhep,
169*0Sstevel@tonic-gate 	const char	*fmt,
170*0Sstevel@tonic-gate 	...
171*0Sstevel@tonic-gate )
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	va_list		ap;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	va_start(ap, fmt);
176*0Sstevel@tonic-gate 	mhd_vperror(mhep, fmt, ap);
177*0Sstevel@tonic-gate 	va_end(ap);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate /*PRINTFLIKE1*/
181*0Sstevel@tonic-gate void
mhd_perror(const char * fmt,...)182*0Sstevel@tonic-gate mhd_perror(
183*0Sstevel@tonic-gate 	const char	*fmt,
184*0Sstevel@tonic-gate 	...
185*0Sstevel@tonic-gate )
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	va_list		ap;
188*0Sstevel@tonic-gate 	mhd_error_t	status = mhd_null_error;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	(void) mhd_error(&status, errno, NULL);
191*0Sstevel@tonic-gate 	va_start(ap, fmt);
192*0Sstevel@tonic-gate 	mhd_vperror(&status, fmt, ap);
193*0Sstevel@tonic-gate 	va_end(ap);
194*0Sstevel@tonic-gate 	mhd_clrerror(&status);
195*0Sstevel@tonic-gate }
196