annotate toys/pending/klogd.c @ 950:a4a6bcf32657

klogd, submitted by Ashwini Sharma.
author Rob Landley <rob@landley.net>
date Mon, 15 Jul 2013 13:12:08 -0500
parents
children 144d5ba7d410
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 *
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Sandeep Sharma <sandeep.jack2756@gmail.com>
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"
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
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"
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include <signal.h>
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 GLOBALS(
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 long level;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 int fd;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 )
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #if CFG_KLOGD_SOURCE_RING_BUFFER
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #include <sys/klog.h>
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 * Open klogd with ring buffer as log source
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 static void open_klogd(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 klogctl(1, NULL, 0);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 * Read kernel ring buffer
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 static int read_klogd(char *bufptr, int len)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 return klogctl(2, bufptr, len);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 * Set log level to LEVEL
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 static void set_log_level(int level)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 klogctl(8, NULL, level);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 * Close klog
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 static void close_klogd(void)
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 klogctl(7, NULL, 0);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 klogctl(0, NULL, 0);
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 #else
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 #include<paths.h>
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 #ifndef _PATH_KLOG
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 #error "_PATH_KLOG is not known"
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 #endif
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 * Open klog with /proc/kmsg as log source
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 static void open_klogd(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 TT.fd = xopen(_PATH_KLOG, O_RDONLY);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 * Read log to local buffer
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 static int read_klogd(char *bufptr, int len)
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 return xread(TT.fd, bufptr, len);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 * Set log level to LEVEL by writing to PATH_PRINTK
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 static void set_log_level(int level)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 FILE *fptr = xfopen("/proc/sys/kernel/printk", "w");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 fprintf(fptr, "%u\n", level);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 fclose(fptr);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 fptr = NULL;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 * set log level while exiting
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 static void close_klogd(void)
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 set_log_level(7);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 xclose(TT.fd);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 #endif
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 * Handle signals
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 static void handle_signal(int sig)
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 close_klogd();
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 syslog(LOG_NOTICE,"KLOGD: Daemon exiting......");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 exit(1);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 static int go_daemon(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 int fd;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
116
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 fd = open("/dev/null", O_RDWR);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 if (fd < 0) fd = open("/", O_RDONLY, 0666);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 pid_t pid = fork();
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
120
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 if (pid < 0) {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 error_msg("DAEMON: fail to fork");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 return -1;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 if (pid) exit(EXIT_SUCCESS);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
126
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 setsid();
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 dup2(fd, 0);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 dup2(fd, 1);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 dup2(fd, 2);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 if (fd > 2) close(fd);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 return 0;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
134
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 /*
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 * 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
137 * "used" amount to track next read to start.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 */
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 void klogd_main(void)
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 char msg_buffer[16348]; //LOG_LINE_LENGTH - Ring buffer size
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 int prio, size;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 int used = 0;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 char *start, *line_start;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 sigatexit(handle_signal);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 if(toys.optflags & FLAG_c) set_log_level(TT.level); //set log level
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 if(!(toys.optflags & FLAG_n)) go_daemon(); //Make it daemon
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 open_klogd();
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 while(1) {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 start = msg_buffer + used; //start updated for re-read.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 size = read_klogd(start, sizeof(msg_buffer)-1-used);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 if (size < 0) perror_exit("error reading file:");
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 start[size] = '\0'; //Ensure last line to be NUL terminated.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 if(used) start = msg_buffer;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 while(1) {
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 if((line_start = strsep(&start, "\n")) != NULL && start != NULL) used = 0;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 else { //Incomplete line, copy it to start of buff.
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 used = strlen(line_start);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 strcpy(msg_buffer, line_start);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 if(used < (sizeof(msg_buffer) - 1)) break;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 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
165 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 prio = LOG_INFO; //we dont know priority, mark it INFO
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 if(*line_start == '<') { //we have new line to syslog
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 line_start++;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 if(line_start) prio = (int)strtoul(line_start, &line_start, 10);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 if(*line_start == '>') line_start++;
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 if(*line_start) syslog(prio, "%s", line_start);
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 }
a4a6bcf32657 klogd, submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 }