blob: 9c73b00ca3ea0551fd9fa143d4d95db04e088c29 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdlib.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <errno.h>
5#include <sys/klog.h>
6#include <string.h>
7
James Sullinsedee93d2012-06-24 07:07:42 -05008#define FALLBACK_KLOG_BUF_SHIFT 17 /* CONFIG_LOG_BUF_SHIFT from our kernel */
9#define FALLBACK_KLOG_BUF_LEN (1 << FALLBACK_KLOG_BUF_SHIFT)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010
11int dmesg_main(int argc, char **argv)
12{
James Sullinsedee93d2012-06-24 07:07:42 -050013 char *buffer;
14 char *p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015 ssize_t ret;
James Sullinsedee93d2012-06-24 07:07:42 -050016 int n, op, klog_buf_len;
17
18 klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
19
20 if (klog_buf_len <= 0) {
21 klog_buf_len = FALLBACK_KLOG_BUF_LEN;
22 }
23
24 buffer = (char *)malloc(klog_buf_len + 1);
25
26 if (!buffer) {
27 perror("malloc");
28 return EXIT_FAILURE;
29 }
30
31 p = buffer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33 if((argc == 2) && (!strcmp(argv[1],"-c"))) {
34 op = KLOG_READ_CLEAR;
35 } else {
36 op = KLOG_READ_ALL;
37 }
38
James Sullinsedee93d2012-06-24 07:07:42 -050039 n = klogctl(op, buffer, klog_buf_len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 if (n < 0) {
41 perror("klogctl");
42 return EXIT_FAILURE;
43 }
44 buffer[n] = '\0';
45
46 while((ret = write(STDOUT_FILENO, p, n))) {
47 if (ret == -1) {
48 if (errno == EINTR)
49 continue;
50 perror("write");
51 return EXIT_FAILURE;
52 }
53 p += ret;
54 n -= ret;
55 }
56
57 return 0;
58}