1 /*-
2 * Copyright (c) 2008 Yahoo!, Inc.
3 * All rights reserved.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include "mptutil.h"
42
43 static CONFIG_PAGE_LOG_0 *
mpt_get_events(int fd,U16 * IOCStatus)44 mpt_get_events(int fd, U16 *IOCStatus)
45 {
46
47 return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG,
48 0, 0, 0, IOCStatus));
49 }
50
51 /*
52 * 1 2 3 4 5 6 7
53 * 1234567890123456789012345678901234567890123456789012345678901234567890
54 * < ID> < time > <ty> <X XX XX XX XX XX XX XX XX XX XX XX XX XX |..............|
55 * ID Time Type Log Data
56 */
57 static void
mpt_print_event(MPI_LOG_0_ENTRY * entry,int verbose)58 mpt_print_event(MPI_LOG_0_ENTRY *entry, int verbose)
59 {
60 int i;
61
62 printf("%5d %7ds %4x ", entry->LogSequence, entry->TimeStamp,
63 entry->LogEntryQualifier);
64 for (i = 0; i < 14; i++)
65 printf("%02x ", entry->LogData[i]);
66 printf("|");
67 for (i = 0; i < 14; i++)
68 printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] :
69 '.');
70 printf("|\n");
71 printf(" ");
72 for (i = 0; i < 14; i++)
73 printf("%02x ", entry->LogData[i + 14]);
74 printf("|");
75 for (i = 0; i < 14; i++)
76 printf("%c", isprint(entry->LogData[i + 14]) ?
77 entry->LogData[i + 14] : '.');
78 printf("|\n");
79 }
80
81 static int
event_compare(const void * first,const void * second)82 event_compare(const void *first, const void *second)
83 {
84 MPI_LOG_0_ENTRY * const *one;
85 MPI_LOG_0_ENTRY * const *two;
86
87 one = first;
88 two = second;
89 return ((*one)->LogSequence - ((*two)->LogSequence));
90 }
91
92 static int
show_events(int ac,char ** av)93 show_events(int ac, char **av)
94 {
95 CONFIG_PAGE_LOG_0 *log;
96 MPI_LOG_0_ENTRY **entries;
97 int ch, error, fd, i, num_events, verbose;
98
99 fd = mpt_open(mpt_unit);
100 if (fd < 0) {
101 error = errno;
102 warn("mpt_open");
103 return (error);
104 }
105
106 log = mpt_get_events(fd, NULL);
107 if (log == NULL) {
108 error = errno;
109 warn("Failed to get event log info");
110 return (error);
111 }
112
113 /* Default settings. */
114 verbose = 0;
115
116 /* Parse any options. */
117 optind = 1;
118 while ((ch = getopt(ac, av, "v")) != -1) {
119 switch (ch) {
120 case 'v':
121 verbose = 1;
122 break;
123 case '?':
124 default:
125 free(log);
126 close(fd);
127 return (EINVAL);
128 }
129 }
130 ac -= optind;
131 av += optind;
132
133 /* Build a list of valid entries and sort them by sequence. */
134 entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries);
135 if (entries == NULL) {
136 free(log);
137 close(fd);
138 return (ENOMEM);
139 }
140 num_events = 0;
141 for (i = 0; i < log->NumLogEntries; i++) {
142 if (log->LogEntry[i].LogEntryQualifier ==
143 MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED)
144 continue;
145 entries[num_events] = &log->LogEntry[i];
146 num_events++;
147 }
148
149 qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare);
150
151 if (num_events == 0)
152 printf("Event log is empty\n");
153 else {
154 printf(" ID Time Type Log Data\n");
155 for (i = 0; i < num_events; i++)
156 mpt_print_event(entries[i], verbose);
157 }
158
159 free(entries);
160 free(log);
161 close(fd);
162
163 return (0);
164 }
165 MPT_COMMAND(show, events, show_events);
166