3
0

syslogd.c 18 KB

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