annotate toys/pending/klogd.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents 6a06541c090c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* klogd.c - Klogd, The kernel log Dameon.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
3 * Copyright 2013 Sandeep Sharma <sandeep.jack2756@gmail.com>
997
8b1814e4c987 Ashwini Sharma said that Kyungwan Han should be in the contact info for the commands he sent recently.
Rob Landley <rob@landley.net>
parents: 995
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * No standard
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 USE_KLOGD(NEWTOY(klogd, "c#<1>8n", TOYFLAG_SBIN))
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config KLOGD
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 bool "klogd"
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
12 default n
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 usage: klogd [-n] [-c N]
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 -c N Print to console messages more urgent than prio N (1-8)"
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 -n Run in foreground.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 config KLOGD_SOURCE_RING_BUFFER
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 bool "enable kernel ring buffer as log source."
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 default n
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 depends on KLOGD
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #define FOR_klogd
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
27 #include <signal.h>
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
28 #include <sys/klog.h>
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 GLOBALS(
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 long level;
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
31
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 int fd;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 )
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 static void set_log_level(int level)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
37 if (CFG_KLOGD_SOURCE_RING_BUFFER)
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
38 klogctl(8, NULL, level);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
39 else {
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 FILE *fptr = xfopen("/proc/sys/kernel/printk", "w");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 fprintf(fptr, "%u\n", level);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 fclose(fptr);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 fptr = NULL;
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
44 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 }
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
46
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 static void handle_signal(int sig)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 {
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
49 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
50 klogctl(7, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
51 klogctl(0, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
52 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
53 set_log_level(7);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
54 xclose(TT.fd);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
55 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 syslog(LOG_NOTICE,"KLOGD: Daemon exiting......");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 exit(1);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 * Read kernel ring buffer in local buff and keep track of
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 * "used" amount to track next read to start.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 void klogd_main(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 {
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
66 int prio, size, used = 0;
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
67 char *start, *line_start, msg_buffer[16348]; //LOG_LINE_LENGTH - Ring buffer size
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 sigatexit(handle_signal);
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
70 if (toys.optflags & FLAG_c) set_log_level(TT.level); //set log level
1391
6a06541c090c Use libc daemon() instead of pending daemonize.
Rob Landley <rob@landley.net>
parents: 997
diff changeset
71 if (!(toys.optflags & FLAG_n)) daemon(0, 0); //Make it daemon
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
72
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
73 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
74 syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
75 klogctl(1, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
76 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
77 TT.fd = xopen("/proc/kmsg", O_RDONLY); //_PATH_KLOG in paths.h
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
78 syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
79 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 while(1) {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 start = msg_buffer + used; //start updated for re-read.
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
84 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
85 size = klogctl(2, start, sizeof(msg_buffer) - used - 1);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
86 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
87 size = xread(TT.fd, start, sizeof(msg_buffer) - used - 1);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
88 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 if (size < 0) perror_exit("error reading file:");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 start[size] = '\0'; //Ensure last line to be NUL terminated.
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
91 if (used) start = msg_buffer;
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
92 while(start) {
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
93 if ((line_start = strsep(&start, "\n")) != NULL && start != NULL) used = 0;
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 else { //Incomplete line, copy it to start of buff.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 used = strlen(line_start);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 strcpy(msg_buffer, line_start);
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
97 if (used < (sizeof(msg_buffer) - 1)) break;
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 used = 0; //we have buffer full, log it as it is.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 prio = LOG_INFO; //we dont know priority, mark it INFO
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
101 if (*line_start == '<') { //we have new line to syslog
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 line_start++;
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
103 if (line_start) prio = (int)strtoul(line_start, &line_start, 10);
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
104 if (*line_start == '>') line_start++;
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 }
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
106 if (*line_start) syslog(prio, "%s", line_start);
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 }
974
5a50be9d00a7 The attached KLOGD patch is an improved version of the one you have in hg.
Ashwini Sharma <ak.ashwini@gmail.com>
parents: 955
diff changeset
109 }