blob: 736adf7d189478f84195658725f2bfdc471cb04d [file] [log] [blame]
Eric Andersen3843e961999-11-25 07:30:46 +00001/*
2 * Mini syslogd implementation for busybox
3 *
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000022
Eric Andersen3843e961999-11-25 07:30:46 +000023#include "internal.h"
Eric Andersenb99df0f1999-11-24 09:04:33 +000024#include <stdio.h>
25#include <sys/socket.h>
26#include <sys/un.h>
27#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000028#include <time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <ctype.h>
34#include <netdb.h>
Eric Andersenb99df0f1999-11-24 09:04:33 +000035
36
Eric Andersen3843e961999-11-25 07:30:46 +000037/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
38#define SYSLOG_NAMES
39#include <sys/syslog.h>
40
41/* Path for the file where all log messages are written */
42#define __LOG_FILE "/var/log/messages"
43/* Path to the current console device */
44#define __DEV_CONSOLE "/dev/console"
45
46
47static char* logFilePath = __LOG_FILE;
48/* interval between marks in seconds */
49static int MarkInterval = 20*60;
50/* localhost's name */
51static char LocalHostName[32];
52
53static const char syslogd_usage[] =
54 "syslogd [OPTION]...\n\n"
55 "Linux system logging utility.\n\n"
56 "Options:\n"
57 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
58 "\t-n\tDo not fork into the background (for when run by init)\n"
59 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
60
61
62
63/* try to open up the specified device */
64static int device_open(char *device, int mode)
65{
66 int m, f, fd = -1;
67
68 m = mode | O_NONBLOCK;
69
70 /* Retry up to 5 times */
71 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
73 break;
74 if (fd < 0)
75 return fd;
76 /* Reset original flags. */
77 if (m != mode)
78 fcntl(fd, F_SETFL, mode);
79 return fd;
Eric Andersenb99df0f1999-11-24 09:04:33 +000080}
81
Eric Andersen3843e961999-11-25 07:30:46 +000082/* print a message to the log file */
83static void message(char *fmt, ...)
84{
85 int fd;
86 va_list arguments;
87
88 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
89 va_start(arguments, fmt);
90 vdprintf(fd, fmt, arguments);
91 va_end(arguments);
92 close(fd);
93 } else {
94 /* Always send console messages to /dev/console so people will see them. */
95 if ((fd = device_open(__DEV_CONSOLE, 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 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000107 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000108}
109
Eric Andersen3843e961999-11-25 07:30:46 +0000110static 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;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000116
Eric Andersen3843e961999-11-25 07:30:46 +0000117 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
118 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
119 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
120 snprintf (res, sizeof(res), "<%d>", pri);
121 else
122 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
123
124 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
125 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
126 {
127 time(&now);
128 timestamp = ctime(&now) + 4;
129 timestamp[15] = '\0';
130 } else {
131 timestamp = msg;
132 timestamp[15] = '\0';
133 msg += 16;
134 }
135
136 /* todo: supress duplicates */
137
138 /* now spew out the message to wherever it is supposed to go */
139 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
140}
141
142static void quit_signal(int sig)
143{
144 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
145 exit( TRUE);
146}
147
148static void restart_signal(int sig)
149{
150 /* pretend to restart */
151 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
152}
153
154static void domark(int sig)
155{
156 if (MarkInterval > 0) {
157 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
158 signal(SIGALRM, domark);
159 alarm(MarkInterval);
160 }
161}
162
163static void doSyslogd(void)
164{
165 struct sockaddr_un sunx;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000166 int fd, conn;
167 size_t addrLength;
Eric Andersen3843e961999-11-25 07:30:46 +0000168 char buf[1024];
169 char *q, *p = buf;
170 int readSize;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000171
Eric Andersen3843e961999-11-25 07:30:46 +0000172 /* Remove any preexisting socket/file */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000173 unlink(_PATH_LOG);
174
Eric Andersen3843e961999-11-25 07:30:46 +0000175 /* Set up sig handlers */
176 signal(SIGINT, quit_signal);
177 signal(SIGTERM, quit_signal);
178 signal(SIGQUIT, quit_signal);
179 signal(SIGHUP, restart_signal);
180 signal(SIGALRM, domark);
181 alarm(MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000182
Eric Andersen3843e961999-11-25 07:30:46 +0000183 memset(&sunx, 0, sizeof(sunx));
184 sunx.sun_family = AF_UNIX; /* Unix domain socket */
185 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
186 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
187 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
188 exit( FALSE);
189 }
190
191 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
192 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
193 (chmod(_PATH_LOG, 0666) < 0) ||
194 (listen(fd, 5)) )
195 {
196 perror("Could not connect to socket " _PATH_LOG);
197 exit( FALSE);
198 }
199
200
201 /* Get localhost's name */
202 gethostname(LocalHostName, sizeof(LocalHostName));
203 if ( (p = strchr(LocalHostName, '.')) ) {
204 *p++ = '\0';
205 }
206
207 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
208 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
Eric Andersenb99df0f1999-11-24 09:04:33 +0000209
210
Eric Andersen3843e961999-11-25 07:30:46 +0000211 while ((conn = accept(fd, (struct sockaddr *) &sunx,
212 &addrLength)) >= 0)
213 {
214 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
215 {
216 char line[1025];
217 unsigned char c;
218 int pri = (LOG_USER|LOG_NOTICE);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000219
Eric Andersen3843e961999-11-25 07:30:46 +0000220 memset (line, 0, sizeof(line));
221 p = buf;
222 q = line;
223 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
224 if (c == '<') {
225 /* Parse the magic priority number */
226 pri = 0;
227 while (isdigit(*(++p))) {
228 pri = 10 * pri + (*p - '0');
229 }
230 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
231 pri = (LOG_USER|LOG_NOTICE);
232 } else if (c == '\n') {
233 *q++ = ' ';
234 } else if (iscntrl(c)&&(c<0177)) {
235 *q++ = '^';
236 *q++ = c ^ 0100;
237 } else {
238 *q++ = c;
239 }
240 p++;
241 }
242 *q = '\0';
Eric Andersenb99df0f1999-11-24 09:04:33 +0000243
Eric Andersen3843e961999-11-25 07:30:46 +0000244 /* Now log it */
245 logMessage( pri, line);
246 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000247 close(conn);
248 }
249
Eric Andersenb99df0f1999-11-24 09:04:33 +0000250 close(fd);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000251}
252
253
Eric Andersen3843e961999-11-25 07:30:46 +0000254extern int syslogd_main(int argc, char **argv)
255{
256 int pid;
257 int doFork = TRUE;
258
259 while (--argc > 0 && **(++argv) == '-') {
260 while (*(++(*argv))) {
261 switch (**argv) {
262 case 'm':
263 if (--argc == 0) {
264 usage(syslogd_usage);
265 }
266 MarkInterval = atoi(*(++argv))*60;
267 break;
268 case 'n':
269 doFork = FALSE;
270 break;
271 case 'O':
272 if (--argc == 0) {
273 usage(syslogd_usage);
274 }
275 logFilePath = *(++argv);
276 break;
277 default:
278 usage(syslogd_usage);
279 }
280 }
281 }
282
283 if (doFork == TRUE) {
284 pid = fork();
285 if ( pid < 0 )
286 exit( pid);
287 else if ( pid == 0 ) {
288 doSyslogd();
289 }
290 } else {
291 doSyslogd();
292 }
293 exit( TRUE);
294}
295
Eric Andersenb99df0f1999-11-24 09:04:33 +0000296