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