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