blob: 8ae75c9743eb269adbffbe025b0c75d1de384574 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini syslogd implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen3843e961999-11-25 07:30:46 +00006 *
Erik Andersenf13df372000-04-18 23:51:51 +00007 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
8 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00009 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000010 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000011 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000012 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3843e961999-11-25 07:30:46 +000014 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000015
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000016#include "busybox.h"
Eric Andersenb186d981999-12-03 09:19:54 +000017#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000018#include <sys/un.h>
Eric Andersenb186d981999-12-03 09:19:54 +000019
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000020/* SYSLOG_NAMES defined to pull some extra junk from syslog.h: */
21/* prioritynames[] and facilitynames[]. uclibc pulls those in _rwdata_! :( */
22
Eric Andersen3843e961999-11-25 07:30:46 +000023#define SYSLOG_NAMES
24#include <sys/syslog.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000025#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000026
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000027#if ENABLE_FEATURE_REMOTE_LOG
28#include <netinet/in.h>
Denis Vlasenko4998c812007-02-14 20:51:46 +000029#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000031#if ENABLE_FEATURE_IPC_SYSLOG
32#include <sys/ipc.h>
33#include <sys/sem.h>
34#include <sys/shm.h>
35#endif
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000036
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000037
38#define DEBUG 0
39
40// Semaphore operation structures
41struct shbuf_ds {
42 int32_t size; // size of data written
43 int32_t head; // start of message list
44 int32_t tail; // end of message list
45 char data[1]; // data/messages
46}; // shared memory pointer
47
48struct globals {
49
50 const char *logFilePath;
51 int logFD;
52
53 /* This is not very useful, is bloat, and broken:
54 * can deadlock if alarmed to make MARK while writing to IPC buffer
55 * (semaphores are down but do_mark routine tries to down them again) */
56#ifdef SYSLOGD_MARK
57 /* interval between marks in seconds */
58 int markInterval;
59#endif
60
61 /* level of messages to be locally logged */
62 int logLevel;
Eric Andersen3843e961999-11-25 07:30:46 +000063
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000064#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000065 /* max size of message file before being rotated */
66 unsigned logFileSize;
67 /* number of rotated message files */
68 unsigned logFileRotate;
69 unsigned curFileSize;
70 smallint isRegular;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000071#endif
72
Denis Vlasenkoceab8702007-01-04 17:57:54 +000073#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000074 /* udp socket for logging to remote host */
75 int remoteFD;
76 len_and_sockaddr* remoteAddr;
Eric Andersenced2cef2000-07-20 23:41:24 +000077#endif
78
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000079#if ENABLE_FEATURE_IPC_SYSLOG
80 int shmid; // ipc shared memory id
81 int s_semid; // ipc semaphore id
82 int shm_size;
83 struct sembuf SMwup[1];
84 struct sembuf SMwdn[3];
85 struct shbuf_ds *shbuf;
86#endif
87
88 time_t last_log_time;
89 /* localhost's name */
90 char localHostName[64];
91
92}; /* struct globals */
93
94static const struct globals init_globals = {
95 .logFilePath = "/var/log/messages",
96 .logFD = -1,
97#ifdef SYSLOGD_MARK
98 .markInterval = 20 * 60,
99#endif
100 .logLevel = 8,
101#if ENABLE_FEATURE_ROTATE_LOGFILE
102 .logFileSize = 200 * 1024,
103 .logFileRotate = 1,
104#endif
105#if ENABLE_FEATURE_REMOTE_LOG
106 .remoteFD = -1,
107#endif
108#if ENABLE_FEATURE_IPC_SYSLOG
109 .shmid = -1,
110 .s_semid = -1,
111 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
112 .SMwup = { {1, -1, IPC_NOWAIT} },
113 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
114#endif
115 // FIXME: hidden tail with lotsa zeroes is here....
116};
117
118#define G (*ptr_to_globals)
119
120
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000121/* We are using bb_common_bufsiz1 for buffering: */
122enum { MAX_READ = (BUFSIZ/6) & ~0xf };
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000123/* We recv into RECVBUF... (size: MAX_READ ~== BUFSIZ/6) */
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000124#define RECVBUF bb_common_bufsiz1
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000125/* ...then copy to PARSEBUF, escaping control chars */
126/* (can grow x2 max ~== BUFSIZ/3) */
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000127#define PARSEBUF (bb_common_bufsiz1 + MAX_READ)
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000128/* ...then sprintf into PRINTBUF, adding timestamp (15 chars),
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000129 * host (64), fac.prio (20) to the message */
130/* (growth by: 15 + 64 + 20 + delims = ~110) */
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000131#define PRINTBUF (bb_common_bufsiz1 + 3*MAX_READ)
132/* totals: BUFSIZ == BUFSIZ/6 + BUFSIZ/3 + (BUFSIZ/3+BUFSIZ/6)
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000133 * -- we have BUFSIZ/6 extra at the ent of PRINTBUF
134 * which covers needed ~110 extra bytes (and much more) */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000135
136
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000137/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000138enum {
139 OPTBIT_mark = 0, // -m
140 OPTBIT_nofork, // -n
141 OPTBIT_outfile, // -O
142 OPTBIT_loglevel, // -l
143 OPTBIT_small, // -S
144 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
145 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
146 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
147 USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L
148 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
149
150 OPT_mark = 1 << OPTBIT_mark ,
151 OPT_nofork = 1 << OPTBIT_nofork ,
152 OPT_outfile = 1 << OPTBIT_outfile ,
153 OPT_loglevel = 1 << OPTBIT_loglevel,
154 OPT_small = 1 << OPTBIT_small ,
155 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
156 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
157 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
158 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0,
159 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
160};
161#define OPTION_STR "m:nO:l:S" \
162 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
163 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
164 USE_FEATURE_REMOTE_LOG( "R:" ) \
165 USE_FEATURE_REMOTE_LOG( "L" ) \
166 USE_FEATURE_IPC_SYSLOG( "C::")
167#define OPTION_DECL *opt_m, *opt_l \
168 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
169 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
170 USE_FEATURE_REMOTE_LOG( ,*opt_R) \
171 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000172#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000173 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
174 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
175 USE_FEATURE_REMOTE_LOG( ,&opt_R) \
176 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000177
Eric Andersen871d93c2002-09-17 20:06:29 +0000178
Mark Whitley6317c4b2001-03-12 22:51:50 +0000179/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000180#if ENABLE_FEATURE_IPC_SYSLOG
181
Eric Andersend4a5e252003-12-19 11:32:14 +0000182#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
183#error Sorry, you must set the syslogd buffer size to at least 4KB.
184#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
185#endif
186
Mark Whitley6317c4b2001-03-12 22:51:50 +0000187/* our shared key */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000188#define KEY_ID ((long)0x414e4547) /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000189
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000190static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000191{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000192 if (G.shmid != -1) {
193 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000194 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000195 if (G.shmid != -1) {
196 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000197 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000198 if (G.s_semid != -1) {
199 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000200 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000201}
202
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000203static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000204{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000205 if (DEBUG)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000206 printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000207
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000208 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 1023);
209 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000210 bb_perror_msg_and_die("shmget");
211 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000212
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000213 G.shbuf = shmat(G.shmid, NULL, 0);
214 if (!G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000215 bb_perror_msg_and_die("shmat");
216 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000217
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000218 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data);
219 G.shbuf->head = G.shbuf->tail = 0;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000220
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000221 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000222 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
223 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000224 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000225 G.s_semid = semget(KEY_ID, 2, 0);
226 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000227 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000228 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000229 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000230 }
231}
232
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000233/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000234static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000235{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000236 int old_tail, new_tail;
237 char *c;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000238
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000239 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000240 bb_perror_msg_and_die("SMwdn");
241 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000242
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000243 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000244 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000245 * tail == position where to store next syslog message.
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000246 * head == position of next message to retrieve ("print").
247 * if head == tail, there is no "unprinted" messages left.
248 * head is typically advanced by separate "reader" program,
249 * but if there isn't one, we have to do it ourself.
250 * messages are NUL-separated.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000251 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000252 len++; /* length with NUL included */
253 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000254 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000255 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000256 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000257 /* No need to move head if shbuf->head <= old_tail,
258 * else... */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000259 if (old_tail < G.shbuf->head && G.shbuf->head <= new_tail) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000260 /* ...need to move head forward */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000261 c = memchr(G.shbuf->data + new_tail, '\0',
262 G.shbuf->size - new_tail);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000263 if (!c) /* no NUL ahead of us, wrap around */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000264 c = memchr(G.shbuf->data, '\0', old_tail);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000265 if (!c) { /* still nothing? point to this msg... */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000266 G.shbuf->head = old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000267 } else {
268 /* convert pointer to offset + skip NUL */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000269 G.shbuf->head = c - G.shbuf->data + 1;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000270 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000271 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000272 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000273 memcpy(G.shbuf->data + old_tail, msg, len);
274 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000275 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000276 /* we need to break up the message and wrap it around */
277 /* k == available buffer space ahead of old tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000278 int k = G.shbuf->size - old_tail - 1;
279 if (G.shbuf->head > old_tail) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000280 /* we are going to overwrite head, need to
281 * move it out of the way */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000282 c = memchr(G.shbuf->data, '\0', old_tail);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000283 if (!c) { /* nothing? point to this msg... */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000284 G.shbuf->head = old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000285 } else { /* convert pointer to offset + skip NUL */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000286 G.shbuf->head = c - G.shbuf->data + 1;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000287 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000288 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000289 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000290 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000291 msg += k;
292 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000293 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000294 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000295 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000296 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000297 bb_perror_msg_and_die("SMwup");
298 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000299 if (DEBUG)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000300 printf("head:%d tail:%d\n", G.shbuf->head, G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000301}
Rob Landley028ba282006-08-28 20:16:42 +0000302#else
303void ipcsyslog_cleanup(void);
304void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000305void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000306#endif /* FEATURE_IPC_SYSLOG */
307
308
Erik Andersen983b51b2000-04-04 18:14:25 +0000309/* Print a message to the log file. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000310static void log_locally(char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000311{
Denis Vlasenkob8934972007-01-04 18:02:32 +0000312 struct flock fl;
313 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000314
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000315#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000316 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000317 log_to_shmem(msg, len);
318 return;
319 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000320#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000321 if (G.logFD >= 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000322 time_t cur;
323 time(&cur);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000324 if (G.last_log_time != cur) {
325 G.last_log_time = cur; /* reopen log file every second */
326 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000327 goto reopen;
328 }
329 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000330 reopen:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000331 G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000332 | O_NOCTTY | O_APPEND | O_NONBLOCK);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000333 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000334 /* cannot open logfile? - print to /dev/console then */
335 int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
336 if (fd < 0)
337 fd = 2; /* then stderr, dammit */
338 full_write(fd, msg, len);
339 if (fd != 2)
340 close(fd);
341 return;
342 }
343#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000344 {
345 struct stat statf;
346
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000347 G.isRegular = (fstat(G.logFD, &statf) == 0 && (statf.st_mode & S_IFREG));
Denis Vlasenkob8934972007-01-04 18:02:32 +0000348 /* bug (mostly harmless): can wrap around if file > 4gb */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000349 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000350 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000351#endif
352 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000353
Denis Vlasenkob8934972007-01-04 18:02:32 +0000354 fl.l_whence = SEEK_SET;
355 fl.l_start = 0;
356 fl.l_len = 1;
357 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000358 fcntl(G.logFD, F_SETLKW, &fl);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000359
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000360#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000361 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
362 if (G.logFileRotate) { /* always 0..99 */
363 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000364 char oldFile[i];
365 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000366 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000367 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
368 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000369 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000370 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000371 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000372 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000373 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000374 /* newFile == "f.0" now */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000375 rename(G.logFilePath, newFile);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000376 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000377 fcntl(G.logFD, F_SETLKW, &fl);
378 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000379 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000380 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000381 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000382 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000383 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000384#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000385 full_write(G.logFD, msg, len);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000386 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000387 fcntl(G.logFD, F_SETLKW, &fl);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000388}
389
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000390static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000391{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000393
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000394 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000395 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000396 while (c_fac->c_name) {
397 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
398 c_fac++; continue;
399 }
400 /* facility is found, look for prio */
401 c_pri = prioritynames;
402 while (c_pri->c_name) {
403 if (c_pri->c_val != LOG_PRI(pri)) {
404 c_pri++; continue;
405 }
406 snprintf(res20, 20, "%s.%s",
407 c_fac->c_name, c_pri->c_name);
408 return;
409 }
410 /* prio not found, bail out */
411 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000412 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000413 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000414 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000415}
Eric Andersen3843e961999-11-25 07:30:46 +0000416
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000417/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000418 * NB: some callers cheat and supply 0 when they know
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000419 * that there is no timestamp, short-cutting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000420static void timestamp_and_log(int pri, char *msg, int len)
421{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000422 char *timestamp;
423
424 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
425 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
426 ) {
Denis Vlasenkobae79482007-01-09 23:42:43 +0000427 time_t now;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 time(&now);
429 timestamp = ctime(&now) + 4;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 } else {
431 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 msg += 16;
433 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000434 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000435
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000436 /* Log message locally (to file or shared mem) */
437 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000438 if (LOG_PRI(pri) < G.logLevel) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000439 if (option_mask32 & OPT_small)
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000440 sprintf(PRINTBUF, "%s %s\n", timestamp, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000441 else {
442 char res[20];
443 parse_fac_prio_20(pri, res);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000444 sprintf(PRINTBUF, "%s %s %s %s\n", timestamp, G.localHostName, res, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000445 }
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000446 log_locally(PRINTBUF);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000447 }
448 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000449}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000450
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000451static void split_escape_and_log(char *tmpbuf, int len)
452{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000453 char *p = tmpbuf;
454
455 tmpbuf += len;
456 while (p < tmpbuf) {
457 char c;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000458 char *q = PARSEBUF;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000459 int pri = (LOG_USER | LOG_NOTICE);
460
461 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000462 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000463 pri = bb_strtou(p + 1, &p, 10);
464 if (*p == '>') p++;
465 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) {
466 pri = (LOG_USER | LOG_NOTICE);
467 }
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000468 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000469
470 while ((c = *p++)) {
471 if (c == '\n')
472 c = ' ';
473 if (!(c & ~0x1f)) {
474 *q++ = '^';
475 c += '@'; /* ^@, ^A, ^B... */
476 }
477 *q++ = c;
478 }
479 *q = '\0';
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000480 /* Now log it */
481 timestamp_and_log(pri, PARSEBUF, q - PARSEBUF);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000482 }
Eric Andersen3843e961999-11-25 07:30:46 +0000483}
484
485static void quit_signal(int sig)
486{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000487 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0);
Denis Vlasenko1c962f22007-01-09 23:44:57 +0000488 puts("syslogd exiting");
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000489 if (ENABLE_FEATURE_IPC_SYSLOG)
490 ipcsyslog_cleanup();
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000491 exit(1);
Eric Andersen3843e961999-11-25 07:30:46 +0000492}
493
Denis Vlasenko4998c812007-02-14 20:51:46 +0000494#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000495static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000496{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000497 if (G.markInterval) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000498 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000499 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 }
Eric Andersen3843e961999-11-25 07:30:46 +0000501}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000502#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000503
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000504static void do_syslogd(void) ATTRIBUTE_NORETURN;
505static void do_syslogd(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000506{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 struct sockaddr_un sunx;
Erik Andersen983b51b2000-04-04 18:14:25 +0000508 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000509 fd_set fds;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000510 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000511
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000512 /* Set up signal handlers */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000513 signal(SIGINT, quit_signal);
514 signal(SIGTERM, quit_signal);
515 signal(SIGQUIT, quit_signal);
516 signal(SIGHUP, SIG_IGN);
517 signal(SIGCHLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000518#ifdef SIGCLD
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000519 signal(SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000520#endif
Denis Vlasenko4998c812007-02-14 20:51:46 +0000521#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000522 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000523 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000524#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000525
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000526 memset(&sunx, 0, sizeof(sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000527 sunx.sun_family = AF_UNIX;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000528 strcpy(sunx.sun_path, "/dev/log");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000530 /* Unlink old /dev/log or object it points to. */
531 /* (if it exists, bind will fail) */
532 logmode = LOGMODE_NONE;
533 dev_log_name = xmalloc_readlink_or_warn("/dev/log");
534 logmode = LOGMODE_STDIO;
535 if (dev_log_name) {
536 int fd = xopen(".", O_NONBLOCK);
537 xchdir("/dev");
538 /* we do not check whether this is a link also */
539 unlink(dev_log_name);
540 fchdir(fd);
541 close(fd);
542 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
543 free(dev_log_name);
544 } else {
545 unlink("/dev/log");
546 }
547
548 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
549 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
550
551 if (chmod("/dev/log", 0666) < 0) {
552 bb_perror_msg_and_die("cannot set permission on /dev/log");
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000553 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000554 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000555 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000556 }
Eric Andersenea906502001-04-05 20:55:17 +0000557
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000558 timestamp_and_log(LOG_SYSLOG | LOG_INFO,
559 (char*)"syslogd started: BusyBox v" BB_VER, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560
Erik Andersen983b51b2000-04-04 18:14:25 +0000561 for (;;) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000562 FD_ZERO(&fds);
563 FD_SET(sock_fd, &fds);
Erik Andersenf13df372000-04-18 23:51:51 +0000564
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000565 if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) {
Eric Andersen871d93c2002-09-17 20:06:29 +0000566 if (errno == EINTR) {
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000567 /* alarm may have happened */
Eric Andersen871d93c2002-09-17 20:06:29 +0000568 continue;
569 }
Denis Vlasenko14c19402006-09-30 19:20:00 +0000570 bb_perror_msg_and_die("select");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000573 if (FD_ISSET(sock_fd, &fds)) {
574 int i;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000575 i = recv(sock_fd, RECVBUF, MAX_READ - 1, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000576 if (i <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000577 bb_perror_msg_and_die("UNIX socket error");
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000578 /* TODO: maybe suppress duplicates? */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000579#if ENABLE_FEATURE_REMOTE_LOG
580 /* We are not modifying log messages in any way before send */
581 /* Remote site cannot trust _us_ anyway and need to do validation again */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000582 if (G.remoteAddr) {
583 if (-1 == G.remoteFD) {
584 G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000585 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000586 if (-1 != G.remoteFD) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000587 /* send message to remote logger, ignore possible error */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000588 sendto(G.remoteFD, RECVBUF, i, MSG_DONTWAIT,
589 &G.remoteAddr->sa, G.remoteAddr->len);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000590 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000591 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000592#endif
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000593 RECVBUF[i] = '\0';
594 split_escape_and_log(RECVBUF, i);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000595 } /* FD_ISSET() */
596 } /* for */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000597}
598
Denis Vlasenko06af2162007-02-03 17:28:39 +0000599int syslogd_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000600int syslogd_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000601{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000602 char OPTION_DECL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000605 PTR_TO_GLOBALS = xzalloc(sizeof(G));
606 memcpy(ptr_to_globals, &init_globals, sizeof(init_globals));
607
Eric Andersen394cf222000-12-11 16:48:50 +0000608 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000609 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000610 getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000611#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000612 if (option_mask32 & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000613 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000614#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000615 //if (option_mask32 & OPT_nofork) // -n
616 //if (option_mask32 & OPT_outfile) // -O
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000617 if (option_mask32 & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000618 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000619 //if (option_mask32 & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000620#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000621 if (option_mask32 & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000622 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000623 if (option_mask32 & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000624 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000625#endif
626#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000627 if (option_mask32 & OPT_remotelog) { // -R
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000628 G.remoteAddr = xhost2sockaddr(opt_R, 514);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000630 //if (option_mask32 & OPT_locallog) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000631#endif
632#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000633 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000634 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000635#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636
Eric Andersen4ed17822000-12-11 19:28:29 +0000637 /* If they have not specified remote logging, then log locally */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000638 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
639 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000640
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 /* Store away localhost's name before the fork */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000642 gethostname(G.localHostName, sizeof(G.localHostName));
643 p = strchr(G.localHostName, '.');
Denis Vlasenko14c19402006-09-30 19:20:00 +0000644 if (p) {
Glenn L McGrathfe538ba2003-09-10 23:35:45 +0000645 *p = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 }
647
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000648 if (!(option_mask32 & OPT_nofork)) {
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000649#ifdef BB_NOMMU
Russ Dilla1fece22003-12-15 21:57:44 +0000650 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000651#else
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000652 bb_daemonize();
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000653#endif
Eric Andersen35e643b2003-07-28 07:40:39 +0000654 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000655 umask(0);
656 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000657 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000658}