xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_sysevent.c (revision 95c2d3023b88b9097d9822eb47ace5466e6d1cf4)
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 2008 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 <sys/sysevent/eventdefs.h>
30 #include <sys/sysevent.h>
31 #include <sys/sysevent_impl.h>
32 #include <sys/fm/protocol.h>
33 #include <sys/sysmacros.h>
34 #include <sys/dumphdr.h>
35 #include <sys/dumpadm.h>
36 #include <sys/fm/util.h>
37 
38 #include <libsysevent.h>
39 #include <libnvpair.h>
40 #include <alloca.h>
41 #include <limits.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 
47 #undef MUTEX_HELD
48 #undef RW_READ_HELD
49 #undef RW_WRITE_HELD
50 
51 #include <fmd_api.h>
52 #include <fmd_log.h>
53 #include <fmd_subr.h>
54 #include <fmd_dispq.h>
55 #include <fmd_dr.h>
56 #include <fmd_module.h>
57 #include <fmd_protocol.h>
58 #include <fmd_scheme.h>
59 #include <fmd_error.h>
60 
61 #include <fmd.h>
62 
63 static char *sysev_channel;	/* event channel to which we are subscribed */
64 static char *sysev_class;	/* event class to which we are subscribed */
65 static char *sysev_device;	/* device path to use for replaying events */
66 static char *sysev_sid;		/* event channel subscriber identifier */
67 static void *sysev_evc;		/* event channel cookie from evc_bind */
68 
69 static fmd_xprt_t *sysev_xprt;
70 static int sysev_xprt_refcnt;
71 static fmd_hdl_t *sysev_hdl;
72 
73 static struct sysev_stats {
74 	fmd_stat_t dump_replay;
75 	fmd_stat_t dump_lost;
76 	fmd_stat_t bad_class;
77 	fmd_stat_t bad_attr;
78 	fmd_stat_t eagain;
79 } sysev_stats = {
80 	{ "dump_replay", FMD_TYPE_UINT64, "events replayed from dump device" },
81 	{ "dump_lost", FMD_TYPE_UINT64, "events lost from dump device" },
82 	{ "bad_class", FMD_TYPE_UINT64, "events dropped due to invalid class" },
83 	{ "bad_attr", FMD_TYPE_UINT64, "events dropped due to invalid nvlist" },
84 	{ "eagain", FMD_TYPE_UINT64, "events retried due to low memory" },
85 };
86 
87 static pthread_cond_t sysev_cv = PTHREAD_COND_INITIALIZER;
88 static pthread_mutex_t sysev_mutex = PTHREAD_MUTEX_INITIALIZER;
89 static int sysev_replay_wait = 1;
90 static int sysev_exiting;
91 
92 /*
93  * Entry point for legacy sysevents.  This function is responsible for two
94  * things: passing off interesting events to the DR handler, and converting
95  * sysevents into resource events that modules can then subscribe to.
96  */
97 static void
98 sysev_legacy(sysevent_t *sep)
99 {
100 	const char *class = sysevent_get_class_name(sep);
101 	const char *subclass = sysevent_get_subclass_name(sep);
102 	char *fullclass;
103 	size_t len;
104 	nvlist_t *attr, *nvl;
105 	fmd_event_t *e;
106 	hrtime_t hrt;
107 
108 	/* notify the DR subsystem of the event */
109 	fmd_dr_event(sep);
110 
111 	/* get the matching sysevent name */
112 	len = snprintf(NULL, 0, "%s%s.%s", SYSEVENT_RSRC_CLASS,
113 	    class, subclass);
114 	fullclass = alloca(len + 1);
115 	(void) snprintf(fullclass, len + 1, "%s%s.%s",
116 	    SYSEVENT_RSRC_CLASS, class, subclass);
117 
118 	/* construct the event payload */
119 	(void) nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva);
120 	(void) nvlist_add_string(nvl, FM_CLASS, fullclass);
121 	(void) nvlist_add_uint8(nvl, FM_VERSION, FM_RSRC_VERSION);
122 	if (sysevent_get_attr_list(sep, &attr) == 0) {
123 		(void) nvlist_merge(nvl, attr, 0);
124 		nvlist_free(attr);
125 	}
126 
127 	/*
128 	 * Dispatch the event.  Ideally, we'd like to use the same transport
129 	 * interface as sysev_recv(), but because the legacy sysevent mechanism
130 	 * puts in a thread outside fmd's control, using the module APIs is
131 	 * impossible.
132 	 */
133 	sysevent_get_time(sep, &hrt);
134 	(void) nvlist_lookup_string(nvl, FM_CLASS, &fullclass);
135 	e = fmd_event_create(FMD_EVT_PROTOCOL, hrt, nvl, fullclass);
136 	fmd_dispq_dispatch(fmd.d_disp, e, fullclass);
137 }
138 
139 /*
140  * Receive an event from the SysEvent channel and post it to our transport.
141  * Under extreme low-memory situations where we cannot event unpack the event,
142  * we can request that SysEvent redeliver the event later by returning EAGAIN.
143  * If we do this too many times, the kernel will drop the event.  Rather than
144  * keeping state per-event, we simply attempt a garbage-collect, hoping that
145  * enough free memory will be available by the time the event is redelivered.
146  */
147 static int
148 sysev_recv(sysevent_t *sep, void *arg)
149 {
150 	uint64_t seq = sysevent_get_seq(sep);
151 	fmd_xprt_t *xp = arg;
152 	nvlist_t *nvl;
153 	hrtime_t hrt;
154 	int rc = 0;
155 
156 	(void) pthread_mutex_lock(&sysev_mutex);
157 	if (sysev_exiting == 1) {
158 		while (sysev_xprt_refcnt > 0)
159 			(void) pthread_cond_wait(&sysev_cv, &sysev_mutex);
160 		(void) pthread_mutex_unlock(&sysev_mutex);
161 		return (EAGAIN);
162 	}
163 	sysev_xprt_refcnt++;
164 	while (sysev_replay_wait)
165 		(void) pthread_cond_wait(&sysev_cv, &sysev_mutex);
166 	(void) pthread_mutex_unlock(&sysev_mutex);
167 
168 	if (strcmp(sysevent_get_class_name(sep), EC_FM) != 0) {
169 		fmd_hdl_error(sysev_hdl, "discarding event 0x%llx: unexpected"
170 		    " transport class %s\n", seq, sysevent_get_class_name(sep));
171 		sysev_stats.bad_class.fmds_value.ui64++;
172 	} else if (sysevent_get_attr_list(sep, &nvl) != 0) {
173 		if (errno == EAGAIN || errno == ENOMEM) {
174 			fmd_modhash_tryapply(fmd.d_mod_hash, fmd_module_trygc);
175 			fmd_scheme_hash_trygc(fmd.d_schemes);
176 			sysev_stats.eagain.fmds_value.ui64++;
177 			rc = EAGAIN;
178 		} else {
179 			fmd_hdl_error(sysev_hdl, "discarding event 0x%llx: "
180 			    "missing or invalid payload", seq);
181 			sysev_stats.bad_attr.fmds_value.ui64++;
182 		}
183 	} else {
184 		sysevent_get_time(sep, &hrt);
185 		fmd_xprt_post(sysev_hdl, xp, nvl, hrt);
186 	}
187 
188 	(void) pthread_mutex_lock(&sysev_mutex);
189 	if (--sysev_xprt_refcnt == 0 && sysev_exiting == 1)
190 		(void) pthread_cond_broadcast(&sysev_cv);
191 	(void) pthread_mutex_unlock(&sysev_mutex);
192 
193 	return (rc);
194 }
195 
196 /*
197  * Checksum algorithm used by the dump transport for verifying the content of
198  * error reports saved on the dump device (copy of the kernel's checksum32()).
199  */
200 static uint32_t
201 sysev_checksum(void *cp_arg, size_t length)
202 {
203 	uchar_t *cp, *ep;
204 	uint32_t sum = 0;
205 
206 	for (cp = cp_arg, ep = cp + length; cp < ep; cp++)
207 		sum = ((sum >> 1) | (sum << 31)) + *cp;
208 
209 	return (sum);
210 }
211 
212 /*
213  * Replay saved events from the dump transport.  This function is installed as
214  * the timer callback and is called only once during the module's lifetime.
215  */
216 /*ARGSUSED*/
217 static void
218 sysev_replay(fmd_hdl_t *hdl, id_t id, void *arg)
219 {
220 	char *dumpdev;
221 	off64_t off, off0;
222 	int fd, err;
223 
224 	/*
225 	 * Determine the appropriate dump device to use for replaying pending
226 	 * error reports.  If the device property is NULL (default), we
227 	 * open and query /dev/dump to determine the current dump device.
228 	 */
229 	if ((dumpdev = sysev_device) == NULL) {
230 		if ((fd = open("/dev/dump", O_RDONLY)) == -1) {
231 			fmd_hdl_error(hdl, "failed to open /dev/dump "
232 			    "to locate dump device for event replay");
233 			goto done;
234 		}
235 
236 		dumpdev = alloca(PATH_MAX);
237 		err = ioctl(fd, DIOCGETDEV, dumpdev);
238 		(void) close(fd);
239 
240 		if (err == -1) {
241 			if (errno != ENODEV) {
242 				fmd_hdl_error(hdl, "failed to obtain "
243 				    "path to dump device for event replay");
244 			}
245 			goto done;
246 		}
247 	}
248 
249 	if (strcmp(dumpdev, "/dev/null") == 0)
250 		goto done; /* return silently and skip replay for /dev/null */
251 
252 	/*
253 	 * Open the appropriate device and then determine the offset of the
254 	 * start of the ereport dump region located at the end of the device.
255 	 */
256 	if ((fd = open64(dumpdev, O_RDWR | O_DSYNC)) == -1) {
257 		fmd_hdl_error(hdl, "failed to open dump transport %s "
258 		    "(pending events will not be replayed)", dumpdev);
259 		goto done;
260 	}
261 
262 	off = DUMP_OFFSET + DUMP_LOGSIZE + DUMP_ERPTSIZE;
263 	off = off0 = lseek64(fd, -off, SEEK_END) & -DUMP_OFFSET;
264 
265 	if (off == (off64_t)-1LL) {
266 		fmd_hdl_error(hdl, "failed to seek dump transport %s "
267 		    "(pending events will not be replayed)", dumpdev);
268 		(void) close(fd);
269 		goto done;
270 	}
271 
272 	/*
273 	 * The ereport dump region is a sequence of erpt_dump_t headers each of
274 	 * which is followed by packed nvlist data.  We iterate over them in
275 	 * order, unpacking and dispatching each one to our dispatch queue.
276 	 */
277 	for (;;) {
278 		char nvbuf[ERPT_DATA_SZ];
279 		uint32_t chksum;
280 		erpt_dump_t ed;
281 		nvlist_t *nvl;
282 
283 		fmd_timeval_t ftv, tod;
284 		hrtime_t hrt;
285 		uint64_t ena;
286 
287 		if (pread64(fd, &ed, sizeof (ed), off) != sizeof (ed)) {
288 			fmd_hdl_error(hdl, "failed to read from dump "
289 			    "transport %s (pending events lost)", dumpdev);
290 			break;
291 		}
292 
293 		if (ed.ed_magic == 0 && ed.ed_size == 0)
294 			break; /* end of list: all zero */
295 
296 		if (ed.ed_magic == 0) {
297 			off += sizeof (ed) + ed.ed_size;
298 			continue; /* continue searching */
299 		}
300 
301 		if (ed.ed_magic != ERPT_MAGIC) {
302 			/*
303 			 * Stop reading silently if the first record has the
304 			 * wrong magic number; this likely indicates that we
305 			 * rebooted from non-FMA bits or paged over the dump.
306 			 */
307 			if (off == off0)
308 				break;
309 
310 			fmd_hdl_error(hdl, "invalid dump transport "
311 			    "record at %llx (magic number %x, expected %x)\n",
312 			    (u_longlong_t)off, ed.ed_magic, ERPT_MAGIC);
313 			break;
314 		}
315 
316 		if (ed.ed_size > ERPT_DATA_SZ) {
317 			fmd_hdl_error(hdl, "invalid dump transport "
318 			    "record at %llx size (%u exceeds limit)\n",
319 			    (u_longlong_t)off, ed.ed_size);
320 			break;
321 		}
322 
323 		if (pread64(fd, nvbuf, ed.ed_size,
324 		    off + sizeof (ed)) != ed.ed_size) {
325 			fmd_hdl_error(hdl, "failed to read dump "
326 			    "transport event (offset %llx)", (u_longlong_t)off);
327 
328 			sysev_stats.dump_lost.fmds_value.ui64++;
329 			goto next;
330 		}
331 
332 		if ((chksum = sysev_checksum(nvbuf,
333 		    ed.ed_size)) != ed.ed_chksum) {
334 			fmd_hdl_error(hdl, "dump transport event at "
335 			    "offset %llx is corrupt (checksum %x != %x)\n",
336 			    (u_longlong_t)off, chksum, ed.ed_chksum);
337 
338 			sysev_stats.dump_lost.fmds_value.ui64++;
339 			goto next;
340 		}
341 
342 		if ((err = nvlist_xunpack(nvbuf,
343 		    ed.ed_size, &nvl, &fmd.d_nva)) != 0) {
344 			fmd_hdl_error(hdl, "failed to unpack dump "
345 			    "transport event at offset %llx: %s\n",
346 			    (u_longlong_t)off, fmd_strerror(err));
347 
348 			sysev_stats.dump_lost.fmds_value.ui64++;
349 			goto next;
350 		}
351 
352 		/*
353 		 * If ed_hrt_nsec is set it contains the gethrtime() value from
354 		 * when the event was originally enqueued for the transport.
355 		 * If it is zero, we use the weaker bound ed_hrt_base instead.
356 		 */
357 		if (ed.ed_hrt_nsec != 0)
358 			hrt = ed.ed_hrt_nsec;
359 		else
360 			hrt = ed.ed_hrt_base;
361 
362 		/*
363 		 * If this is an FMA protocol event of class "ereport.*" that
364 		 * contains valid ENA, we can improve the precision of 'hrt'.
365 		 */
366 		if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) == 0)
367 			hrt = fmd_time_ena2hrt(hrt, ena);
368 
369 		/*
370 		 * Now convert 'hrt' to an adjustable TOD based on the values
371 		 * in ed_tod_base which correspond to one another and are
372 		 * sampled before reboot using the old gethrtime() clock.
373 		 * fmd_event_recreate() will use this TOD value to re-assign
374 		 * the event an updated gethrtime() value based on the current
375 		 * value of the non-adjustable gethrtime() clock.  Phew.
376 		 */
377 		tod.ftv_sec = ed.ed_tod_base.sec;
378 		tod.ftv_nsec = ed.ed_tod_base.nsec;
379 		fmd_time_hrt2tod(ed.ed_hrt_base, &tod, hrt, &ftv);
380 
381 		(void) nvlist_remove_all(nvl, FMD_EVN_TOD);
382 		(void) nvlist_add_uint64_array(nvl,
383 		    FMD_EVN_TOD, (uint64_t *)&ftv, 2);
384 
385 		fmd_xprt_post(hdl, sysev_xprt, nvl, 0);
386 		sysev_stats.dump_replay.fmds_value.ui64++;
387 
388 next:
389 		/*
390 		 * Reset the magic number for the event record to zero so that
391 		 * we do not replay the same event multiple times.
392 		 */
393 		ed.ed_magic = 0;
394 
395 		if (pwrite64(fd, &ed, sizeof (ed), off) != sizeof (ed)) {
396 			fmd_hdl_error(hdl, "failed to mark dump "
397 			    "transport event (offset %llx)", (u_longlong_t)off);
398 		}
399 
400 		off += sizeof (ed) + ed.ed_size;
401 	}
402 
403 	(void) close(fd);
404 done:
405 	(void) pthread_mutex_lock(&sysev_mutex);
406 	sysev_replay_wait = 0;
407 	(void) pthread_cond_broadcast(&sysev_cv);
408 	(void) pthread_mutex_unlock(&sysev_mutex);
409 }
410 
411 static const fmd_prop_t sysev_props[] = {
412 	{ "class", FMD_TYPE_STRING, EC_ALL },		/* event class */
413 	{ "device", FMD_TYPE_STRING, NULL },		/* replay device */
414 	{ "channel", FMD_TYPE_STRING, FM_ERROR_CHAN },	/* channel name */
415 	{ "sid", FMD_TYPE_STRING, "fmd" },		/* subscriber id */
416 	{ NULL, 0, NULL }
417 };
418 
419 static const fmd_hdl_ops_t sysev_ops = {
420 	NULL,		/* fmdo_recv */
421 	sysev_replay,	/* fmdo_timeout */
422 	NULL,		/* fmdo_close */
423 	NULL,		/* fmdo_stats */
424 	NULL,		/* fmdo_gc */
425 	NULL,		/* fmdo_send */
426 };
427 
428 static const fmd_hdl_info_t sysev_info = {
429 	"SysEvent Transport Agent", "1.0", &sysev_ops, sysev_props
430 };
431 
432 /*
433  * Bind to the sysevent channel we use for listening for error events and then
434  * subscribe to appropriate events received over this channel.  Setup the
435  * legacy sysevent handler for creating sysevent resources and forwarding DR
436  * events.
437  */
438 void
439 sysev_init(fmd_hdl_t *hdl)
440 {
441 	uint_t flags;
442 	const char *subclasses[] = { EC_SUB_ALL };
443 
444 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &sysev_info) != 0)
445 		return; /* invalid property settings */
446 
447 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (sysev_stats) /
448 	    sizeof (fmd_stat_t), (fmd_stat_t *)&sysev_stats);
449 
450 	sysev_channel = fmd_prop_get_string(hdl, "channel");
451 	sysev_class = fmd_prop_get_string(hdl, "class");
452 	sysev_device = fmd_prop_get_string(hdl, "device");
453 	sysev_sid = fmd_prop_get_string(hdl, "sid");
454 
455 	if (sysev_channel == NULL)
456 		fmd_hdl_abort(hdl, "channel property must be defined\n");
457 
458 	if (sysev_sid == NULL)
459 		fmd_hdl_abort(hdl, "sid property must be defined\n");
460 
461 	if ((errno = sysevent_evc_bind(sysev_channel, &sysev_evc,
462 	    EVCH_CREAT | EVCH_HOLD_PEND)) != 0) {
463 		fmd_hdl_abort(hdl, "failed to bind to event transport "
464 		    "channel %s", sysev_channel);
465 	}
466 
467 	sysev_xprt = fmd_xprt_open(hdl, FMD_XPRT_RDONLY, NULL, NULL);
468 	sysev_hdl = hdl;
469 
470 	/*
471 	 * If we're subscribing to the default channel, keep our subscription
472 	 * active even if we die unexpectedly so we continue queuing events.
473 	 * If we're not (e.g. running under fmsim), do not specify SUB_KEEP so
474 	 * that our event channel will be destroyed if we die unpleasantly.
475 	 */
476 	if (strcmp(sysev_channel, FM_ERROR_CHAN) == 0)
477 		flags = EVCH_SUB_KEEP | EVCH_SUB_DUMP;
478 	else
479 		flags = EVCH_SUB_DUMP;
480 
481 	errno = sysevent_evc_subscribe(sysev_evc,
482 	    sysev_sid, sysev_class, sysev_recv, sysev_xprt, flags);
483 
484 	if (errno != 0) {
485 		if (errno == EEXIST) {
486 			fmd_hdl_abort(hdl, "another fault management daemon is "
487 			    "active on transport channel %s\n", sysev_channel);
488 		} else {
489 			fmd_hdl_abort(hdl, "failed to subscribe to %s on "
490 			    "transport channel %s", sysev_class, sysev_channel);
491 		}
492 	}
493 
494 	/*
495 	 * Once the transport is open, install a single timer to fire at once
496 	 * in the context of the module's thread to run sysev_replay().  This
497 	 * thread will block in its first fmd_xprt_post() until fmd is ready.
498 	 */
499 	fmd_hdl_debug(hdl, "transport '%s' open\n", sysev_channel);
500 	(void) fmd_timer_install(hdl, NULL, NULL, 0);
501 
502 	/*
503 	 * Open the legacy sysevent handle and subscribe to all events.  These
504 	 * are automatically converted to "resource.sysevent.*" events so that
505 	 * modules can manage these events without additional infrastructure.
506 	 */
507 	if (geteuid() != 0)
508 		return;
509 
510 	if ((fmd.d_sysev_hdl =
511 	    sysevent_bind_handle(sysev_legacy)) == NULL)
512 		fmd_hdl_abort(hdl, "failed to bind to legacy sysevent channel");
513 
514 	if (sysevent_subscribe_event(fmd.d_sysev_hdl, EC_ALL,
515 	    subclasses, 1) != 0)
516 		fmd_hdl_abort(hdl, "failed to subscribe to legacy sysevents");
517 }
518 
519 /*
520  * Close the channel by unsubscribing and unbinding.  We only do this when a
521  * a non-default channel has been selected.  If we're using FM_ERROR_CHAN,
522  * the system default, we do *not* want to unsubscribe because the kernel will
523  * remove the subscriber queue and any events published in our absence will
524  * therefore be lost.  This scenario may occur when, for example, fmd is sent
525  * a SIGTERM by init(1M) during reboot but an error is detected and makes it
526  * into the sysevent channel queue before init(1M) manages to call uadmin(2).
527  */
528 void
529 sysev_fini(fmd_hdl_t *hdl)
530 {
531 	if (strcmp(sysev_channel, FM_ERROR_CHAN) != 0) {
532 		sysevent_evc_unsubscribe(sysev_evc, sysev_sid);
533 		sysevent_evc_unbind(sysev_evc);
534 	}
535 
536 	if (fmd.d_sysev_hdl != NULL)
537 		sysevent_unbind_handle(fmd.d_sysev_hdl);
538 
539 	if (sysev_xprt != NULL) {
540 		/*
541 		 * Wait callback returns before destroy the transport.
542 		 */
543 		(void) pthread_mutex_lock(&sysev_mutex);
544 		sysev_exiting = 1;
545 		while (sysev_xprt_refcnt > 0)
546 			(void) pthread_cond_wait(&sysev_cv, &sysev_mutex);
547 		(void) pthread_mutex_unlock(&sysev_mutex);
548 		fmd_xprt_close(hdl, sysev_xprt);
549 	}
550 
551 	fmd_prop_free_string(hdl, sysev_class);
552 	fmd_prop_free_string(hdl, sysev_channel);
553 	fmd_prop_free_string(hdl, sysev_device);
554 	fmd_prop_free_string(hdl, sysev_sid);
555 }
556