aboutsummaryrefslogtreecommitdiff
path: root/sysklogd
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /sysklogd
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'sysklogd')
-rw-r--r--sysklogd/logger.c191
-rw-r--r--sysklogd/syslogd.c552
2 files changed, 374 insertions, 369 deletions
diff --git a/sysklogd/logger.c b/sysklogd/logger.c
index aab95b984..a9e0afcc8 100644
--- a/sysklogd/logger.c
+++ b/sysklogd/logger.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini logger implementation for busybox 3 * Mini logger implementation for busybox
3 * 4 *
@@ -39,21 +40,22 @@
39 */ 40 */
40#include <sys/syslog.h> 41#include <sys/syslog.h>
41typedef struct _code { 42typedef struct _code {
42 char *c_name; 43 char *c_name;
43 int c_val; 44 int c_val;
44} CODE; 45} CODE;
45extern CODE prioritynames[]; 46extern CODE prioritynames[];
46extern CODE facilitynames[]; 47extern CODE facilitynames[];
47#endif 48#endif
48 49
49static const char logger_usage[] = 50static const char logger_usage[] =
50 "logger [OPTION]... [MESSAGE]\n\n" 51 "logger [OPTION]... [MESSAGE]\n\n"
51 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n" 52 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n"
52 "Options:\n" 53 "Options:\n"
53 "\t-s\tLog to stderr as well as the system log.\n" 54 "\t-s\tLog to stderr as well as the system log.\n"
54 "\t-t\tLog using the specified tag (defaults to user name).\n" 55 "\t-t\tLog using the specified tag (defaults to user name).\n"
55 "\t-p\tEnter the message with the specified priority.\n" 56
56 "\t\tThis may be numerical or a ``facility.level'' pair.\n"; 57 "\t-p\tEnter the message with the specified priority.\n"
58 "\t\tThis may be numerical or a ``facility.level'' pair.\n";
57 59
58 60
59/* Decode a symbolic name to a numeric value 61/* Decode a symbolic name to a numeric value
@@ -61,20 +63,19 @@ static const char logger_usage[] =
61 * Copyright (c) 1983, 1993 63 * Copyright (c) 1983, 1993
62 * The Regents of the University of California. All rights reserved. 64 * The Regents of the University of California. All rights reserved.
63 */ 65 */
64static int 66static int decode(char *name, CODE * codetab)
65decode(char* name, CODE* codetab)
66{ 67{
67 CODE *c; 68 CODE *c;
68 69
69 if (isdigit(*name)) 70 if (isdigit(*name))
70 return (atoi(name)); 71 return (atoi(name));
71 for (c = codetab; c->c_name; c++) { 72 for (c = codetab; c->c_name; c++) {
72 if (!strcasecmp(name, c->c_name)) { 73 if (!strcasecmp(name, c->c_name)) {
73 return (c->c_val); 74 return (c->c_val);
75 }
74 } 76 }
75 }
76 77
77 return (-1); 78 return (-1);
78} 79}
79 80
80/* Decode a symbolic name to a numeric value 81/* Decode a symbolic name to a numeric value
@@ -82,96 +83,94 @@ decode(char* name, CODE* codetab)
82 * Copyright (c) 1983, 1993 83 * Copyright (c) 1983, 1993
83 * The Regents of the University of California. All rights reserved. 84 * The Regents of the University of California. All rights reserved.
84 */ 85 */
85static int 86static int pencode(char *s)
86pencode(char* s)
87{ 87{
88 char *save; 88 char *save;
89 int lev, fac=LOG_USER; 89 int lev, fac = LOG_USER;
90 90
91 for (save = s; *s && *s != '.'; ++s); 91 for (save = s; *s && *s != '.'; ++s);
92 if (*s) { 92 if (*s) {
93 *s = '\0'; 93 *s = '\0';
94 fac = decode(save, facilitynames); 94 fac = decode(save, facilitynames);
95 if (fac < 0) { 95 if (fac < 0) {
96 fprintf(stderr, "unknown facility name: %s\n", save); 96 fprintf(stderr, "unknown facility name: %s\n", save);
97 exit( FALSE); 97 exit(FALSE);
98 }
99 *s++ = '.';
100 } else {
101 s = save;
102 }
103 lev = decode(s, prioritynames);
104 if (lev < 0) {
105 fprintf(stderr, "unknown priority name: %s\n", save);
106 exit(FALSE);
98 } 107 }
99 *s++ = '.'; 108 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
100 }
101 else {
102 s = save;
103 }
104 lev = decode(s, prioritynames);
105 if (lev < 0) {
106 fprintf(stderr, "unknown priority name: %s\n", save);
107 exit( FALSE);
108 }
109 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
110} 109}
111 110
112 111
113extern int logger_main(int argc, char **argv) 112extern int logger_main(int argc, char **argv)
114{ 113{
115 int pri = LOG_USER|LOG_NOTICE; 114 int pri = LOG_USER | LOG_NOTICE;
116 int option = 0; 115 int option = 0;
117 int fromStdinFlag=FALSE; 116 int fromStdinFlag = FALSE;
118 int stopLookingAtMeLikeThat=FALSE; 117 int stopLookingAtMeLikeThat = FALSE;
119 char *message, buf[1024], name[128]; 118 char *message, buf[1024], name[128];
120 119
121 /* Fill out the name string early (may be overwritten later */ 120 /* Fill out the name string early (may be overwritten later */
122 my_getpwuid(name, geteuid()); 121 my_getpwuid(name, geteuid());
123 122
124 /* Parse any options */ 123 /* Parse any options */
125 while (--argc > 0 && **(++argv) == '-') { 124 while (--argc > 0 && **(++argv) == '-') {
126 if (*((*argv)+1) == '\0') { 125 if (*((*argv) + 1) == '\0') {
127 fromStdinFlag=TRUE; 126 fromStdinFlag = TRUE;
128 }
129 stopLookingAtMeLikeThat=FALSE;
130 while (*(++(*argv)) && stopLookingAtMeLikeThat==FALSE) {
131 switch (**argv) {
132 case 's':
133 option |= LOG_PERROR;
134 break;
135 case 'p':
136 if (--argc == 0) {
137 usage(logger_usage);
138 } 127 }
139 pri = pencode(*(++argv)); 128 stopLookingAtMeLikeThat = FALSE;
140 stopLookingAtMeLikeThat=TRUE; 129 while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) {
141 break; 130 switch (**argv) {
142 case 't': 131 case 's':
143 if (--argc == 0) { 132 option |= LOG_PERROR;
144 usage(logger_usage); 133 break;
134 case 'p':
135 if (--argc == 0) {
136 usage(logger_usage);
137 }
138 pri = pencode(*(++argv));
139 stopLookingAtMeLikeThat = TRUE;
140 break;
141 case 't':
142 if (--argc == 0) {
143 usage(logger_usage);
144 }
145 strncpy(name, *(++argv), sizeof(name));
146 stopLookingAtMeLikeThat = TRUE;
147 break;
148 default:
149 usage(logger_usage);
150 }
145 } 151 }
146 strncpy(name, *(++argv), sizeof(name));
147 stopLookingAtMeLikeThat=TRUE;
148 break;
149 default:
150 usage(logger_usage);
151 }
152 } 152 }
153 }
154 153
155 if (fromStdinFlag==TRUE) { 154 if (fromStdinFlag == TRUE) {
156 /* read from stdin */ 155 /* read from stdin */
157 int c, i=0; 156 int c, i = 0;
158 while ((c = getc(stdin)) != EOF && i<sizeof(buf)) { 157
159 buf[i++]=c; 158 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
160 } 159 buf[i++] = c;
161 message=buf; 160 }
162 } else { 161 message = buf;
163 if (argc>=1) {
164 message = *argv;
165 } else { 162 } else {
166 fprintf(stderr, "No message\n"); 163 if (argc >= 1) {
167 exit( FALSE); 164 message = *argv;
165 } else {
166 fprintf(stderr, "No message\n");
167 exit(FALSE);
168 }
168 } 169 }
169 }
170 170
171 openlog( name, option, (pri | LOG_FACMASK)); 171 openlog(name, option, (pri | LOG_FACMASK));
172 syslog( pri, message); 172 syslog(pri, message);
173 closelog(); 173 closelog();
174 174
175 exit( TRUE); 175 exit(TRUE);
176} 176}
177
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index c48d5a435..db535044c 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini syslogd implementation for busybox 3 * Mini syslogd implementation for busybox
3 * 4 *
@@ -49,266 +50,272 @@ extern int ksyslog(int type, char *buf, int len);
49#define __LOG_FILE "/var/log/messages" 50#define __LOG_FILE "/var/log/messages"
50 51
51 52
52static char* logFilePath = __LOG_FILE; 53static char *logFilePath = __LOG_FILE;
54
53/* interval between marks in seconds */ 55/* interval between marks in seconds */
54static int MarkInterval = 20*60; 56static int MarkInterval = 20 * 60;
57
55/* localhost's name */ 58/* localhost's name */
56static char LocalHostName[32]; 59static char LocalHostName[32];
57 60
58static const char syslogd_usage[] = 61static const char syslogd_usage[] =
59 "syslogd [OPTION]...\n\n" 62 "syslogd [OPTION]...\n\n"
60 "Linux system and kernel (provides klogd) logging utility.\n" 63 "Linux system and kernel (provides klogd) logging utility.\n"
61 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n" 64 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
62 "Options:\n" 65 "Options:\n"
63 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n" 66 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
64 "\t-n\tDo not fork into the background (for when run by init)\n" 67 "\t-n\tDo not fork into the background (for when run by init)\n"
65#ifdef BB_KLOGD 68#ifdef BB_KLOGD
66 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n" 69 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
67#endif 70#endif
68 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n"; 71 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
69 72
70 73
71/* print a message to the log file */ 74/* print a message to the log file */
72static void message(char *fmt, ...) 75static void message(char *fmt, ...)
73{ 76{
74 int fd; 77 int fd;
75 va_list arguments; 78 va_list arguments;
76 79
77 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) { 80 if (
78 va_start(arguments, fmt); 81 (fd =
79 vdprintf(fd, fmt, arguments); 82 device_open(logFilePath,
80 va_end(arguments); 83 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
81 close(fd); 84 O_NONBLOCK)) >= 0) {
82 } else { 85 va_start(arguments, fmt);
83 /* Always send console messages to /dev/console so people will see them. */ 86 vdprintf(fd, fmt, arguments);
84 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) { 87 va_end(arguments);
85 va_start(arguments, fmt); 88 close(fd);
86 vdprintf(fd, fmt, arguments);
87 va_end(arguments);
88 close(fd);
89 } else { 89 } else {
90 fprintf(stderr, "Bummer, can't print: "); 90 /* Always send console messages to /dev/console so people will see them. */
91 va_start(arguments, fmt); 91 if (
92 vfprintf(stderr, fmt, arguments); 92 (fd =
93 fflush(stderr); 93 device_open(_PATH_CONSOLE,
94 va_end(arguments); 94 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
95 va_start(arguments, fmt);
96 vdprintf(fd, fmt, arguments);
97 va_end(arguments);
98 close(fd);
99 } else {
100 fprintf(stderr, "Bummer, can't print: ");
101 va_start(arguments, fmt);
102 vfprintf(stderr, fmt, arguments);
103 fflush(stderr);
104 va_end(arguments);
105 }
95 } 106 }
96 }
97} 107}
98 108
99static void logMessage( int pri, char* msg) 109static void logMessage(int pri, char *msg)
100{ 110{
101 time_t now; 111 time_t now;
102 char *timestamp; 112 char *timestamp;
103 static char res[20]; 113 static char res[20];
104 CODE *c_pri, *c_fac; 114 CODE *c_pri, *c_fac;
105 115
106 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++); 116 for (c_fac = facilitynames;
107 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++); 117 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
108 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0') 118 for (c_pri = prioritynames;
109 snprintf (res, sizeof(res), "<%d>", pri); 119 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
110 else 120 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
111 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); 121 snprintf(res, sizeof(res), "<%d>", pri);
112 122 else
113 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || 123 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
114 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 124
115 { 125 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
116 time(&now); 126 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
117 timestamp = ctime(&now) + 4; 127 time(&now);
118 timestamp[15] = '\0'; 128 timestamp = ctime(&now) + 4;
119 } else { 129 timestamp[15] = '\0';
120 timestamp = msg; 130 } else {
121 timestamp[15] = '\0'; 131 timestamp = msg;
122 msg += 16; 132 timestamp[15] = '\0';
123 } 133 msg += 16;
124 134 }
125 /* todo: supress duplicates */ 135
126 136 /* todo: supress duplicates */
127 /* now spew out the message to wherever it is supposed to go */ 137
128 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg); 138 /* now spew out the message to wherever it is supposed to go */
139 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
129} 140}
130 141
131static void quit_signal(int sig) 142static void quit_signal(int sig)
132{ 143{
133 logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting."); 144 logMessage(LOG_SYSLOG | LOG_INFO, "System log daemon exiting.");
134 unlink( _PATH_LOG); 145 unlink(_PATH_LOG);
135 exit( TRUE); 146 exit(TRUE);
136} 147}
137 148
138static void restart_signal(int sig) 149static void restart_signal(int sig)
139{ 150{
140 /* pretend to restart */ 151 /* pretend to restart */
141 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting"); 152 logMessage(LOG_SYSLOG | LOG_INFO, "syslogd restarting");
142} 153}
143 154
144static void domark(int sig) 155static void domark(int sig)
145{ 156{
146 if (MarkInterval > 0) { 157 if (MarkInterval > 0) {
147 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --"); 158 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
148 alarm(MarkInterval); 159 alarm(MarkInterval);
149 } 160 }
150} 161}
151 162
152static void doSyslogd(void) 163static void doSyslogd(void)
153{ 164{
154 struct sockaddr_un sunx; 165 struct sockaddr_un sunx;
155 int fd, conn; 166 int fd, conn;
156 size_t addrLength; 167 size_t addrLength;
157 char buf[1024]; 168 char buf[1024];
158 char *q, *p = buf; 169 char *q, *p = buf;
159 int readSize; 170 int readSize;
160 171
161 /* Set up sig handlers */ 172 /* Set up sig handlers */
162 signal(SIGINT, quit_signal); 173 signal(SIGINT, quit_signal);
163 signal(SIGTERM, quit_signal); 174 signal(SIGTERM, quit_signal);
164 signal(SIGQUIT, quit_signal); 175 signal(SIGQUIT, quit_signal);
165 signal(SIGHUP, restart_signal); 176 signal(SIGHUP, restart_signal);
166 signal(SIGALRM, domark); 177 signal(SIGALRM, domark);
167 alarm(MarkInterval); 178 alarm(MarkInterval);
168 179
169 /* Remove any preexisting socket/file */ 180 /* Remove any preexisting socket/file */
170 unlink(_PATH_LOG); 181 unlink(_PATH_LOG);
171 182
172 memset(&sunx, 0, sizeof(sunx)); 183 memset(&sunx, 0, sizeof(sunx));
173 sunx.sun_family = AF_UNIX; /* Unix domain socket */ 184 sunx.sun_family = AF_UNIX; /* Unix domain socket */
174 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path)); 185 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
175 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) { 186 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
176 perror("Couldn't obtain descriptor for socket " _PATH_LOG); 187 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
177 exit( FALSE); 188 exit(FALSE);
178 } 189 }
179
180 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
181 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
182 (listen(fd, 5)) )
183 {
184 perror("Could not connect to socket " _PATH_LOG);
185 exit( FALSE);
186 }
187
188 umask(0);
189 if (chmod(_PATH_LOG, 0666) < 0) {
190 perror("Could not set permission on " _PATH_LOG);
191 exit (FALSE);
192 }
193
194 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
195 "BusyBox v" BB_VER " (" BB_BT ")");
196
197
198 while ((conn = accept(fd, (struct sockaddr *) &sunx,
199 &addrLength)) >= 0)
200 {
201 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
202 {
203 char line[1025];
204 unsigned char c;
205 int pri = (LOG_USER|LOG_NOTICE);
206
207 memset (line, 0, sizeof(line));
208 p = buf;
209 q = line;
210 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
211 if (c == '<') {
212 /* Parse the magic priority number */
213 pri = 0;
214 while (isdigit(*(++p))) {
215 pri = 10 * pri + (*p - '0');
216 }
217 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
218 pri = (LOG_USER|LOG_NOTICE);
219 } else if (c == '\n') {
220 *q++ = ' ';
221 } else if (iscntrl(c)&&(c<0177)) {
222 *q++ = '^';
223 *q++ = c ^ 0100;
224 } else {
225 *q++ = c;
226 }
227 p++;
228 }
229 *q = '\0';
230 190
231 /* Now log it */ 191 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
232 logMessage( pri, line); 192 if ((bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
193 (listen(fd, 5))) {
194 perror("Could not connect to socket " _PATH_LOG);
195 exit(FALSE);
233 } 196 }
234 close(conn);
235 }
236 197
237 close(fd); 198 umask(0);
199 if (chmod(_PATH_LOG, 0666) < 0) {
200 perror("Could not set permission on " _PATH_LOG);
201 exit(FALSE);
202 }
203
204 logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: "
205 "BusyBox v" BB_VER " (" BB_BT ")");
206
207
208 while ((conn = accept(fd, (struct sockaddr *) &sunx,
209 &addrLength)) >= 0) {
210 while ((readSize = read(conn, buf, sizeof(buf))) > 0) {
211 char line[1025];
212 unsigned char c;
213 int pri = (LOG_USER | LOG_NOTICE);
214
215 memset(line, 0, sizeof(line));
216 p = buf;
217 q = line;
218 while (p && (c = *p) && q < &line[sizeof(line) - 1]) {
219 if (c == '<') {
220 /* Parse the magic priority number */
221 pri = 0;
222 while (isdigit(*(++p))) {
223 pri = 10 * pri + (*p - '0');
224 }
225 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
226 pri = (LOG_USER | LOG_NOTICE);
227 } else if (c == '\n') {
228 *q++ = ' ';
229 } else if (iscntrl(c) && (c < 0177)) {
230 *q++ = '^';
231 *q++ = c ^ 0100;
232 } else {
233 *q++ = c;
234 }
235 p++;
236 }
237 *q = '\0';
238
239 /* Now log it */
240 logMessage(pri, line);
241 }
242 close(conn);
243 }
244
245 close(fd);
238} 246}
239 247
240#ifdef BB_KLOGD 248#ifdef BB_KLOGD
241 249
242static void klogd_signal(int sig) 250static void klogd_signal(int sig)
243{ 251{
244 ksyslog(7, NULL, 0); 252 ksyslog(7, NULL, 0);
245 ksyslog(0, 0, 0); 253 ksyslog(0, 0, 0);
246 logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting."); 254 logMessage(LOG_SYSLOG | LOG_INFO, "Kernel log daemon exiting.");
247 exit( TRUE); 255 exit(TRUE);
248} 256}
249 257
250static void doKlogd(void) 258static void doKlogd(void)
251{ 259{
252 int priority=LOG_INFO; 260 int priority = LOG_INFO;
253 char log_buffer[4096]; 261 char log_buffer[4096];
254 char *logp; 262 char *logp;
255 263
256 /* Set up sig handlers */ 264 /* Set up sig handlers */
257 signal(SIGINT, klogd_signal); 265 signal(SIGINT, klogd_signal);
258 signal(SIGKILL, klogd_signal); 266 signal(SIGKILL, klogd_signal);
259 signal(SIGTERM, klogd_signal); 267 signal(SIGTERM, klogd_signal);
260 signal(SIGHUP, klogd_signal); 268 signal(SIGHUP, klogd_signal);
261 logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: " 269 logMessage(LOG_SYSLOG | LOG_INFO, "klogd started: "
262 "BusyBox v" BB_VER " (" BB_BT ")"); 270 "BusyBox v" BB_VER " (" BB_BT ")");
263 271
264 ksyslog(1, NULL, 0); 272 ksyslog(1, NULL, 0);
265 273
266 while (1) { 274 while (1) {
267 /* Use kernel syscalls */ 275 /* Use kernel syscalls */
268 memset(log_buffer, '\0', sizeof(log_buffer)); 276 memset(log_buffer, '\0', sizeof(log_buffer));
269 if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) { 277 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
270 char message[80]; 278 char message[80];
271 if ( errno == EINTR ) 279
272 continue; 280 if (errno == EINTR)
273 snprintf(message, 79, "klogd: Error return from sys_sycall: " \ 281 continue;
274 "%d - %s.\n", errno, strerror(errno)); 282 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
275 logMessage(LOG_SYSLOG|LOG_ERR, message); 283 "%d - %s.\n", errno, strerror(errno));
276 exit(1); 284 logMessage(LOG_SYSLOG | LOG_ERR, message);
285 exit(1);
286 }
287 logp = log_buffer;
288 if (*log_buffer == '<') {
289 switch (*(log_buffer + 1)) {
290 case '0':
291 priority = LOG_EMERG;
292 break;
293 case '1':
294 priority = LOG_ALERT;
295 break;
296 case '2':
297 priority = LOG_CRIT;
298 break;
299 case '3':
300 priority = LOG_ERR;
301 break;
302 case '4':
303 priority = LOG_WARNING;
304 break;
305 case '5':
306 priority = LOG_NOTICE;
307 break;
308 case '6':
309 priority = LOG_INFO;
310 break;
311 case '7':
312 default:
313 priority = LOG_DEBUG;
314 }
315 logp += 3;
316 }
317 logMessage(LOG_KERN | priority, logp);
277 } 318 }
278 logp=log_buffer;
279 if ( *log_buffer == '<' )
280 {
281 switch ( *(log_buffer+1) )
282 {
283 case '0':
284 priority = LOG_EMERG;
285 break;
286 case '1':
287 priority = LOG_ALERT;
288 break;
289 case '2':
290 priority = LOG_CRIT;
291 break;
292 case '3':
293 priority = LOG_ERR;
294 break;
295 case '4':
296 priority = LOG_WARNING;
297 break;
298 case '5':
299 priority = LOG_NOTICE;
300 break;
301 case '6':
302 priority = LOG_INFO;
303 break;
304 case '7':
305 default:
306 priority = LOG_DEBUG;
307 }
308 logp+=3;
309 }
310 logMessage(LOG_KERN|priority, logp);
311 }
312 319
313} 320}
314 321
@@ -316,76 +323,75 @@ static void doKlogd(void)
316 323
317extern int syslogd_main(int argc, char **argv) 324extern int syslogd_main(int argc, char **argv)
318{ 325{
319 int pid, klogd_pid; 326 int pid, klogd_pid;
320 int doFork = TRUE; 327 int doFork = TRUE;
328
321#ifdef BB_KLOGD 329#ifdef BB_KLOGD
322 int startKlogd = TRUE; 330 int startKlogd = TRUE;
323#endif 331#endif
324 int stopDoingThat = FALSE; 332 int stopDoingThat = FALSE;
325 char *p; 333 char *p;
326 char **argv1=argv; 334 char **argv1 = argv;
327 335
328 while (--argc > 0 && **(++argv1) == '-') { 336 while (--argc > 0 && **(++argv1) == '-') {
329 stopDoingThat = FALSE; 337 stopDoingThat = FALSE;
330 while (stopDoingThat == FALSE && *(++(*argv1))) { 338 while (stopDoingThat == FALSE && *(++(*argv1))) {
331 switch (**argv1) { 339 switch (**argv1) {
332 case 'm': 340 case 'm':
333 if (--argc == 0) { 341 if (--argc == 0) {
334 usage(syslogd_usage); 342 usage(syslogd_usage);
335 } 343 }
336 MarkInterval = atoi(*(++argv1))*60; 344 MarkInterval = atoi(*(++argv1)) * 60;
337 break; 345 break;
338 case 'n': 346 case 'n':
339 doFork = FALSE; 347 doFork = FALSE;
340 break; 348 break;
341#ifdef BB_KLOGD 349#ifdef BB_KLOGD
342 case 'K': 350 case 'K':
343 startKlogd = FALSE; 351 startKlogd = FALSE;
344 break; 352 break;
345#endif 353#endif
346 case 'O': 354 case 'O':
347 if (--argc == 0) { 355 if (--argc == 0) {
348 usage(syslogd_usage); 356 usage(syslogd_usage);
357 }
358 logFilePath = *(++argv1);
359 stopDoingThat = TRUE;
360 break;
361 default:
362 usage(syslogd_usage);
363 }
349 } 364 }
350 logFilePath = *(++argv1);
351 stopDoingThat = TRUE;
352 break;
353 default:
354 usage(syslogd_usage);
355 }
356 } 365 }
357 } 366
358 367 /* Store away localhost's name before the fork */
359 /* Store away localhost's name before the fork */ 368 gethostname(LocalHostName, sizeof(LocalHostName));
360 gethostname(LocalHostName, sizeof(LocalHostName)); 369 if ((p = strchr(LocalHostName, '.'))) {
361 if ( (p = strchr(LocalHostName, '.')) ) { 370 *p++ = '\0';
362 *p++ = '\0'; 371 }
363 } 372
364 373 if (doFork == TRUE) {
365 if (doFork == TRUE) { 374 pid = fork();
366 pid = fork(); 375 if (pid < 0)
367 if ( pid < 0 ) 376 exit(pid);
368 exit( pid); 377 else if (pid == 0) {
369 else if ( pid == 0 ) { 378 strncpy(argv[0], "syslogd", strlen(argv[0]));
370 strncpy(argv[0], "syslogd",strlen(argv[0])); 379 doSyslogd();
371 doSyslogd(); 380 }
381 } else {
382 doSyslogd();
372 } 383 }
373 } else {
374 doSyslogd();
375 }
376 384
377#ifdef BB_KLOGD 385#ifdef BB_KLOGD
378 /* Start up the klogd process */ 386 /* Start up the klogd process */
379 if (startKlogd == TRUE) { 387 if (startKlogd == TRUE) {
380 klogd_pid = fork(); 388 klogd_pid = fork();
381 if (klogd_pid == 0 ) { 389 if (klogd_pid == 0) {
382 strncpy(argv[0], "klogd", strlen(argv[0])); 390 strncpy(argv[0], "klogd", strlen(argv[0]));
383 doKlogd(); 391 doKlogd();
392 }
384 } 393 }
385 }
386#endif 394#endif
387 395
388 exit( TRUE); 396 exit(TRUE);
389} 397}
390
391