aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/dmesg.c
blob: 10032562d4bc0c8dbe9efe0ce0aa4a3ce0825d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* dmesg.c - display/control kernel ring buffer.
 *
 * Copyright 2006, 2007 Rob Landley <rob@landley.net>
 *
 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/dmesg.html

USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN))

config DMESG
  bool "dmesg"
  default y
  help
    usage: dmesg [-n level] [-s bufsize] | -c

    Print or control the kernel ring buffer.

    -n	Set kernel logging level (1-9).
    -s	Size of buffer to read (in bytes), default 16384.
    -c	Clear the ring buffer after printing.
*/

#define FOR_dmesg
#include "toys.h"
#include <sys/klog.h>

GLOBALS(
  long level;
  long size;
)

void dmesg_main(void)
{
  // For -n just tell kernel to which messages to keep.
  if (toys.optflags & 2) {
    if (klogctl(8, NULL, TT.level)) error_exit("klogctl");
  } else {
    int size, i, last = '\n';
    char *data;

    // Figure out how much data we need, and fetch it.
    size = TT.size;
    if (size<2) size = 16384;
    data = xmalloc(size);
    size = klogctl(3 + (toys.optflags&1), data, size);
    if (size < 0) error_exit("klogctl");

    // Display data, filtering out level markers.
    for (i=0; i<size; ) {
      if (last=='\n' && data[i]=='<') i += 3;
      else xputc(last = data[i++]);
    }
    if (last!='\n') xputc('\n');
    if (CFG_TOYBOX_FREE) free(data);
  }
}