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