-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmesg_u.c
153 lines (142 loc) · 3.63 KB
/
dmesg_u.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* dmesg - toolbox
Copyright 2007-2015 PC GO Ld.
Copyright 2015 libdll.so
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <string.h>
#if (defined __GNU__ && defined __MACH__) || (defined __sun && defined __SVR4)
#include <stdio.h>
#if defined __GNU__ && defined __MACH__
#include <unistd.h>
#include <fcntl.h>
#endif
extern int cat_main(int, char **);
#elif defined __linux__
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/klog.h>
#define KLOG_BUF_SHIFT 17 /* CONFIG_LOG_BUF_SHIFT default */
#define KLOG_BUF_LEN (1 << KLOG_BUF_SHIFT)
#define KLOG_READ_ALL 3
#define KLOG_READ_CLEAR 4
#else
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if defined MAC_OS_X_VERSION_MIN_REQUIRED && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
#define _USE_APPLE_PROC_KMSGBUF
#endif
#endif
#ifdef _USE_APPLE_PROC_KMSGBUF
#include <libproc.h>
#include <limits.h>
#ifndef MSG_BSIZE
#define MSG_BSIZE (16*1024)
#endif
#else
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/msgbuf.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#endif
static void print_usage(const char *name) {
fprintf(stderr, "Usage: %s"
#if !defined _USE_APPLE_PROC_KMSGBUF && !(defined __sun && defined __SVR4)
" [--read-clear|-c]"
#endif
"\n", name);
}
int dmesg_main(int argc, char **argv) {
int clear = 0;
if(argc == 2) {
if(strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "--read-clear") == 0) clear = 1;
else if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage(argv[0]);
return 0;
}
}
#if (defined __GNU__ && defined __MACH__) || (defined __sun && defined __SVR4)
char *cat_argv[] = { "cat",
#if defined __GNU__ && defined __MACH__
"/var/log/dmesg",
#elif defined __sun && defined __SVR4
"/var/adm/messages",
#else
#error "Preprocessor fatal"
#endif
NULL };
int r = cat_main(2, cat_argv);
if(r == 0) {
putchar('\n');
#if defined __GNU__ && defined __MACH__
if(clear) {
int fd = open("/var/log/dmesg", O_TRUNC);
if(fd == -1 || close(fd) < 0) {
perror("dmesg");
return 1;
}
}
#endif
}
return r;
#else
ssize_t ret;
#ifdef __linux__
char buffer[KLOG_BUF_LEN + 1];
char *p = buffer;
int op = clear ? KLOG_READ_CLEAR : KLOG_READ_ALL;
int n = klogctl(op, buffer, KLOG_BUF_LEN);
if (n < 0) {
perror("klogctl");
return EXIT_FAILURE;
}
#elif defined _USE_APPLE_PROC_KMSGBUF
char buffer[MSG_BSIZE];
char *p = buffer;
int n = proc_kmsgbuf(buffer, sizeof buffer);
if(!n) {
perror("proc_kmsgbuf");
return 1;
}
#else
size_t n;
if(sysctlbyname("kern.msgbuf", NULL, &n, NULL, 0) < 0) {
perror("sysctlbyname");
return 1;
}
if(!n) return 0; // ?
char buffer[n + 2], *p = buffer;
if(sysctlbyname("kern.msgbuf", buffer, &n, NULL, 0) < 0) {
perror("sysctlbyname");
return 1;
}
if(clear && sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof clear) < 0) {
perror("sysctlbyname");
return 1;
}
//char *p = buffer;
if(buffer[n - 1] != '\n') buffer[n++] = '\n';
while(!*p && n) {
p++;
n--;
}
#endif
buffer[n] = 0;
while((ret = write(STDOUT_FILENO, p, n))) {
if(ret == -1) {
if(errno == EINTR) continue;
perror("write");
return EXIT_FAILURE;
}
p += ret;
n -= ret;
}
return 0;
#endif
}