Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini syslogd implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 6 | * |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 7 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
| 8 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 9 | * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 10 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 11 | * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001 |
Mark Whitley | 6bff9cc | 2001-03-12 23:41:34 +0000 | [diff] [blame] | 12 | * |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
| 27 | */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 28 | |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 31 | #include <ctype.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 32 | #include <errno.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
Glenn L McGrath | 5529b7b | 2004-07-22 04:23:18 +0000 | [diff] [blame] | 34 | #include <getopt.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 35 | #include <netdb.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 36 | #include <paths.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <stdarg.h> |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 39 | #include <stdbool.h> |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 40 | #include <time.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 41 | #include <string.h> |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 42 | #include <unistd.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 43 | #include <sys/socket.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 44 | #include <sys/types.h> |
| 45 | #include <sys/un.h> |
Erik Andersen | 7d6ba57 | 2000-04-19 20:02:50 +0000 | [diff] [blame] | 46 | #include <sys/param.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 47 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 48 | #include "busybox.h" |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 50 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ |
| 51 | #define SYSLOG_NAMES |
| 52 | #include <sys/syslog.h> |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 53 | #include <sys/uio.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 54 | |
| 55 | /* Path for the file where all log messages are written */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 56 | #define __LOG_FILE "/var/log/messages" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 57 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 58 | /* Path to the unix socket */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 59 | static char lfile[MAXPATHLEN]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 61 | static const char *logFilePath = __LOG_FILE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 63 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 64 | /* max size of message file before being rotated */ |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 65 | static int logFileSize = 200 * 1024; |
| 66 | |
| 67 | /* number of rotated message files */ |
| 68 | static int logFileRotate = 1; |
| 69 | #endif |
| 70 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 71 | /* interval between marks in seconds */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | static int MarkInterval = 20 * 60; |
| 73 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 74 | /* localhost's name */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 75 | static char LocalHostName[64]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 76 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 78 | #include <netinet/in.h> |
| 79 | /* udp socket for logging to remote host */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 80 | static int remotefd = -1; |
Eric Andersen | 75813ee | 2004-08-26 23:15:29 +0000 | [diff] [blame] | 81 | static struct sockaddr_in remoteaddr; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 82 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 83 | /* where do we log? */ |
| 84 | static char *RemoteHost; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 85 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 86 | /* what port to log to? */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 87 | static int RemotePort = 514; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 88 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 89 | /* To remote log or not to remote log, that is the question. */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 90 | static int doRemoteLog = FALSE; |
Eric Andersen | 70d09ed | 2000-12-11 16:24:16 +0000 | [diff] [blame] | 91 | static int local_logging = FALSE; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 94 | /* Make loging output smaller. */ |
| 95 | static bool small = false; |
| 96 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 97 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 98 | #define MAXLINE 1024 /* maximum line length */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 99 | |
| 100 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 101 | /* circular buffer variables/structures */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 102 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 103 | |
| 104 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 |
| 105 | #error Sorry, you must set the syslogd buffer size to at least 4KB. |
| 106 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE |
| 107 | #endif |
| 108 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 109 | #include <sys/ipc.h> |
| 110 | #include <sys/sem.h> |
| 111 | #include <sys/shm.h> |
| 112 | |
| 113 | /* our shared key */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 114 | static const long KEY_ID = 0x414e4547; /*"GENA" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 115 | |
| 116 | // Semaphore operation structures |
| 117 | static struct shbuf_ds { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 118 | int size; // size of data written |
| 119 | int head; // start of message list |
| 120 | int tail; // end of message list |
| 121 | char data[1]; // data/messages |
| 122 | } *buf = NULL; // shared memory pointer |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 123 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 124 | static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup |
| 125 | static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 126 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 127 | static int shmid = -1; // ipc shared memory id |
| 128 | static int s_semid = -1; // ipc semaphore id |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 129 | static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 130 | static int circular_logging = FALSE; |
| 131 | |
| 132 | /* |
| 133 | * sem_up - up()'s a semaphore. |
| 134 | */ |
| 135 | static inline void sem_up(int semid) |
| 136 | { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 137 | if (semop(semid, SMwup, 1) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | bb_perror_msg_and_die("semop[SMwup]"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 139 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
| 143 | * sem_down - down()'s a semaphore |
| 144 | */ |
| 145 | static inline void sem_down(int semid) |
| 146 | { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 147 | if (semop(semid, SMwdn, 3) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | bb_perror_msg_and_die("semop[SMwdn]"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 149 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 152 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 153 | static void ipcsyslog_cleanup(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 154 | { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 155 | printf("Exiting Syslogd!\n"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 156 | if (shmid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 157 | shmdt(buf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 158 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 159 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 160 | if (shmid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 161 | shmctl(shmid, IPC_RMID, NULL); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 162 | } |
| 163 | if (s_semid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 164 | semctl(s_semid, 0, IPC_RMID, 0); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 165 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 166 | } |
| 167 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 168 | static void ipcsyslog_init(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 169 | { |
| 170 | if (buf == NULL) { |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 171 | if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | bb_perror_msg_and_die("shmget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 173 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 174 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 175 | if ((buf = shmat(shmid, NULL, 0)) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_perror_msg_and_die("shmat"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 177 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 178 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 179 | buf->size = shm_size - sizeof(*buf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 180 | buf->head = buf->tail = 0; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 181 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 182 | // we'll trust the OS to set initial semval to 0 (let's hope) |
| 183 | if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) { |
| 184 | if (errno == EEXIST) { |
| 185 | if ((s_semid = semget(KEY_ID, 2, 0)) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 186 | bb_perror_msg_and_die("semget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 187 | } |
| 188 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 189 | bb_perror_msg_and_die("semget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } else { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 193 | printf("Buffer already allocated just grab the semaphore?"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* write message to buffer */ |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 198 | static void circ_message(const char *msg) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 199 | { |
| 200 | int l = strlen(msg) + 1; /* count the whole message w/ '\0' included */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 201 | |
| 202 | sem_down(s_semid); |
| 203 | |
| 204 | /* |
| 205 | * Circular Buffer Algorithm: |
| 206 | * -------------------------- |
| 207 | * |
| 208 | * Start-off w/ empty buffer of specific size SHM_SIZ |
| 209 | * Start filling it up w/ messages. I use '\0' as separator to break up messages. |
| 210 | * This is also very handy since we can do printf on message. |
| 211 | * |
| 212 | * Once the buffer is full we need to get rid of the first message in buffer and |
| 213 | * insert the new message. (Note: if the message being added is >1 message then |
| 214 | * we will need to "remove" >1 old message from the buffer). The way this is done |
| 215 | * is the following: |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 216 | * When we reach the end of the buffer we set a mark and start from the beginning. |
| 217 | * Now what about the beginning and end of the buffer? Well we have the "head" |
| 218 | * index/pointer which is the starting point for the messages and we have "tail" |
| 219 | * index/pointer which is the ending point for the messages. When we "display" the |
| 220 | * messages we start from the beginning and continue until we reach "tail". If we |
| 221 | * reach end of buffer, then we just start from the beginning (offset 0). "head" and |
| 222 | * "tail" are actually offsets from the beginning of the buffer. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 223 | * |
| 224 | * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 225 | * a threasafe way of handling shared memory operations. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 226 | */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 227 | if ((buf->tail + l) < buf->size) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 228 | /* before we append the message we need to check the HEAD so that we won't |
| 229 | overwrite any of the message that we still need and adjust HEAD to point |
| 230 | to the next message! */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 231 | if (buf->tail < buf->head) { |
| 232 | if ((buf->tail + l) >= buf->head) { |
| 233 | /* we need to move the HEAD to point to the next message |
| 234 | * Theoretically we have enough room to add the whole message to the |
| 235 | * buffer, because of the first outer IF statement, so we don't have |
| 236 | * to worry about overflows here! |
| 237 | */ |
| 238 | int k = buf->tail + l - buf->head; /* we need to know how many bytes |
| 239 | we are overwriting to make |
| 240 | enough room */ |
| 241 | char *c = |
| 242 | memchr(buf->data + buf->head + k, '\0', |
| 243 | buf->size - (buf->head + k)); |
| 244 | if (c != NULL) { /* do a sanity check just in case! */ |
| 245 | buf->head = c - buf->data + 1; /* we need to convert pointer to |
| 246 | offset + skip the '\0' since |
| 247 | we need to point to the beginning |
| 248 | of the next message */ |
| 249 | /* Note: HEAD is only used to "retrieve" messages, it's not used |
| 250 | when writing messages into our buffer */ |
| 251 | } else { /* show an error message to know we messed up? */ |
| 252 | printf("Weird! Can't find the terminator token??? \n"); |
| 253 | buf->head = 0; |
| 254 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 255 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 256 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 257 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 258 | /* in other cases no overflows have been done yet, so we don't care! */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 259 | /* we should be ok to append the message now */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 260 | strncpy(buf->data + buf->tail, msg, l); /* append our message */ |
| 261 | buf->tail += l; /* count full message w/ '\0' terminating char */ |
| 262 | } else { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 263 | /* we need to break up the message and "circle" it around */ |
| 264 | char *c; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 265 | int k = buf->tail + l - buf->size; /* count # of bytes we don't fit */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 266 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 267 | /* We need to move HEAD! This is always the case since we are going |
| 268 | * to "circle" the message. |
| 269 | */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 270 | c = memchr(buf->data + k, '\0', buf->size - k); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 271 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 272 | if (c != NULL) { /* if we don't have '\0'??? weird!!! */ |
| 273 | /* move head pointer */ |
| 274 | buf->head = c - buf->data + 1; |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 275 | |
| 276 | /* now write the first part of the message */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 277 | strncpy(buf->data + buf->tail, msg, l - k - 1); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 278 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 279 | /* ALWAYS terminate end of buffer w/ '\0' */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 280 | buf->data[buf->size - 1] = '\0'; |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 281 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 282 | /* now write out the rest of the string to the beginning of the buffer */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 283 | strcpy(buf->data, &msg[l - k - 1]); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 284 | |
| 285 | /* we need to place the TAIL at the end of the message */ |
| 286 | buf->tail = k + 1; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 287 | } else { |
| 288 | printf |
| 289 | ("Weird! Can't find the terminator token from the beginning??? \n"); |
| 290 | buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 291 | } |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 292 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 293 | } |
| 294 | sem_up(s_semid); |
| 295 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 296 | #endif /* CONFIG_FEATURE_IPC_SYSLOG */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 297 | |
Erik Andersen | c053e41 | 2000-03-21 01:31:24 +0000 | [diff] [blame] | 298 | /* Note: There is also a function called "message()" in init.c */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 299 | /* Print a message to the log file. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 300 | static void message(char *fmt, ...) __attribute__ ((format(printf, 1, 2))); |
| 301 | static void message(char *fmt, ...) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 302 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 303 | int fd; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 304 | struct flock fl; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 305 | va_list arguments; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 306 | |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 307 | fl.l_whence = SEEK_SET; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 308 | fl.l_start = 0; |
| 309 | fl.l_len = 1; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 310 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 311 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 312 | if ((circular_logging == TRUE) && (buf != NULL)) { |
| 313 | char b[1024]; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 314 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 315 | va_start(arguments, fmt); |
| 316 | vsnprintf(b, sizeof(b) - 1, fmt, arguments); |
| 317 | va_end(arguments); |
| 318 | circ_message(b); |
| 319 | |
| 320 | } else |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 321 | #endif |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 322 | if ((fd = |
| 323 | device_open(logFilePath, |
| 324 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 325 | O_NONBLOCK)) >= 0) { |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 326 | fl.l_type = F_WRLCK; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 327 | fcntl(fd, F_SETLKW, &fl); |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 328 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
| 329 | if ( logFileSize > 0 ) { |
| 330 | struct stat statf; |
| 331 | int r = fstat(fd, &statf); |
| 332 | if( !r && (statf.st_mode & S_IFREG) |
| 333 | && (lseek(fd,0,SEEK_END) > logFileSize) ) { |
| 334 | if(logFileRotate > 0) { |
| 335 | int i; |
Eric Andersen | 2c6b418 | 2005-09-12 19:16:11 +0000 | [diff] [blame] | 336 | char oldFile[(strlen(logFilePath)+4)], newFile[(strlen(logFilePath)+4)]; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 337 | for(i=logFileRotate-1;i>0;i--) { |
| 338 | sprintf(oldFile, "%s.%d", logFilePath, i-1); |
| 339 | sprintf(newFile, "%s.%d", logFilePath, i); |
| 340 | rename(oldFile, newFile); |
| 341 | } |
| 342 | sprintf(newFile, "%s.%d", logFilePath, 0); |
| 343 | fl.l_type = F_UNLCK; |
| 344 | fcntl (fd, F_SETLKW, &fl); |
| 345 | close(fd); |
| 346 | rename(logFilePath, newFile); |
| 347 | fd = device_open (logFilePath, |
| 348 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 349 | O_NONBLOCK); |
| 350 | fl.l_type = F_WRLCK; |
| 351 | fcntl (fd, F_SETLKW, &fl); |
| 352 | } else { |
| 353 | ftruncate( fd, 0 ); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | #endif |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 358 | va_start(arguments, fmt); |
| 359 | vdprintf(fd, fmt, arguments); |
| 360 | va_end(arguments); |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 361 | fl.l_type = F_UNLCK; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 362 | fcntl(fd, F_SETLKW, &fl); |
| 363 | close(fd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 364 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 365 | /* Always send console messages to /dev/console so people will see them. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 366 | if ((fd = |
| 367 | device_open(_PATH_CONSOLE, |
| 368 | O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { |
| 369 | va_start(arguments, fmt); |
| 370 | vdprintf(fd, fmt, arguments); |
| 371 | va_end(arguments); |
| 372 | close(fd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 373 | } else { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 374 | fprintf(stderr, "Bummer, can't print: "); |
| 375 | va_start(arguments, fmt); |
| 376 | vfprintf(stderr, fmt, arguments); |
| 377 | fflush(stderr); |
| 378 | va_end(arguments); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 379 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 380 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Eric Andersen | f91b928 | 2004-09-08 10:56:06 +0000 | [diff] [blame] | 383 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
| 384 | static void init_RemoteLog(void) |
| 385 | { |
| 386 | memset(&remoteaddr, 0, sizeof(remoteaddr)); |
| 387 | remotefd = socket(AF_INET, SOCK_DGRAM, 0); |
| 388 | |
| 389 | if (remotefd < 0) { |
| 390 | bb_error_msg("cannot create socket"); |
| 391 | } |
| 392 | |
| 393 | remoteaddr.sin_family = AF_INET; |
| 394 | remoteaddr.sin_addr = *(struct in_addr *) *(xgethostbyname(RemoteHost))->h_addr_list; |
| 395 | remoteaddr.sin_port = htons(RemotePort); |
| 396 | } |
| 397 | #endif |
| 398 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 399 | static void logMessage(int pri, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 400 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 401 | time_t now; |
| 402 | char *timestamp; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 403 | static char res[20] = ""; |
Eric Andersen | 192c35f | 2004-09-02 22:22:17 +0000 | [diff] [blame] | 404 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | f91b928 | 2004-09-08 10:56:06 +0000 | [diff] [blame] | 405 | static char line[MAXLINE + 1]; |
Eric Andersen | 192c35f | 2004-09-02 22:22:17 +0000 | [diff] [blame] | 406 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 407 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 408 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 409 | if (pri != 0) { |
| 410 | for (c_fac = facilitynames; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 411 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 412 | for (c_pri = prioritynames; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 413 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); |
| 414 | if (c_fac->c_name == NULL || c_pri->c_name == NULL) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 415 | snprintf(res, sizeof(res), "<%d>", pri); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 416 | } else { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 417 | snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 418 | } |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 419 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 420 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 421 | if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 422 | msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | time(&now); |
| 424 | timestamp = ctime(&now) + 4; |
| 425 | timestamp[15] = '\0'; |
| 426 | } else { |
| 427 | timestamp = msg; |
| 428 | timestamp[15] = '\0'; |
| 429 | msg += 16; |
| 430 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 431 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 432 | /* todo: supress duplicates */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 433 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 434 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 435 | if (doRemoteLog == TRUE) { |
| 436 | /* trying connect the socket */ |
| 437 | if (-1 == remotefd) { |
| 438 | init_RemoteLog(); |
| 439 | } |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 440 | |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 441 | /* if we have a valid socket, send the message */ |
| 442 | if (-1 != remotefd) { |
| 443 | now = 1; |
Paul Fox | 27cbffd | 2005-07-20 18:02:11 +0000 | [diff] [blame] | 444 | snprintf(line, sizeof(line), "<%d>%s", pri, msg); |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 445 | |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 446 | retry: |
| 447 | /* send message to remote logger */ |
| 448 | if(( -1 == sendto(remotefd, line, strlen(line), 0, |
| 449 | (struct sockaddr *) &remoteaddr, |
| 450 | sizeof(remoteaddr))) && (errno == EINTR)) { |
| 451 | /* sleep now seconds and retry (with now * 2) */ |
| 452 | sleep(now); |
| 453 | now *= 2; |
| 454 | goto retry; |
| 455 | } |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 458 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 459 | if (local_logging == TRUE) |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 460 | #endif |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 461 | { |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 462 | /* now spew out the message to wherever it is supposed to go */ |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 463 | if (small) |
| 464 | message("%s %s\n", timestamp, msg); |
| 465 | else |
| 466 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
| 467 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static void quit_signal(int sig) |
| 471 | { |
Eric Andersen | 238bc40 | 2001-05-07 17:55:05 +0000 | [diff] [blame] | 472 | logMessage(LOG_SYSLOG | LOG_INFO, "System log daemon exiting."); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 473 | unlink(lfile); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 474 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 475 | ipcsyslog_cleanup(); |
| 476 | #endif |
| 477 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 478 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 481 | static void domark(int sig) |
| 482 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 483 | if (MarkInterval > 0) { |
| 484 | logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --"); |
| 485 | alarm(MarkInterval); |
| 486 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Eric Andersen | e527207 | 2003-07-22 22:15:21 +0000 | [diff] [blame] | 489 | /* This must be a #define, since when CONFIG_DEBUG and BUFFERS_GO_IN_BSS are |
Eric Andersen | 22ecf04 | 2001-07-02 17:32:40 +0000 | [diff] [blame] | 490 | * enabled, we otherwise get a "storage size isn't constant error. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 491 | static int serveConnection(char *tmpbuf, int n_read) |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 492 | { |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 493 | char *p = tmpbuf; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 494 | |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 495 | while (p < tmpbuf + n_read) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 496 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 497 | int pri = (LOG_USER | LOG_NOTICE); |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 498 | int num_lt = 0; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 499 | char line[MAXLINE + 1]; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 500 | unsigned char c; |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 501 | char *q = line; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 502 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 503 | while ((c = *p) && q < &line[sizeof(line) - 1]) { |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 504 | if (c == '<' && num_lt == 0) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 505 | /* Parse the magic priority number. */ |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 506 | num_lt++; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 507 | pri = 0; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 508 | while (isdigit(*(++p))) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 509 | pri = 10 * pri + (*p - '0'); |
| 510 | } |
Eric Andersen | 46ba568 | 2003-05-23 09:29:57 +0000 | [diff] [blame] | 511 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) { |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 512 | pri = (LOG_USER | LOG_NOTICE); |
Eric Andersen | 46ba568 | 2003-05-23 09:29:57 +0000 | [diff] [blame] | 513 | } |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 514 | } else if (c == '\n') { |
| 515 | *q++ = ' '; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 516 | } else if (iscntrl(c) && (c < 0177)) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 517 | *q++ = '^'; |
| 518 | *q++ = c ^ 0100; |
| 519 | } else { |
| 520 | *q++ = c; |
| 521 | } |
| 522 | p++; |
| 523 | } |
| 524 | *q = '\0'; |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 525 | p++; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 526 | /* Now log it */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 527 | logMessage(pri, line); |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 528 | } |
Mark Whitley | bff6b18 | 2001-03-27 20:17:58 +0000 | [diff] [blame] | 529 | return n_read; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 532 | static void doSyslogd(void) __attribute__ ((noreturn)); |
| 533 | static void doSyslogd(void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 534 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 535 | struct sockaddr_un sunx; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 536 | socklen_t addrLength; |
| 537 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 538 | int sock_fd; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 539 | fd_set fds; |
| 540 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 541 | /* Set up signal handlers. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 542 | signal(SIGINT, quit_signal); |
| 543 | signal(SIGTERM, quit_signal); |
| 544 | signal(SIGQUIT, quit_signal); |
| 545 | signal(SIGHUP, SIG_IGN); |
| 546 | signal(SIGCHLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 547 | #ifdef SIGCLD |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 548 | signal(SIGCLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 549 | #endif |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 550 | signal(SIGALRM, domark); |
| 551 | alarm(MarkInterval); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 552 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 553 | /* Create the syslog file so realpath() can work. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 554 | if (realpath(_PATH_LOG, lfile) != NULL) { |
| 555 | unlink(lfile); |
| 556 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 557 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 558 | memset(&sunx, 0, sizeof(sunx)); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 559 | sunx.sun_family = AF_UNIX; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 560 | strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); |
| 561 | if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 562 | bb_perror_msg_and_die("Couldn't get file descriptor for socket " |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 563 | _PATH_LOG); |
| 564 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 565 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 566 | addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); |
| 567 | if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 568 | bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 569 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 570 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 571 | if (chmod(lfile, 0666) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 572 | bb_perror_msg_and_die("Could not set permission on " _PATH_LOG); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 573 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 574 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 575 | if (circular_logging == TRUE) { |
| 576 | ipcsyslog_init(); |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 577 | } |
| 578 | #endif |
| 579 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 580 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 581 | if (doRemoteLog == TRUE) { |
| 582 | init_RemoteLog(); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 583 | } |
| 584 | #endif |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 585 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 586 | logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 587 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 588 | for (;;) { |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 589 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 590 | FD_ZERO(&fds); |
| 591 | FD_SET(sock_fd, &fds); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 592 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 593 | if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 594 | if (errno == EINTR) { |
| 595 | /* alarm may have happened. */ |
| 596 | continue; |
| 597 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 598 | bb_perror_msg_and_die("select error"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 599 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 600 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 601 | if (FD_ISSET(sock_fd, &fds)) { |
| 602 | int i; |
"Vladimir N. Oleynik" | b32b1db | 2005-10-15 13:49:21 +0000 | [diff] [blame] | 603 | #if MAXLINE > BUFSIZ |
| 604 | # define TMP_BUF_SZ BUFSIZ |
| 605 | #else |
| 606 | # define TMP_BUF_SZ MAXLINE |
| 607 | #endif |
| 608 | #define tmpbuf bb_common_bufsiz1 |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 609 | |
"Vladimir N. Oleynik" | b32b1db | 2005-10-15 13:49:21 +0000 | [diff] [blame] | 610 | if ((i = recv(sock_fd, tmpbuf, TMP_BUF_SZ, 0)) > 0) { |
| 611 | tmpbuf[i] = '\0'; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 612 | serveConnection(tmpbuf, i); |
| 613 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 614 | bb_perror_msg_and_die("UNIX socket error"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 615 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 616 | } /* FD_ISSET() */ |
| 617 | } /* for main loop */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 620 | extern int syslogd_main(int argc, char **argv) |
| 621 | { |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 622 | int opt; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 623 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 624 | int doFork = TRUE; |
| 625 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 626 | char *p; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 627 | |
Eric Andersen | 394cf22 | 2000-12-11 16:48:50 +0000 | [diff] [blame] | 628 | /* do normal option parsing */ |
Glenn L McGrath | 5529b7b | 2004-07-22 04:23:18 +0000 | [diff] [blame] | 629 | while ((opt = getopt(argc, argv, "m:nO:s:Sb:R:LC::")) > 0) { |
Eric Andersen | 394cf22 | 2000-12-11 16:48:50 +0000 | [diff] [blame] | 630 | switch (opt) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 631 | case 'm': |
| 632 | MarkInterval = atoi(optarg) * 60; |
| 633 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 634 | case 'n': |
| 635 | doFork = FALSE; |
| 636 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 637 | case 'O': |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 638 | logFilePath = optarg; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 639 | break; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 640 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
| 641 | case 's': |
| 642 | logFileSize = atoi(optarg) * 1024; |
| 643 | break; |
| 644 | case 'b': |
| 645 | logFileRotate = atoi(optarg); |
| 646 | if( logFileRotate > 99 ) logFileRotate = 99; |
| 647 | break; |
| 648 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 649 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 650 | case 'R': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 651 | RemoteHost = bb_xstrdup(optarg); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 652 | if ((p = strchr(RemoteHost, ':'))) { |
| 653 | RemotePort = atoi(p + 1); |
| 654 | *p = '\0'; |
| 655 | } |
| 656 | doRemoteLog = TRUE; |
| 657 | break; |
| 658 | case 'L': |
| 659 | local_logging = TRUE; |
| 660 | break; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 661 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 662 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 663 | case 'C': |
Glenn L McGrath | a79220d | 2003-09-26 00:49:05 +0000 | [diff] [blame] | 664 | if (optarg) { |
| 665 | int buf_size = atoi(optarg); |
| 666 | if (buf_size >= 4) { |
Glenn L McGrath | df2c565 | 2004-02-22 12:17:33 +0000 | [diff] [blame] | 667 | shm_size = buf_size * 1024; |
Glenn L McGrath | a79220d | 2003-09-26 00:49:05 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 670 | circular_logging = TRUE; |
| 671 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 672 | #endif |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 673 | case 'S': |
| 674 | small = true; |
| 675 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 676 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 677 | bb_show_usage(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 678 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 681 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 682 | /* If they have not specified remote logging, then log locally */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 683 | if (doRemoteLog == FALSE) |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 684 | local_logging = TRUE; |
| 685 | #endif |
| 686 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 687 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 688 | /* Store away localhost's name before the fork */ |
| 689 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 690 | if ((p = strchr(LocalHostName, '.'))) { |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 691 | *p = '\0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 694 | umask(0); |
| 695 | |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 696 | if (doFork == TRUE) { |
Glenn L McGrath | dc72f3a | 2003-08-29 07:35:08 +0000 | [diff] [blame] | 697 | #if defined(__uClinux__) |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 698 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
| 699 | #else /* __uClinux__ */ |
| 700 | if(daemon(0, 1) < 0) |
| 701 | bb_perror_msg_and_die("daemon"); |
| 702 | #endif /* __uClinux__ */ |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 703 | } |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 704 | doSyslogd(); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 705 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 706 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 707 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 708 | |
| 709 | /* |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 710 | Local Variables |
| 711 | c-file-style: "linux" |
| 712 | c-basic-offset: 4 |
| 713 | tab-width: 4 |
| 714 | End: |
| 715 | */ |