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