syslogd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. IF_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. IF_FEATURE_REMOTE_LOG( \
  67. /* udp socket for remote logging */ \
  68. int remoteFD; \
  69. len_and_sockaddr* remoteAddr; \
  70. ) \
  71. IF_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. IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
  137. IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
  138. IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
  139. IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
  140. IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
  141. IF_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 = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
  148. OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
  149. OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
  150. OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
  151. OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
  152. OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
  153. };
  154. #define OPTION_STR "m:nO:l:S" \
  155. IF_FEATURE_ROTATE_LOGFILE("s:" ) \
  156. IF_FEATURE_ROTATE_LOGFILE("b:" ) \
  157. IF_FEATURE_REMOTE_LOG( "R:" ) \
  158. IF_FEATURE_REMOTE_LOG( "L" ) \
  159. IF_FEATURE_IPC_SYSLOG( "C::") \
  160. IF_FEATURE_SYSLOGD_DUP( "D" )
  161. #define OPTION_DECL *opt_m, *opt_l \
  162. IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
  163. IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
  164. IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
  165. #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
  166. IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
  167. IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
  168. IF_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \
  169. IF_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. /* Jan 18 00:11:22 msg... */
  384. /* 01234567890123456 */
  385. if (len < 16 || msg[3] != ' ' || msg[6] != ' '
  386. || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
  387. ) {
  388. time(&now);
  389. timestamp = ctime(&now) + 4; /* skip day of week */
  390. } else {
  391. now = 0;
  392. timestamp = msg;
  393. msg += 16;
  394. }
  395. timestamp[15] = '\0';
  396. if (option_mask32 & OPT_small)
  397. sprintf(G.printbuf, "%s %s\n", timestamp, msg);
  398. else {
  399. char res[20];
  400. parse_fac_prio_20(pri, res);
  401. sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
  402. }
  403. /* Log message locally (to file or shared mem) */
  404. log_locally(now, G.printbuf);
  405. }
  406. static void timestamp_and_log_internal(const char *msg)
  407. {
  408. /* -L, or no -R */
  409. if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
  410. return;
  411. timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
  412. }
  413. /* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
  414. * embedded NULs. Split messages on each of these NULs, parse prio,
  415. * escape control chars and log each locally. */
  416. static void split_escape_and_log(char *tmpbuf, int len)
  417. {
  418. char *p = tmpbuf;
  419. tmpbuf += len;
  420. while (p < tmpbuf) {
  421. char c;
  422. char *q = G.parsebuf;
  423. int pri = (LOG_USER | LOG_NOTICE);
  424. if (*p == '<') {
  425. /* Parse the magic priority number */
  426. pri = bb_strtou(p + 1, &p, 10);
  427. if (*p == '>')
  428. p++;
  429. if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
  430. pri = (LOG_USER | LOG_NOTICE);
  431. }
  432. while ((c = *p++)) {
  433. if (c == '\n')
  434. c = ' ';
  435. if (!(c & ~0x1f) && c != '\t') {
  436. *q++ = '^';
  437. c += '@'; /* ^@, ^A, ^B... */
  438. }
  439. *q++ = c;
  440. }
  441. *q = '\0';
  442. /* Now log it */
  443. if (LOG_PRI(pri) < G.logLevel)
  444. timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
  445. }
  446. }
  447. #ifdef SYSLOGD_MARK
  448. static void do_mark(int sig)
  449. {
  450. if (G.markInterval) {
  451. timestamp_and_log_internal("-- MARK --");
  452. alarm(G.markInterval);
  453. }
  454. }
  455. #endif
  456. /* Don't inline: prevent struct sockaddr_un to take up space on stack
  457. * permanently */
  458. static NOINLINE int create_socket(void)
  459. {
  460. struct sockaddr_un sunx;
  461. int sock_fd;
  462. char *dev_log_name;
  463. memset(&sunx, 0, sizeof(sunx));
  464. sunx.sun_family = AF_UNIX;
  465. /* Unlink old /dev/log or object it points to. */
  466. /* (if it exists, bind will fail) */
  467. strcpy(sunx.sun_path, "/dev/log");
  468. dev_log_name = xmalloc_follow_symlinks("/dev/log");
  469. if (dev_log_name) {
  470. safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
  471. free(dev_log_name);
  472. }
  473. unlink(sunx.sun_path);
  474. sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
  475. xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
  476. chmod("/dev/log", 0666);
  477. return sock_fd;
  478. }
  479. #if ENABLE_FEATURE_REMOTE_LOG
  480. static int try_to_resolve_remote(void)
  481. {
  482. if (!G.remoteAddr) {
  483. unsigned now = monotonic_sec();
  484. /* Don't resolve name too often - DNS timeouts can be big */
  485. if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
  486. return -1;
  487. G.last_dns_resolve = now;
  488. G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
  489. if (!G.remoteAddr)
  490. return -1;
  491. }
  492. return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
  493. }
  494. #endif
  495. static void do_syslogd(void) NORETURN;
  496. static void do_syslogd(void)
  497. {
  498. int sock_fd;
  499. #if ENABLE_FEATURE_SYSLOGD_DUP
  500. int last_sz = -1;
  501. char *last_buf;
  502. char *recvbuf = G.recvbuf;
  503. #else
  504. #define recvbuf (G.recvbuf)
  505. #endif
  506. /* Set up signal handlers (so that they interrupt read()) */
  507. signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
  508. signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
  509. //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
  510. signal(SIGHUP, SIG_IGN);
  511. #ifdef SYSLOGD_MARK
  512. signal(SIGALRM, do_mark);
  513. alarm(G.markInterval);
  514. #endif
  515. sock_fd = create_socket();
  516. if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
  517. ipcsyslog_init();
  518. }
  519. timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
  520. while (!bb_got_signal) {
  521. ssize_t sz;
  522. #if ENABLE_FEATURE_SYSLOGD_DUP
  523. last_buf = recvbuf;
  524. if (recvbuf == G.recvbuf)
  525. recvbuf = G.recvbuf + MAX_READ;
  526. else
  527. recvbuf = G.recvbuf;
  528. #endif
  529. read_again:
  530. sz = read(sock_fd, recvbuf, MAX_READ - 1);
  531. if (sz < 0) {
  532. if (!bb_got_signal)
  533. bb_perror_msg("read from /dev/log");
  534. break;
  535. }
  536. /* Drop trailing '\n' and NULs (typically there is one NUL) */
  537. while (1) {
  538. if (sz == 0)
  539. goto read_again;
  540. /* man 3 syslog says: "A trailing newline is added when needed".
  541. * However, neither glibc nor uclibc do this:
  542. * syslog(prio, "test") sends "test\0" to /dev/log,
  543. * syslog(prio, "test\n") sends "test\n\0".
  544. * IOW: newline is passed verbatim!
  545. * I take it to mean that it's syslogd's job
  546. * to make those look identical in the log files. */
  547. if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
  548. break;
  549. sz--;
  550. }
  551. #if ENABLE_FEATURE_SYSLOGD_DUP
  552. if ((option_mask32 & OPT_dup) && (sz == last_sz))
  553. if (memcmp(last_buf, recvbuf, sz) == 0)
  554. continue;
  555. last_sz = sz;
  556. #endif
  557. #if ENABLE_FEATURE_REMOTE_LOG
  558. /* We are not modifying log messages in any way before send */
  559. /* Remote site cannot trust _us_ anyway and need to do validation again */
  560. if (G.remoteAddrStr) {
  561. if (-1 == G.remoteFD) {
  562. G.remoteFD = try_to_resolve_remote();
  563. if (-1 == G.remoteFD)
  564. goto no_luck;
  565. }
  566. /* Stock syslogd sends it '\n'-terminated
  567. * over network, mimic that */
  568. recvbuf[sz] = '\n';
  569. /* send message to remote logger, ignore possible error */
  570. /* TODO: on some errors, close and set G.remoteFD to -1
  571. * so that DNS resolution and connect is retried? */
  572. sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
  573. &G.remoteAddr->u.sa, G.remoteAddr->len);
  574. no_luck: ;
  575. }
  576. #endif
  577. if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
  578. recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
  579. split_escape_and_log(recvbuf, sz);
  580. }
  581. } /* while (!bb_got_signal) */
  582. timestamp_and_log_internal("syslogd exiting");
  583. puts("syslogd exiting");
  584. if (ENABLE_FEATURE_IPC_SYSLOG)
  585. ipcsyslog_cleanup();
  586. kill_myself_with_sig(bb_got_signal);
  587. #undef recvbuf
  588. }
  589. int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  590. int syslogd_main(int argc UNUSED_PARAM, char **argv)
  591. {
  592. char OPTION_DECL;
  593. int opts;
  594. INIT_G();
  595. #if ENABLE_FEATURE_REMOTE_LOG
  596. G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
  597. #endif
  598. /* do normal option parsing */
  599. opt_complementary = "=0"; /* no non-option params */
  600. opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
  601. #ifdef SYSLOGD_MARK
  602. if (opts & OPT_mark) // -m
  603. G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
  604. #endif
  605. //if (opts & OPT_nofork) // -n
  606. //if (opts & OPT_outfile) // -O
  607. if (opts & OPT_loglevel) // -l
  608. G.logLevel = xatou_range(opt_l, 1, 8);
  609. //if (opts & OPT_small) // -S
  610. #if ENABLE_FEATURE_ROTATE_LOGFILE
  611. if (opts & OPT_filesize) // -s
  612. G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
  613. if (opts & OPT_rotatecnt) // -b
  614. G.logFileRotate = xatou_range(opt_b, 0, 99);
  615. #endif
  616. #if ENABLE_FEATURE_IPC_SYSLOG
  617. if (opt_C) // -Cn
  618. G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
  619. #endif
  620. /* If they have not specified remote logging, then log locally */
  621. if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
  622. option_mask32 |= OPT_locallog;
  623. /* Store away localhost's name before the fork */
  624. G.hostname = safe_gethostname();
  625. *strchrnul(G.hostname, '.') = '\0';
  626. if (!(opts & OPT_nofork)) {
  627. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  628. }
  629. umask(0);
  630. write_pidfile("/var/run/syslogd.pid");
  631. do_syslogd();
  632. /* return EXIT_SUCCESS; */
  633. }
  634. /* Clean up. Needed because we are included from syslogd_and_logger.c */
  635. #undef DEBUG
  636. #undef SYSLOGD_MARK
  637. #undef SYSLOGD_WRLOCK
  638. #undef G
  639. #undef GLOBALS
  640. #undef INIT_G
  641. #undef OPTION_STR
  642. #undef OPTION_DECL
  643. #undef OPTION_PARAM