3
0

syslogd.c 16 KB

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