syslogd.c 18 KB

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