syslogd.c 19 KB

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