annotate toys/pending/klogd.c @ 995:893c86bbe452

Add daemonize function to lib for klogd and syslogd
author Felix Janda <felix.janda@posteo.de>
date Sat, 10 Aug 2013 20:18:18 +0200
parents 00c7b6f4104a
children 8b1814e4c987
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>
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * No standard
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_KLOGD(NEWTOY(klogd, "c#<1>8n", TOYFLAG_SBIN))
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config KLOGD
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 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
11 default n
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: klogd [-n] [-c N]
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 -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
16 -n Run in foreground.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 config KLOGD_SOURCE_RING_BUFFER
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 bool "enable kernel ring buffer as log source."
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 default n
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 depends on KLOGD
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 */
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 #define FOR_klogd
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #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
26 #include <signal.h>
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
27 #include <sys/klog.h>
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 GLOBALS(
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 long level;
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
30
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 int fd;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 )
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 static void set_log_level(int level)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 {
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
36 if (CFG_KLOGD_SOURCE_RING_BUFFER)
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
37 klogctl(8, NULL, level);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
38 else {
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 FILE *fptr = xfopen("/proc/sys/kernel/printk", "w");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 fprintf(fptr, "%u\n", level);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 fclose(fptr);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 fptr = NULL;
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
43 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 }
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
45
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 static void handle_signal(int sig)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 {
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
48 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
49 klogctl(7, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
50 klogctl(0, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
51 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
52 set_log_level(7);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
53 xclose(TT.fd);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
54 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 syslog(LOG_NOTICE,"KLOGD: Daemon exiting......");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 exit(1);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 }
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 * 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
61 * "used" amount to track next read to start.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 void klogd_main(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 {
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
65 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
66 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
67
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 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
69 if (toys.optflags & FLAG_c) set_log_level(TT.level); //set log level
995
893c86bbe452 Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents: 994
diff changeset
70 if (!(toys.optflags & FLAG_n)) daemonize(); //Make it daemon
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
71
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
72 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
73 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
74 klogctl(1, NULL, 0);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
75 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
76 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
77 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
78 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 while(1) {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 start = msg_buffer + used; //start updated for re-read.
994
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
83 if (CFG_KLOGD_SOURCE_RING_BUFFER) {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
84 size = klogctl(2, start, sizeof(msg_buffer) - used - 1);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
85 } else {
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
86 size = xread(TT.fd, start, sizeof(msg_buffer) - used - 1);
00c7b6f4104a Cleanup klogd
Felix Janda <felix.janda@posteo.de>
parents: 974
diff changeset
87 }
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 if (size < 0) perror_exit("error reading file:");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 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
90 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
91 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
92 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
93 else { //Incomplete line, copy it to start of buff.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 used = strlen(line_start);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 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
96 if (used < (sizeof(msg_buffer) - 1)) break;
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 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
98 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 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
100 if (*line_start == '<') { //we have new line to syslog
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 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
102 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
103 if (*line_start == '>') line_start++;
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 }
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
105 if (*line_start) syslog(prio, "%s", line_start);
950
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 }
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
108 }