blob: 788b05815461231a895c86bb9a60df376740b353 [file] [log] [blame]
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/klog.h>
#include <string.h>
// From Android sys/klog.h
#ifndef KLOG_READ_ALL
#define KLOG_READ_ALL 3
#endif
#ifndef KLOG_READ_CLEAR
#define KLOG_READ_CLEAR 4
#endif
#ifndef KLOG_SIZE_BUFFER
#define KLOG_SIZE_BUFFER 10
#endif
int dmesg_main(int argc, char **argv)
{
char *buffer;
char *p;
ssize_t ret;
int n, op, bufsize, err = EXIT_FAILURE;
bufsize = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
if (bufsize <= 0) {
perror("klogctl size_buffer");
goto done;
}
buffer = malloc(bufsize);
if (!buffer) {
perror("malloc");
goto done;
}
buffer[0] = 0;
if((argc == 2) && (!strcmp(argv[1],"-c"))) {
op = KLOG_READ_CLEAR;
} else {
op = KLOG_READ_ALL;
}
n = klogctl(op, buffer, bufsize);
if (n < 0) {
perror("klogctl");
goto done_free;
}
buffer[n] = '\0';
p = buffer;
while ((ret = write(STDOUT_FILENO, p, n))) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror("write");
goto done_free;
}
p += ret;
n -= ret;
}
err = 0;
done_free:
free(buffer);
done:
return err;
}