syslogd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 "libbb.h"
  16. #include <paths.h>
  17. #include <sys/un.h>
  18. /* SYSLOG_NAMES defined to pull prioritynames[] and facilitynames[]
  19. * from syslog.h. Grrrr - glibc puts those in _rwdata_! :( */
  20. #define SYSLOG_NAMES
  21. #define SYSLOG_NAMES_CONST /* uclibc is saner :) */
  22. #include <sys/syslog.h>
  23. #include <sys/uio.h>
  24. #if ENABLE_FEATURE_REMOTE_LOG
  25. #include <netinet/in.h>
  26. #endif
  27. #if ENABLE_FEATURE_IPC_SYSLOG
  28. #include <sys/ipc.h>
  29. #include <sys/sem.h>
  30. #include <sys/shm.h>
  31. #endif
  32. #define DEBUG 0
  33. /* MARK code is not very useful, is bloat, and broken:
  34. * can deadlock if alarmed to make MARK while writing to IPC buffer
  35. * (semaphores are down but do_mark routine tries to down them again) */
  36. #undef SYSLOGD_MARK
  37. enum { MAX_READ = 256 };
  38. /* Semaphore operation structures */
  39. struct shbuf_ds {
  40. int32_t size; /* size of data - 1 */
  41. int32_t tail; /* end of message list */
  42. char data[1]; /* data/messages */
  43. };
  44. /* Allows us to have smaller initializer. Ugly. */
  45. #define GLOBALS \
  46. const char *logFilePath; \
  47. int logFD; \
  48. /* interval between marks in seconds */ \
  49. /*int markInterval;*/ \
  50. /* level of messages to be logged */ \
  51. int logLevel; \
  52. USE_FEATURE_ROTATE_LOGFILE( \
  53. /* max size of file before rotation */ \
  54. unsigned logFileSize; \
  55. /* number of rotated message files */ \
  56. unsigned logFileRotate; \
  57. unsigned curFileSize; \
  58. smallint isRegular; \
  59. ) \
  60. USE_FEATURE_REMOTE_LOG( \
  61. /* udp socket for remote logging */ \
  62. int remoteFD; \
  63. len_and_sockaddr* remoteAddr; \
  64. ) \
  65. USE_FEATURE_IPC_SYSLOG( \
  66. int shmid; /* ipc shared memory id */ \
  67. int s_semid; /* ipc semaphore id */ \
  68. int shm_size; \
  69. struct sembuf SMwup[1]; \
  70. struct sembuf SMwdn[3]; \
  71. )
  72. struct init_globals {
  73. GLOBALS
  74. };
  75. struct globals {
  76. GLOBALS
  77. #if ENABLE_FEATURE_IPC_SYSLOG
  78. struct shbuf_ds *shbuf;
  79. #endif
  80. time_t last_log_time;
  81. /* localhost's name */
  82. char localHostName[64];
  83. /* We recv into recvbuf... */
  84. char recvbuf[MAX_READ];
  85. /* ...then copy to parsebuf, escaping control chars */
  86. /* (can grow x2 max) */
  87. char parsebuf[MAX_READ*2];
  88. /* ...then sprintf into printbuf, adding timestamp (15 chars),
  89. * host (64), fac.prio (20) to the message */
  90. /* (growth by: 15 + 64 + 20 + delims = ~110) */
  91. char printbuf[MAX_READ*2 + 128];
  92. };
  93. static const struct init_globals init_data = {
  94. .logFilePath = "/var/log/messages",
  95. .logFD = -1,
  96. #ifdef SYSLOGD_MARK
  97. .markInterval = 20 * 60,
  98. #endif
  99. .logLevel = 8,
  100. #if ENABLE_FEATURE_ROTATE_LOGFILE
  101. .logFileSize = 200 * 1024,
  102. .logFileRotate = 1,
  103. #endif
  104. #if ENABLE_FEATURE_REMOTE_LOG
  105. .remoteFD = -1,
  106. #endif
  107. #if ENABLE_FEATURE_IPC_SYSLOG
  108. .shmid = -1,
  109. .s_semid = -1,
  110. .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
  111. .SMwup = { {1, -1, IPC_NOWAIT} },
  112. .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
  113. #endif
  114. };
  115. #define G (*ptr_to_globals)
  116. /* Options */
  117. enum {
  118. OPTBIT_mark = 0, // -m
  119. OPTBIT_nofork, // -n
  120. OPTBIT_outfile, // -O
  121. OPTBIT_loglevel, // -l
  122. OPTBIT_small, // -S
  123. USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
  124. USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
  125. USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
  126. USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L
  127. USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
  128. OPT_mark = 1 << OPTBIT_mark ,
  129. OPT_nofork = 1 << OPTBIT_nofork ,
  130. OPT_outfile = 1 << OPTBIT_outfile ,
  131. OPT_loglevel = 1 << OPTBIT_loglevel,
  132. OPT_small = 1 << OPTBIT_small ,
  133. OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
  134. OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
  135. OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
  136. OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0,
  137. OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
  138. };
  139. #define OPTION_STR "m:nO:l:S" \
  140. USE_FEATURE_ROTATE_LOGFILE("s:" ) \
  141. USE_FEATURE_ROTATE_LOGFILE("b:" ) \
  142. USE_FEATURE_REMOTE_LOG( "R:" ) \
  143. USE_FEATURE_REMOTE_LOG( "L" ) \
  144. USE_FEATURE_IPC_SYSLOG( "C::")
  145. #define OPTION_DECL *opt_m, *opt_l \
  146. USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
  147. USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
  148. USE_FEATURE_REMOTE_LOG( ,*opt_R) \
  149. USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
  150. #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
  151. USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
  152. USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
  153. USE_FEATURE_REMOTE_LOG( ,&opt_R) \
  154. USE_FEATURE_IPC_SYSLOG( ,&opt_C)
  155. /* circular buffer variables/structures */
  156. #if ENABLE_FEATURE_IPC_SYSLOG
  157. #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
  158. #error Sorry, you must set the syslogd buffer size to at least 4KB.
  159. #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
  160. #endif
  161. /* our shared key */
  162. #define KEY_ID ((long)0x414e4547) /* "GENA" */
  163. static void ipcsyslog_cleanup(void)
  164. {
  165. if (G.shmid != -1) {
  166. shmdt(G.shbuf);
  167. }
  168. if (G.shmid != -1) {
  169. shmctl(G.shmid, IPC_RMID, NULL);
  170. }
  171. if (G.s_semid != -1) {
  172. semctl(G.s_semid, 0, IPC_RMID, 0);
  173. }
  174. }
  175. static void ipcsyslog_init(void)
  176. {
  177. if (DEBUG)
  178. printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size);
  179. G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
  180. if (G.shmid == -1) {
  181. bb_perror_msg_and_die("shmget");
  182. }
  183. G.shbuf = shmat(G.shmid, NULL, 0);
  184. if (!G.shbuf) {
  185. bb_perror_msg_and_die("shmat");
  186. }
  187. memset(G.shbuf, 0, G.shm_size);
  188. G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
  189. /*G.shbuf->tail = 0;*/
  190. // we'll trust the OS to set initial semval to 0 (let's hope)
  191. G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
  192. if (G.s_semid == -1) {
  193. if (errno == EEXIST) {
  194. G.s_semid = semget(KEY_ID, 2, 0);
  195. if (G.s_semid != -1)
  196. return;
  197. }
  198. bb_perror_msg_and_die("semget");
  199. }
  200. }
  201. /* Write message to shared mem buffer */
  202. static void log_to_shmem(const char *msg, int len)
  203. {
  204. int old_tail, new_tail;
  205. if (semop(G.s_semid, G.SMwdn, 3) == -1) {
  206. bb_perror_msg_and_die("SMwdn");
  207. }
  208. /* Circular Buffer Algorithm:
  209. * --------------------------
  210. * tail == position where to store next syslog message.
  211. * tail's max value is (shbuf->size - 1)
  212. * Last byte of buffer is never used and remains NUL.
  213. */
  214. len++; /* length with NUL included */
  215. again:
  216. old_tail = G.shbuf->tail;
  217. new_tail = old_tail + len;
  218. if (new_tail < G.shbuf->size) {
  219. /* store message, set new tail */
  220. memcpy(G.shbuf->data + old_tail, msg, len);
  221. G.shbuf->tail = new_tail;
  222. } else {
  223. /* k == available buffer space ahead of old tail */
  224. int k = G.shbuf->size - old_tail;
  225. /* copy what fits to the end of buffer, and repeat */
  226. memcpy(G.shbuf->data + old_tail, msg, k);
  227. msg += k;
  228. len -= k;
  229. G.shbuf->tail = 0;
  230. goto again;
  231. }
  232. if (semop(G.s_semid, G.SMwup, 1) == -1) {
  233. bb_perror_msg_and_die("SMwup");
  234. }
  235. if (DEBUG)
  236. printf("tail:%d\n", G.shbuf->tail);
  237. }
  238. #else
  239. void ipcsyslog_cleanup(void);
  240. void ipcsyslog_init(void);
  241. void log_to_shmem(const char *msg);
  242. #endif /* FEATURE_IPC_SYSLOG */
  243. /* Print a message to the log file. */
  244. static void log_locally(char *msg)
  245. {
  246. struct flock fl;
  247. int len = strlen(msg);
  248. #if ENABLE_FEATURE_IPC_SYSLOG
  249. if ((option_mask32 & OPT_circularlog) && G.shbuf) {
  250. log_to_shmem(msg, len);
  251. return;
  252. }
  253. #endif
  254. if (G.logFD >= 0) {
  255. time_t cur;
  256. time(&cur);
  257. if (G.last_log_time != cur) {
  258. G.last_log_time = cur; /* reopen log file every second */
  259. close(G.logFD);
  260. goto reopen;
  261. }
  262. } else {
  263. reopen:
  264. G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
  265. | O_NOCTTY | O_APPEND | O_NONBLOCK);
  266. if (G.logFD < 0) {
  267. /* cannot open logfile? - print to /dev/console then */
  268. int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
  269. if (fd < 0)
  270. fd = 2; /* then stderr, dammit */
  271. full_write(fd, msg, len);
  272. if (fd != 2)
  273. close(fd);
  274. return;
  275. }
  276. #if ENABLE_FEATURE_ROTATE_LOGFILE
  277. {
  278. struct stat statf;
  279. G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
  280. /* bug (mostly harmless): can wrap around if file > 4gb */
  281. G.curFileSize = statf.st_size;
  282. }
  283. #endif
  284. }
  285. fl.l_whence = SEEK_SET;
  286. fl.l_start = 0;
  287. fl.l_len = 1;
  288. fl.l_type = F_WRLCK;
  289. fcntl(G.logFD, F_SETLKW, &fl);
  290. #if ENABLE_FEATURE_ROTATE_LOGFILE
  291. if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
  292. if (G.logFileRotate) { /* always 0..99 */
  293. int i = strlen(G.logFilePath) + 3 + 1;
  294. char oldFile[i];
  295. char newFile[i];
  296. i = G.logFileRotate - 1;
  297. /* rename: f.8 -> f.9; f.7 -> f.8; ... */
  298. while (1) {
  299. sprintf(newFile, "%s.%d", G.logFilePath, i);
  300. if (i == 0) break;
  301. sprintf(oldFile, "%s.%d", G.logFilePath, --i);
  302. rename(oldFile, newFile);
  303. }
  304. /* newFile == "f.0" now */
  305. rename(G.logFilePath, newFile);
  306. fl.l_type = F_UNLCK;
  307. fcntl(G.logFD, F_SETLKW, &fl);
  308. close(G.logFD);
  309. goto reopen;
  310. }
  311. ftruncate(G.logFD, 0);
  312. }
  313. G.curFileSize +=
  314. #endif
  315. full_write(G.logFD, msg, len);
  316. fl.l_type = F_UNLCK;
  317. fcntl(G.logFD, F_SETLKW, &fl);
  318. }
  319. static void parse_fac_prio_20(int pri, char *res20)
  320. {
  321. const CODE *c_pri, *c_fac;
  322. if (pri != 0) {
  323. c_fac = facilitynames;
  324. while (c_fac->c_name) {
  325. if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
  326. c_fac++; continue;
  327. }
  328. /* facility is found, look for prio */
  329. c_pri = prioritynames;
  330. while (c_pri->c_name) {
  331. if (c_pri->c_val != LOG_PRI(pri)) {
  332. c_pri++; continue;
  333. }
  334. snprintf(res20, 20, "%s.%s",
  335. c_fac->c_name, c_pri->c_name);
  336. return;
  337. }
  338. /* prio not found, bail out */
  339. break;
  340. }
  341. snprintf(res20, 20, "<%d>", pri);
  342. }
  343. }
  344. /* len parameter is used only for "is there a timestamp?" check.
  345. * NB: some callers cheat and supply len==0 when they know
  346. * that there is no timestamp, short-circuiting the test. */
  347. static void timestamp_and_log(int pri, char *msg, int len)
  348. {
  349. char *timestamp;
  350. if (len < 16 || msg[3] != ' ' || msg[6] != ' '
  351. || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
  352. ) {
  353. time_t now;
  354. time(&now);
  355. timestamp = ctime(&now) + 4;
  356. } else {
  357. timestamp = msg;
  358. msg += 16;
  359. }
  360. timestamp[15] = '\0';
  361. /* Log message locally (to file or shared mem) */
  362. if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
  363. if (LOG_PRI(pri) < G.logLevel) {
  364. if (option_mask32 & OPT_small)
  365. sprintf(G.printbuf, "%s %s\n", timestamp, msg);
  366. else {
  367. char res[20];
  368. parse_fac_prio_20(pri, res);
  369. sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg);
  370. }
  371. log_locally(G.printbuf);
  372. }
  373. }
  374. }
  375. static void split_escape_and_log(char *tmpbuf, int len)
  376. {
  377. char *p = tmpbuf;
  378. tmpbuf += len;
  379. while (p < tmpbuf) {
  380. char c;
  381. char *q = G.parsebuf;
  382. int pri = (LOG_USER | LOG_NOTICE);
  383. if (*p == '<') {
  384. /* Parse the magic priority number */
  385. pri = bb_strtou(p + 1, &p, 10);
  386. if (*p == '>')
  387. p++;
  388. if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
  389. pri = (LOG_USER | LOG_NOTICE);
  390. }
  391. while ((c = *p++)) {
  392. if (c == '\n')
  393. c = ' ';
  394. if (!(c & ~0x1f) && c != '\t') {
  395. *q++ = '^';
  396. c += '@'; /* ^@, ^A, ^B... */
  397. }
  398. *q++ = c;
  399. }
  400. *q = '\0';
  401. /* Now log it */
  402. timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
  403. }
  404. }
  405. static void quit_signal(int sig)
  406. {
  407. timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0);
  408. puts("syslogd exiting");
  409. if (ENABLE_FEATURE_IPC_SYSLOG)
  410. ipcsyslog_cleanup();
  411. exit(1);
  412. }
  413. #ifdef SYSLOGD_MARK
  414. static void do_mark(int sig)
  415. {
  416. if (G.markInterval) {
  417. timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0);
  418. alarm(G.markInterval);
  419. }
  420. }
  421. #endif
  422. static void do_syslogd(void) ATTRIBUTE_NORETURN;
  423. static void do_syslogd(void)
  424. {
  425. struct sockaddr_un sunx;
  426. int sock_fd;
  427. char *dev_log_name;
  428. /* Set up signal handlers */
  429. signal(SIGINT, quit_signal);
  430. signal(SIGTERM, quit_signal);
  431. signal(SIGQUIT, quit_signal);
  432. signal(SIGHUP, SIG_IGN);
  433. signal(SIGCHLD, SIG_IGN);
  434. #ifdef SIGCLD
  435. signal(SIGCLD, SIG_IGN);
  436. #endif
  437. #ifdef SYSLOGD_MARK
  438. signal(SIGALRM, do_mark);
  439. alarm(G.markInterval);
  440. #endif
  441. remove_pidfile("/var/run/syslogd.pid");
  442. memset(&sunx, 0, sizeof(sunx));
  443. sunx.sun_family = AF_UNIX;
  444. strcpy(sunx.sun_path, "/dev/log");
  445. /* Unlink old /dev/log or object it points to. */
  446. /* (if it exists, bind will fail) */
  447. logmode = LOGMODE_NONE;
  448. dev_log_name = xmalloc_readlink_or_warn("/dev/log");
  449. logmode = LOGMODE_STDIO;
  450. if (dev_log_name) {
  451. int fd = xopen(".", O_NONBLOCK);
  452. xchdir("/dev");
  453. /* we do not check whether this is a link also */
  454. unlink(dev_log_name);
  455. fchdir(fd);
  456. close(fd);
  457. safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
  458. free(dev_log_name);
  459. } else {
  460. unlink("/dev/log");
  461. }
  462. sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
  463. xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
  464. if (chmod("/dev/log", 0666) < 0) {
  465. bb_perror_msg_and_die("cannot set permission on /dev/log");
  466. }
  467. if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
  468. ipcsyslog_init();
  469. }
  470. timestamp_and_log(LOG_SYSLOG | LOG_INFO,
  471. (char*)"syslogd started: BusyBox v" BB_VER, 0);
  472. for (;;) {
  473. size_t sz;
  474. read_again:
  475. sz = safe_read(sock_fd, G.recvbuf, MAX_READ - 1);
  476. if (sz < 0) {
  477. bb_perror_msg_and_die("read from /dev/log");
  478. }
  479. /* Drop trailing NULs (typically there is one NUL) */
  480. while (1) {
  481. if (sz == 0)
  482. goto read_again;
  483. /* man 3 syslog says: "A trailing newline is added when needed".
  484. * However, neither glibc nor uclibc do this:
  485. * syslog(prio, "test") sends "test\0" to /dev/log,
  486. * syslog(prio, "test\n") sends "test\n\0",
  487. * IOW: newline is passed verbatim!
  488. * I take it to mean that it's syslogd's job
  489. * to make those look identical in the log files */
  490. if (G.recvbuf[sz-1] && G.recvbuf[sz-1] != '\n')
  491. break;
  492. sz--;
  493. }
  494. /* Maybe we need to add '\n' here, not later?
  495. * It looks like stock syslogd does send '\n' over network,
  496. * but we do not (see sendto below) */
  497. G.recvbuf[sz] = '\0'; /* make sure it *is* NUL terminated */
  498. /* TODO: maybe suppress duplicates? */
  499. #if ENABLE_FEATURE_REMOTE_LOG
  500. /* We are not modifying log messages in any way before send */
  501. /* Remote site cannot trust _us_ anyway and need to do validation again */
  502. if (G.remoteAddr) {
  503. if (-1 == G.remoteFD) {
  504. G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0);
  505. }
  506. if (-1 != G.remoteFD) {
  507. /* send message to remote logger, ignore possible error */
  508. sendto(G.remoteFD, G.recvbuf, sz, MSG_DONTWAIT,
  509. &G.remoteAddr->sa, G.remoteAddr->len);
  510. }
  511. }
  512. #endif
  513. split_escape_and_log(G.recvbuf, sz);
  514. } /* for */
  515. }
  516. int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  517. int syslogd_main(int argc, char **argv)
  518. {
  519. char OPTION_DECL;
  520. char *p;
  521. PTR_TO_GLOBALS = memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data));
  522. /* do normal option parsing */
  523. opt_complementary = "=0"; /* no non-option params */
  524. getopt32(argv, OPTION_STR, OPTION_PARAM);
  525. #ifdef SYSLOGD_MARK
  526. if (option_mask32 & OPT_mark) // -m
  527. G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
  528. #endif
  529. //if (option_mask32 & OPT_nofork) // -n
  530. //if (option_mask32 & OPT_outfile) // -O
  531. if (option_mask32 & OPT_loglevel) // -l
  532. G.logLevel = xatou_range(opt_l, 1, 8);
  533. //if (option_mask32 & OPT_small) // -S
  534. #if ENABLE_FEATURE_ROTATE_LOGFILE
  535. if (option_mask32 & OPT_filesize) // -s
  536. G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
  537. if (option_mask32 & OPT_rotatecnt) // -b
  538. G.logFileRotate = xatou_range(opt_b, 0, 99);
  539. #endif
  540. #if ENABLE_FEATURE_REMOTE_LOG
  541. if (option_mask32 & OPT_remotelog) { // -R
  542. G.remoteAddr = xhost2sockaddr(opt_R, 514);
  543. }
  544. //if (option_mask32 & OPT_locallog) // -L
  545. #endif
  546. #if ENABLE_FEATURE_IPC_SYSLOG
  547. if (opt_C) // -Cn
  548. G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
  549. #endif
  550. /* If they have not specified remote logging, then log locally */
  551. if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
  552. option_mask32 |= OPT_locallog;
  553. /* Store away localhost's name before the fork */
  554. gethostname(G.localHostName, sizeof(G.localHostName));
  555. p = strchr(G.localHostName, '.');
  556. if (p) {
  557. *p = '\0';
  558. }
  559. if (!(option_mask32 & OPT_nofork)) {
  560. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  561. }
  562. umask(0);
  563. write_pidfile("/var/run/syslogd.pid");
  564. do_syslogd();
  565. /* return EXIT_SUCCESS; */
  566. }