xref: /titanic_44/usr/src/cmd/fm/fmd/common/fmd_subr.c (revision 3c4993fb5a74112f361d71dab20997bdc749a7fb)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <atomic.h>
30 #include <alloca.h>
31 #include <syslog.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <exacct.h>
38 
39 #include <fmd_subr.h>
40 #include <fmd_conf.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 &&
186 	    tp != NULL && tp->thr_errdepth == 1 &&
187 	    (nvl = fmd_protocol_fmderror(err, format, ap)) != NULL) {
188 
189 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
190 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
191 
192 		(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
193 		if (!(fmd.d_errlog->log_flags & FMD_LF_BUSY))
194 			fmd_log_append(fmd.d_errlog, e, NULL);
195 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
196 
197 		fmd_dispq_dispatch(fmd.d_disp, e, class);
198 	}
199 
200 	if (tp != NULL)
201 		tp->thr_errdepth--;
202 
203 	if (err == EFMD_EXIT) {
204 		int core = 0;
205 
206 		(void) fmd_conf_getprop(fmd.d_conf, "core", &core);
207 		if (core)
208 			fmd_panic("forcing core dump at user request\n");
209 
210 		exit(FMD_EXIT_ERROR);
211 	}
212 }
213 
214 /*PRINTFLIKE2*/
215 void
216 fmd_error(int err, const char *format, ...)
217 {
218 	va_list ap;
219 
220 	va_start(ap, format);
221 	fmd_verror(err, format, ap);
222 	va_end(ap);
223 }
224 
225 void
226 fmd_vdprintf(int mask, const char *format, va_list ap)
227 {
228 	fmd_thread_t *tp;
229 	char *msg;
230 	size_t len;
231 	char c;
232 
233 	if (!(fmd.d_fmd_debug & mask))
234 		return; /* none of the specified modes are enabled */
235 
236 	if ((tp = pthread_getspecific(fmd.d_key)) != NULL)
237 		(void) tp->thr_trfunc(tp->thr_trdata, mask, format, ap);
238 
239 	if (fmd.d_fmd_dbout == 0)
240 		return; /* no debugging output sinks are enabled */
241 
242 	len = vsnprintf(&c, 1, format, ap);
243 	msg = alloca(len + 2);
244 	(void) vsnprintf(msg, len + 1, format, ap);
245 
246 	if (msg[len - 1] != '\n')
247 		(void) strcpy(&msg[len], "\n");
248 
249 	if (fmd.d_fmd_dbout & FMD_DBOUT_STDERR) {
250 		(void) pthread_mutex_lock(&fmd.d_err_lock);
251 		(void) fprintf(stderr, "%s DEBUG: %s", fmd.d_pname, msg);
252 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
253 	}
254 
255 	if (fmd.d_fmd_dbout & FMD_DBOUT_SYSLOG) {
256 		syslog(LOG_DEBUG | LOG_DAEMON,
257 		    "%s DEBUG: %s", fmd.d_pname, msg);
258 	}
259 }
260 
261 /*PRINTFLIKE2*/
262 void
263 fmd_dprintf(int mask, const char *format, ...)
264 {
265 	va_list ap;
266 
267 	va_start(ap, format);
268 	fmd_vdprintf(mask, format, ap);
269 	va_end(ap);
270 }
271 
272 /*
273  * The fmd_trace.c routines set tr_file and tr_line to NULL and 0 respectively.
274  * If they are invoked from a macro (see <fmd_subr.h>) this tail function is
275  * called as part of the TRACE() macro to fill in these fields from the cpp
276  * macro values for __FILE__ and __LINE__.  No locking is needed because all
277  * trace buffers are allocated separately for each fmd thread.
278  */
279 void
280 fmd_trace_cpp(void *ptr, const char *file, int line)
281 {
282 	fmd_tracerec_t *trp = ptr;
283 
284 	if (trp != NULL) {
285 		trp->tr_file = file;
286 		trp->tr_line = line;
287 	}
288 }
289 
290 /*
291  * The fmd_trace() function is the wrapper for the tracing routines provided in
292  * fmd_trace.c.  It is invoked by the TRACE() macro in <fmd_subr.h>, and uses
293  * the per-thread trace buffer set up in fmd_thread.c to trace debugging info.
294  */
295 /*PRINTFLIKE2*/
296 void *
297 fmd_trace(uint_t tag, const char *format, ...)
298 {
299 	fmd_thread_t *tp = pthread_getspecific(fmd.d_key);
300 	va_list ap;
301 	void *trp;
302 
303 	if (tp == NULL)
304 		return (NULL); /* drop trace record if not ready yet */
305 
306 	va_start(ap, format);
307 	trp = tp->thr_trfunc(tp->thr_trdata, tag, format, ap);
308 	va_end(ap);
309 
310 	return (trp);
311 }
312 
313 const char *
314 fmd_ea_strerror(int err)
315 {
316 	switch (err) {
317 	case EXR_OK:		return ("no exacct error");
318 	case EXR_SYSCALL_FAIL:	return (fmd_strerror(errno));
319 	case EXR_CORRUPT_FILE:	return ("file corruption detected");
320 	case EXR_EOF:		return ("end-of-file reached");
321 	case EXR_NO_CREATOR:	return ("creator tag mismatch");
322 	case EXR_INVALID_BUF:	return ("invalid unpack buffer");
323 	case EXR_NOTSUPP:	return ("exacct operation not supported");
324 	case EXR_UNKN_VERSION:	return ("unsupported exacct file version");
325 	case EXR_INVALID_OBJ:	return ("invalid exacct object");
326 	default:		return ("unknown exacct error");
327 	}
328 }
329 
330 /*
331  * Create a local ENA value for fmd-generated ereports.  We use ENA Format 1
332  * with the low bits of gethrtime() and pthread_self() as the processor ID.
333  */
334 uint64_t
335 fmd_ena(void)
336 {
337 	hrtime_t hrt = fmd_time_gethrtime();
338 
339 	return ((uint64_t)((FM_ENA_FMT1 & ENA_FORMAT_MASK) |
340 	    ((pthread_self() << ENA_FMT1_CPUID_SHFT) & ENA_FMT1_CPUID_MASK) |
341 	    ((hrt << ENA_FMT1_TIME_SHFT) & ENA_FMT1_TIME_MASK)));
342 }
343 
344 /*
345  * fmd_ntz32() computes the number of trailing zeroes.  The algorithm here is
346  * from "Hacker's Delight" by Henry Warren, Jr.
347  */
348 uint32_t
349 fmd_ntz32(uint32_t x)
350 {
351 	uint_t n = 1;
352 
353 	if (x == 0)
354 		return (32);
355 
356 	if ((x & 0xFFFF) == 0) {
357 		n += 16;
358 		x >>= 16;
359 	}
360 
361 	if ((x & 0xFF) == 0) {
362 		n += 8;
363 		x >>= 8;
364 	}
365 
366 	if ((x & 0xF) == 0) {
367 		n += 4;
368 		x >>= 4;
369 	}
370 
371 	if ((x & 0x3) == 0) {
372 		n += 2;
373 		x >>= 2;
374 	}
375 
376 	return (n - (x & 1));
377 }
378