2 * dmesg.c -- Print out the contents of the kernel ring buffer
4 * Copyright (C) 1993 Theodore Ts'o <tytso@athena.mit.edu>
5 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
7 * This program comes with ABSOLUTELY NO WARRANTY.
13 #include <sys/syslog.h>
15 #include <sys/sysinfo.h>
19 #include <sys/types.h>
32 #include "closestream.h"
34 #include "timeutils.h"
35 #include "monotonic.h"
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
67 const char *scheme
; /* name used in termina-colors.d/dmesg.scheme */
68 const char *dflt
; /* default color ESC sequence */
74 DMESG_COLOR_TIMEBREAK
,
82 static const struct dmesg_color colors
[] =
84 [DMESG_COLOR_SUBSYS
] = { "subsys", UL_COLOR_BROWN
},
85 [DMESG_COLOR_TIME
] = { "time", UL_COLOR_GREEN
},
86 [DMESG_COLOR_TIMEBREAK
] = { "timebreak",UL_COLOR_GREEN UL_COLOR_BOLD
},
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
}
94 #define dmesg_enable_color(_id) \
95 color_scheme_enable(colors[_id].scheme, colors[_id].dflt);
98 * Priority and facility names
106 * Priority names -- based on sys/syslog.h
108 static const struct dmesg_name level_names
[] =
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") }
121 * sys/syslog.h uses (f << 3) for all facility codes.
122 * We want to use the codes as array indexes, so shift back...
124 * Note that libc LOG_FAC() macro returns the base codes, not the
127 #define FAC_BASE(f) ((f) >> 3)
128 #define LOG_RAW_FAC_PRI(fac, pri) LOG_MAKEPRI((fac << 3), (pri))
130 static const struct dmesg_name facility_names
[] =
132 [FAC_BASE(LOG_KERN
)] = { "kern", N_("kernel messages") },
133 [FAC_BASE(LOG_USER
)] = { "user", N_("random user-level messages") },
134 [FAC_BASE(LOG_MAIL
)] = { "mail", N_("mail system") },
135 [FAC_BASE(LOG_DAEMON
)] = { "daemon", N_("system daemons") },
136 [FAC_BASE(LOG_AUTH
)] = { "auth", N_("security/authorization messages") },
137 [FAC_BASE(LOG_SYSLOG
)] = { "syslog", N_("messages generated internally by syslogd") },
138 [FAC_BASE(LOG_LPR
)] = { "lpr", N_("line printer subsystem") },
139 [FAC_BASE(LOG_NEWS
)] = { "news", N_("network news subsystem") },
140 [FAC_BASE(LOG_UUCP
)] = { "uucp", N_("UUCP subsystem") },
141 [FAC_BASE(LOG_CRON
)] = { "cron", N_("clock daemon") },
142 [FAC_BASE(LOG_AUTHPRIV
)] = { "authpriv", N_("security/authorization messages (private)") },
143 [FAC_BASE(LOG_FTP
)] = { "ftp", N_("FTP daemon") },
146 /* supported methods to read message buffer
149 DMESG_METHOD_KMSG
, /* read messages from /dev/kmsg (default) */
150 DMESG_METHOD_SYSLOG
, /* klogctl() buffer */
151 DMESG_METHOD_MMAP
/* mmap file with records (see --file) */
155 DMESG_TIMEFTM_NONE
= 0,
156 DMESG_TIMEFTM_CTIME
, /* [ctime] */
157 DMESG_TIMEFTM_CTIME_DELTA
, /* [ctime <delta>] */
158 DMESG_TIMEFTM_DELTA
, /* [<delta>] */
159 DMESG_TIMEFTM_RELTIME
, /* [relative] */
160 DMESG_TIMEFTM_TIME
, /* [time] */
161 DMESG_TIMEFTM_TIME_DELTA
, /* [time <delta>] */
162 DMESG_TIMEFTM_ISO8601
/* 2013-06-13T22:11:00,123456+0100 */
164 #define is_timefmt(c, f) ((c)->time_fmt == (DMESG_TIMEFTM_ ##f))
166 struct dmesg_control
{
167 /* bit arrays -- see include/bitops.h */
168 char levels
[ARRAY_SIZE(level_names
) / NBBY
+ 1];
169 char facilities
[ARRAY_SIZE(facility_names
) / NBBY
+ 1];
171 struct timeval lasttime
; /* last printed timestamp */
172 struct tm lasttm
; /* last localtime */
173 struct timeval boot_time
; /* system boot time */
174 usec_t suspended_time
; /* time spent in suspended state */
176 int action
; /* SYSLOG_ACTION_* */
177 int method
; /* DMESG_METHOD_* */
179 size_t bufsize
; /* size of syslog buffer */
181 int kmsg
; /* /dev/kmsg file descriptor */
182 ssize_t kmsg_first_read
;/* initial read() return code */
184 * the kernel will give EINVAL if we do read() on /proc/kmsg with
185 * length insufficient for the next message. messages may be up to
186 * PRINTK_MESSAGE_MAX, which is defined as 2048, so we must be
187 * able to buffer at least that much in one call
189 char kmsg_buf
[2048]; /* buffer to read kmsg data */
191 usec_t since
; /* filter records by time */
192 usec_t until
; /* filter records by time */
195 * For the --file option we mmap whole file. The unnecessary (already
196 * printed) pages are always unmapped. The result is that we have in
197 * memory only the currently used page(s).
202 unsigned int time_fmt
; /* time format */
204 struct ul_jsonwrt jfmt
; /* -J formatting */
206 unsigned int follow
:1, /* wait for new messages */
207 end
:1, /* seek to the of buffer */
208 raw
:1, /* raw mode */
209 noesc
:1, /* no escape */
210 fltr_lev
:1, /* filter out by levels[] */
211 fltr_fac
:1, /* filter out by facilities[] */
212 decode
:1, /* use "facility: level: " prefix */
213 pager
:1, /* pipe output into a pager */
214 color
:1, /* colorize messages */
215 json
:1, /* JSON output */
216 force_prefix
:1; /* force timestamp and decode prefix
218 int indent
; /* due to timestamps if newline */
221 struct dmesg_record
{
229 const char *next
; /* buffer with next unparsed record */
230 size_t next_size
; /* size of the next buffer */
233 #define INIT_DMESG_RECORD(_r) do { \
235 (_r)->mesg_size = 0; \
236 (_r)->facility = -1; \
238 (_r)->tv.tv_sec = 0; \
239 (_r)->tv.tv_usec = 0; \
242 static int process_kmsg(struct dmesg_control
*ctl
);
244 static int set_level_color(int log_level
, const char *mesg
, size_t mesgsz
)
250 id
= DMESG_COLOR_ALERT
;
253 id
= DMESG_COLOR_CRIT
;
256 id
= DMESG_COLOR_ERR
;
259 id
= DMESG_COLOR_WARN
;
265 /* well, sometimes the messages contains important keywords, but in
266 * non-warning/error messages
268 if (id
< 0 && memmem(mesg
, mesgsz
, "segfault at", 11))
269 id
= DMESG_COLOR_SEGFAULT
;
272 dmesg_enable_color(id
);
274 return id
>= 0 ? 0 : -1;
277 static void __attribute__((__noreturn__
)) usage(void)
282 fputs(USAGE_HEADER
, out
);
283 fprintf(out
, _(" %s [options]\n"), program_invocation_short_name
);
285 fputs(USAGE_SEPARATOR
, out
);
286 fputs(_("Display or control the kernel ring buffer.\n"), out
);
288 fputs(USAGE_OPTIONS
, out
);
289 fputs(_(" -C, --clear clear the kernel ring buffer\n"), out
);
290 fputs(_(" -c, --read-clear read and clear all messages\n"), out
);
291 fputs(_(" -D, --console-off disable printing messages to console\n"), out
);
292 fputs(_(" -E, --console-on enable printing messages to console\n"), out
);
293 fputs(_(" -F, --file <file> use the file instead of the kernel log buffer\n"), out
);
294 fputs(_(" -f, --facility <list> restrict output to defined facilities\n"), out
);
295 fputs(_(" -H, --human human readable output\n"), out
);
296 fputs(_(" -J, --json use JSON output format\n"), out
);
297 fputs(_(" -k, --kernel display kernel messages\n"), out
);
299 _(" -L, --color[=<when>] colorize messages (%s, %s or %s)\n"), "auto", "always", "never");
301 " %s\n", USAGE_COLORS_DEFAULT
);
302 fputs(_(" -l, --level <list> restrict output to defined levels\n"), out
);
303 fputs(_(" -n, --console-level <level> set level of messages printed to console\n"), out
);
304 fputs(_(" -P, --nopager do not pipe output into a pager\n"), out
);
305 fputs(_(" -p, --force-prefix force timestamp output on each line of multi-line messages\n"), out
);
306 fputs(_(" -r, --raw print the raw message buffer\n"), out
);
307 fputs(_(" --noescape don't escape unprintable character\n"), out
);
308 fputs(_(" -S, --syslog force to use syslog(2) rather than /dev/kmsg\n"), out
);
309 fputs(_(" -s, --buffer-size <size> buffer size to query the kernel ring buffer\n"), out
);
310 fputs(_(" -u, --userspace display userspace messages\n"), out
);
311 fputs(_(" -w, --follow wait for new messages\n"), out
);
312 fputs(_(" -W, --follow-new wait and print only new messages\n"), out
);
313 fputs(_(" -x, --decode decode facility and level to readable string\n"), out
);
314 fputs(_(" -d, --show-delta show time delta between printed messages\n"), out
);
315 fputs(_(" -e, --reltime show local time and time delta in readable format\n"), out
);
316 fputs(_(" -T, --ctime show human-readable timestamp (may be inaccurate!)\n"), out
);
317 fputs(_(" -t, --notime don't show any timestamp with messages\n"), out
);
318 fputs(_(" --time-format <format> show timestamp using the given format:\n"
319 " [delta|reltime|ctime|notime|iso]\n"
320 "Suspending/resume will make ctime and iso timestamps inaccurate.\n"), out
);
321 fputs(_(" --since <time> display the lines since the specified time\n"), out
);
322 fputs(_(" --until <time> display the lines until the specified time\n"), out
);
324 fputs(USAGE_SEPARATOR
, out
);
325 fprintf(out
, USAGE_HELP_OPTIONS(29));
326 fputs(_("\nSupported log facilities:\n"), out
);
327 for (i
= 0; i
< ARRAY_SIZE(level_names
); i
++)
328 fprintf(out
, " %7s - %s\n",
329 facility_names
[i
].name
,
330 _(facility_names
[i
].help
));
332 fputs(_("\nSupported log levels (priorities):\n"), out
);
333 for (i
= 0; i
< ARRAY_SIZE(level_names
); i
++)
334 fprintf(out
, " %7s - %s\n",
336 _(level_names
[i
].help
));
338 fprintf(out
, USAGE_MAN_TAIL("dmesg(1)"));
343 * LEVEL ::= <number> | <name>
344 * <number> ::= @len is set: number in range <0..N>, where N < ARRAY_SIZE(level_names)
345 * ::= @len not set: number in range <1..N>, where N <= ARRAY_SIZE(level_names)
346 * <name> ::= case-insensitive text
348 * Note that @len argument is not set when parsing "-n <level>" command line
349 * option. The console_level is interpreted as "log level less than the value".
351 * For example "dmesg -n 8" or "dmesg -n debug" enables debug console log
352 * level by klogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, 8). The @str argument
353 * has to be parsed to number in range <1..8>.
355 static int parse_level(const char *str
, size_t len
)
369 long x
= strtol(str
, &end
, 10) - offset
;
371 if (!errno
&& end
&& end
> str
&& (size_t) (end
- str
) == len
&&
372 x
>= 0 && (size_t) x
< ARRAY_SIZE(level_names
))
377 for (i
= 0; i
< ARRAY_SIZE(level_names
); i
++) {
378 const char *n
= level_names
[i
].name
;
380 if (strncasecmp(str
, n
, len
) == 0 && *(n
+ len
) == '\0')
386 err(EXIT_FAILURE
, _("failed to parse level '%s'"), str
);
388 errx(EXIT_FAILURE
, _("unknown level '%s'"), str
);
393 * FACILITY ::= <number> | <name>
394 * <number> ::= number in range <0..N>, where N < ARRAY_SIZE(facility_names)
395 * <name> ::= case-insensitive text
397 static int parse_facility(const char *str
, size_t len
)
407 long x
= strtol(str
, &end
, 10);
409 if (!errno
&& end
&& end
> str
&& (size_t) (end
- str
) == len
&&
410 x
>= 0 && (size_t) x
< ARRAY_SIZE(facility_names
))
415 for (i
= 0; i
< ARRAY_SIZE(facility_names
); i
++) {
416 const char *n
= facility_names
[i
].name
;
418 if (strncasecmp(str
, n
, len
) == 0 && *(n
+ len
) == '\0')
424 err(EXIT_FAILURE
, _("failed to parse facility '%s'"), str
);
426 errx(EXIT_FAILURE
, _("unknown facility '%s'"), str
);
431 * Parses numerical prefix used for all messages in kernel ring buffer.
433 * Priorities/facilities are encoded into a single 32-bit quantity, where the
434 * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
437 * Note that the number has to end with '>' or ',' char.
439 static const char *parse_faclev(const char *str
, int *fac
, int *lev
)
448 num
= strtol(str
, &end
, 10);
450 if (!errno
&& end
&& end
> str
) {
454 if (*lev
< 0 || (size_t) *lev
> ARRAY_SIZE(level_names
))
456 if (*fac
< 0 || (size_t) *fac
> ARRAY_SIZE(facility_names
))
458 return end
+ 1; /* skip '<' or ',' */
465 * Parses timestamp from syslog message prefix, expected format:
467 * seconds.microseconds]
469 * the ']' is the timestamp field terminator.
471 static const char *parse_syslog_timestamp(const char *str0
, struct timeval
*tv
)
473 const char *str
= str0
;
480 tv
->tv_sec
= strtol(str
, &end
, 10);
482 if (!errno
&& end
&& *end
== '.' && *(end
+ 1)) {
485 tv
->tv_usec
= strtol(str
, &end
, 10);
487 if (errno
|| !end
|| end
== str
|| *end
!= ']')
490 return end
+ 1; /* skip ']' */
494 * Parses timestamp from /dev/kmsg, expected formats:
499 * the ',' is fields separators and ';' items terminator (for the last item)
501 static const char *parse_kmsg_timestamp(const char *str0
, struct timeval
*tv
)
503 const char *str
= str0
;
511 usec
= strtoumax(str
, &end
, 10);
513 if (!errno
&& end
&& (*end
== ';' || *end
== ',')) {
514 tv
->tv_usec
= usec
% 1000000;
515 tv
->tv_sec
= usec
/ 1000000;
519 return end
+ 1; /* skip separator */
523 static double time_diff(struct timeval
*a
, struct timeval
*b
)
525 return (a
->tv_sec
- b
->tv_sec
) + (a
->tv_usec
- b
->tv_usec
) / 1E6
;
528 static int get_syslog_buffer_size(void)
530 int n
= klogctl(SYSLOG_ACTION_SIZE_BUFFER
, NULL
, 0);
532 return n
> 0 ? n
: 0;
536 * Reads messages from regular file by mmap
538 static ssize_t
mmap_file_buffer(struct dmesg_control
*ctl
, char **buf
)
546 fd
= open(ctl
->filename
, O_RDONLY
);
548 err(EXIT_FAILURE
, _("cannot open %s"), ctl
->filename
);
550 err(EXIT_FAILURE
, _("stat of %s failed"), ctl
->filename
);
552 *buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
553 if (*buf
== MAP_FAILED
)
554 err(EXIT_FAILURE
, _("cannot mmap: %s"), ctl
->filename
);
555 ctl
->mmap_buff
= *buf
;
556 ctl
->pagesize
= getpagesize();
563 * Reads messages from kernel ring buffer by klogctl()
565 static ssize_t
read_syslog_buffer(struct dmesg_control
*ctl
, char **buf
)
571 sz
= ctl
->bufsize
+ 8;
572 *buf
= xmalloc(sz
* sizeof(char));
573 rc
= klogctl(SYSLOG_ACTION_READ_ALL
, *buf
, sz
);
577 *buf
= xmalloc(sz
* sizeof(char));
578 rc
= klogctl(SYSLOG_ACTION_READ_ALL
, *buf
, sz
);
581 if ((size_t) rc
!= sz
|| sz
> (1 << 28))
593 * Top level function to read (and print in case of kmesg) messages
595 static ssize_t
process_buffer(struct dmesg_control
*ctl
, char **buf
)
599 switch (ctl
->method
) {
600 case DMESG_METHOD_MMAP
:
601 n
= mmap_file_buffer(ctl
, buf
);
603 case DMESG_METHOD_SYSLOG
:
605 ctl
->bufsize
= get_syslog_buffer_size();
607 n
= read_syslog_buffer(ctl
, buf
);
609 case DMESG_METHOD_KMSG
:
613 n
= process_kmsg(ctl
);
616 abort(); /* impossible method -> drop core */
622 static int fwrite_hex(const char *buf
, size_t size
, FILE *out
)
626 for (i
= 0; i
< size
; i
++) {
627 int rc
= fprintf(out
, "\\x%02hhx", buf
[i
]);
635 * Prints to 'out' and non-printable chars are replaced with \x<hex> sequences.
637 static void safe_fwrite(struct dmesg_control
*ctl
, const char *buf
, size_t size
, int indent
, FILE *out
)
643 memset(&s
, 0, sizeof (s
));
645 for (i
= 0; i
< size
; i
++) {
646 const char *p
= buf
+ i
;
656 len
= mbrtowc(&wc
, p
, size
- i
, &s
);
658 if (len
== 0) /* L'\0' */
661 if (len
== (size_t)-1 || len
== (size_t)-2) { /* invalid sequence */
662 memset(&s
, 0, sizeof (s
));
665 } else if (len
> 1) {
666 if (!iswprint(wc
) && !iswspace(wc
)) /* non-printable multibyte */
673 if (!isprint((unsigned char) *p
) &&
674 !isspace((unsigned char) *p
)) /* non-printable */
681 rc
= fwrite_hex(p
, len
, out
);
682 else if (*p
== '\n' && *(p
+ 1) && indent
) {
683 rc
= fwrite(p
, 1, len
, out
) != len
;
684 if (fprintf(out
, "%*s", indent
, "") != indent
)
687 rc
= fwrite(p
, 1, len
, out
) != len
;
691 err(EXIT_FAILURE
, _("write failed"));
697 static const char *skip_item(const char *begin
, const char *end
, const char *sep
)
699 while (begin
< end
) {
702 if (c
== '\0' || strchr(sep
, c
))
710 * Parses one record from syslog(2) buffer
712 static int get_next_syslog_record(struct dmesg_control
*ctl
,
713 struct dmesg_record
*rec
)
716 const char *begin
= NULL
;
718 if (ctl
->method
!= DMESG_METHOD_MMAP
&&
719 ctl
->method
!= DMESG_METHOD_SYSLOG
)
722 if (!rec
->next
|| !rec
->next_size
)
725 INIT_DMESG_RECORD(rec
);
728 * Unmap already printed file data from memory
730 if (ctl
->mmap_buff
&& (size_t) (rec
->next
- ctl
->mmap_buff
) > ctl
->pagesize
) {
731 void *x
= ctl
->mmap_buff
;
733 ctl
->mmap_buff
+= ctl
->pagesize
;
734 munmap(x
, ctl
->pagesize
);
737 for (i
= 0; i
< rec
->next_size
; i
++) {
738 const char *p
= rec
->next
+ i
;
739 const char *end
= NULL
;
743 if (i
+ 1 == rec
->next_size
) {
746 } else if (*p
== '\n' && *(p
+ 1) == '<')
749 if (begin
&& !*begin
)
750 begin
= NULL
; /* zero(s) at the end of the buffer? */
754 continue; /* error or empty line? */
757 if (ctl
->fltr_lev
|| ctl
->fltr_fac
|| ctl
->decode
|| ctl
->color
|| ctl
->json
)
758 begin
= parse_faclev(begin
+ 1, &rec
->facility
,
761 begin
= skip_item(begin
, end
, ">");
764 if (*begin
== '[' && (*(begin
+ 1) == ' ' ||
765 isdigit(*(begin
+ 1)))) {
767 if (!is_timefmt(ctl
, NONE
))
768 begin
= parse_syslog_timestamp(begin
+ 1, &rec
->tv
);
770 begin
= skip_item(begin
, end
, "]");
772 if (begin
< end
&& *begin
== ' ')
777 rec
->mesg_size
= end
- begin
;
779 /* Don't count \n from the last message to the message size */
780 if (*end
!= '\n' && *(end
- 1) == '\n')
783 rec
->next_size
-= end
- rec
->next
;
784 rec
->next
= rec
->next_size
> 0 ? end
+ 1 : NULL
;
785 if (rec
->next_size
> 0)
794 static usec_t
record_time(struct dmesg_control
*ctl
, struct dmesg_record
*rec
)
796 return timeval_to_usec(&ctl
->boot_time
) +
797 ctl
->suspended_time
+
798 timeval_to_usec(&rec
->tv
);
801 static int accept_record(struct dmesg_control
*ctl
, struct dmesg_record
*rec
)
803 if (ctl
->fltr_lev
&& (rec
->facility
< 0 ||
804 !isset(ctl
->levels
, rec
->level
)))
807 if (ctl
->fltr_fac
&& (rec
->facility
< 0 ||
808 !isset(ctl
->facilities
, rec
->facility
)))
811 if (ctl
->since
&& ctl
->since
>= record_time(ctl
, rec
))
814 if (ctl
->until
&& ctl
->until
<= record_time(ctl
, rec
))
820 static void raw_print(struct dmesg_control
*ctl
, const char *buf
, size_t size
)
824 if (!ctl
->mmap_buff
) {
826 * Print whole ring buffer
828 safe_fwrite(ctl
, buf
, size
, 0, stdout
);
829 lastc
= buf
[size
- 1];
832 * Print file in small chunks to save memory
835 size_t sz
= size
> ctl
->pagesize
? ctl
->pagesize
: size
;
836 char *x
= ctl
->mmap_buff
;
838 safe_fwrite(ctl
, x
, sz
, 0, stdout
);
841 ctl
->mmap_buff
+= sz
;
850 static struct tm
*record_localtime(struct dmesg_control
*ctl
,
851 struct dmesg_record
*rec
,
854 time_t t
= record_time(ctl
, rec
) / USEC_PER_SEC
;
855 return localtime_r(&t
, tm
);
858 static char *record_ctime(struct dmesg_control
*ctl
,
859 struct dmesg_record
*rec
,
860 char *buf
, size_t bufsiz
)
864 record_localtime(ctl
, rec
, &tm
);
866 /* TRANSLATORS: dmesg uses strftime() fo generate date-time string
867 where %a is abbreviated name of the day, %b is abbreviated month
868 name and %e is day of the month as a decimal number. Please, set
869 proper month/day order here */
870 if (strftime(buf
, bufsiz
, _("%a %b %e %H:%M:%S %Y"), &tm
) == 0)
875 static char *short_ctime(struct tm
*tm
, char *buf
, size_t bufsiz
)
877 /* TRANSLATORS: dmesg uses strftime() fo generate date-time string
878 where: %b is abbreviated month and %e is day of the month as a
879 decimal number. Please, set proper month/day order here. */
880 if (strftime(buf
, bufsiz
, _("%b%e %H:%M"), tm
) == 0)
885 static char *iso_8601_time(struct dmesg_control
*ctl
, struct dmesg_record
*rec
,
886 char *buf
, size_t bufsz
)
888 struct timeval tv
= usec_to_timeval(
889 timeval_to_usec(&ctl
->boot_time
) + ctl
->suspended_time
+
890 timeval_to_usec(&rec
->tv
)
893 if (strtimeval_iso(&tv
, ISO_TIMESTAMP_COMMA_T
, buf
, bufsz
) != 0)
899 static double record_count_delta(struct dmesg_control
*ctl
,
900 struct dmesg_record
*rec
)
904 if (timerisset(&ctl
->lasttime
))
905 delta
= time_diff(&rec
->tv
, &ctl
->lasttime
);
907 ctl
->lasttime
= rec
->tv
;
911 static const char *get_subsys_delimiter(const char *mesg
, size_t mesg_size
)
913 const char *p
= mesg
;
914 size_t sz
= mesg_size
;
917 const char *d
= strnchr(p
, sz
, ':');
922 if (sz
>= 2 && isblank(*(d
+ 1)))
930 #define is_facpri_valid(_r) \
931 (((_r)->level > -1) && ((_r)->level < (int) ARRAY_SIZE(level_names)) && \
932 ((_r)->facility > -1) && \
933 ((_r)->facility < (int) ARRAY_SIZE(facility_names)))
936 static void print_record(struct dmesg_control
*ctl
,
937 struct dmesg_record
*rec
)
940 char fpbuf
[32] = "\0";
941 char tsbuf
[64] = "\0";
942 size_t mesg_size
= rec
->mesg_size
;
944 char *mesg_copy
= NULL
;
945 const char *line
= NULL
;
947 if (!accept_record(ctl
, rec
))
950 if (!rec
->mesg_size
) {
957 if (!ul_jsonwrt_is_ready(&ctl
->jfmt
)) {
958 ul_jsonwrt_init(&ctl
->jfmt
, stdout
, 0);
959 ul_jsonwrt_root_open(&ctl
->jfmt
);
960 ul_jsonwrt_array_open(&ctl
->jfmt
, "dmesg");
962 ul_jsonwrt_object_open(&ctl
->jfmt
, NULL
);
966 * Compose syslog(2) compatible raw output -- used for /dev/kmsg for
967 * backward compatibility with syslog(2) buffers only
970 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
),
972 LOG_RAW_FAC_PRI(rec
->facility
, rec
->level
),
973 (long) rec
->tv
.tv_sec
,
974 (long) rec
->tv
.tv_usec
);
978 /* Store decode information (facility & priority level) in a buffer */
979 if (!ctl
->json
&& ctl
->decode
&& is_facpri_valid(rec
))
980 snprintf(fpbuf
, sizeof(fpbuf
), "%-6s:%-6s: ",
981 facility_names
[rec
->facility
].name
,
982 level_names
[rec
->level
].name
);
984 /* Store the timestamp in a buffer */
985 switch (ctl
->time_fmt
) {
988 case DMESG_TIMEFTM_NONE
:
991 case DMESG_TIMEFTM_CTIME
:
992 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "[%s] ",
993 record_ctime(ctl
, rec
, buf
, sizeof(buf
)));
995 case DMESG_TIMEFTM_CTIME_DELTA
:
996 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "[%s <%12.06f>] ",
997 record_ctime(ctl
, rec
, buf
, sizeof(buf
)),
998 record_count_delta(ctl
, rec
));
1000 case DMESG_TIMEFTM_DELTA
:
1001 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "[<%12.06f>] ",
1002 record_count_delta(ctl
, rec
));
1004 case DMESG_TIMEFTM_RELTIME
:
1005 record_localtime(ctl
, rec
, &cur
);
1006 delta
= record_count_delta(ctl
, rec
);
1007 if (cur
.tm_min
!= ctl
->lasttm
.tm_min
||
1008 cur
.tm_hour
!= ctl
->lasttm
.tm_hour
||
1009 cur
.tm_yday
!= ctl
->lasttm
.tm_yday
) {
1011 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "[%s] ",
1012 short_ctime(&cur
, buf
,
1016 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
),
1017 "[ %+8.06f] ", delta
);
1019 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
),
1020 "[ %+9.06f] ", delta
);
1024 case DMESG_TIMEFTM_TIME
:
1025 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
),
1026 ctl
->json
? "%5ld.%06ld" : "[%5ld.%06ld] ",
1027 (long)rec
->tv
.tv_sec
,
1028 (long)rec
->tv
.tv_usec
);
1030 case DMESG_TIMEFTM_TIME_DELTA
:
1031 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "[%5ld.%06ld <%12.06f>] ",
1032 (long)rec
->tv
.tv_sec
,
1033 (long)rec
->tv
.tv_usec
,
1034 record_count_delta(ctl
, rec
));
1036 case DMESG_TIMEFTM_ISO8601
:
1037 ctl
->indent
= snprintf(tsbuf
, sizeof(tsbuf
), "%s ",
1038 iso_8601_time(ctl
, rec
, buf
,
1045 ctl
->indent
+= strlen(fpbuf
);
1048 /* Output the decode information */
1050 fputs(fpbuf
, stdout
);
1051 } else if (ctl
->json
&& is_facpri_valid(rec
)) {
1053 ul_jsonwrt_value_s(&ctl
->jfmt
, "fac", facility_names
[rec
->facility
].name
);
1054 ul_jsonwrt_value_s(&ctl
->jfmt
, "pri", level_names
[rec
->level
].name
);
1056 ul_jsonwrt_value_u64(&ctl
->jfmt
, "pri", LOG_RAW_FAC_PRI(rec
->facility
, rec
->level
));
1059 /* Output the timestamp buffer */
1061 /* Colorize the timestamp */
1063 dmesg_enable_color(timebreak
? DMESG_COLOR_TIMEBREAK
:
1065 if (ctl
->time_fmt
!= DMESG_TIMEFTM_RELTIME
) {
1067 ul_jsonwrt_value_raw(&ctl
->jfmt
, "time", tsbuf
);
1069 fputs(tsbuf
, stdout
);
1072 * For relative timestamping, the first line's
1073 * timestamp is the offset and all other lines will
1074 * report an offset of 0.000000.
1076 fputs(!line
? tsbuf
: "[ +0.000000] ", stdout
);
1083 * A kernel message may contain several lines of output, separated
1084 * by '\n'. If the timestamp and decode outputs are forced then each
1085 * line of the message must be displayed with that information.
1087 if (ctl
->force_prefix
) {
1089 mesg_copy
= xstrdup(rec
->mesg
);
1090 line
= strtok(mesg_copy
, "\n");
1092 goto done
; /* only when something is wrong */
1093 mesg_size
= strlen(line
);
1097 mesg_size
= rec
->mesg_size
;
1100 /* Colorize kernel message output */
1102 /* Subsystem prefix */
1103 const char *subsys
= get_subsys_delimiter(line
, mesg_size
);
1107 dmesg_enable_color(DMESG_COLOR_SUBSYS
);
1108 safe_fwrite(ctl
, line
, subsys
- line
, ctl
->indent
, stdout
);
1111 mesg_size
-= subsys
- line
;
1114 /* Error, alert .. etc. colors */
1115 has_color
= set_level_color(rec
->level
, line
, mesg_size
) == 0;
1116 safe_fwrite(ctl
, line
, mesg_size
, ctl
->indent
, stdout
);
1121 ul_jsonwrt_value_s(&ctl
->jfmt
, "msg", line
);
1123 safe_fwrite(ctl
, line
, mesg_size
, ctl
->indent
, stdout
);
1126 /* Get the next line */
1127 if (ctl
->force_prefix
) {
1128 line
= strtok(NULL
, "\n");
1129 if (line
&& *line
) {
1131 mesg_size
= strlen(line
);
1139 ul_jsonwrt_object_close(&ctl
->jfmt
);
1145 * Prints the 'buf' kernel ring buffer; the messages are filtered out according
1146 * to 'levels' and 'facilities' bitarrays.
1148 static void print_buffer(struct dmesg_control
*ctl
,
1149 const char *buf
, size_t size
)
1151 struct dmesg_record rec
= { .next
= buf
, .next_size
= size
};
1154 raw_print(ctl
, buf
, size
);
1158 while (get_next_syslog_record(ctl
, &rec
) == 0)
1159 print_record(ctl
, &rec
);
1162 static ssize_t
read_kmsg_one(struct dmesg_control
*ctl
)
1166 /* kmsg returns EPIPE if record was modified while reading */
1168 size
= read(ctl
->kmsg
, ctl
->kmsg_buf
,
1169 sizeof(ctl
->kmsg_buf
) - 1);
1170 } while (size
< 0 && errno
== EPIPE
);
1175 static int init_kmsg(struct dmesg_control
*ctl
)
1177 int mode
= O_RDONLY
;
1184 ctl
->kmsg
= open("/dev/kmsg", mode
);
1189 * Seek after the last record available at the time
1190 * the last SYSLOG_ACTION_CLEAR was issued.
1192 * ... otherwise SYSLOG_ACTION_CLEAR will have no effect for kmsg.
1194 lseek(ctl
->kmsg
, 0, ctl
->end
? SEEK_END
: SEEK_DATA
);
1197 * Old kernels (<3.5) can successfully open /dev/kmsg for read-only,
1198 * but read() returns -EINVAL :-(((
1200 * Let's try to read the first record. The record is later processed in
1203 ctl
->kmsg_first_read
= read_kmsg_one(ctl
);
1204 if (ctl
->kmsg_first_read
< 0) {
1214 * /dev/kmsg record format:
1216 * faclev,seqnum,timestamp[optional, ...];message\n
1220 * - fields are separated by ','
1221 * - last field is terminated by ';'
1224 #define LAST_KMSG_FIELD(s) (!s || !*s || *(s - 1) == ';')
1226 static int parse_kmsg_record(struct dmesg_control
*ctl
,
1227 struct dmesg_record
*rec
,
1231 const char *p
= buf
, *end
;
1233 if (sz
== 0 || !buf
|| !*buf
)
1236 end
= buf
+ (sz
- 1);
1237 INIT_DMESG_RECORD(rec
);
1239 while (p
< end
&& isspace(*p
))
1242 /* A) priority and facility */
1243 if (ctl
->fltr_lev
|| ctl
->fltr_fac
|| ctl
->decode
||
1244 ctl
->raw
|| ctl
->color
|| ctl
->json
)
1245 p
= parse_faclev(p
, &rec
->facility
, &rec
->level
);
1247 p
= skip_item(p
, end
, ",");
1248 if (LAST_KMSG_FIELD(p
))
1251 /* B) sequence number */
1252 p
= skip_item(p
, end
, ",;");
1253 if (LAST_KMSG_FIELD(p
))
1257 if (is_timefmt(ctl
, NONE
))
1258 p
= skip_item(p
, end
, ",;");
1260 p
= parse_kmsg_timestamp(p
, &rec
->tv
);
1261 if (LAST_KMSG_FIELD(p
))
1264 /* D) optional fields (ignore) */
1265 p
= skip_item(p
, end
, ";");
1268 /* E) message text */
1270 p
= skip_item(p
, end
, "\n");
1274 /* The message text is terminated by \n, but it's possible that the
1275 * message contains another stuff behind this linebreak; in this case
1276 * the previous skip_item() returns pointer to the stuff behind \n.
1277 * Let's normalize all these situations and make sure we always point to
1280 * Note that the next unhexmangle_to_buffer() will replace \n by \0.
1282 if (*p
&& *p
!= '\n')
1286 * Kernel escapes non-printable characters, unfortunately kernel
1287 * definition of "non-printable" is too strict. On UTF8 console we can
1288 * print many chars, so let's decode from kernel.
1290 rec
->mesg_size
= unhexmangle_to_buffer(rec
->mesg
,
1291 (char *) rec
->mesg
, p
- rec
->mesg
+ 1);
1293 rec
->mesg_size
--; /* don't count \0 */
1295 /* F) message tags (ignore) */
1301 * Note that each read() call for /dev/kmsg returns always one record. It means
1302 * that we don't have to read whole message buffer before the records parsing.
1304 * So this function does not compose one huge buffer (like read_syslog_buffer())
1305 * and print_buffer() is unnecessary. All is done in this function.
1307 * Returns 0 on success, -1 on error.
1309 static int process_kmsg(struct dmesg_control
*ctl
)
1311 struct dmesg_record rec
;
1314 if (ctl
->method
!= DMESG_METHOD_KMSG
|| ctl
->kmsg
< 0)
1318 * The very first read() call is done in kmsg_init() where we test
1319 * /dev/kmsg usability. The return code from the initial read() is
1320 * stored in ctl->kmsg_first_read;
1322 sz
= ctl
->kmsg_first_read
;
1325 *(ctl
->kmsg_buf
+ sz
) = '\0'; /* for debug messages */
1327 if (parse_kmsg_record(ctl
, &rec
,
1328 ctl
->kmsg_buf
, (size_t) sz
) == 0)
1329 print_record(ctl
, &rec
);
1331 sz
= read_kmsg_one(ctl
);
1337 static int which_time_format(const char *s
)
1339 if (!strcmp(s
, "notime"))
1340 return DMESG_TIMEFTM_NONE
;
1341 if (!strcmp(s
, "ctime"))
1342 return DMESG_TIMEFTM_CTIME
;
1343 if (!strcmp(s
, "delta"))
1344 return DMESG_TIMEFTM_DELTA
;
1345 if (!strcmp(s
, "reltime"))
1346 return DMESG_TIMEFTM_RELTIME
;
1347 if (!strcmp(s
, "iso"))
1348 return DMESG_TIMEFTM_ISO8601
;
1349 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
1353 static inline int dmesg_get_boot_time(struct timeval
*tv
)
1355 char *str
= getenv("DMESG_TEST_BOOTIME");
1356 uintmax_t sec
, usec
;
1358 if (str
&& sscanf(str
, "%ju.%ju", &sec
, &usec
) == 2) {
1361 return tv
->tv_sec
>= 0 && tv
->tv_usec
>= 0 ? 0 : -EINVAL
;
1364 return get_boot_time(tv
);
1367 static inline usec_t
dmesg_get_suspended_time(void)
1369 if (getenv("DMESG_TEST_BOOTIME"))
1371 return get_suspended_time();
1374 # define dmesg_get_boot_time get_boot_time
1375 # define dmesg_get_suspended_time get_suspended_time
1378 int main(int argc
, char *argv
[])
1382 int console_level
= 0;
1386 static struct dmesg_control ctl
= {
1388 .action
= SYSLOG_ACTION_READ_ALL
,
1389 .method
= DMESG_METHOD_KMSG
,
1391 .time_fmt
= DMESG_TIMEFTM_TIME
,
1394 int colormode
= UL_COLORMODE_UNDEF
;
1396 OPT_TIME_FORMAT
= CHAR_MAX
+ 1,
1402 static const struct option longopts
[] = {
1403 { "buffer-size", required_argument
, NULL
, 's' },
1404 { "clear", no_argument
, NULL
, 'C' },
1405 { "color", optional_argument
, NULL
, 'L' },
1406 { "console-level", required_argument
, NULL
, 'n' },
1407 { "console-off", no_argument
, NULL
, 'D' },
1408 { "console-on", no_argument
, NULL
, 'E' },
1409 { "decode", no_argument
, NULL
, 'x' },
1410 { "file", required_argument
, NULL
, 'F' },
1411 { "facility", required_argument
, NULL
, 'f' },
1412 { "follow", no_argument
, NULL
, 'w' },
1413 { "follow-new", no_argument
, NULL
, 'W' },
1414 { "human", no_argument
, NULL
, 'H' },
1415 { "help", no_argument
, NULL
, 'h' },
1416 { "json", no_argument
, NULL
, 'J' },
1417 { "kernel", no_argument
, NULL
, 'k' },
1418 { "level", required_argument
, NULL
, 'l' },
1419 { "since", required_argument
, NULL
, OPT_SINCE
},
1420 { "syslog", no_argument
, NULL
, 'S' },
1421 { "raw", no_argument
, NULL
, 'r' },
1422 { "read-clear", no_argument
, NULL
, 'c' },
1423 { "reltime", no_argument
, NULL
, 'e' },
1424 { "show-delta", no_argument
, NULL
, 'd' },
1425 { "ctime", no_argument
, NULL
, 'T' },
1426 { "noescape", no_argument
, NULL
, OPT_NOESC
},
1427 { "notime", no_argument
, NULL
, 't' },
1428 { "nopager", no_argument
, NULL
, 'P' },
1429 { "until", required_argument
, NULL
, OPT_UNTIL
},
1430 { "userspace", no_argument
, NULL
, 'u' },
1431 { "version", no_argument
, NULL
, 'V' },
1432 { "time-format", required_argument
, NULL
, OPT_TIME_FORMAT
},
1433 { "force-prefix", no_argument
, NULL
, 'p' },
1434 { NULL
, 0, NULL
, 0 }
1437 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1438 { 'C','D','E','c','n','r' }, /* clear,off,on,read-clear,level,raw*/
1439 { 'H','r' }, /* human, raw */
1440 { 'L','r' }, /* color, raw */
1441 { 'S','w' }, /* syslog,follow */
1442 { 'T','r' }, /* ctime, raw */
1443 { 'd','r' }, /* delta, raw */
1444 { 'e','r' }, /* reltime, raw */
1445 { 'r','x' }, /* raw, decode */
1446 { 'r','t' }, /* notime, raw */
1449 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1451 setlocale(LC_ALL
, "");
1452 bindtextdomain(PACKAGE
, LOCALEDIR
);
1453 textdomain(PACKAGE
);
1454 close_stdout_atexit();
1456 while ((c
= getopt_long(argc
, argv
, "CcDdEeF:f:HhJkL::l:n:iPprSs:TtuVWwx",
1457 longopts
, NULL
)) != -1) {
1459 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1463 ctl
.action
= SYSLOG_ACTION_CLEAR
;
1466 ctl
.action
= SYSLOG_ACTION_READ_CLEAR
;
1469 ctl
.action
= SYSLOG_ACTION_CONSOLE_OFF
;
1475 ctl
.action
= SYSLOG_ACTION_CONSOLE_ON
;
1478 ctl
.time_fmt
= DMESG_TIMEFTM_RELTIME
;
1481 ctl
.filename
= optarg
;
1482 ctl
.method
= DMESG_METHOD_MMAP
;
1486 if (string_to_bitarray(optarg
,
1487 ctl
.facilities
, parse_facility
, 0) < 0)
1488 return EXIT_FAILURE
;
1491 ctl
.time_fmt
= DMESG_TIMEFTM_RELTIME
;
1492 colormode
= UL_COLORMODE_AUTO
;
1500 setbit(ctl
.facilities
, FAC_BASE(LOG_KERN
));
1503 colormode
= UL_COLORMODE_AUTO
;
1505 colormode
= colormode_or_err(optarg
,
1506 _("unsupported color mode"));
1510 if (string_to_bitarray(optarg
,
1511 ctl
.levels
, parse_level
,
1512 ARRAY_SIZE(level_names
)) < 0)
1513 return EXIT_FAILURE
;
1516 ctl
.action
= SYSLOG_ACTION_CONSOLE_LEVEL
;
1517 console_level
= parse_level(optarg
, 0);
1523 ctl
.force_prefix
= 1;
1529 ctl
.method
= DMESG_METHOD_SYSLOG
;
1532 ctl
.bufsize
= strtou32_or_err(optarg
,
1533 _("invalid buffer size argument"));
1534 if (ctl
.bufsize
< 4096)
1538 ctl
.time_fmt
= DMESG_TIMEFTM_CTIME
;
1541 ctl
.time_fmt
= DMESG_TIMEFTM_NONE
;
1545 for (n
= 1; (size_t) n
< ARRAY_SIZE(facility_names
); n
++)
1546 setbit(ctl
.facilities
, n
);
1558 case OPT_TIME_FORMAT
:
1559 ctl
.time_fmt
= which_time_format(optarg
);
1566 if (parse_timestamp(optarg
, &ctl
.since
) < 0)
1567 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1572 if (parse_timestamp(optarg
, &ctl
.until
) < 0)
1573 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1579 print_version(EXIT_SUCCESS
);
1581 errtryhelp(EXIT_FAILURE
);
1585 if (argc
!= optind
) {
1586 warnx(_("bad usage"));
1587 errtryhelp(EXIT_FAILURE
);
1591 ctl
.time_fmt
= DMESG_TIMEFTM_TIME
;
1593 ctl
.force_prefix
= 0;
1599 if ((is_timefmt(&ctl
, RELTIME
) ||
1600 is_timefmt(&ctl
, CTIME
) ||
1601 is_timefmt(&ctl
, ISO8601
)) ||
1604 if (dmesg_get_boot_time(&ctl
.boot_time
) != 0)
1605 ctl
.time_fmt
= DMESG_TIMEFTM_NONE
;
1607 ctl
.suspended_time
= dmesg_get_suspended_time();
1611 switch (ctl
.time_fmt
) {
1612 case DMESG_TIMEFTM_CTIME
:
1613 ctl
.time_fmt
= DMESG_TIMEFTM_CTIME_DELTA
;
1615 case DMESG_TIMEFTM_TIME
:
1616 ctl
.time_fmt
= DMESG_TIMEFTM_TIME_DELTA
;
1618 case DMESG_TIMEFTM_ISO8601
:
1619 warnx(_("--show-delta is ignored when used together with iso8601 time format"));
1622 ctl
.time_fmt
= DMESG_TIMEFTM_DELTA
;
1627 ctl
.color
= colors_init(colormode
, "dmesg") ? 1 : 0;
1630 ctl
.pager
= nopager
? 0 : ctl
.pager
;
1632 switch (ctl
.action
) {
1633 case SYSLOG_ACTION_READ_ALL
:
1634 case SYSLOG_ACTION_READ_CLEAR
:
1635 if (ctl
.method
== DMESG_METHOD_KMSG
&& init_kmsg(&ctl
) != 0)
1636 ctl
.method
= DMESG_METHOD_SYSLOG
;
1639 && ctl
.method
!= DMESG_METHOD_KMSG
1640 && (ctl
.fltr_lev
|| ctl
.fltr_fac
))
1641 errx(EXIT_FAILURE
, _("--raw can be used together with --level or "
1642 "--facility only when reading messages from /dev/kmsg"));
1644 /* only kmsg supports multi-line messages */
1645 if (ctl
.force_prefix
&& ctl
.method
!= DMESG_METHOD_KMSG
)
1646 ctl
.force_prefix
= 0;
1649 n
= process_buffer(&ctl
, &buf
);
1651 print_buffer(&ctl
, buf
, n
);
1656 if (ctl
.json
&& ul_jsonwrt_is_ready(&ctl
.jfmt
)) {
1657 ul_jsonwrt_array_close(&ctl
.jfmt
);
1658 ul_jsonwrt_root_close(&ctl
.jfmt
);
1661 err(EXIT_FAILURE
, _("read kernel buffer failed"));
1662 else if (ctl
.action
== SYSLOG_ACTION_READ_CLEAR
)
1667 case SYSLOG_ACTION_CLEAR
:
1668 if (klogctl(SYSLOG_ACTION_CLEAR
, NULL
, 0) < 0)
1669 err(EXIT_FAILURE
, _("clear kernel buffer failed"));
1671 case SYSLOG_ACTION_CONSOLE_OFF
:
1672 case SYSLOG_ACTION_CONSOLE_ON
:
1673 klog_rc
= klogctl(ctl
.action
, NULL
, 0);
1675 case SYSLOG_ACTION_CONSOLE_LEVEL
:
1676 klog_rc
= klogctl(ctl
.action
, NULL
, console_level
);
1679 errx(EXIT_FAILURE
, _("unsupported command"));
1685 err(EXIT_FAILURE
, _("klogctl failed"));
1687 return EXIT_SUCCESS
;