1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <atomic.h>
#include <alloca.h>
#include <syslog.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <exacct.h>
#include <fmd_subr.h>
#include <fmd_conf.h>
#include <fmd_error.h>
#include <fmd_thread.h>
#include <fmd_protocol.h>
#include <fmd_event.h>
#include <fmd_dispq.h>
#include <fmd_log.h>
#include <fmd.h>
int
fmd_assert(const char *expr, const char *file, int line)
{
fmd_panic("\"%s\", line %d: assertion failed: %s\n", file, line, expr);
/*NOTREACHED*/
return (0);
}
/*
* To implement a reasonable panic() equivalent for fmd, we atomically bump a
* global counter of calls to fmd_vpanic() and attempt to print a panic message
* to stderr and dump core as a result of raising SIGABRT. This function must
* not attempt to grab any locks so that it can be called from any fmd code.
*/
void
fmd_vpanic(const char *format, va_list ap)
{
int oserr = errno;
pthread_t tid = pthread_self();
fmd_thread_t *tp;
char msg[BUFSIZ];
size_t len;
/*
* If this is not the first call to fmd_vpanic(), then check d_panictid
* to see if we are the panic thread. If so, then proceed directly to
* abort() because we have recursively panicked. If not, then pause()
* indefinitely waiting for the panic thread to terminate the daemon.
*/
if (atomic_add_32_nv(&fmd.d_panicrefs, 1) != 1) {
while (fmd.d_panictid != tid)
(void) pause();
goto abort;
}
/*
* Use fmd.d_pid != 0 as a cheap test to see if fmd.d_key is valid
* (i.e. we're after fmd_create() and before fmd_destroy()).
*/
if (fmd.d_pid != 0 && (tp = pthread_getspecific(fmd.d_key)) != NULL)
(void) tp->thr_trfunc(tp->thr_trdata, FMD_DBG_ERR, format, ap);
fmd.d_panicstr = msg;
fmd.d_panictid = tid;
(void) snprintf(msg, sizeof (msg), "%s: ABORT: ",
fmd.d_pname ? fmd.d_pname : "fmd");
len = strlen(msg);
(void) vsnprintf(msg + len, sizeof (msg) - len, format, ap);
if (strchr(format, '\n') == NULL) {
len = strlen(msg);
(void) snprintf(msg + len, sizeof (msg) - len, ": %s\n",
fmd_strerror(oserr));
}
(void) write(STDERR_FILENO, msg, strlen(msg));
abort:
abort();
_exit(FMD_EXIT_ERROR);
}
/*PRINTFLIKE1*/
void
fmd_panic(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fmd_vpanic(format, ap);
va_end(ap);
}
void
fmd_verror(int err, const char *format, va_list ap)
{
int oserr = errno;
fmd_thread_t *tp;
nvlist_t *nvl;
fmd_event_t *e;
char *class;
if ((tp = pthread_getspecific(fmd.d_key)) != NULL) {
(void) tp->thr_trfunc(tp->thr_trdata, FMD_DBG_ERR, format, ap);
tp->thr_errdepth++;
}
(void) pthread_mutex_lock(&fmd.d_err_lock);
if (fmd.d_errstats != NULL && err >= EFMD_UNKNOWN && err < EFMD_END)
fmd.d_errstats[err - EFMD_UNKNOWN].fmds_value.ui64++;
if (fmd.d_fg || !fmd.d_running) {
(void) fprintf(stderr, "%s: ", fmd.d_pname);
(void) vfprintf(stderr, format, ap);
if (strchr(format, '\n') == NULL)
(void) fprintf(stderr, ": %s\n", fmd_strerror(oserr));
}
(void) pthread_mutex_unlock(&fmd.d_err_lock);
/*
* If we are at error nesting level one and running in the background,
* log the error as an ereport to our own log and dispatch it. If the
* FMD_LF_BUSY flag is set, we can't attempt to log the event because
* a replay is running and we will deadlock on ourself in log_append.
*/
if (!fmd.d_fg && fmd.d_running &&
tp != NULL && tp->thr_errdepth == 1 &&
(nvl = fmd_protocol_fmderror(err, format, ap)) != NULL) {
(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
if (!(fmd.d_errlog->log_flags & FMD_LF_BUSY))
fmd_log_append(fmd.d_errlog, e, NULL);
(void) pthread_rwlock_unlock(&fmd.d_log_lock);
fmd_dispq_dispatch(fmd.d_disp, e, class);
}
if (tp != NULL)
tp->thr_errdepth--;
if (err == EFMD_EXIT) {
int core = 0;
(void) fmd_conf_getprop(fmd.d_conf, "core", &core);
if (core)
fmd_panic("forcing core dump at user request\n");
exit(FMD_EXIT_ERROR);
}
}
/*PRINTFLIKE2*/
void
fmd_error(int err, const char *format, ...)
{
va_list ap;
va_start(ap, format);
fmd_verror(err, format, ap);
va_end(ap);
}
void
fmd_vdprintf(int mask, const char *format, va_list ap)
{
fmd_thread_t *tp;
char *msg;
size_t len;
char c;
if (!(fmd.d_fmd_debug & mask))
return; /* none of the specified modes are enabled */
if ((tp = pthread_getspecific(fmd.d_key)) != NULL)
(void) tp->thr_trfunc(tp->thr_trdata, mask, format, ap);
if (fmd.d_fmd_dbout == 0)
return; /* no debugging output sinks are enabled */
len = vsnprintf(&c, 1, format, ap);
msg = alloca(len + 2);
(void) vsnprintf(msg, len + 1, format, ap);
if (msg[len - 1] != '\n')
(void) strcpy(&msg[len], "\n");
if (fmd.d_fmd_dbout & FMD_DBOUT_STDERR) {
(void) pthread_mutex_lock(&fmd.d_err_lock);
(void) fprintf(stderr, "%s DEBUG: %s", fmd.d_pname, msg);
(void) pthread_mutex_unlock(&fmd.d_err_lock);
}
if (fmd.d_fmd_dbout & FMD_DBOUT_SYSLOG) {
syslog(LOG_DEBUG | LOG_DAEMON,
"%s DEBUG: %s", fmd.d_pname, msg);
}
}
/*PRINTFLIKE2*/
void
fmd_dprintf(int mask, const char *format, ...)
{
va_list ap;
va_start(ap, format);
fmd_vdprintf(mask, format, ap);
va_end(ap);
}
/*
* The fmd_trace.c routines set tr_file and tr_line to NULL and 0 respectively.
* If they are invoked from a macro (see <fmd_subr.h>) this tail function is
* called as part of the TRACE() macro to fill in these fields from the cpp
* macro values for __FILE__ and __LINE__. No locking is needed because all
* trace buffers are allocated separately for each fmd thread.
*/
void
fmd_trace_cpp(void *ptr, const char *file, int line)
{
fmd_tracerec_t *trp = ptr;
if (trp != NULL) {
trp->tr_file = file;
trp->tr_line = line;
}
}
/*
* The fmd_trace() function is the wrapper for the tracing routines provided in
* fmd_trace.c. It is invoked by the TRACE() macro in <fmd_subr.h>, and uses
* the per-thread trace buffer set up in fmd_thread.c to trace debugging info.
*/
/*PRINTFLIKE2*/
void *
fmd_trace(uint_t tag, const char *format, ...)
{
fmd_thread_t *tp = pthread_getspecific(fmd.d_key);
va_list ap;
void *trp;
if (tp == NULL)
return (NULL); /* drop trace record if not ready yet */
va_start(ap, format);
trp = tp->thr_trfunc(tp->thr_trdata, tag, format, ap);
va_end(ap);
return (trp);
}
const char *
fmd_ea_strerror(int err)
{
switch (err) {
case EXR_OK: return ("no exacct error");
case EXR_SYSCALL_FAIL: return (fmd_strerror(errno));
case EXR_CORRUPT_FILE: return ("file corruption detected");
case EXR_EOF: return ("end-of-file reached");
case EXR_NO_CREATOR: return ("creator tag mismatch");
case EXR_INVALID_BUF: return ("invalid unpack buffer");
case EXR_NOTSUPP: return ("exacct operation not supported");
case EXR_UNKN_VERSION: return ("unsupported exacct file version");
case EXR_INVALID_OBJ: return ("invalid exacct object");
default: return ("unknown exacct error");
}
}
/*
* Create a local ENA value for fmd-generated ereports. We use ENA Format 1
* with the low bits of gethrtime() and pthread_self() as the processor ID.
*/
uint64_t
fmd_ena(void)
{
hrtime_t hrt = fmd_time_gethrtime();
return ((uint64_t)((FM_ENA_FMT1 & ENA_FORMAT_MASK) |
((pthread_self() << ENA_FMT1_CPUID_SHFT) & ENA_FMT1_CPUID_MASK) |
((hrt << ENA_FMT1_TIME_SHFT) & ENA_FMT1_TIME_MASK)));
}
/*
* fmd_ntz32() computes the number of trailing zeroes. The algorithm here is
* from "Hacker's Delight" by Henry Warren, Jr.
*/
uint32_t
fmd_ntz32(uint32_t x)
{
uint_t n = 1;
if (x == 0)
return (32);
if ((x & 0xFFFF) == 0) {
n += 16;
x >>= 16;
}
if ((x & 0xFF) == 0) {
n += 8;
x >>= 8;
}
if ((x & 0xF) == 0) {
n += 4;
x >>= 4;
}
if ((x & 0x3) == 0) {
n += 2;
x >>= 2;
}
return (n - (x & 1));
}
|