2023-11-05 21:42:26 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/klog.h>
|
|
|
|
#define BUF_SIZE 8196
|
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
int main(int argc, char **argv) {
|
2023-11-05 21:42:26 +03:00
|
|
|
unsigned int s_size = 0;
|
|
|
|
unsigned int n_level = 0;
|
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "s:n:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
s_size = atoi(optarg);
|
|
|
|
break;
|
2023-11-05 21:42:26 +03:00
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
case 'n':
|
|
|
|
n_level = atoi(optarg);
|
|
|
|
break;
|
2023-11-05 21:42:26 +03:00
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
default:
|
|
|
|
printf("dmesg\n\t[-n=L Set console logging level] [-s=S Buffer Size]\n");
|
|
|
|
return 0;
|
2023-11-05 21:42:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup */
|
|
|
|
if (n_level)
|
|
|
|
if (klogctl(8, NULL, (long)n_level) < 0) {
|
|
|
|
fprintf(stderr, "dmesg: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s_size)
|
|
|
|
s_size = klogctl(10, NULL, 0);
|
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
|
2023-11-05 21:42:26 +03:00
|
|
|
/* Get kernel log */
|
|
|
|
char *buf = malloc(s_size + 1);
|
|
|
|
if (buf == NULL) {
|
|
|
|
fprintf(stderr, "dmesg: malloc failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = klogctl(3, buf, s_size);
|
|
|
|
if (n <= 0) {
|
|
|
|
free(buf);
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fprintf(stderr, "dmesg: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:24:49 +03:00
|
|
|
|
2023-11-05 21:42:26 +03:00
|
|
|
/* Print */
|
|
|
|
write(STDOUT_FILENO, buf, n);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|