view toys/lsb/dmesg.c @ 1539:3e85af1f7e22 draft

First batch of sed tests. Only good for TEST_HOST=1 at the moment because the test infrastructure itself depends on sed, so if an unfinished sed is in the $PATH it goes boing. But hey, corner cases! I have... more.
author Rob Landley <>
date Wed, 29 Oct 2014 18:44:33 -0500
parents 786841fdb1e0
children d26f235323db
line wrap: on
line source

/* dmesg.c - display/control kernel ring buffer.
 * Copyright 2006, 2007 Rob Landley <>


config DMESG
  bool "dmesg"
  default y
    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>

  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);