2 * Mini syslogd implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
25 #include <sys/socket.h>
29 #include <sys/types.h>
37 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
39 #include <sys/syslog.h>
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"
47 static char* logFilePath = __LOG_FILE;
48 /* interval between marks in seconds */
49 static int MarkInterval = 20*60;
50 /* localhost's name */
51 static char LocalHostName[32];
53 static const char syslogd_usage[] =
54 "syslogd [OPTION]...\n\n"
55 "Linux system logging utility.\n\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";
63 /* try to open up the specified device */
64 static int device_open(char *device, int mode)
68 m = mode | O_NONBLOCK;
70 /* Retry up to 5 times */
71 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
76 /* Reset original flags. */
78 fcntl(fd, F_SETFL, mode);
82 /* print a message to the log file */
83 static void message(char *fmt, ...)
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);
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);
101 fprintf(stderr, "Bummer, can't print: ");
102 va_start(arguments, fmt);
103 vfprintf(stderr, fmt, arguments);
110 static void logMessage( int pri, char* msg)
117 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);
122 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
124 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
125 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
128 timestamp = ctime(&now) + 4;
129 timestamp[15] = '\0';
132 timestamp[15] = '\0';
136 /* todo: supress duplicates */
138 /* now spew out the message to wherever it is supposed to go */
139 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
142 static void quit_signal(int sig)
144 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
148 static void restart_signal(int sig)
150 /* pretend to restart */
151 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
154 static void domark(int sig)
156 if (MarkInterval > 0) {
157 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
158 signal(SIGALRM, domark);
163 static void doSyslogd(void)
165 struct sockaddr_un sunx;
172 /* Remove any preexisting socket/file */
175 /* 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);
183 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);
191 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
192 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
193 (chmod(_PATH_LOG, 0666) < 0) ||
196 perror("Could not connect to socket " _PATH_LOG);
201 /* Get localhost's name */
202 gethostname(LocalHostName, sizeof(LocalHostName));
203 if ( (p = strchr(LocalHostName, '.')) ) {
207 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
208 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
211 while ((conn = accept(fd, (struct sockaddr *) &sunx,
214 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
218 int pri = (LOG_USER|LOG_NOTICE);
220 memset (line, 0, sizeof(line));
223 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
225 /* Parse the magic priority number */
227 while (isdigit(*(++p))) {
228 pri = 10 * pri + (*p - '0');
230 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
231 pri = (LOG_USER|LOG_NOTICE);
232 } else if (c == '\n') {
234 } else if (iscntrl(c)&&(c<0177)) {
245 logMessage( pri, line);
254 extern int syslogd_main(int argc, char **argv)
259 while (--argc > 0 && **(++argv) == '-') {
260 while (*(++(*argv))) {
264 usage(syslogd_usage);
266 MarkInterval = atoi(*(++argv))*60;
273 usage(syslogd_usage);
275 logFilePath = *(++argv);
278 usage(syslogd_usage);
283 if (doFork == TRUE) {
287 else if ( pid == 0 ) {