syslogd.c 18 KB

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