7712 mandoc -Tlint does always exit with error code 0
[unleashed.git] / usr / src / cmd / lvm / rpc.metamhd / mhd_error.c
blob5714cb36a1cafac721b78307fe2b010783b6592a
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "mhd_local.h"
31 #include <syslog.h>
34 * debug stuff
36 #ifdef MHD_DEBUG
37 int mhd_debug = MHD_DEBUG;
38 #endif
41 * free and clear error
43 void
44 mhd_clrerror(
45 mhd_error_t *mhep
48 if (mhep->name != NULL)
49 Free(mhep->name);
50 (void) memset(mhep, 0, sizeof (*mhep));
54 * setup error
56 int
57 mhd_error(
58 mhd_error_t *mhep,
59 int errnum,
60 char *name
63 mhd_clrerror(mhep);
64 if (errnum != 0) {
65 mhep->errnum = errnum;
66 if (name != NULL)
67 mhep->name = Strdup(name);
68 return (-1);
70 return (0);
74 * mhd_error_t to string
76 static char *
77 mhd_strerror(
78 mhd_error_t *mhep
81 static char buf[1024];
82 char *emsg;
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);
98 * printf-like log
100 static void
101 mhd_vprintf(
102 const char *fmt,
103 va_list ap
106 if (isatty(fileno(stderr))) {
107 static mutex_t stderr_mx = DEFAULTMUTEX;
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);
115 vsyslog(LOG_ERR, fmt, ap);
118 /*PRINTFLIKE1*/
119 void
120 mhd_eprintf(
121 const char *fmt,
125 va_list ap;
127 va_start(ap, fmt);
128 mhd_vprintf(fmt, ap);
129 va_end(ap);
133 * printf-like perror() log
135 /*PRINTFLIKE2*/
136 static void
137 mhd_vperror(
138 mhd_error_t *mhep,
139 const char *fmt,
140 va_list ap
143 char buf[1024];
144 char *p = buf;
145 size_t len = sizeof (buf);
146 int n;
148 if ((mhep->name != NULL) && (mhep->name[0] != '\0')) {
149 n = snprintf(p, len, "%s: ", mhep->name);
150 p += n;
151 len -= n;
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;
161 (void) snprintf(p, len, "%s", mhd_strerror(mhep));
162 mhd_eprintf("%s\n", buf);
165 /*PRINTFLIKE2*/
166 void
167 mhde_perror(
168 mhd_error_t *mhep,
169 const char *fmt,
173 va_list ap;
175 va_start(ap, fmt);
176 mhd_vperror(mhep, fmt, ap);
177 va_end(ap);
180 /*PRINTFLIKE1*/
181 void
182 mhd_perror(
183 const char *fmt,
187 va_list ap;
188 mhd_error_t status = mhd_null_error;
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);