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 "med_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 #define MED_DEBUG 0
37*0Sstevel@tonic-gate #ifdef MED_DEBUG
38*0Sstevel@tonic-gate static int med_debug = MED_DEBUG;
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate * free and clear error
43*0Sstevel@tonic-gate */
44*0Sstevel@tonic-gate static void
med_clrerror(med_err_t * medep)45*0Sstevel@tonic-gate med_clrerror(
46*0Sstevel@tonic-gate med_err_t *medep
47*0Sstevel@tonic-gate )
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate if (medep->med_node != NULL)
50*0Sstevel@tonic-gate Free(medep->med_node);
51*0Sstevel@tonic-gate if (medep->med_misc != NULL)
52*0Sstevel@tonic-gate Free(medep->med_misc);
53*0Sstevel@tonic-gate (void) memset(medep, 0, sizeof (*medep));
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate * Exported Entry Points
58*0Sstevel@tonic-gate */
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate * setup error
62*0Sstevel@tonic-gate */
63*0Sstevel@tonic-gate int
med_error(med_err_t * medep,int errnum,char * misc)64*0Sstevel@tonic-gate med_error(
65*0Sstevel@tonic-gate med_err_t *medep,
66*0Sstevel@tonic-gate int errnum,
67*0Sstevel@tonic-gate char *misc
68*0Sstevel@tonic-gate )
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate med_clrerror(medep);
71*0Sstevel@tonic-gate if (errnum != 0) {
72*0Sstevel@tonic-gate medep->med_errno = errnum;
73*0Sstevel@tonic-gate if (med_debug && misc != NULL)
74*0Sstevel@tonic-gate medep->med_misc = Strdup(misc);
75*0Sstevel@tonic-gate medep->med_node = Strdup(mynode());
76*0Sstevel@tonic-gate return (-1);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate return (0);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate * med_err_t to string
83*0Sstevel@tonic-gate */
84*0Sstevel@tonic-gate static char *
med_strerror(med_err_t * medep)85*0Sstevel@tonic-gate med_strerror(
86*0Sstevel@tonic-gate med_err_t *medep
87*0Sstevel@tonic-gate )
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate static char buf[1024];
90*0Sstevel@tonic-gate char *p = buf;
91*0Sstevel@tonic-gate char *emsg;
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate if (medep->med_errno < 0) {
94*0Sstevel@tonic-gate if ((emsg = med_errnum_to_str(medep->med_errno)) != NULL)
95*0Sstevel@tonic-gate return (emsg);
96*0Sstevel@tonic-gate (void) sprintf(p,
97*0Sstevel@tonic-gate "unknown mediator errno %d\n", medep->med_errno);
98*0Sstevel@tonic-gate return (buf);
99*0Sstevel@tonic-gate } else {
100*0Sstevel@tonic-gate if ((emsg = strerror(medep->med_errno)) != NULL)
101*0Sstevel@tonic-gate return (emsg);
102*0Sstevel@tonic-gate (void) sprintf(p,
103*0Sstevel@tonic-gate "errno %d out of range", medep->med_errno);
104*0Sstevel@tonic-gate return (buf);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate * printf-like log
110*0Sstevel@tonic-gate */
111*0Sstevel@tonic-gate static void
med_vprintf(const char * fmt,va_list ap)112*0Sstevel@tonic-gate med_vprintf(
113*0Sstevel@tonic-gate const char *fmt,
114*0Sstevel@tonic-gate va_list ap
115*0Sstevel@tonic-gate )
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate if (isatty(fileno(stderr))) {
118*0Sstevel@tonic-gate #ifdef _REENTRANT
119*0Sstevel@tonic-gate static mutex_t stderr_mx = DEFAULTMUTEX;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate med_mx_lock(&stderr_mx);
122*0Sstevel@tonic-gate #endif /* _REENTRANT */
123*0Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
124*0Sstevel@tonic-gate (void) fflush(stderr);
125*0Sstevel@tonic-gate (void) fsync(fileno(stderr));
126*0Sstevel@tonic-gate #ifdef _REENTRANT
127*0Sstevel@tonic-gate med_mx_unlock(&stderr_mx);
128*0Sstevel@tonic-gate #endif /* _REENTRANT */
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate vsyslog(LOG_ERR, fmt, ap);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate /*PRINTFLIKE1*/
134*0Sstevel@tonic-gate void
med_eprintf(const char * fmt,...)135*0Sstevel@tonic-gate med_eprintf(
136*0Sstevel@tonic-gate const char *fmt,
137*0Sstevel@tonic-gate ...
138*0Sstevel@tonic-gate )
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate va_list ap;
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate va_start(ap, fmt);
143*0Sstevel@tonic-gate med_vprintf(fmt, ap);
144*0Sstevel@tonic-gate va_end(ap);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate * printf-like perror() log
149*0Sstevel@tonic-gate */
150*0Sstevel@tonic-gate /*PRINTFLIKE2*/
151*0Sstevel@tonic-gate static void
med_vperror(med_err_t * medep,const char * fmt,va_list ap)152*0Sstevel@tonic-gate med_vperror(
153*0Sstevel@tonic-gate med_err_t *medep,
154*0Sstevel@tonic-gate const char *fmt,
155*0Sstevel@tonic-gate va_list ap
156*0Sstevel@tonic-gate )
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate char buf[1024];
159*0Sstevel@tonic-gate char *p = buf;
160*0Sstevel@tonic-gate size_t len = sizeof (buf);
161*0Sstevel@tonic-gate int n;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate if ((medep->med_node != NULL) && (medep->med_node[0] != '\0')) {
164*0Sstevel@tonic-gate n = snprintf(p, len, "%s: ", medep->med_node);
165*0Sstevel@tonic-gate p += n;
166*0Sstevel@tonic-gate len -= n;
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate if ((medep->med_misc != NULL) && (medep->med_misc[0] != '\0')) {
169*0Sstevel@tonic-gate n = snprintf(p, len, "%s: ", medep->med_misc);
170*0Sstevel@tonic-gate p += n;
171*0Sstevel@tonic-gate len -= n;
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate if ((fmt != NULL) && (*fmt != '\0')) {
174*0Sstevel@tonic-gate n = vsnprintf(p, len, fmt, ap);
175*0Sstevel@tonic-gate p += n;
176*0Sstevel@tonic-gate len -= n;
177*0Sstevel@tonic-gate n = snprintf(p, len, ": ");
178*0Sstevel@tonic-gate p += n;
179*0Sstevel@tonic-gate len -= n;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate (void) snprintf(p, len, "%s", med_strerror(medep));
182*0Sstevel@tonic-gate med_eprintf("%s\n", buf);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*PRINTFLIKE2*/
186*0Sstevel@tonic-gate void
medde_perror(med_err_t * medep,const char * fmt,...)187*0Sstevel@tonic-gate medde_perror(
188*0Sstevel@tonic-gate med_err_t *medep,
189*0Sstevel@tonic-gate const char *fmt,
190*0Sstevel@tonic-gate ...
191*0Sstevel@tonic-gate )
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate va_list ap;
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate va_start(ap, fmt);
196*0Sstevel@tonic-gate med_vperror(medep, fmt, ap);
197*0Sstevel@tonic-gate va_end(ap);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate /*PRINTFLIKE1*/
201*0Sstevel@tonic-gate void
med_perror(const char * fmt,...)202*0Sstevel@tonic-gate med_perror(
203*0Sstevel@tonic-gate const char *fmt,
204*0Sstevel@tonic-gate ...
205*0Sstevel@tonic-gate )
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate va_list ap;
208*0Sstevel@tonic-gate med_err_t status = med_null_err;
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate (void) med_error(&status, errno, NULL);
211*0Sstevel@tonic-gate va_start(ap, fmt);
212*0Sstevel@tonic-gate med_vperror(&status, fmt, ap);
213*0Sstevel@tonic-gate va_end(ap);
214*0Sstevel@tonic-gate med_clrerror(&status);
215*0Sstevel@tonic-gate }
216