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