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