]>
Commit | Line | Data |
---|---|---|
5ef05369 KZ |
1 | /* |
2 | * dmesg.c -- Print out the contents of the kernel ring buffer | |
7eda085c | 3 | * |
5ef05369 KZ |
4 | * Copyright (C) 1993 Theodore Ts'o <tytso@athena.mit.edu> |
5 | * Copyright (C) 2011 Karel Zak <kzak@redhat.com> | |
6 | * | |
7 | * This program comes with ABSOLUTELY NO WARRANTY. | |
6dbe3af9 | 8 | */ |
6dbe3af9 KZ |
9 | #include <linux/unistd.h> |
10 | #include <stdio.h> | |
11 | #include <getopt.h> | |
fd6b7a7f | 12 | #include <stdlib.h> |
15673c15 | 13 | #include <sys/klog.h> |
f06ec64f | 14 | #include <sys/syslog.h> |
bd304d92 | 15 | #include <sys/time.h> |
42fac79a | 16 | #include <sys/sysinfo.h> |
15103c4b | 17 | #include <ctype.h> |
bd304d92 | 18 | #include <time.h> |
c672220f KZ |
19 | #include <sys/mman.h> |
20 | #include <sys/types.h> | |
21 | #include <sys/stat.h> | |
22 | #include <unistd.h> | |
23 | #include <fcntl.h> | |
5423ccb1 | 24 | |
15103c4b | 25 | #include "c.h" |
098ce273 | 26 | #include "colors.h" |
5423ccb1 | 27 | #include "nls.h" |
15673c15 | 28 | #include "strutils.h" |
15103c4b | 29 | #include "xalloc.h" |
b8300c0a | 30 | #include "widechar.h" |
e12c9866 | 31 | #include "all-io.h" |
636a6207 | 32 | #include "bitops.h" |
efb8854f | 33 | #include "closestream.h" |
94920134 | 34 | #include "optutils.h" |
ddca870a | 35 | #include "mangle.h" |
aa192520 | 36 | #include "pager.h" |
6dbe3af9 | 37 | |
59a14899 KZ |
38 | /* Close the log. Currently a NOP. */ |
39 | #define SYSLOG_ACTION_CLOSE 0 | |
40 | /* Open the log. Currently a NOP. */ | |
41 | #define SYSLOG_ACTION_OPEN 1 | |
42 | /* Read from the log. */ | |
43 | #define SYSLOG_ACTION_READ 2 | |
44 | /* Read all messages remaining in the ring buffer. (allowed for non-root) */ | |
45 | #define SYSLOG_ACTION_READ_ALL 3 | |
46 | /* Read and clear all messages remaining in the ring buffer */ | |
47 | #define SYSLOG_ACTION_READ_CLEAR 4 | |
48 | /* Clear ring buffer. */ | |
49 | #define SYSLOG_ACTION_CLEAR 5 | |
50 | /* Disable printk's to console */ | |
51 | #define SYSLOG_ACTION_CONSOLE_OFF 6 | |
52 | /* Enable printk's to console */ | |
53 | #define SYSLOG_ACTION_CONSOLE_ON 7 | |
54 | /* Set level of messages printed to console */ | |
55 | #define SYSLOG_ACTION_CONSOLE_LEVEL 8 | |
56 | /* Return number of unread characters in the log buffer */ | |
57 | #define SYSLOG_ACTION_SIZE_UNREAD 9 | |
58 | /* Return size of the log buffer */ | |
59 | #define SYSLOG_ACTION_SIZE_BUFFER 10 | |
60 | ||
5aaee63c KZ |
61 | /* |
62 | * Colors | |
63 | */ | |
64 | #define DMESG_COLOR_SUBSYS UL_COLOR_BROWN | |
65 | #define DMESG_COLOR_TIME UL_COLOR_GREEN | |
66 | #define DMESG_COLOR_RELTIME UL_COLOR_BOLD_GREEN | |
67 | #define DMESG_COLOR_ALERT UL_COLOR_REVERSE UL_COLOR_RED | |
68 | #define DMESG_COLOR_CRIT UL_COLOR_BOLD_RED | |
69 | #define DMESG_COLOR_ERR UL_COLOR_RED | |
70 | #define DMESG_COLOR_WARN UL_COLOR_BOLD | |
71 | #define DMESG_COLOR_SEGFAULT UL_COLOR_HALFBRIGHT UL_COLOR_RED | |
72 | ||
f06ec64f | 73 | /* |
5ef05369 | 74 | * Priority and facility names |
f06ec64f KZ |
75 | */ |
76 | struct dmesg_name { | |
77 | const char *name; | |
78 | const char *help; | |
79 | }; | |
80 | ||
5ef05369 KZ |
81 | /* |
82 | * Priority names -- based on sys/syslog.h | |
83 | */ | |
f06ec64f KZ |
84 | static const struct dmesg_name level_names[] = |
85 | { | |
86 | [LOG_EMERG] = { "emerg", N_("system is unusable") }, | |
87 | [LOG_ALERT] = { "alert", N_("action must be taken immediately") }, | |
88 | [LOG_CRIT] = { "crit", N_("critical conditions") }, | |
89 | [LOG_ERR] = { "err", N_("error conditions") }, | |
90 | [LOG_WARNING] = { "warn", N_("warning conditions") }, | |
91 | [LOG_NOTICE] = { "notice",N_("normal but significant condition") }, | |
92 | [LOG_INFO] = { "info", N_("informational") }, | |
93 | [LOG_DEBUG] = { "debug", N_("debug-level messages") } | |
94 | }; | |
95 | ||
85f3cc55 KZ |
96 | /* |
97 | * sys/syslog.h uses (f << 3) for all facility codes. | |
98 | * We want to use the codes as array idexes, so shift back... | |
99 | * | |
100 | * Note that libc LOG_FAC() macro returns the base codes, not the | |
101 | * shifted code :-) | |
102 | */ | |
103 | #define FAC_BASE(f) ((f) >> 3) | |
104 | ||
105 | static const struct dmesg_name facility_names[] = | |
106 | { | |
107 | [FAC_BASE(LOG_KERN)] = { "kern", N_("kernel messages") }, | |
108 | [FAC_BASE(LOG_USER)] = { "user", N_("random user-level messages") }, | |
109 | [FAC_BASE(LOG_MAIL)] = { "mail", N_("mail system") }, | |
110 | [FAC_BASE(LOG_DAEMON)] = { "daemon", N_("system daemons") }, | |
111 | [FAC_BASE(LOG_AUTH)] = { "auth", N_("security/authorization messages") }, | |
112 | [FAC_BASE(LOG_SYSLOG)] = { "syslog", N_("messages generated internally by syslogd") }, | |
113 | [FAC_BASE(LOG_LPR)] = { "lpr", N_("line printer subsystem") }, | |
114 | [FAC_BASE(LOG_NEWS)] = { "news", N_("network news subsystem") }, | |
115 | [FAC_BASE(LOG_UUCP)] = { "uucp", N_("UUCP subsystem") }, | |
116 | [FAC_BASE(LOG_CRON)] = { "cron", N_("clock daemon") }, | |
117 | [FAC_BASE(LOG_AUTHPRIV)] = { "authpriv", N_("security/authorization messages (private)") }, | |
118 | [FAC_BASE(LOG_FTP)] = { "ftp", N_("ftp daemon") }, | |
119 | }; | |
120 | ||
e6471b9f KZ |
121 | /* supported methods to read message buffer |
122 | */ | |
123 | enum { | |
7af23060 | 124 | DMESG_METHOD_KMSG, /* read messages from /dev/kmsg (default) */ |
e6471b9f KZ |
125 | DMESG_METHOD_SYSLOG, /* klogctl() buffer */ |
126 | DMESG_METHOD_MMAP /* mmap file with records (see --file) */ | |
127 | }; | |
128 | ||
776eabe7 SK |
129 | enum { |
130 | DMESG_TIMEFTM_NONE = 0, | |
131 | DMESG_TIMEFTM_CTIME, /* [ctime] */ | |
132 | DMESG_TIMEFTM_CTIME_DELTA, /* [ctime <delta>] */ | |
133 | DMESG_TIMEFTM_DELTA, /* [<delta>] */ | |
134 | DMESG_TIMEFTM_RELTIME, /* [relative] */ | |
135 | DMESG_TIMEFTM_TIME, /* [time] */ | |
8a8be309 SK |
136 | DMESG_TIMEFTM_TIME_DELTA, /* [time <delta>] */ |
137 | DMESG_TIMEFTM_ISO8601 /* 2013-06-13T22:11:00,123456+0100 */ | |
776eabe7 | 138 | }; |
15a1e371 | 139 | #define is_timefmt(c, f) ((c)->time_fmt == (DMESG_TIMEFTM_ ##f)) |
776eabe7 | 140 | |
aca1633a KZ |
141 | struct dmesg_control { |
142 | /* bit arrays -- see include/bitops.h */ | |
143 | char levels[ARRAY_SIZE(level_names) / NBBY + 1]; | |
144 | char facilities[ARRAY_SIZE(facility_names) / NBBY + 1]; | |
145 | ||
bd304d92 | 146 | struct timeval lasttime; /* last printed timestamp */ |
60464b1f | 147 | struct tm lasttm; /* last localtime */ |
42fac79a | 148 | time_t boot_time; /* system boot time */ |
bd304d92 | 149 | |
e6471b9f KZ |
150 | int action; /* SYSLOG_ACTION_* */ |
151 | int method; /* DMESG_METHOD_* */ | |
298a073c KZ |
152 | |
153 | size_t bufsize; /* size of syslog buffer */ | |
154 | ||
7af23060 | 155 | int kmsg; /* /dev/kmsg file descriptor */ |
298a073c KZ |
156 | ssize_t kmsg_first_read;/* initial read() return code */ |
157 | char kmsg_buf[BUFSIZ];/* buffer to read kmsg data */ | |
e6471b9f | 158 | |
c672220f KZ |
159 | /* |
160 | * For the --file option we mmap whole file. The unnecessary (already | |
161 | * printed) pages are always unmapped. The result is that we have in | |
455fe9a0 | 162 | * memory only the currently used page(s). |
c672220f | 163 | */ |
9b3a6984 | 164 | char *filename; |
c672220f KZ |
165 | char *mmap_buff; |
166 | size_t pagesize; | |
776eabe7 | 167 | unsigned int time_fmt; /* time format */ |
c672220f | 168 | |
0fd12a96 KZ |
169 | unsigned int follow:1, /* wait for new messages */ |
170 | raw:1, /* raw mode */ | |
9feec79c KZ |
171 | fltr_lev:1, /* filter out by levels[] */ |
172 | fltr_fac:1, /* filter out by facilities[] */ | |
173 | decode:1, /* use "facility: level: " prefix */ | |
aa192520 | 174 | pager:1, /* pipe output into a pager */ |
098ce273 | 175 | color:1; /* colorize messages */ |
aca1633a | 176 | }; |
f4fa5b44 | 177 | |
a7ee94f2 KZ |
178 | struct dmesg_record { |
179 | const char *mesg; | |
180 | size_t mesg_size; | |
181 | ||
182 | int level; | |
183 | int facility; | |
bd304d92 | 184 | struct timeval tv; |
a7ee94f2 KZ |
185 | |
186 | const char *next; /* buffer with next unparsed record */ | |
187 | size_t next_size; /* size of the next buffer */ | |
188 | }; | |
189 | ||
ddca870a KZ |
190 | #define INIT_DMESG_RECORD(_r) do { \ |
191 | (_r)->mesg = NULL; \ | |
192 | (_r)->mesg_size = 0; \ | |
193 | (_r)->facility = -1; \ | |
194 | (_r)->level = -1; \ | |
195 | (_r)->tv.tv_sec = 0; \ | |
196 | (_r)->tv.tv_usec = 0; \ | |
197 | } while (0) | |
198 | ||
199 | static int read_kmsg(struct dmesg_control *ctl); | |
200 | ||
5aaee63c | 201 | static int set_level_color(int log_level, const char *mesg, size_t mesgsz) |
098ce273 OO |
202 | { |
203 | switch (log_level) { | |
204 | case LOG_ALERT: | |
5aaee63c | 205 | color_enable(DMESG_COLOR_ALERT); |
098ce273 OO |
206 | return 0; |
207 | case LOG_CRIT: | |
5aaee63c | 208 | color_enable(DMESG_COLOR_CRIT); |
098ce273 OO |
209 | return 0; |
210 | case LOG_ERR: | |
5aaee63c KZ |
211 | color_enable(DMESG_COLOR_ERR); |
212 | return 0; | |
213 | case LOG_WARNING: | |
214 | color_enable(DMESG_COLOR_WARN); | |
098ce273 OO |
215 | return 0; |
216 | default: | |
217 | break; | |
218 | } | |
219 | ||
5aaee63c KZ |
220 | /* well, sometimes the messges contains important keywords, but in |
221 | * non-warning/error messages | |
222 | */ | |
223 | if (memmem(mesg, mesgsz, "segfault at", 11)) { | |
224 | color_enable(DMESG_COLOR_SEGFAULT); | |
225 | return 0; | |
226 | } | |
227 | ||
098ce273 OO |
228 | return 1; |
229 | } | |
ddca870a | 230 | |
4a3b7949 | 231 | static void __attribute__((__noreturn__)) usage(FILE *out) |
15103c4b | 232 | { |
738767b9 | 233 | size_t i; |
f06ec64f | 234 | |
fbbc4c88 SK |
235 | fputs(USAGE_HEADER, out); |
236 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); | |
237 | fputs(USAGE_OPTIONS, out); | |
238 | fputs(_(" -C, --clear clear the kernel ring buffer\n"), out); | |
239 | fputs(_(" -c, --read-clear read and clear all messages\n"), out); | |
240 | fputs(_(" -D, --console-off disable printing messages to console\n"), out); | |
fbbc4c88 SK |
241 | fputs(_(" -E, --console-on enable printing messages to console\n"), out); |
242 | fputs(_(" -F, --file <file> use the file instead of the kernel log buffer\n"), out); | |
243 | fputs(_(" -f, --facility <list> restrict output to defined facilities\n"), out); | |
f0a3a1ca | 244 | fputs(_(" -H, --human human readable output\n"), out); |
fbbc4c88 | 245 | fputs(_(" -k, --kernel display kernel messages\n"), out); |
9bc2b51a | 246 | fputs(_(" -L, --color[=<when>] colorize messages (auto, always or never)\n"), out); |
fbbc4c88 SK |
247 | fputs(_(" -l, --level <list> restrict output to defined levels\n"), out); |
248 | fputs(_(" -n, --console-level <level> set level of messages printed to console\n"), out); | |
aa192520 | 249 | fputs(_(" -P, --nopager do not pipe output into a pager\n"), out); |
fbbc4c88 SK |
250 | fputs(_(" -r, --raw print the raw message buffer\n"), out); |
251 | fputs(_(" -S, --syslog force to use syslog(2) rather than /dev/kmsg\n"), out); | |
252 | fputs(_(" -s, --buffer-size <size> buffer size to query the kernel ring buffer\n"), out); | |
fbbc4c88 SK |
253 | fputs(_(" -u, --userspace display userspace messages\n"), out); |
254 | fputs(_(" -w, --follow wait for new messages\n"), out); | |
255 | fputs(_(" -x, --decode decode facility and level to readable string\n"), out); | |
06dd56f9 SK |
256 | fputs(_(" -d, --show-delta show time delta between printed messages\n"), out); |
257 | fputs(_(" -e, --reltime show local time and time delta in readable format\n"), out); | |
258 | fputs(_(" -T, --ctime show human readable timestamp\n"), out); | |
259 | fputs(_(" -t, --notime don't print messages timestamp\n"), out); | |
babf605d | 260 | fputs(_(" --time-format <format> show time stamp using format:\n" |
06dd56f9 SK |
261 | " [delta|reltime|ctime|notime|iso]\n" |
262 | "Suspending/resume will make ctime and iso timestamps inaccurate.\n"), out); | |
fbbc4c88 SK |
263 | fputs(USAGE_SEPARATOR, out); |
264 | fputs(USAGE_HELP, out); | |
265 | fputs(USAGE_VERSION, out); | |
dcd16b0f | 266 | fputs(_("\nSupported log facilities:\n"), out); |
fbbc4c88 | 267 | for (i = 0; i < ARRAY_SIZE(level_names); i++) |
963ac507 | 268 | fprintf(out, " %7s - %s\n", |
fbbc4c88 SK |
269 | facility_names[i].name, |
270 | _(facility_names[i].help)); | |
85f3cc55 | 271 | |
dcd16b0f | 272 | fputs(_("\nSupported log levels (priorities):\n"), out); |
fbbc4c88 | 273 | for (i = 0; i < ARRAY_SIZE(level_names); i++) |
963ac507 | 274 | fprintf(out, " %7s - %s\n", |
fbbc4c88 SK |
275 | level_names[i].name, |
276 | _(level_names[i].help)); | |
0a86a1a6 | 277 | fputs(USAGE_SEPARATOR, out); |
30b44cf1 | 278 | fprintf(out, USAGE_MAN_TAIL("dmesg(1)")); |
4a3b7949 | 279 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
6dbe3af9 KZ |
280 | } |
281 | ||
5ef05369 KZ |
282 | /* |
283 | * LEVEL ::= <number> | <name> | |
8c8fa302 BM |
284 | * <number> ::= @len is set: number in range <0..N>, where N < ARRAY_SIZE(level_names) |
285 | * ::= @len not set: number in range <1..N>, where N <= ARRAY_SIZE(level_names) | |
5ef05369 | 286 | * <name> ::= case-insensitive text |
8c8fa302 BM |
287 | * |
288 | * Note that @len argument is not set when parsing "-n <level>" command line | |
289 | * option. The console_level is intepreted as "log level less than the value". | |
290 | * | |
291 | * For example "dmesg -n 8" or "dmesg -n debug" enables debug console log | |
292 | * level by klogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, 8). The @str argument | |
293 | * has to be parsed to number in range <1..8>. | |
5ef05369 | 294 | */ |
5c8f6bc6 | 295 | static int parse_level(const char *str, size_t len) |
f06ec64f | 296 | { |
8c8fa302 BM |
297 | int offset = 0; |
298 | ||
5c8f6bc6 | 299 | if (!str) |
f06ec64f | 300 | return -1; |
8c8fa302 | 301 | if (!len) { |
5c8f6bc6 | 302 | len = strlen(str); |
8c8fa302 BM |
303 | offset = 1; |
304 | } | |
5c8f6bc6 | 305 | errno = 0; |
f06ec64f | 306 | |
5c8f6bc6 KZ |
307 | if (isdigit(*str)) { |
308 | char *end = NULL; | |
8c8fa302 | 309 | long x = strtol(str, &end, 10) - offset; |
5c8f6bc6 | 310 | |
738767b9 KZ |
311 | if (!errno && end && end > str && (size_t) (end - str) == len && |
312 | x >= 0 && (size_t) x < ARRAY_SIZE(level_names)) | |
8c8fa302 | 313 | return x + offset; |
5c8f6bc6 | 314 | } else { |
738767b9 KZ |
315 | size_t i; |
316 | ||
5c8f6bc6 KZ |
317 | for (i = 0; i < ARRAY_SIZE(level_names); i++) { |
318 | const char *n = level_names[i].name; | |
319 | ||
320 | if (strncasecmp(str, n, len) == 0 && *(n + len) == '\0') | |
8c8fa302 | 321 | return i + offset; |
5c8f6bc6 KZ |
322 | } |
323 | } | |
324 | ||
325 | if (errno) | |
326 | err(EXIT_FAILURE, _("failed to parse level '%s'"), str); | |
327 | ||
328 | errx(EXIT_FAILURE, _("unknown level '%s'"), str); | |
f06ec64f KZ |
329 | return -1; |
330 | } | |
331 | ||
5ef05369 KZ |
332 | /* |
333 | * FACILITY ::= <number> | <name> | |
334 | * <number> ::= number in range <0..N>, where N < ARRAY_SIZE(facility_names) | |
335 | * <name> ::= case-insensitive text | |
336 | */ | |
0e24df3b KZ |
337 | static int parse_facility(const char *str, size_t len) |
338 | { | |
0e24df3b KZ |
339 | if (!str) |
340 | return -1; | |
341 | if (!len) | |
342 | len = strlen(str); | |
343 | errno = 0; | |
344 | ||
345 | if (isdigit(*str)) { | |
346 | char *end = NULL; | |
738767b9 | 347 | long x = strtol(str, &end, 10); |
0e24df3b | 348 | |
738767b9 KZ |
349 | if (!errno && end && end > str && (size_t) (end - str) == len && |
350 | x >= 0 && (size_t) x < ARRAY_SIZE(facility_names)) | |
351 | return x; | |
0e24df3b | 352 | } else { |
738767b9 KZ |
353 | size_t i; |
354 | ||
0e24df3b KZ |
355 | for (i = 0; i < ARRAY_SIZE(facility_names); i++) { |
356 | const char *n = facility_names[i].name; | |
357 | ||
358 | if (strncasecmp(str, n, len) == 0 && *(n + len) == '\0') | |
359 | return i; | |
360 | } | |
361 | } | |
362 | ||
363 | if (errno) | |
364 | err(EXIT_FAILURE, _("failed to parse facility '%s'"), str); | |
365 | ||
366 | errx(EXIT_FAILURE, _("unknown facility '%s'"), str); | |
367 | return -1; | |
368 | } | |
369 | ||
5ef05369 KZ |
370 | /* |
371 | * Parses numerical prefix used for all messages in kernel ring buffer. | |
372 | * | |
373 | * Priorities/facilities are encoded into a single 32-bit quantity, where the | |
374 | * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility | |
375 | * (0-big number). | |
376 | * | |
ddca870a | 377 | * Note that the number has to end with '>' or ',' char. |
5ef05369 | 378 | */ |
636a6207 KZ |
379 | static const char *parse_faclev(const char *str, int *fac, int *lev) |
380 | { | |
381 | long num; | |
382 | char *end = NULL; | |
383 | ||
384 | if (!str) | |
385 | return str; | |
386 | ||
387 | errno = 0; | |
388 | num = strtol(str, &end, 10); | |
389 | ||
390 | if (!errno && end && end > str) { | |
391 | *fac = LOG_FAC(num); | |
392 | *lev = LOG_PRI(num); | |
85f3cc55 | 393 | |
738767b9 | 394 | if (*lev < 0 || (size_t) *lev > ARRAY_SIZE(level_names)) |
85f3cc55 | 395 | *lev = -1; |
738767b9 | 396 | if (*fac < 0 || (size_t) *fac > ARRAY_SIZE(facility_names)) |
85f3cc55 | 397 | *fac = -1; |
ddca870a | 398 | return end + 1; /* skip '<' or ',' */ |
636a6207 KZ |
399 | } |
400 | ||
401 | return str; | |
402 | } | |
403 | ||
ddca870a KZ |
404 | /* |
405 | * Parses timestamp from syslog message prefix, expected format: | |
406 | * | |
407 | * seconds.microseconds] | |
408 | * | |
409 | * the ']' is the timestamp field terminator. | |
410 | */ | |
411 | static const char *parse_syslog_timestamp(const char *str0, struct timeval *tv) | |
bd304d92 KZ |
412 | { |
413 | const char *str = str0; | |
414 | char *end = NULL; | |
415 | ||
416 | if (!str0) | |
417 | return str0; | |
418 | ||
419 | errno = 0; | |
420 | tv->tv_sec = strtol(str, &end, 10); | |
421 | ||
422 | if (!errno && end && *end == '.' && *(end + 1)) { | |
423 | str = end + 1; | |
424 | end = NULL; | |
425 | tv->tv_usec = strtol(str, &end, 10); | |
426 | } | |
427 | if (errno || !end || end == str || *end != ']') | |
428 | return str0; | |
429 | ||
430 | return end + 1; /* skip ']' */ | |
431 | } | |
432 | ||
ddca870a KZ |
433 | /* |
434 | * Parses timestamp from /dev/kmsg, expected formats: | |
435 | * | |
436 | * microseconds, | |
437 | * microseconds; | |
438 | * | |
439 | * the ',' is fields separators and ';' items terminator (for the last item) | |
440 | */ | |
441 | static const char *parse_kmsg_timestamp(const char *str0, struct timeval *tv) | |
442 | { | |
443 | const char *str = str0; | |
444 | char *end = NULL; | |
445 | uint64_t usec; | |
446 | ||
447 | if (!str0) | |
448 | return str0; | |
449 | ||
450 | errno = 0; | |
451 | usec = strtoumax(str, &end, 10); | |
452 | ||
453 | if (!errno && end && (*end == ';' || *end == ',')) { | |
454 | tv->tv_usec = usec % 1000000; | |
455 | tv->tv_sec = usec / 1000000; | |
456 | } else | |
457 | return str0; | |
458 | ||
459 | return end + 1; /* skip separator */ | |
460 | } | |
461 | ||
bd304d92 KZ |
462 | |
463 | static double time_diff(struct timeval *a, struct timeval *b) | |
464 | { | |
465 | return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / 1E6; | |
466 | } | |
467 | ||
7ff1f63f | 468 | static int get_syslog_buffer_size(void) |
eed99b2a KZ |
469 | { |
470 | int n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0); | |
471 | ||
472 | return n > 0 ? n : 0; | |
473 | } | |
474 | ||
42fac79a KZ |
475 | static time_t get_boot_time(void) |
476 | { | |
477 | struct sysinfo info; | |
478 | struct timeval tv; | |
479 | ||
480 | if (sysinfo(&info) != 0) | |
481 | warn(_("sysinfo failed")); | |
482 | else if (gettimeofday(&tv, NULL) != 0) | |
483 | warn(_("gettimeofday failed")); | |
484 | else | |
485 | return tv.tv_sec -= info.uptime; | |
486 | return 0; | |
487 | } | |
488 | ||
c672220f | 489 | /* |
7ff1f63f | 490 | * Reads messages from regular file by mmap |
c672220f | 491 | */ |
7ff1f63f | 492 | static ssize_t mmap_file_buffer(struct dmesg_control *ctl, char **buf) |
c672220f KZ |
493 | { |
494 | struct stat st; | |
9b3a6984 | 495 | int fd; |
c672220f | 496 | |
9b3a6984 KZ |
497 | if (!ctl->filename) |
498 | return -1; | |
499 | ||
500 | fd = open(ctl->filename, O_RDONLY); | |
c672220f | 501 | if (fd < 0) |
9b3a6984 | 502 | err(EXIT_FAILURE, _("cannot open %s"), ctl->filename); |
c672220f | 503 | if (fstat(fd, &st)) |
9b3a6984 | 504 | err(EXIT_FAILURE, _("stat failed %s"), ctl->filename); |
c672220f KZ |
505 | |
506 | *buf = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
507 | if (*buf == MAP_FAILED) | |
9b3a6984 | 508 | err(EXIT_FAILURE, _("cannot mmap: %s"), ctl->filename); |
c672220f KZ |
509 | ctl->mmap_buff = *buf; |
510 | ctl->pagesize = getpagesize(); | |
511 | close(fd); | |
512 | ||
513 | return st.st_size; | |
514 | } | |
515 | ||
5ef05369 | 516 | /* |
7ff1f63f | 517 | * Reads messages from kernel ring buffer by klogctl() |
5ef05369 | 518 | */ |
7ff1f63f | 519 | static ssize_t read_syslog_buffer(struct dmesg_control *ctl, char **buf) |
65e3eed9 KZ |
520 | { |
521 | size_t sz; | |
522 | int rc = -1; | |
65e3eed9 | 523 | |
e6471b9f KZ |
524 | if (ctl->bufsize) { |
525 | sz = ctl->bufsize + 8; | |
65e3eed9 | 526 | *buf = xmalloc(sz * sizeof(char)); |
e6471b9f | 527 | rc = klogctl(ctl->action, *buf, sz); |
65e3eed9 KZ |
528 | } else { |
529 | sz = 16392; | |
530 | while (1) { | |
531 | *buf = xmalloc(sz * sizeof(char)); | |
532 | rc = klogctl(SYSLOG_ACTION_READ_ALL, *buf, sz); | |
738767b9 KZ |
533 | if (rc < 0) |
534 | break; | |
535 | if ((size_t) rc != sz || sz > (1 << 28)) | |
65e3eed9 KZ |
536 | break; |
537 | free(*buf); | |
538 | *buf = NULL; | |
539 | sz *= 4; | |
540 | } | |
541 | ||
e6471b9f | 542 | if (rc > 0 && ctl->action == SYSLOG_ACTION_READ_CLEAR) |
65e3eed9 KZ |
543 | rc = klogctl(SYSLOG_ACTION_READ_CLEAR, *buf, sz); |
544 | } | |
545 | ||
546 | return rc; | |
547 | } | |
548 | ||
7ff1f63f KZ |
549 | /* |
550 | * Top level function to read messages | |
551 | */ | |
e6471b9f KZ |
552 | static ssize_t read_buffer(struct dmesg_control *ctl, char **buf) |
553 | { | |
554 | ssize_t n = -1; | |
555 | ||
556 | switch (ctl->method) { | |
557 | case DMESG_METHOD_MMAP: | |
7ff1f63f | 558 | n = mmap_file_buffer(ctl, buf); |
e6471b9f KZ |
559 | break; |
560 | case DMESG_METHOD_SYSLOG: | |
561 | if (!ctl->bufsize) | |
7ff1f63f | 562 | ctl->bufsize = get_syslog_buffer_size(); |
e6471b9f | 563 | |
7ff1f63f | 564 | n = read_syslog_buffer(ctl, buf); |
e6471b9f | 565 | break; |
ddca870a KZ |
566 | case DMESG_METHOD_KMSG: |
567 | /* | |
568 | * Since kernel 3.5.0 | |
569 | */ | |
570 | n = read_kmsg(ctl); | |
c677ffba KZ |
571 | if (n == 0 && ctl->action == SYSLOG_ACTION_READ_CLEAR) |
572 | n = klogctl(SYSLOG_ACTION_CLEAR, NULL, 0); | |
ddca870a | 573 | break; |
e6471b9f KZ |
574 | } |
575 | ||
576 | return n; | |
577 | } | |
578 | ||
b8300c0a KZ |
579 | static int fwrite_hex(const char *buf, size_t size, FILE *out) |
580 | { | |
738767b9 | 581 | size_t i; |
b8300c0a KZ |
582 | |
583 | for (i = 0; i < size; i++) { | |
584 | int rc = fprintf(out, "\\x%02x", buf[i]); | |
585 | if (rc < 0) | |
586 | return rc; | |
587 | } | |
588 | return 0; | |
589 | } | |
590 | ||
5ef05369 KZ |
591 | /* |
592 | * Prints to 'out' and non-printable chars are replaced with \x<hex> sequences. | |
593 | */ | |
b8300c0a KZ |
594 | static void safe_fwrite(const char *buf, size_t size, FILE *out) |
595 | { | |
738767b9 | 596 | size_t i; |
b8300c0a KZ |
597 | #ifdef HAVE_WIDECHAR |
598 | mbstate_t s; | |
599 | memset(&s, 0, sizeof (s)); | |
600 | #endif | |
601 | for (i = 0; i < size; i++) { | |
602 | const char *p = buf + i; | |
603 | int rc, hex = 0; | |
ddca870a | 604 | size_t len = 1; |
b8300c0a KZ |
605 | |
606 | #ifdef HAVE_WIDECHAR | |
607 | wchar_t wc; | |
0720766e | 608 | len = mbrtowc(&wc, p, size - i, &s); |
b8300c0a KZ |
609 | |
610 | if (len == 0) /* L'\0' */ | |
611 | return; | |
612 | ||
730d5e77 | 613 | if (len == (size_t)-1 || len == (size_t)-2) { /* invalid sequence */ |
b8300c0a KZ |
614 | memset(&s, 0, sizeof (s)); |
615 | len = hex = 1; | |
b8300c0a KZ |
616 | } else if (len > 1 && !iswprint(wc)) { /* non-printable multibyte */ |
617 | hex = 1; | |
b8300c0a | 618 | } |
0720766e PU |
619 | i += len - 1; |
620 | #else | |
621 | if (!isprint((unsigned int) *p) && | |
622 | !isspace((unsigned int) *p)) /* non-printable */ | |
623 | hex = 1; | |
624 | #endif | |
b8300c0a KZ |
625 | if (hex) |
626 | rc = fwrite_hex(p, len, out); | |
627 | else | |
85f3cc55 | 628 | rc = fwrite(p, 1, len, out) != len; |
b8300c0a KZ |
629 | if (rc != 0) |
630 | err(EXIT_FAILURE, _("write failed")); | |
631 | } | |
632 | } | |
633 | ||
ddca870a KZ |
634 | static const char *skip_item(const char *begin, const char *end, const char *sep) |
635 | { | |
636 | while (begin < end) { | |
637 | int c = *begin++; | |
638 | ||
639 | if (c == '\0' || strchr(sep, c)) | |
640 | break; | |
641 | } | |
642 | ||
643 | return begin; | |
644 | } | |
645 | ||
60464b1f KZ |
646 | /* |
647 | * Parses one record from syslog(2) buffer | |
648 | */ | |
7af23060 KZ |
649 | static int get_next_syslog_record(struct dmesg_control *ctl, |
650 | struct dmesg_record *rec) | |
f4fa5b44 | 651 | { |
738767b9 | 652 | size_t i; |
b8300c0a KZ |
653 | const char *begin = NULL; |
654 | ||
7af23060 KZ |
655 | if (ctl->method != DMESG_METHOD_MMAP && |
656 | ctl->method != DMESG_METHOD_SYSLOG) | |
657 | return -1; | |
658 | ||
a7ee94f2 KZ |
659 | if (!rec->next || !rec->next_size) |
660 | return 1; | |
f4fa5b44 | 661 | |
ddca870a | 662 | INIT_DMESG_RECORD(rec); |
a7ee94f2 | 663 | |
c672220f KZ |
664 | /* |
665 | * Unmap already printed file data from memory | |
666 | */ | |
667 | if (ctl->mmap_buff && (size_t) (rec->next - ctl->mmap_buff) > ctl->pagesize) { | |
668 | void *x = ctl->mmap_buff; | |
669 | ||
670 | ctl->mmap_buff += ctl->pagesize; | |
671 | munmap(x, ctl->pagesize); | |
672 | } | |
673 | ||
a7ee94f2 KZ |
674 | for (i = 0; i < rec->next_size; i++) { |
675 | const char *p = rec->next + i; | |
676 | const char *end = NULL; | |
b8300c0a KZ |
677 | |
678 | if (!begin) | |
679 | begin = p; | |
a7ee94f2 | 680 | if (i + 1 == rec->next_size) { |
b8300c0a | 681 | end = p + 1; |
f4fa5b44 | 682 | i++; |
22f69825 KZ |
683 | } else if (*p == '\n' && *(p + 1) == '<') |
684 | end = p; | |
685 | ||
a7ee94f2 KZ |
686 | if (begin && !*begin) |
687 | begin = NULL; /* zero(s) at the end of the buffer? */ | |
b8300c0a KZ |
688 | if (!begin || !end) |
689 | continue; | |
690 | if (end <= begin) | |
691 | continue; /* error or empty line? */ | |
692 | ||
636a6207 | 693 | if (*begin == '<') { |
bb901317 | 694 | if (ctl->fltr_lev || ctl->fltr_fac || ctl->decode || ctl->color) |
a7ee94f2 KZ |
695 | begin = parse_faclev(begin + 1, &rec->facility, |
696 | &rec->level); | |
ddca870a KZ |
697 | else |
698 | begin = skip_item(begin, end, ">"); | |
b8300c0a KZ |
699 | } |
700 | ||
bd304d92 KZ |
701 | if (*begin == '[' && (*(begin + 1) == ' ' || |
702 | isdigit(*(begin + 1)))) { | |
776eabe7 SK |
703 | |
704 | if (!is_timefmt(ctl, NONE)) | |
ddca870a | 705 | begin = parse_syslog_timestamp(begin + 1, &rec->tv); |
776eabe7 | 706 | else |
ddca870a KZ |
707 | begin = skip_item(begin, end, "]"); |
708 | ||
31c9099a KZ |
709 | if (begin < end && *begin == ' ') |
710 | begin++; | |
a7ee94f2 | 711 | } |
d74b8dfc | 712 | |
a7ee94f2 KZ |
713 | rec->mesg = begin; |
714 | rec->mesg_size = end - begin; | |
b8300c0a | 715 | |
a7ee94f2 KZ |
716 | rec->next_size -= end - rec->next; |
717 | rec->next = rec->next_size > 0 ? end + 1 : NULL; | |
59202950 PU |
718 | if (rec->next_size > 0) |
719 | rec->next_size--; | |
a7ee94f2 KZ |
720 | |
721 | return 0; | |
722 | } | |
723 | ||
724 | return 1; | |
725 | } | |
726 | ||
727 | static int accept_record(struct dmesg_control *ctl, struct dmesg_record *rec) | |
728 | { | |
729 | if (ctl->fltr_lev && (rec->facility < 0 || | |
730 | !isset(ctl->levels, rec->level))) | |
731 | return 0; | |
732 | ||
733 | if (ctl->fltr_fac && (rec->facility < 0 || | |
734 | !isset(ctl->facilities, rec->facility))) | |
735 | return 0; | |
736 | ||
737 | return 1; | |
738 | } | |
739 | ||
7ff1f63f | 740 | static void raw_print(struct dmesg_control *ctl, const char *buf, size_t size) |
c672220f KZ |
741 | { |
742 | int lastc = '\n'; | |
743 | ||
744 | if (!ctl->mmap_buff) { | |
745 | /* | |
746 | * Print whole ring buffer | |
747 | */ | |
748 | safe_fwrite(buf, size, stdout); | |
749 | lastc = buf[size - 1]; | |
750 | } else { | |
751 | /* | |
752 | * Print file in small chunks to save memory | |
753 | */ | |
754 | while (size) { | |
755 | size_t sz = size > ctl->pagesize ? ctl->pagesize : size; | |
756 | char *x = ctl->mmap_buff; | |
757 | ||
758 | safe_fwrite(x, sz, stdout); | |
759 | lastc = x[sz - 1]; | |
760 | size -= sz; | |
761 | ctl->mmap_buff += sz; | |
762 | munmap(x, sz); | |
763 | } | |
764 | } | |
765 | ||
766 | if (lastc != '\n') | |
767 | putchar('\n'); | |
768 | } | |
769 | ||
60464b1f KZ |
770 | static struct tm *record_localtime(struct dmesg_control *ctl, |
771 | struct dmesg_record *rec, | |
772 | struct tm *tm) | |
773 | { | |
774 | time_t t = ctl->boot_time + rec->tv.tv_sec; | |
775 | return localtime_r(&t, tm); | |
776 | } | |
777 | ||
0d1b3300 KZ |
778 | static char *record_ctime(struct dmesg_control *ctl, |
779 | struct dmesg_record *rec, | |
780 | char *buf, size_t bufsiz) | |
781 | { | |
60464b1f | 782 | struct tm tm; |
0d1b3300 | 783 | |
60464b1f | 784 | record_localtime(ctl, rec, &tm); |
0d1b3300 | 785 | |
60464b1f | 786 | if (strftime(buf, bufsiz, "%a %b %e %H:%M:%S %Y", &tm) == 0) |
0d1b3300 KZ |
787 | *buf = '\0'; |
788 | return buf; | |
789 | } | |
790 | ||
60464b1f KZ |
791 | static char *short_ctime(struct tm *tm, char *buf, size_t bufsiz) |
792 | { | |
793 | if (strftime(buf, bufsiz, "%b%e %H:%M", tm) == 0) | |
794 | *buf = '\0'; | |
795 | return buf; | |
796 | } | |
797 | ||
8a8be309 SK |
798 | static char *iso_8601_time(struct dmesg_control *ctl, struct dmesg_record *rec, |
799 | char *buf, size_t bufsiz) | |
800 | { | |
801 | struct tm tm; | |
802 | size_t len; | |
803 | record_localtime(ctl, rec, &tm); | |
804 | if (strftime(buf, bufsiz, "%Y-%m-%dT%H:%M:%S", &tm) == 0) { | |
805 | *buf = '\0'; | |
806 | return buf; | |
807 | } | |
808 | len = strlen(buf); | |
809 | snprintf(buf + len, bufsiz - len, ",%06d", (int)rec->tv.tv_usec); | |
810 | len = strlen(buf); | |
811 | strftime(buf + len, bufsiz - len, "%z", &tm); | |
812 | return buf; | |
813 | } | |
814 | ||
60464b1f KZ |
815 | static double record_count_delta(struct dmesg_control *ctl, |
816 | struct dmesg_record *rec) | |
817 | { | |
818 | double delta = 0; | |
819 | ||
820 | if (timerisset(&ctl->lasttime)) | |
821 | delta = time_diff(&rec->tv, &ctl->lasttime); | |
822 | ||
823 | ctl->lasttime = rec->tv; | |
824 | return delta; | |
825 | } | |
826 | ||
5aaee63c KZ |
827 | static const char *get_subsys_delimiter(const char *mesg, size_t mesg_size) |
828 | { | |
829 | const char *p = mesg; | |
830 | size_t sz = mesg_size; | |
831 | ||
832 | while (sz > 0) { | |
833 | const char *d = strnchr(p, sz, ':'); | |
834 | if (!d) | |
835 | return NULL; | |
836 | sz -= d - p; | |
837 | if (sz) { | |
838 | if (isblank(*(d + 1))) | |
839 | return d; | |
840 | p = d + 1; | |
841 | } | |
842 | } | |
843 | return NULL; | |
844 | } | |
845 | ||
60464b1f KZ |
846 | static void print_record(struct dmesg_control *ctl, |
847 | struct dmesg_record *rec) | |
7af23060 | 848 | { |
0d1b3300 | 849 | char buf[256]; |
098ce273 | 850 | int has_color = 0; |
5aaee63c KZ |
851 | const char *mesg; |
852 | size_t mesg_size; | |
7af23060 KZ |
853 | |
854 | if (!accept_record(ctl, rec)) | |
855 | return; | |
856 | ||
857 | if (!rec->mesg_size) { | |
858 | putchar('\n'); | |
859 | return; | |
860 | } | |
861 | ||
0d1b3300 KZ |
862 | /* |
863 | * compose syslog(2) compatible raw output -- used for /dev/kmsg for | |
864 | * backward compatibility with syslog(2) buffers only | |
865 | */ | |
37b04d6c | 866 | if (ctl->raw) { |
37b04d6c KZ |
867 | printf("<%d>[%5d.%06d] ", |
868 | LOG_MAKEPRI(rec->facility, rec->level), | |
869 | (int) rec->tv.tv_sec, | |
870 | (int) rec->tv.tv_usec); | |
871 | ||
872 | goto mesg; | |
873 | } | |
874 | ||
0d1b3300 KZ |
875 | /* |
876 | * facility : priority : | |
877 | */ | |
ae6288da SK |
878 | if (ctl->decode && |
879 | -1 < rec->level && rec->level < (int) ARRAY_SIZE(level_names) && | |
880 | -1 < rec->facility && rec->facility < (int) ARRAY_SIZE(facility_names)) | |
7af23060 KZ |
881 | printf("%-6s:%-6s: ", facility_names[rec->facility].name, |
882 | level_names[rec->level].name); | |
883 | ||
776eabe7 SK |
884 | if (ctl->color) |
885 | color_enable(DMESG_COLOR_TIME); | |
0d1b3300 | 886 | |
776eabe7 | 887 | switch (ctl->time_fmt) { |
60464b1f KZ |
888 | double delta; |
889 | struct tm cur; | |
776eabe7 SK |
890 | case DMESG_TIMEFTM_NONE: |
891 | break; | |
892 | case DMESG_TIMEFTM_CTIME: | |
893 | printf("[%s] ", record_ctime(ctl, rec, buf, sizeof(buf))); | |
894 | break; | |
895 | case DMESG_TIMEFTM_CTIME_DELTA: | |
896 | printf("[%s <%12.06f>] ", | |
897 | record_ctime(ctl, rec, buf, sizeof(buf)), | |
898 | record_count_delta(ctl, rec)); | |
899 | break; | |
900 | case DMESG_TIMEFTM_DELTA: | |
901 | printf("[<%12.06f>] ", record_count_delta(ctl, rec)); | |
902 | break; | |
903 | case DMESG_TIMEFTM_RELTIME: | |
60464b1f KZ |
904 | record_localtime(ctl, rec, &cur); |
905 | delta = record_count_delta(ctl, rec); | |
776eabe7 | 906 | if (cur.tm_min != ctl->lasttm.tm_min || |
60464b1f | 907 | cur.tm_hour != ctl->lasttm.tm_hour || |
5aaee63c | 908 | cur.tm_yday != ctl->lasttm.tm_yday) { |
60464b1f | 909 | printf("[%s] ", short_ctime(&cur, buf, sizeof(buf))); |
5aaee63c | 910 | } else { |
5aaee63c KZ |
911 | if (delta < 10) |
912 | printf("[ %+8.06f] ", delta); | |
913 | else | |
914 | printf("[ %+9.06f] ", delta); | |
915 | } | |
60464b1f | 916 | ctl->lasttm = cur; |
776eabe7 SK |
917 | break; |
918 | case DMESG_TIMEFTM_TIME: | |
919 | printf("[%5d.%06d] ", (int)rec->tv.tv_sec, (int)rec->tv.tv_usec); | |
920 | break; | |
921 | case DMESG_TIMEFTM_TIME_DELTA: | |
922 | printf("[%5d.%06d <%12.06f>] ", (int)rec->tv.tv_sec, | |
923 | (int)rec->tv.tv_usec, record_count_delta(ctl, rec)); | |
924 | break; | |
8a8be309 SK |
925 | case DMESG_TIMEFTM_ISO8601: |
926 | printf("%s ", iso_8601_time(ctl, rec, buf, sizeof(buf))); | |
927 | break; | |
776eabe7 SK |
928 | default: |
929 | abort(); | |
60464b1f | 930 | } |
7af23060 | 931 | |
776eabe7 SK |
932 | if (ctl->color) |
933 | color_disable(); | |
ddca870a | 934 | |
37b04d6c | 935 | mesg: |
5aaee63c KZ |
936 | mesg = rec->mesg; |
937 | mesg_size = rec->mesg_size; | |
938 | ||
939 | /* Colorize output */ | |
940 | if (ctl->color) { | |
941 | /* subsystem prefix */ | |
942 | const char *subsys = get_subsys_delimiter(mesg, mesg_size); | |
943 | if (subsys) { | |
944 | color_enable(DMESG_COLOR_SUBSYS); | |
945 | safe_fwrite(mesg, subsys - mesg, stdout); | |
946 | color_disable(); | |
947 | ||
948 | mesg_size -= subsys - mesg; | |
949 | mesg = subsys; | |
950 | } | |
951 | /* error, alert .. etc. colors */ | |
952 | has_color = set_level_color(rec->level, mesg, mesg_size) == 0; | |
953 | safe_fwrite(mesg, mesg_size, stdout); | |
954 | if (has_color) | |
955 | color_disable(); | |
956 | } else | |
957 | safe_fwrite(mesg, mesg_size, stdout); | |
098ce273 | 958 | |
5aaee63c | 959 | if (*(mesg + mesg_size - 1) != '\n') |
7af23060 KZ |
960 | putchar('\n'); |
961 | } | |
962 | ||
a7ee94f2 KZ |
963 | /* |
964 | * Prints the 'buf' kernel ring buffer; the messages are filtered out according | |
965 | * to 'levels' and 'facilities' bitarrays. | |
966 | */ | |
7ff1f63f KZ |
967 | static void print_buffer(struct dmesg_control *ctl, |
968 | const char *buf, size_t size) | |
a7ee94f2 KZ |
969 | { |
970 | struct dmesg_record rec = { .next = buf, .next_size = size }; | |
971 | ||
972 | if (ctl->raw) { | |
7ff1f63f | 973 | raw_print(ctl, buf, size); |
a7ee94f2 KZ |
974 | return; |
975 | } | |
976 | ||
7af23060 KZ |
977 | while (get_next_syslog_record(ctl, &rec) == 0) |
978 | print_record(ctl, &rec); | |
979 | } | |
a7ee94f2 | 980 | |
4ceb601d MB |
981 | static ssize_t read_kmsg_one(struct dmesg_control *ctl) |
982 | { | |
983 | ssize_t size; | |
984 | ||
985 | /* kmsg returns EPIPE if record was modified while reading */ | |
986 | do { | |
987 | size = read(ctl->kmsg, ctl->kmsg_buf, | |
988 | sizeof(ctl->kmsg_buf) - 1); | |
989 | } while (size < 0 && errno == EPIPE); | |
990 | ||
991 | return size; | |
992 | } | |
993 | ||
7af23060 KZ |
994 | static int init_kmsg(struct dmesg_control *ctl) |
995 | { | |
0fd12a96 KZ |
996 | int mode = O_RDONLY; |
997 | ||
998 | if (!ctl->follow) | |
999 | mode |= O_NONBLOCK; | |
1000 | ||
1001 | ctl->kmsg = open("/dev/kmsg", mode); | |
c677ffba KZ |
1002 | if (ctl->kmsg < 0) |
1003 | return -1; | |
1004 | ||
1005 | /* | |
1006 | * Seek after the last record available at the time | |
1007 | * the last SYSLOG_ACTION_CLEAR was issued. | |
1008 | * | |
1009 | * ... otherwise SYSLOG_ACTION_CLEAR will have no effect for kmsg. | |
1010 | */ | |
1011 | lseek(ctl->kmsg, 0, SEEK_DATA); | |
298a073c KZ |
1012 | |
1013 | /* | |
1014 | * Old kernels (<3.5) allow to successfully open /dev/kmsg for | |
1015 | * read-only, but read() returns -EINVAL :-((( | |
1016 | * | |
1017 | * Let's try to read the first record. The record is later processed in | |
1018 | * read_kmsg(). | |
1019 | */ | |
4ceb601d | 1020 | ctl->kmsg_first_read = read_kmsg_one(ctl); |
298a073c KZ |
1021 | if (ctl->kmsg_first_read < 0) { |
1022 | close(ctl->kmsg); | |
1023 | ctl->kmsg = -1; | |
1024 | return -1; | |
1025 | } | |
1026 | ||
c677ffba | 1027 | return 0; |
f4fa5b44 KZ |
1028 | } |
1029 | ||
ddca870a KZ |
1030 | /* |
1031 | * /dev/kmsg record format: | |
1032 | * | |
1033 | * faclev,seqnum,timestamp[optional, ...];messgage\n | |
1034 | * TAGNAME=value | |
1035 | * ... | |
1036 | * | |
1037 | * - fields are separated by ',' | |
1038 | * - last field is terminated by ';' | |
1039 | * | |
1040 | */ | |
1041 | #define LAST_KMSG_FIELD(s) (!s || !*s || *(s - 1) == ';') | |
1042 | ||
1043 | static int parse_kmsg_record(struct dmesg_control *ctl, | |
1044 | struct dmesg_record *rec, | |
1045 | char *buf, | |
1046 | size_t sz) | |
1047 | { | |
1048 | const char *p = buf, *end; | |
1049 | ||
1050 | if (sz == 0 || !buf || !*buf) | |
1051 | return -1; | |
1052 | ||
1053 | end = buf + (sz - 1); | |
1054 | INIT_DMESG_RECORD(rec); | |
1055 | ||
1056 | while (p < end && isspace(*p)) | |
1057 | p++; | |
1058 | ||
1059 | /* A) priority and facility */ | |
098ce273 OO |
1060 | if (ctl->fltr_lev || ctl->fltr_fac || ctl->decode || |
1061 | ctl->raw || ctl->color) | |
ddca870a KZ |
1062 | p = parse_faclev(p, &rec->facility, &rec->level); |
1063 | else | |
1064 | p = skip_item(p, end, ","); | |
1065 | if (LAST_KMSG_FIELD(p)) | |
1066 | goto mesg; | |
1067 | ||
1068 | /* B) sequence number */ | |
1069 | p = skip_item(p, end, ",;"); | |
1070 | if (LAST_KMSG_FIELD(p)) | |
1071 | goto mesg; | |
1072 | ||
1073 | /* C) timestamp */ | |
776eabe7 | 1074 | if (is_timefmt(ctl, NONE)) |
ddca870a KZ |
1075 | p = skip_item(p, end, ",;"); |
1076 | else | |
1077 | p = parse_kmsg_timestamp(p, &rec->tv); | |
1078 | if (LAST_KMSG_FIELD(p)) | |
1079 | goto mesg; | |
1080 | ||
1081 | /* D) optional fields (ignore) */ | |
1082 | p = skip_item(p, end, ";"); | |
1083 | if (LAST_KMSG_FIELD(p)) | |
1084 | goto mesg; | |
1085 | ||
1086 | mesg: | |
1087 | /* E) message text */ | |
1088 | rec->mesg = p; | |
1089 | p = skip_item(p, end, "\n"); | |
1090 | ||
1091 | if (!p) | |
1092 | return -1; | |
1093 | ||
1094 | rec->mesg_size = p - rec->mesg; | |
1095 | ||
1096 | /* | |
1097 | * Kernel escapes non-printable characters, unfortuately kernel | |
1098 | * definition of "non-printable" is too strict. On UTF8 console we can | |
1099 | * print many chars, so let's decode from kernel. | |
1100 | */ | |
1101 | unhexmangle_to_buffer(rec->mesg, (char *) rec->mesg, rec->mesg_size + 1); | |
1102 | ||
1103 | /* F) message tags (ignore) */ | |
1104 | ||
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | /* | |
1109 | * Note that each read() call for /dev/kmsg returns always one record. It means | |
1110 | * that we don't have to read whole message buffer before the records parsing. | |
1111 | * | |
1112 | * So this function does not compose one huge buffer (like read_syslog_buffer()) | |
1113 | * and print_buffer() is unnecessary. All is done in this function. | |
1114 | * | |
1115 | * Returns 0 on success, -1 on error. | |
1116 | */ | |
1117 | static int read_kmsg(struct dmesg_control *ctl) | |
1118 | { | |
ddca870a | 1119 | struct dmesg_record rec; |
298a073c | 1120 | ssize_t sz; |
ddca870a KZ |
1121 | |
1122 | if (ctl->method != DMESG_METHOD_KMSG || ctl->kmsg < 0) | |
1123 | return -1; | |
1124 | ||
298a073c KZ |
1125 | /* |
1126 | * The very first read() call is done in kmsg_init() where we test | |
1127 | * /dev/kmsg usability. The return code from the initial read() is | |
1128 | * stored in ctl->kmsg_first_read; | |
1129 | */ | |
1130 | sz = ctl->kmsg_first_read; | |
ddca870a | 1131 | |
298a073c KZ |
1132 | while (sz > 0) { |
1133 | *(ctl->kmsg_buf + sz) = '\0'; /* for debug messages */ | |
ddca870a | 1134 | |
298a073c KZ |
1135 | if (parse_kmsg_record(ctl, &rec, |
1136 | ctl->kmsg_buf, (size_t) sz) == 0) | |
1137 | print_record(ctl, &rec); | |
37b04d6c | 1138 | |
4ceb601d | 1139 | sz = read_kmsg_one(ctl); |
298a073c | 1140 | } |
ddca870a KZ |
1141 | |
1142 | return 0; | |
1143 | } | |
1144 | ||
babf605d SK |
1145 | static int which_time_format(const char *optarg) |
1146 | { | |
1147 | if (!strcmp(optarg, "notime")) | |
1148 | return DMESG_TIMEFTM_NONE; | |
1149 | if (!strcmp(optarg, "ctime")) | |
1150 | return DMESG_TIMEFTM_CTIME; | |
1151 | if (!strcmp(optarg, "delta")) | |
1152 | return DMESG_TIMEFTM_DELTA; | |
1153 | if (!strcmp(optarg, "reltime")) | |
1154 | return DMESG_TIMEFTM_RELTIME; | |
8a8be309 SK |
1155 | if (!strcmp(optarg, "iso")) |
1156 | return DMESG_TIMEFTM_ISO8601; | |
babf605d SK |
1157 | errx(EXIT_FAILURE, _("unknown time format: %s"), optarg); |
1158 | } | |
1159 | ||
15103c4b MP |
1160 | int main(int argc, char *argv[]) |
1161 | { | |
1162 | char *buf = NULL; | |
aa192520 | 1163 | int c, nopager = 0; |
48c5c662 | 1164 | int console_level = 0; |
6e9b06cc | 1165 | int klog_rc = 0; |
776eabe7 | 1166 | int delta = 0; |
6e9b06cc | 1167 | ssize_t n; |
9b3a6984 | 1168 | static struct dmesg_control ctl = { |
e6471b9f | 1169 | .filename = NULL, |
7ff1f63f | 1170 | .action = SYSLOG_ACTION_READ_ALL, |
ed61acc2 | 1171 | .method = DMESG_METHOD_KMSG, |
7af23060 | 1172 | .kmsg = -1, |
776eabe7 | 1173 | .time_fmt = DMESG_TIMEFTM_TIME, |
9b3a6984 | 1174 | }; |
9bc2b51a | 1175 | int colormode = UL_COLORMODE_NEVER; |
babf605d SK |
1176 | enum { |
1177 | OPT_TIME_FORMAT = CHAR_MAX + 1, | |
1178 | }; | |
6dbe3af9 | 1179 | |
4a3b7949 | 1180 | static const struct option longopts[] = { |
4a3b7949 | 1181 | { "buffer-size", required_argument, NULL, 's' }, |
5ef05369 | 1182 | { "clear", no_argument, NULL, 'C' }, |
9bc2b51a | 1183 | { "color", optional_argument, NULL, 'L' }, |
4a3b7949 | 1184 | { "console-level", required_argument, NULL, 'n' }, |
bd304d92 KZ |
1185 | { "console-off", no_argument, NULL, 'D' }, |
1186 | { "console-on", no_argument, NULL, 'E' }, | |
5ef05369 | 1187 | { "decode", no_argument, NULL, 'x' }, |
c672220f | 1188 | { "file", required_argument, NULL, 'F' }, |
0e24df3b | 1189 | { "facility", required_argument, NULL, 'f' }, |
0fd12a96 | 1190 | { "follow", no_argument, NULL, 'w' }, |
f0a3a1ca | 1191 | { "human", no_argument, NULL, 'H' }, |
4a3b7949 | 1192 | { "help", no_argument, NULL, 'h' }, |
25000180 | 1193 | { "kernel", no_argument, NULL, 'k' }, |
5ef05369 | 1194 | { "level", required_argument, NULL, 'l' }, |
ed61acc2 | 1195 | { "syslog", no_argument, NULL, 'S' }, |
5ef05369 KZ |
1196 | { "raw", no_argument, NULL, 'r' }, |
1197 | { "read-clear", no_argument, NULL, 'c' }, | |
60464b1f | 1198 | { "reltime", no_argument, NULL, 'e' }, |
bd304d92 | 1199 | { "show-delta", no_argument, NULL, 'd' }, |
42fac79a | 1200 | { "ctime", no_argument, NULL, 'T' }, |
d74b8dfc | 1201 | { "notime", no_argument, NULL, 't' }, |
aa192520 | 1202 | { "nopager", no_argument, NULL, 'P' }, |
25000180 | 1203 | { "userspace", no_argument, NULL, 'u' }, |
5ef05369 | 1204 | { "version", no_argument, NULL, 'V' }, |
babf605d | 1205 | { "time-format", required_argument, NULL, OPT_TIME_FORMAT }, |
4a3b7949 KZ |
1206 | { NULL, 0, NULL, 0 } |
1207 | }; | |
1208 | ||
43d2eeef KZ |
1209 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
1210 | { 'C','D','E','c','n' }, /* clear,off,on,read-clear,level*/ | |
f0a3a1ca | 1211 | { 'H','r' }, /* human, raw */ |
5aaee63c | 1212 | { 'L','r' }, /* color, raw */ |
43d2eeef KZ |
1213 | { 'S','w' }, /* syslog,follow */ |
1214 | { 0 } | |
1215 | }; | |
1216 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1217 | ||
df1dddf9 KZ |
1218 | setlocale(LC_ALL, ""); |
1219 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1220 | textdomain(PACKAGE); | |
efb8854f | 1221 | atexit(close_stdout); |
7eda085c | 1222 | |
9bc2b51a | 1223 | while ((c = getopt_long(argc, argv, "CcDdEeF:f:HhkL::l:n:iPrSs:TtuVwx", |
aca1633a | 1224 | longopts, NULL)) != -1) { |
43d2eeef KZ |
1225 | |
1226 | err_exclusive_options(c, longopts, excl, excl_st); | |
1227 | ||
df1dddf9 | 1228 | switch (c) { |
04199860 | 1229 | case 'C': |
e6471b9f | 1230 | ctl.action = SYSLOG_ACTION_CLEAR; |
04199860 | 1231 | break; |
df1dddf9 | 1232 | case 'c': |
e6471b9f | 1233 | ctl.action = SYSLOG_ACTION_READ_CLEAR; |
df1dddf9 | 1234 | break; |
bd304d92 | 1235 | case 'D': |
e6471b9f | 1236 | ctl.action = SYSLOG_ACTION_CONSOLE_OFF; |
2170174e | 1237 | break; |
bd304d92 | 1238 | case 'd': |
776eabe7 | 1239 | delta = 1; |
bd304d92 KZ |
1240 | break; |
1241 | case 'E': | |
e6471b9f | 1242 | ctl.action = SYSLOG_ACTION_CONSOLE_ON; |
2170174e | 1243 | break; |
60464b1f | 1244 | case 'e': |
776eabe7 | 1245 | ctl.time_fmt = DMESG_TIMEFTM_RELTIME; |
60464b1f | 1246 | break; |
c672220f | 1247 | case 'F': |
9b3a6984 | 1248 | ctl.filename = optarg; |
e6471b9f | 1249 | ctl.method = DMESG_METHOD_MMAP; |
c672220f | 1250 | break; |
0e24df3b | 1251 | case 'f': |
aca1633a | 1252 | ctl.fltr_fac = 1; |
c87638ad KZ |
1253 | if (string_to_bitarray(optarg, |
1254 | ctl.facilities, parse_facility) < 0) | |
1255 | return EXIT_FAILURE; | |
0e24df3b | 1256 | break; |
f0a3a1ca | 1257 | case 'H': |
776eabe7 | 1258 | ctl.time_fmt = DMESG_TIMEFTM_RELTIME; |
81bd88e4 | 1259 | colormode = UL_COLORMODE_AUTO; |
aa192520 | 1260 | ctl.pager = 1; |
f0a3a1ca | 1261 | break; |
5ef05369 KZ |
1262 | case 'h': |
1263 | usage(stdout); | |
df1dddf9 | 1264 | break; |
25000180 | 1265 | case 'k': |
aca1633a KZ |
1266 | ctl.fltr_fac = 1; |
1267 | setbit(ctl.facilities, FAC_BASE(LOG_KERN)); | |
25000180 | 1268 | break; |
098ce273 | 1269 | case 'L': |
9bc2b51a | 1270 | colormode = UL_COLORMODE_AUTO; |
b7faf991 KZ |
1271 | if (optarg) |
1272 | colormode = colormode_or_err(optarg, | |
1273 | _("unsupported color mode")); | |
098ce273 | 1274 | break; |
636a6207 | 1275 | case 'l': |
aca1633a | 1276 | ctl.fltr_lev= 1; |
c87638ad KZ |
1277 | if (string_to_bitarray(optarg, |
1278 | ctl.levels, parse_level) < 0) | |
1279 | return EXIT_FAILURE; | |
636a6207 | 1280 | break; |
5ef05369 | 1281 | case 'n': |
e6471b9f | 1282 | ctl.action = SYSLOG_ACTION_CONSOLE_LEVEL; |
5ef05369 KZ |
1283 | console_level = parse_level(optarg, 0); |
1284 | break; | |
aa192520 KZ |
1285 | case 'P': |
1286 | nopager = 1; | |
1287 | break; | |
11ea22d5 | 1288 | case 'r': |
aca1633a | 1289 | ctl.raw = 1; |
776eabe7 SK |
1290 | ctl.time_fmt = DMESG_TIMEFTM_NONE; |
1291 | delta = 0; | |
11ea22d5 | 1292 | break; |
ed61acc2 KZ |
1293 | case 'S': |
1294 | ctl.method = DMESG_METHOD_SYSLOG; | |
1295 | break; | |
df1dddf9 | 1296 | case 's': |
e6471b9f KZ |
1297 | ctl.bufsize = strtou32_or_err(optarg, |
1298 | _("invalid buffer size argument")); | |
1299 | if (ctl.bufsize < 4096) | |
1300 | ctl.bufsize = 4096; | |
df1dddf9 | 1301 | break; |
42fac79a | 1302 | case 'T': |
776eabe7 | 1303 | ctl.time_fmt = DMESG_TIMEFTM_CTIME; |
42fac79a | 1304 | break; |
d74b8dfc | 1305 | case 't': |
776eabe7 SK |
1306 | ctl.time_fmt = DMESG_TIMEFTM_NONE; |
1307 | delta = 0; | |
d74b8dfc | 1308 | break; |
25000180 | 1309 | case 'u': |
aca1633a | 1310 | ctl.fltr_fac = 1; |
738767b9 | 1311 | for (n = 1; (size_t) n < ARRAY_SIZE(facility_names); n++) |
aca1633a | 1312 | setbit(ctl.facilities, n); |
25000180 | 1313 | break; |
4a3b7949 KZ |
1314 | case 'V': |
1315 | printf(_("%s from %s\n"), program_invocation_short_name, | |
1316 | PACKAGE_STRING); | |
1317 | return EXIT_SUCCESS; | |
0fd12a96 | 1318 | case 'w': |
0fd12a96 KZ |
1319 | ctl.follow = 1; |
1320 | break; | |
5ef05369 | 1321 | case 'x': |
aca1633a | 1322 | ctl.decode = 1; |
4a3b7949 | 1323 | break; |
babf605d SK |
1324 | case OPT_TIME_FORMAT: |
1325 | ctl.time_fmt = which_time_format(optarg); | |
1326 | break; | |
df1dddf9 KZ |
1327 | case '?': |
1328 | default: | |
4a3b7949 | 1329 | usage(stderr); |
df1dddf9 KZ |
1330 | } |
1331 | } | |
1332 | argc -= optind; | |
1333 | argv += optind; | |
e6c379eb | 1334 | |
15103c4b | 1335 | if (argc > 1) |
4a3b7949 | 1336 | usage(stderr); |
6dbe3af9 | 1337 | |
15a1e371 KZ |
1338 | if (is_timefmt(&ctl, RELTIME) || |
1339 | is_timefmt(&ctl, CTIME) || | |
1340 | is_timefmt(&ctl, ISO8601)) { | |
1341 | ||
f0a3a1ca KZ |
1342 | ctl.boot_time = get_boot_time(); |
1343 | if (!ctl.boot_time) | |
776eabe7 | 1344 | ctl.time_fmt = DMESG_TIMEFTM_NONE; |
f0a3a1ca | 1345 | } |
9bc2b51a | 1346 | |
776eabe7 SK |
1347 | if (delta) |
1348 | switch (ctl.time_fmt) { | |
1349 | case DMESG_TIMEFTM_CTIME: | |
1350 | ctl.time_fmt = DMESG_TIMEFTM_CTIME_DELTA; | |
1351 | break; | |
1352 | case DMESG_TIMEFTM_TIME: | |
1353 | ctl.time_fmt = DMESG_TIMEFTM_TIME_DELTA; | |
1354 | break; | |
1355 | default: | |
1356 | ctl.time_fmt = DMESG_TIMEFTM_DELTA; | |
1357 | } | |
1358 | ||
1359 | if (ctl.raw | |
15a1e371 KZ |
1360 | && (ctl.fltr_lev || ctl.fltr_fac || ctl.decode |
1361 | || !is_timefmt(&ctl, NONE))) | |
776eabe7 SK |
1362 | errx(EXIT_FAILURE, _("--raw can't be used together with level, " |
1363 | "facility, decode, delta, ctime or notime options")); | |
1364 | ||
9bc2b51a | 1365 | ctl.color = colors_init(colormode) ? 1 : 0; |
f0a3a1ca | 1366 | |
aa192520 KZ |
1367 | ctl.pager = nopager ? 0 : ctl.pager; |
1368 | if (ctl.pager) | |
1369 | setup_pager(); | |
1370 | ||
e6471b9f | 1371 | switch (ctl.action) { |
48c5c662 KZ |
1372 | case SYSLOG_ACTION_READ_ALL: |
1373 | case SYSLOG_ACTION_READ_CLEAR: | |
7af23060 KZ |
1374 | if (ctl.method == DMESG_METHOD_KMSG && init_kmsg(&ctl) != 0) |
1375 | ctl.method = DMESG_METHOD_SYSLOG; | |
aa192520 KZ |
1376 | if (ctl.pager) |
1377 | setup_pager(); | |
6e9b06cc KZ |
1378 | n = read_buffer(&ctl, &buf); |
1379 | if (n > 0) | |
1380 | print_buffer(&ctl, buf, n); | |
7ff1f63f | 1381 | if (!ctl.mmap_buff) |
c672220f | 1382 | free(buf); |
6e9b06cc KZ |
1383 | if (n < 0) |
1384 | err(EXIT_FAILURE, _("read kernel buffer failed")); | |
1385 | if (ctl.kmsg >= 0) | |
1386 | close(ctl.kmsg); | |
48c5c662 | 1387 | break; |
04199860 | 1388 | case SYSLOG_ACTION_CLEAR: |
2170174e KZ |
1389 | case SYSLOG_ACTION_CONSOLE_OFF: |
1390 | case SYSLOG_ACTION_CONSOLE_ON: | |
6e9b06cc | 1391 | klog_rc = klogctl(ctl.action, NULL, 0); |
04199860 | 1392 | break; |
48c5c662 | 1393 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
6e9b06cc | 1394 | klog_rc = klogctl(ctl.action, NULL, console_level); |
48c5c662 KZ |
1395 | break; |
1396 | default: | |
1397 | errx(EXIT_FAILURE, _("unsupported command")); | |
1398 | break; | |
df1dddf9 | 1399 | } |
6dbe3af9 | 1400 | |
7af23060 | 1401 | |
6e9b06cc | 1402 | if (klog_rc) |
15103c4b | 1403 | err(EXIT_FAILURE, _("klogctl failed")); |
6dbe3af9 | 1404 | |
15103c4b | 1405 | return EXIT_SUCCESS; |
6dbe3af9 | 1406 | } |