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