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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <strings.h>
28 
29 #include <inj_event.h>
30 #include <inj_err.h>
31 
32 #include <fm/fmd_log.h>
33 
34 typedef struct inj_logfile {
35 	uint64_t ilf_sec;	/* timestamp seconds from previous record */
36 	uint64_t ilf_nsec;	/* timestamp nanoseconds from previous record */
37 	int ilf_index;		/* record index in log file for fake lineno */
38 } inj_logfile_t;
39 
40 /*ARGSUSED*/
41 static int
inj_logfile_event(fmd_log_t * lp,const fmd_log_record_t * rp,void * data)42 inj_logfile_event(fmd_log_t *lp, const fmd_log_record_t *rp, void *data)
43 {
44 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
45 	inj_defn_t *ev = inj_zalloc(sizeof (inj_defn_t));
46 
47 	hrtime_t rec_sec = rp->rec_sec + rp->rec_nsec / NANOSEC;
48 	hrtime_t rec_nsec = rp->rec_nsec % NANOSEC;
49 
50 	inj_logfile_t *ilf = data;
51 	hrtime_t delta;
52 
53 	if (ilf->ilf_index == 1)
54 		goto add_event; /* do not try to adjust time for first record */
55 
56 	/*
57 	 * If the current record's time is before that of the previous record,
58 	 * advance it to the previous record time.  This may occur when delays
59 	 * between capturing ENA and enqueuing a sysevent are observed.
60 	 */
61 	if (rec_sec < ilf->ilf_sec ||
62 	    (rec_sec == ilf->ilf_sec && rec_nsec < ilf->ilf_nsec)) {
63 		warn("record [%d] (%s) timestamp is out of order: "
64 		    "advancing event time to %llx.%llx\n",
65 		    ilf->ilf_index, rp->rec_class, ilf->ilf_sec, ilf->ilf_nsec);
66 		rec_sec = ilf->ilf_sec;
67 		rec_nsec = ilf->ilf_nsec;
68 	}
69 
70 	/*
71 	 * For now, compute the delta between the previous record and this one
72 	 * as a number of nanoseconds and advance the clock.  If a massively
73 	 * large delay is observed (>INT64_MAX ns), we abort.  This could be
74 	 * improved if necessary by sending more than one cmd_addhrt in a loop.
75 	 */
76 	delta = (rec_sec - ilf->ilf_sec) * NANOSEC;
77 	delta += (hrtime_t)rec_nsec - (hrtime_t)ilf->ilf_nsec;
78 
79 	if (delta < 0)
80 		die("record [%d] timestamp delta too large\n", ilf->ilf_index);
81 
82 	if (delta > 0)
83 		inj_cmds_add(inj_cmd_addhrt(delta));
84 
85 add_event:
86 	ev->defn_name = inj_strdup(rp->rec_class);
87 	ev->defn_lineno = ilf->ilf_index++;
88 
89 	if ((errno = nvlist_dup(rp->rec_nvl, &ev->defn_nvl, 0)) != 0)
90 		die("failed to allocate nvl for %s event", rp->rec_class);
91 
92 	cmd->cmd_type = CMD_SEND_EVENT;
93 	cmd->cmd_event = ev;
94 
95 	inj_cmds_add(cmd);
96 
97 	ilf->ilf_sec = rec_sec;
98 	ilf->ilf_nsec = rec_nsec;
99 
100 	return (0);
101 }
102 
103 inj_list_t *
inj_logfile_read(fmd_log_t * lp)104 inj_logfile_read(fmd_log_t *lp)
105 {
106 	const char *label = fmd_log_label(lp);
107 	inj_logfile_t ilf;
108 
109 	if (strcmp(label, "error") != 0)
110 		die("cannot use '%s' log as injector input\n", label);
111 
112 	bzero(&ilf, sizeof (ilf));
113 	ilf.ilf_index = 1;
114 
115 	if (fmd_log_iter(lp, inj_logfile_event, &ilf) != 0) {
116 		die("failed to process log: %s\n",
117 		    fmd_log_errmsg(lp, fmd_log_errno(lp)));
118 	}
119 
120 	return (inj_cmds_get());
121 }
122