blob: 03dcce5c14904097a99d555eb304ad4aaafd545b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini syslogd implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersen3843e961999-11-25 07:30:46 +00007 *
Erik Andersenf13df372000-04-18 23:51:51 +00008 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
9 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000010 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000011 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000012 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000013 *
Eric Andersen3843e961999-11-25 07:30:46 +000014 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000029
Eric Andersen67e32302000-06-19 17:48:02 +000030#include <stdio.h>
31#include <stdlib.h>
Eric Andersen3843e961999-11-25 07:30:46 +000032#include <ctype.h>
Eric Andersenb186d981999-12-03 09:19:54 +000033#include <errno.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000034#include <fcntl.h>
35#include <netdb.h>
Eric Andersenb186d981999-12-03 09:19:54 +000036#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000037#include <signal.h>
38#include <stdarg.h>
Eric Andersen67e32302000-06-19 17:48:02 +000039#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000040#include <string.h>
Eric Andersen67e32302000-06-19 17:48:02 +000041#include <unistd.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000042#include <sys/socket.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000043#include <sys/types.h>
44#include <sys/un.h>
Erik Andersen7d6ba572000-04-19 20:02:50 +000045#include <sys/param.h>
Eric Andersenb186d981999-12-03 09:19:54 +000046
Eric Andersencbe31da2001-02-20 06:14:08 +000047#include "busybox.h"
Eric Andersen67e32302000-06-19 17:48:02 +000048
Eric Andersen3843e961999-11-25 07:30:46 +000049/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
50#define SYSLOG_NAMES
51#include <sys/syslog.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000052#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000053
54/* Path for the file where all log messages are written */
Erik Andersen983b51b2000-04-04 18:14:25 +000055#define __LOG_FILE "/var/log/messages"
Eric Andersen3843e961999-11-25 07:30:46 +000056
Erik Andersen983b51b2000-04-04 18:14:25 +000057/* Path to the unix socket */
Eric Andersen871d93c2002-09-17 20:06:29 +000058static char lfile[MAXPATHLEN];
Eric Andersen3843e961999-11-25 07:30:46 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060static char *logFilePath = __LOG_FILE;
61
Eric Andersen3843e961999-11-25 07:30:46 +000062/* interval between marks in seconds */
Erik Andersene49d5ec2000-02-08 19:58:47 +000063static int MarkInterval = 20 * 60;
64
Eric Andersen3843e961999-11-25 07:30:46 +000065/* localhost's name */
Eric Andersen871d93c2002-09-17 20:06:29 +000066static char LocalHostName[64];
Eric Andersen3843e961999-11-25 07:30:46 +000067
Eric Andersenbdfd0d72001-10-24 05:00:29 +000068#ifdef CONFIG_FEATURE_REMOTE_LOG
Eric Andersenced2cef2000-07-20 23:41:24 +000069#include <netinet/in.h>
70/* udp socket for logging to remote host */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000071static int remotefd = -1;
Glenn L McGrath912d8f42002-11-10 22:46:45 +000072
Eric Andersenced2cef2000-07-20 23:41:24 +000073/* where do we log? */
74static char *RemoteHost;
Glenn L McGrath912d8f42002-11-10 22:46:45 +000075
Eric Andersenced2cef2000-07-20 23:41:24 +000076/* what port to log to? */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000077static int RemotePort = 514;
Glenn L McGrath912d8f42002-11-10 22:46:45 +000078
Eric Andersenced2cef2000-07-20 23:41:24 +000079/* To remote log or not to remote log, that is the question. */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000080static int doRemoteLog = FALSE;
Eric Andersen70d09ed2000-12-11 16:24:16 +000081static int local_logging = FALSE;
Eric Andersenced2cef2000-07-20 23:41:24 +000082#endif
83
Eric Andersen871d93c2002-09-17 20:06:29 +000084
Glenn L McGrath912d8f42002-11-10 22:46:45 +000085#define MAXLINE 1024 /* maximum line length */
Eric Andersen871d93c2002-09-17 20:06:29 +000086
87
Mark Whitley6317c4b2001-03-12 22:51:50 +000088/* circular buffer variables/structures */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000089#ifdef CONFIG_FEATURE_IPC_SYSLOG
Gennady Feldman087bc822001-10-26 16:09:09 +000090#if __GNU_LIBRARY__ < 5
91#error Sorry. Looks like you are using libc5.
92#error libc5 shm support isnt good enough.
93#error Please disable CONFIG_FEATURE_IPC_SYSLOG
94#endif
95
Mark Whitley6317c4b2001-03-12 22:51:50 +000096#include <sys/ipc.h>
97#include <sys/sem.h>
98#include <sys/shm.h>
99
100/* our shared key */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000101static const long KEY_ID = 0x414e4547; /*"GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000102
103// Semaphore operation structures
104static struct shbuf_ds {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000105 int size; // size of data written
106 int head; // start of message list
107 int tail; // end of message list
108 char data[1]; // data/messages
109} *buf = NULL; // shared memory pointer
Mark Whitley6317c4b2001-03-12 22:51:50 +0000110
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000111static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup
112static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn
Mark Whitley6317c4b2001-03-12 22:51:50 +0000113
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000114static int shmid = -1; // ipc shared memory id
115static int s_semid = -1; // ipc semaphore id
116int data_size = 16000; // data size
117int shm_size = 16000 + sizeof(*buf); // our buffer size
Mark Whitley6317c4b2001-03-12 22:51:50 +0000118static int circular_logging = FALSE;
119
120/*
121 * sem_up - up()'s a semaphore.
122 */
123static inline void sem_up(int semid)
124{
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000125 if (semop(semid, SMwup, 1) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_perror_msg_and_die("semop[SMwup]");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000127 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000128}
129
130/*
131 * sem_down - down()'s a semaphore
132 */
133static inline void sem_down(int semid)
134{
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000135 if (semop(semid, SMwdn, 3) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 bb_perror_msg_and_die("semop[SMwdn]");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000137 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000138}
139
Eric Andersen871d93c2002-09-17 20:06:29 +0000140
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000141void ipcsyslog_cleanup(void)
142{
Mark Whitley6317c4b2001-03-12 22:51:50 +0000143 printf("Exiting Syslogd!\n");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000144 if (shmid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000145 shmdt(buf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000146 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000147
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000148 if (shmid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000149 shmctl(shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000150 }
151 if (s_semid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000152 semctl(s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000153 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000154}
155
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000156void ipcsyslog_init(void)
157{
158 if (buf == NULL) {
159 if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 bb_perror_msg_and_die("shmget");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000161 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000162
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000163 if ((buf = shmat(shmid, NULL, 0)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 bb_perror_msg_and_die("shmat");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000165 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000166
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000167 buf->size = data_size;
168 buf->head = buf->tail = 0;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000169
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000170 // we'll trust the OS to set initial semval to 0 (let's hope)
171 if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) {
172 if (errno == EEXIST) {
173 if ((s_semid = semget(KEY_ID, 2, 0)) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 bb_perror_msg_and_die("semget");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000175 }
176 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 bb_perror_msg_and_die("semget");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000178 }
179 }
180 } else {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000181 printf("Buffer already allocated just grab the semaphore?");
182 }
183}
184
185/* write message to buffer */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000186void circ_message(const char *msg)
187{
188 int l = strlen(msg) + 1; /* count the whole message w/ '\0' included */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000189
190 sem_down(s_semid);
191
192 /*
193 * Circular Buffer Algorithm:
194 * --------------------------
195 *
196 * Start-off w/ empty buffer of specific size SHM_SIZ
197 * Start filling it up w/ messages. I use '\0' as separator to break up messages.
198 * This is also very handy since we can do printf on message.
199 *
200 * Once the buffer is full we need to get rid of the first message in buffer and
201 * insert the new message. (Note: if the message being added is >1 message then
202 * we will need to "remove" >1 old message from the buffer). The way this is done
203 * is the following:
Eric Andersen871d93c2002-09-17 20:06:29 +0000204 * When we reach the end of the buffer we set a mark and start from the beginning.
205 * Now what about the beginning and end of the buffer? Well we have the "head"
206 * index/pointer which is the starting point for the messages and we have "tail"
207 * index/pointer which is the ending point for the messages. When we "display" the
208 * messages we start from the beginning and continue until we reach "tail". If we
209 * reach end of buffer, then we just start from the beginning (offset 0). "head" and
210 * "tail" are actually offsets from the beginning of the buffer.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000211 *
212 * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide
Eric Andersen871d93c2002-09-17 20:06:29 +0000213 * a threasafe way of handling shared memory operations.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000214 */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000215 if ((buf->tail + l) < buf->size) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000216 /* before we append the message we need to check the HEAD so that we won't
217 overwrite any of the message that we still need and adjust HEAD to point
218 to the next message! */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000219 if (buf->tail < buf->head) {
220 if ((buf->tail + l) >= buf->head) {
221 /* we need to move the HEAD to point to the next message
222 * Theoretically we have enough room to add the whole message to the
223 * buffer, because of the first outer IF statement, so we don't have
224 * to worry about overflows here!
225 */
226 int k = buf->tail + l - buf->head; /* we need to know how many bytes
227 we are overwriting to make
228 enough room */
229 char *c =
230 memchr(buf->data + buf->head + k, '\0',
231 buf->size - (buf->head + k));
232 if (c != NULL) { /* do a sanity check just in case! */
233 buf->head = c - buf->data + 1; /* we need to convert pointer to
234 offset + skip the '\0' since
235 we need to point to the beginning
236 of the next message */
237 /* Note: HEAD is only used to "retrieve" messages, it's not used
238 when writing messages into our buffer */
239 } else { /* show an error message to know we messed up? */
240 printf("Weird! Can't find the terminator token??? \n");
241 buf->head = 0;
242 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000243 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000244 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000245
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000246 /* in other cases no overflows have been done yet, so we don't care! */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000247 /* we should be ok to append the message now */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000248 strncpy(buf->data + buf->tail, msg, l); /* append our message */
249 buf->tail += l; /* count full message w/ '\0' terminating char */
250 } else {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000251 /* we need to break up the message and "circle" it around */
252 char *c;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000253 int k = buf->tail + l - buf->size; /* count # of bytes we don't fit */
Eric Andersen871d93c2002-09-17 20:06:29 +0000254
Mark Whitley6317c4b2001-03-12 22:51:50 +0000255 /* We need to move HEAD! This is always the case since we are going
256 * to "circle" the message.
257 */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000258 c = memchr(buf->data + k, '\0', buf->size - k);
Eric Andersen871d93c2002-09-17 20:06:29 +0000259
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000260 if (c != NULL) { /* if we don't have '\0'??? weird!!! */
261 /* move head pointer */
262 buf->head = c - buf->data + 1;
Eric Andersen871d93c2002-09-17 20:06:29 +0000263
264 /* now write the first part of the message */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000265 strncpy(buf->data + buf->tail, msg, l - k - 1);
Eric Andersen871d93c2002-09-17 20:06:29 +0000266
Mark Whitley6317c4b2001-03-12 22:51:50 +0000267 /* ALWAYS terminate end of buffer w/ '\0' */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000268 buf->data[buf->size - 1] = '\0';
Eric Andersen871d93c2002-09-17 20:06:29 +0000269
Mark Whitley6317c4b2001-03-12 22:51:50 +0000270 /* now write out the rest of the string to the beginning of the buffer */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000271 strcpy(buf->data, &msg[l - k - 1]);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000272
273 /* we need to place the TAIL at the end of the message */
274 buf->tail = k + 1;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000275 } else {
276 printf
277 ("Weird! Can't find the terminator token from the beginning??? \n");
278 buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000279 }
Eric Andersen871d93c2002-09-17 20:06:29 +0000280
Mark Whitley6317c4b2001-03-12 22:51:50 +0000281 }
282 sem_up(s_semid);
283}
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000284#endif /* CONFIG_FEATURE_IPC_SYSLOG */
Eric Andersen871d93c2002-09-17 20:06:29 +0000285
Erik Andersenc053e412000-03-21 01:31:24 +0000286/* Note: There is also a function called "message()" in init.c */
Erik Andersen983b51b2000-04-04 18:14:25 +0000287/* Print a message to the log file. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000288static void message(char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
289static void message(char *fmt, ...)
Eric Andersen3843e961999-11-25 07:30:46 +0000290{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 int fd;
Erik Andersene3ed1562000-04-19 18:52:56 +0000292 struct flock fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 va_list arguments;
Eric Andersen3843e961999-11-25 07:30:46 +0000294
Erik Andersene3ed1562000-04-19 18:52:56 +0000295 fl.l_whence = SEEK_SET;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000296 fl.l_start = 0;
297 fl.l_len = 1;
Erik Andersene3ed1562000-04-19 18:52:56 +0000298
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000299#ifdef CONFIG_FEATURE_IPC_SYSLOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000300 if ((circular_logging == TRUE) && (buf != NULL)) {
301 char b[1024];
Mark Whitley6317c4b2001-03-12 22:51:50 +0000302
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000303 va_start(arguments, fmt);
304 vsnprintf(b, sizeof(b) - 1, fmt, arguments);
305 va_end(arguments);
306 circ_message(b);
307
308 } else
Mark Whitley6317c4b2001-03-12 22:51:50 +0000309#endif
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000310 if ((fd =
311 device_open(logFilePath,
312 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
313 O_NONBLOCK)) >= 0) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000314 fl.l_type = F_WRLCK;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000315 fcntl(fd, F_SETLKW, &fl);
316 va_start(arguments, fmt);
317 vdprintf(fd, fmt, arguments);
318 va_end(arguments);
Erik Andersene3ed1562000-04-19 18:52:56 +0000319 fl.l_type = F_UNLCK;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000320 fcntl(fd, F_SETLKW, &fl);
321 close(fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000322 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000324 if ((fd =
325 device_open(_PATH_CONSOLE,
326 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
327 va_start(arguments, fmt);
328 vdprintf(fd, fmt, arguments);
329 va_end(arguments);
330 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 } else {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000332 fprintf(stderr, "Bummer, can't print: ");
333 va_start(arguments, fmt);
334 vfprintf(stderr, fmt, arguments);
335 fflush(stderr);
336 va_end(arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 }
Eric Andersen3843e961999-11-25 07:30:46 +0000338 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000339}
340
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000341static void logMessage(int pri, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000342{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 time_t now;
344 char *timestamp;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000345 static char res[20] = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000347
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000348 if (pri != 0) {
349 for (c_fac = facilitynames;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000350 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000351 for (c_pri = prioritynames;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000352 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
353 if (c_fac->c_name == NULL || c_pri->c_name == NULL) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000354 snprintf(res, sizeof(res), "<%d>", pri);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000355 } else {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000356 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000357 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000358 }
Eric Andersen3843e961999-11-25 07:30:46 +0000359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000361 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 time(&now);
363 timestamp = ctime(&now) + 4;
364 timestamp[15] = '\0';
365 } else {
366 timestamp = msg;
367 timestamp[15] = '\0';
368 msg += 16;
369 }
Eric Andersen3843e961999-11-25 07:30:46 +0000370
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 /* todo: supress duplicates */
Eric Andersen3843e961999-11-25 07:30:46 +0000372
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000373#ifdef CONFIG_FEATURE_REMOTE_LOG
Eric Andersenced2cef2000-07-20 23:41:24 +0000374 /* send message to remote logger */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000375 if (-1 != remotefd) {
376 static const int IOV_COUNT = 2;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000377 struct iovec iov[IOV_COUNT];
378 struct iovec *v = iov;
Eric Andersenced2cef2000-07-20 23:41:24 +0000379
Eric Andersen044228d2001-07-17 01:12:36 +0000380 memset(&res, 0, sizeof(res));
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000381 snprintf(res, sizeof(res), "<%d>", pri);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000382 v->iov_base = res;
Eric Andersen871d93c2002-09-17 20:06:29 +0000383 v->iov_len = strlen(res);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000384 v++;
Eric Andersenced2cef2000-07-20 23:41:24 +0000385
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000386 v->iov_base = msg;
Eric Andersen871d93c2002-09-17 20:06:29 +0000387 v->iov_len = strlen(msg);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000388 writev_retry:
Glenn L McGrath877d4182003-02-09 05:07:42 +0000389 if ((-1 == writev(remotefd, iov, IOV_COUNT)) && (errno == EINTR)) {
390 goto writev_retry;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000391 }
392 }
Eric Andersen871d93c2002-09-17 20:06:29 +0000393 if (local_logging == TRUE)
Eric Andersenced2cef2000-07-20 23:41:24 +0000394#endif
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000395 /* now spew out the message to wherever it is supposed to go */
396 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000397}
398
399static void quit_signal(int sig)
400{
Eric Andersen238bc402001-05-07 17:55:05 +0000401 logMessage(LOG_SYSLOG | LOG_INFO, "System log daemon exiting.");
Erik Andersen983b51b2000-04-04 18:14:25 +0000402 unlink(lfile);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000403#ifdef CONFIG_FEATURE_IPC_SYSLOG
Mark Whitley6317c4b2001-03-12 22:51:50 +0000404 ipcsyslog_cleanup();
405#endif
406
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000408}
409
Eric Andersen3843e961999-11-25 07:30:46 +0000410static void domark(int sig)
411{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 if (MarkInterval > 0) {
413 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
414 alarm(MarkInterval);
415 }
Eric Andersen3843e961999-11-25 07:30:46 +0000416}
417
Eric Andersen22ecf042001-07-02 17:32:40 +0000418/* This must be a #define, since when DODEBUG and BUFFERS_GO_IN_BSS are
419 * enabled, we otherwise get a "storage size isn't constant error. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000420static int serveConnection(char *tmpbuf, int n_read)
Pavel Roskinda10ec02000-06-07 21:08:25 +0000421{
Matt Kraaib6ec7812001-08-14 17:32:23 +0000422 char *p = tmpbuf;
Pavel Roskinda10ec02000-06-07 21:08:25 +0000423
Matt Kraaib6ec7812001-08-14 17:32:23 +0000424 while (p < tmpbuf + n_read) {
Pavel Roskinda10ec02000-06-07 21:08:25 +0000425
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000426 int pri = (LOG_USER | LOG_NOTICE);
Eric Andersend4f90ed2003-05-23 09:28:01 +0000427 int num_lt = 0;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000428 char line[MAXLINE + 1];
Pavel Roskinda10ec02000-06-07 21:08:25 +0000429 unsigned char c;
Matt Kraaib6ec7812001-08-14 17:32:23 +0000430 char *q = line;
Pavel Roskinda10ec02000-06-07 21:08:25 +0000431
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000432 while ((c = *p) && q < &line[sizeof(line) - 1]) {
Eric Andersend4f90ed2003-05-23 09:28:01 +0000433 if (c == '<' && num_lt == 0) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000434 /* Parse the magic priority number. */
Eric Andersend4f90ed2003-05-23 09:28:01 +0000435 num_lt++;
Pavel Roskinda10ec02000-06-07 21:08:25 +0000436 pri = 0;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000437 while (isdigit(*(++p))) {
Pavel Roskinda10ec02000-06-07 21:08:25 +0000438 pri = 10 * pri + (*p - '0');
439 }
Eric Andersen46ba5682003-05-23 09:29:57 +0000440 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) {
Eric Andersend4f90ed2003-05-23 09:28:01 +0000441 pri = (LOG_USER | LOG_NOTICE);
Eric Andersen46ba5682003-05-23 09:29:57 +0000442 }
Pavel Roskinda10ec02000-06-07 21:08:25 +0000443 } else if (c == '\n') {
444 *q++ = ' ';
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000445 } else if (iscntrl(c) && (c < 0177)) {
Pavel Roskinda10ec02000-06-07 21:08:25 +0000446 *q++ = '^';
447 *q++ = c ^ 0100;
448 } else {
449 *q++ = c;
450 }
451 p++;
452 }
453 *q = '\0';
Matt Kraaib6ec7812001-08-14 17:32:23 +0000454 p++;
Pavel Roskinda10ec02000-06-07 21:08:25 +0000455 /* Now log it */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000456 logMessage(pri, line);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000457 }
Mark Whitleybff6b182001-03-27 20:17:58 +0000458 return n_read;
Pavel Roskinda10ec02000-06-07 21:08:25 +0000459}
460
Eric Andersenced2cef2000-07-20 23:41:24 +0000461
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000462#ifdef CONFIG_FEATURE_REMOTE_LOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000463static void init_RemoteLog(void)
Eric Andersen871d93c2002-09-17 20:06:29 +0000464{
Eric Andersenced2cef2000-07-20 23:41:24 +0000465
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000466 struct sockaddr_in remoteaddr;
467 struct hostent *hostinfo;
468 int len = sizeof(remoteaddr);
Eric Andersenced2cef2000-07-20 23:41:24 +0000469
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000470 memset(&remoteaddr, 0, len);
Mark Whitleybff6b182001-03-27 20:17:58 +0000471
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000472 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
Eric Andersenced2cef2000-07-20 23:41:24 +0000473
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000474 if (remotefd < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000475 bb_error_msg_and_die("cannot create socket");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000476 }
Eric Andersenced2cef2000-07-20 23:41:24 +0000477
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000478 hostinfo = xgethostbyname(RemoteHost);
Eric Andersenced2cef2000-07-20 23:41:24 +0000479
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000480 remoteaddr.sin_family = AF_INET;
481 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
482 remoteaddr.sin_port = htons(RemotePort);
Eric Andersenced2cef2000-07-20 23:41:24 +0000483
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000484 /* Since we are using UDP sockets, connect just sets the default host and port
485 * for future operations
486 */
487 if (0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000488 bb_error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost,
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000489 RemotePort);
490 }
Eric Andersenced2cef2000-07-20 23:41:24 +0000491
492}
493#endif
494
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000495static void doSyslogd(void) __attribute__ ((noreturn));
496static void doSyslogd(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000497{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000499 socklen_t addrLength;
500
Erik Andersen983b51b2000-04-04 18:14:25 +0000501 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000502 fd_set fds;
503
Erik Andersenf13df372000-04-18 23:51:51 +0000504 /* Set up signal handlers. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000505 signal(SIGINT, quit_signal);
506 signal(SIGTERM, quit_signal);
507 signal(SIGQUIT, quit_signal);
508 signal(SIGHUP, SIG_IGN);
509 signal(SIGCHLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000510#ifdef SIGCLD
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000511 signal(SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000512#endif
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000513 signal(SIGALRM, domark);
514 alarm(MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000515
Erik Andersenf13df372000-04-18 23:51:51 +0000516 /* Create the syslog file so realpath() can work. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000517 if (realpath(_PATH_LOG, lfile) != NULL) {
518 unlink(lfile);
519 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000520
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000521 memset(&sunx, 0, sizeof(sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000522 sunx.sun_family = AF_UNIX;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000523 strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path));
524 if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000525 bb_perror_msg_and_die("Couldn't get file descriptor for socket "
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000526 _PATH_LOG);
527 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000528
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000529 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
530 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000531 bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000532 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000534 if (chmod(lfile, 0666) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000535 bb_perror_msg_and_die("Could not set permission on " _PATH_LOG);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000536 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000537#ifdef CONFIG_FEATURE_IPC_SYSLOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000538 if (circular_logging == TRUE) {
539 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000540 }
541#endif
542
Eric Andersen871d93c2002-09-17 20:06:29 +0000543#ifdef CONFIG_FEATURE_REMOTE_LOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000544 if (doRemoteLog == TRUE) {
545 init_RemoteLog();
Eric Andersen871d93c2002-09-17 20:06:29 +0000546 }
547#endif
Eric Andersenced2cef2000-07-20 23:41:24 +0000548
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000549 logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550
Erik Andersen983b51b2000-04-04 18:14:25 +0000551 for (;;) {
Erik Andersenf13df372000-04-18 23:51:51 +0000552
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000553 FD_ZERO(&fds);
554 FD_SET(sock_fd, &fds);
Erik Andersenf13df372000-04-18 23:51:51 +0000555
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000556 if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) {
Eric Andersen871d93c2002-09-17 20:06:29 +0000557 if (errno == EINTR) {
558 /* alarm may have happened. */
559 continue;
560 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000561 bb_perror_msg_and_die("select error");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000564 if (FD_ISSET(sock_fd, &fds)) {
565 int i;
Erik Andersene3ed1562000-04-19 18:52:56 +0000566
Eric Andersen900c8f32003-05-16 08:35:02 +0000567 RESERVE_CONFIG_BUFFER(tmpbuf, MAXLINE + 1);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000568
Eric Andersen900c8f32003-05-16 08:35:02 +0000569 memset(tmpbuf, '\0', MAXLINE + 1);
570 if ((i = recv(sock_fd, tmpbuf, MAXLINE, 0)) > 0) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000571 serveConnection(tmpbuf, i);
572 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000573 bb_perror_msg_and_die("UNIX socket error");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000574 }
575 RELEASE_CONFIG_BUFFER(tmpbuf);
576 } /* FD_ISSET() */
577 } /* for main loop */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000578}
579
Eric Andersen3843e961999-11-25 07:30:46 +0000580extern int syslogd_main(int argc, char **argv)
581{
Eric Andersene5c24df2001-03-29 21:58:33 +0000582 int opt;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000583
Eric Andersen871d93c2002-09-17 20:06:29 +0000584#if ! defined(__uClinux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 int doFork = TRUE;
Eric Andersen871d93c2002-09-17 20:06:29 +0000586#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000587
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589
Eric Andersen394cf222000-12-11 16:48:50 +0000590 /* do normal option parsing */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000591 while ((opt = getopt(argc, argv, "m:nO:R:LC")) > 0) {
Eric Andersen394cf222000-12-11 16:48:50 +0000592 switch (opt) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000593 case 'm':
594 MarkInterval = atoi(optarg) * 60;
595 break;
Eric Andersen871d93c2002-09-17 20:06:29 +0000596#if ! defined(__uClinux__)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000597 case 'n':
598 doFork = FALSE;
599 break;
Eric Andersen871d93c2002-09-17 20:06:29 +0000600#endif
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000601 case 'O':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000602 logFilePath = bb_xstrdup(optarg);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000603 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000604#ifdef CONFIG_FEATURE_REMOTE_LOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000605 case 'R':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000606 RemoteHost = bb_xstrdup(optarg);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000607 if ((p = strchr(RemoteHost, ':'))) {
608 RemotePort = atoi(p + 1);
609 *p = '\0';
610 }
611 doRemoteLog = TRUE;
612 break;
613 case 'L':
614 local_logging = TRUE;
615 break;
Eric Andersenced2cef2000-07-20 23:41:24 +0000616#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000617#ifdef CONFIG_FEATURE_IPC_SYSLOG
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000618 case 'C':
619 circular_logging = TRUE;
620 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000621#endif
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000622 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000623 bb_show_usage();
Eric Andersen3843e961999-11-25 07:30:46 +0000624 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 }
626
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000627#ifdef CONFIG_FEATURE_REMOTE_LOG
Eric Andersen4ed17822000-12-11 19:28:29 +0000628 /* If they have not specified remote logging, then log locally */
Eric Andersen871d93c2002-09-17 20:06:29 +0000629 if (doRemoteLog == FALSE)
Eric Andersen4ed17822000-12-11 19:28:29 +0000630 local_logging = TRUE;
631#endif
632
Mark Whitley6317c4b2001-03-12 22:51:50 +0000633
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 /* Store away localhost's name before the fork */
635 gethostname(LocalHostName, sizeof(LocalHostName));
636 if ((p = strchr(LocalHostName, '.'))) {
637 *p++ = '\0';
638 }
639
Erik Andersen983b51b2000-04-04 18:14:25 +0000640 umask(0);
641
Eric Andersen871d93c2002-09-17 20:06:29 +0000642#if ! defined(__uClinux__)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000643 if ((doFork == TRUE) && (daemon(0, 1) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000644 bb_perror_msg_and_die("daemon");
Eric Andersen3843e961999-11-25 07:30:46 +0000645 }
Eric Andersen871d93c2002-09-17 20:06:29 +0000646#endif
Eric Andersene5c24df2001-03-29 21:58:33 +0000647 doSyslogd();
Eric Andersenb186d981999-12-03 09:19:54 +0000648
Matt Kraai3e856ce2000-12-01 02:55:13 +0000649 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000650}
Erik Andersen983b51b2000-04-04 18:14:25 +0000651
652/*
Erik Andersene3ed1562000-04-19 18:52:56 +0000653Local Variables
654c-file-style: "linux"
655c-basic-offset: 4
656tab-width: 4
657End:
658*/