3
0

syslogd.c 18 KB

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