OSDN Git Service

Hmm. Missed this change
[android-x86/external-busybox.git] / sysklogd / syslogd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini syslogd implementation for busybox
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <ctype.h>
36 #include <netdb.h>
37 #include <sys/klog.h>
38 #include <errno.h>
39 #include <paths.h>
40
41 #define ksyslog klogctl
42 extern int ksyslog(int type, char *buf, int len);
43
44
45 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
46 #define SYSLOG_NAMES
47 #include <sys/syslog.h>
48
49 /* Path for the file where all log messages are written */
50 #define __LOG_FILE              "/var/log/messages"
51
52
53 static char *logFilePath = __LOG_FILE;
54
55 /* interval between marks in seconds */
56 static int MarkInterval = 20 * 60;
57
58 /* localhost's name */
59 static char LocalHostName[32];
60
61 static const char syslogd_usage[] =
62         "syslogd [OPTION]...\n\n"
63         "Linux system and kernel (provides klogd) logging utility.\n"
64         "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
65         "Options:\n"
66         "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
67         "\t-n\tDo not fork into the background (for when run by init)\n"
68 #ifdef BB_KLOGD
69         "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
70 #endif
71         "\t-O\tSpecify an alternate log file.  default=/var/log/messages\n";
72
73
74 /* Note: There is also a function called "message()" in init.c */
75 /* print a message to the log file */
76 static void message(char *fmt, ...)
77 {
78         int fd;
79         va_list arguments;
80
81         if (
82                 (fd =
83                  device_open(logFilePath,
84                                          O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
85                                          O_NONBLOCK)) >= 0) {
86                 va_start(arguments, fmt);
87                 vdprintf(fd, fmt, arguments);
88                 va_end(arguments);
89                 close(fd);
90         } else {
91                 /* Always send console messages to /dev/console so people will see them. */
92                 if (
93                         (fd =
94                          device_open(_PATH_CONSOLE,
95                                                  O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
96                         va_start(arguments, fmt);
97                         vdprintf(fd, fmt, arguments);
98                         va_end(arguments);
99                         close(fd);
100                 } else {
101                         fprintf(stderr, "Bummer, can't print: ");
102                         va_start(arguments, fmt);
103                         vfprintf(stderr, fmt, arguments);
104                         fflush(stderr);
105                         va_end(arguments);
106                 }
107         }
108 }
109
110 static void logMessage(int pri, char *msg)
111 {
112         time_t now;
113         char *timestamp;
114         static char res[20] = "";
115         CODE *c_pri, *c_fac;
116
117         if (pri != 0) {
118                 for (c_fac = facilitynames;
119                          c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
120                 for (c_pri = prioritynames;
121                          c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
122                 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
123                         snprintf(res, sizeof(res), "<%d>", pri);
124                 else
125                         snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
126         }
127
128         if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
129                 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
130                 time(&now);
131                 timestamp = ctime(&now) + 4;
132                 timestamp[15] = '\0';
133         } else {
134                 timestamp = msg;
135                 timestamp[15] = '\0';
136                 msg += 16;
137         }
138
139         /* todo: supress duplicates */
140
141         /* now spew out the message to wherever it is supposed to go */
142         message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
143 }
144
145 static void quit_signal(int sig)
146 {
147         logMessage(0, "System log daemon exiting.");
148         unlink(_PATH_LOG);
149         exit(TRUE);
150 }
151
152 static void domark(int sig)
153 {
154         if (MarkInterval > 0) {
155                 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
156                 alarm(MarkInterval);
157         }
158 }
159
160 static void doSyslogd(void)
161 {
162         struct sockaddr_un sunx;
163         int fd, conn;
164         size_t addrLength;
165         char buf[1024];
166         char *q, *p = buf;
167         int readSize;
168
169         /* Set up sig handlers */
170         signal(SIGINT, quit_signal);
171         signal(SIGTERM, quit_signal);
172         signal(SIGQUIT, quit_signal);
173         signal(SIGALRM, domark);
174         signal(SIGHUP, SIG_IGN);
175         alarm(MarkInterval);
176
177         /* Remove any preexisting socket/file */
178         unlink(_PATH_LOG);
179
180         memset(&sunx, 0, sizeof(sunx));
181         sunx.sun_family = AF_UNIX;      /* Unix domain socket */
182         strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
183         if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
184                 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
185                 exit(FALSE);
186         }
187
188         addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
189         if ((bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
190                 (listen(fd, 5))) {
191                 perror("Could not connect to socket " _PATH_LOG);
192                 exit(FALSE);
193         }
194
195         umask(0);
196         if (chmod(_PATH_LOG, 0666) < 0) {
197                 perror("Could not set permission on " _PATH_LOG);
198                 exit(FALSE);
199         }
200
201         logMessage(0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
202
203
204         while ((conn = accept(fd, (struct sockaddr *) &sunx,
205                                                   &addrLength)) >= 0) {
206                 while ((readSize = read(conn, buf, sizeof(buf))) > 0) {
207                         char line[1025];
208                         unsigned char c;
209                         int pri = (LOG_USER | LOG_NOTICE);
210
211                         memset(line, 0, sizeof(line));
212                         p = buf;
213                         q = line;
214                         while (p && (c = *p) && q < &line[sizeof(line) - 1]) {
215                                 if (c == '<') {
216                                         /* Parse the magic priority number */
217                                         pri = 0;
218                                         while (isdigit(*(++p))) {
219                                                 pri = 10 * pri + (*p - '0');
220                                         }
221                                         if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
222                                                 pri = (LOG_USER | LOG_NOTICE);
223                                 } else if (c == '\n') {
224                                         *q++ = ' ';
225                                 } else if (iscntrl(c) && (c < 0177)) {
226                                         *q++ = '^';
227                                         *q++ = c ^ 0100;
228                                 } else {
229                                         *q++ = c;
230                                 }
231                                 p++;
232                         }
233                         *q = '\0';
234
235                         /* Now log it */
236                         logMessage(pri, line);
237                 }
238                 close(conn);
239         }
240
241         close(fd);
242 }
243
244 #ifdef BB_KLOGD
245
246 static void klogd_signal(int sig)
247 {
248         ksyslog(7, NULL, 0);
249         ksyslog(0, 0, 0);
250         logMessage(0, "Kernel log daemon exiting.");
251         exit(TRUE);
252 }
253
254 static void doKlogd(void)
255 {
256         int priority = LOG_INFO;
257         char log_buffer[4096];
258         char *logp;
259
260         /* Set up sig handlers */
261         signal(SIGINT, klogd_signal);
262         signal(SIGKILL, klogd_signal);
263         signal(SIGTERM, klogd_signal);
264         signal(SIGHUP, SIG_IGN);
265         logMessage(0, "klogd started: "
266                            "BusyBox v" BB_VER " (" BB_BT ")");
267
268         ksyslog(1, NULL, 0);
269
270         while (1) {
271                 /* Use kernel syscalls */
272                 memset(log_buffer, '\0', sizeof(log_buffer));
273                 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
274                         char message[80];
275
276                         if (errno == EINTR)
277                                 continue;
278                         snprintf(message, 79, "klogd: Error return from sys_sycall: " \
279                                          "%d - %s.\n", errno, strerror(errno));
280                         logMessage(LOG_SYSLOG | LOG_ERR, message);
281                         exit(1);
282                 }
283                 logp = log_buffer;
284                 if (*log_buffer == '<') {
285                         switch (*(log_buffer + 1)) {
286                         case '0':
287                                 priority = LOG_EMERG;
288                                 break;
289                         case '1':
290                                 priority = LOG_ALERT;
291                                 break;
292                         case '2':
293                                 priority = LOG_CRIT;
294                                 break;
295                         case '3':
296                                 priority = LOG_ERR;
297                                 break;
298                         case '4':
299                                 priority = LOG_WARNING;
300                                 break;
301                         case '5':
302                                 priority = LOG_NOTICE;
303                                 break;
304                         case '6':
305                                 priority = LOG_INFO;
306                                 break;
307                         case '7':
308                         default:
309                                 priority = LOG_DEBUG;
310                         }
311                         logp += 3;
312                 }
313                 logMessage(LOG_KERN | priority, logp);
314         }
315
316 }
317
318 #endif
319
320 extern int syslogd_main(int argc, char **argv)
321 {
322         int pid, klogd_pid;
323         int doFork = TRUE;
324
325 #ifdef BB_KLOGD
326         int startKlogd = TRUE;
327 #endif
328         int stopDoingThat = FALSE;
329         char *p;
330         char **argv1 = argv;
331
332         while (--argc > 0 && **(++argv1) == '-') {
333                 stopDoingThat = FALSE;
334                 while (stopDoingThat == FALSE && *(++(*argv1))) {
335                         switch (**argv1) {
336                         case 'm':
337                                 if (--argc == 0) {
338                                         usage(syslogd_usage);
339                                 }
340                                 MarkInterval = atoi(*(++argv1)) * 60;
341                                 break;
342                         case 'n':
343                                 doFork = FALSE;
344                                 break;
345 #ifdef BB_KLOGD
346                         case 'K':
347                                 startKlogd = FALSE;
348                                 break;
349 #endif
350                         case 'O':
351                                 if (--argc == 0) {
352                                         usage(syslogd_usage);
353                                 }
354                                 logFilePath = *(++argv1);
355                                 stopDoingThat = TRUE;
356                                 break;
357                         default:
358                                 usage(syslogd_usage);
359                         }
360                 }
361         }
362
363         /* Store away localhost's name before the fork */
364         gethostname(LocalHostName, sizeof(LocalHostName));
365         if ((p = strchr(LocalHostName, '.'))) {
366                 *p++ = '\0';
367         }
368
369 #ifdef BB_KLOGD
370         /* Start up the klogd process */
371         if (startKlogd == TRUE) {
372                 klogd_pid = fork();
373                 if (klogd_pid == 0) {
374                         strncpy(argv[0], "klogd", strlen(argv[0]));
375                         doKlogd();
376                 }
377         }
378 #endif
379
380         if (doFork == TRUE) {
381                 pid = fork();
382                 if (pid < 0)
383                         exit(pid);
384                 else if (pid == 0) {
385                         strncpy(argv[0], "syslogd", strlen(argv[0]));
386                         doSyslogd();
387                 }
388         } else {
389                 doSyslogd();
390         }
391
392         exit(TRUE);
393 }