3
0

syslogd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. enum {
  39. MAX_READ = 256,
  40. DNS_WAIT_SEC = 2 * 60,
  41. };
  42. /* Semaphore operation structures */
  43. struct shbuf_ds {
  44. int32_t size; /* size of data - 1 */
  45. int32_t tail; /* end of message list */
  46. char data[1]; /* data/messages */
  47. };
  48. /* Allows us to have smaller initializer. Ugly. */
  49. #define GLOBALS \
  50. const char *logFilePath; \
  51. int logFD; \
  52. /* interval between marks in seconds */ \
  53. /*int markInterval;*/ \
  54. /* level of messages to be logged */ \
  55. int logLevel; \
  56. USE_FEATURE_ROTATE_LOGFILE( \
  57. /* max size of file before rotation */ \
  58. unsigned logFileSize; \
  59. /* number of rotated message files */ \
  60. unsigned logFileRotate; \
  61. unsigned curFileSize; \
  62. smallint isRegular; \
  63. ) \
  64. USE_FEATURE_REMOTE_LOG( \
  65. /* udp socket for remote logging */ \
  66. int remoteFD; \
  67. len_and_sockaddr* remoteAddr; \
  68. ) \
  69. USE_FEATURE_IPC_SYSLOG( \
  70. int shmid; /* ipc shared memory id */ \
  71. int s_semid; /* ipc semaphore id */ \
  72. int shm_size; \
  73. struct sembuf SMwup[1]; \
  74. struct sembuf SMwdn[3]; \
  75. )
  76. struct init_globals {
  77. GLOBALS
  78. };
  79. struct globals {
  80. GLOBALS
  81. #if ENABLE_FEATURE_REMOTE_LOG
  82. unsigned last_dns_resolve;
  83. char *remoteAddrStr;
  84. #endif
  85. #if ENABLE_FEATURE_IPC_SYSLOG
  86. struct shbuf_ds *shbuf;
  87. #endif
  88. time_t last_log_time;
  89. /* localhost's name. We print only first 64 chars */
  90. char *hostname;
  91. /* We recv into recvbuf... */
  92. char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
  93. /* ...then copy to parsebuf, escaping control chars */
  94. /* (can grow x2 max) */
  95. char parsebuf[MAX_READ*2];
  96. /* ...then sprintf into printbuf, adding timestamp (15 chars),
  97. * host (64), fac.prio (20) to the message */
  98. /* (growth by: 15 + 64 + 20 + delims = ~110) */
  99. char printbuf[MAX_READ*2 + 128];
  100. };
  101. static const struct init_globals init_data = {
  102. .logFilePath = "/var/log/messages",
  103. .logFD = -1,
  104. #ifdef SYSLOGD_MARK
  105. .markInterval = 20 * 60,
  106. #endif
  107. .logLevel = 8,
  108. #if ENABLE_FEATURE_ROTATE_LOGFILE
  109. .logFileSize = 200 * 1024,
  110. .logFileRotate = 1,
  111. #endif
  112. #if ENABLE_FEATURE_REMOTE_LOG
  113. .remoteFD = -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. USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
  135. USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
  136. USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
  137. USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
  138. USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
  139. USE_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 = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
  146. OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
  147. OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
  148. OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
  149. OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
  150. OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
  151. };
  152. #define OPTION_STR "m:nO:l:S" \
  153. USE_FEATURE_ROTATE_LOGFILE("s:" ) \
  154. USE_FEATURE_ROTATE_LOGFILE("b:" ) \
  155. USE_FEATURE_REMOTE_LOG( "R:" ) \
  156. USE_FEATURE_REMOTE_LOG( "L" ) \
  157. USE_FEATURE_IPC_SYSLOG( "C::") \
  158. USE_FEATURE_SYSLOGD_DUP( "D" )
  159. #define OPTION_DECL *opt_m, *opt_l \
  160. USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
  161. USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
  162. USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
  163. #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
  164. USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
  165. USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
  166. USE_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \
  167. USE_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. struct flock fl;
  260. int len = strlen(msg);
  261. #if ENABLE_FEATURE_IPC_SYSLOG
  262. if ((option_mask32 & OPT_circularlog) && G.shbuf) {
  263. log_to_shmem(msg, len);
  264. return;
  265. }
  266. #endif
  267. if (G.logFD >= 0) {
  268. if (!now)
  269. now = time(NULL);
  270. if (G.last_log_time != now) {
  271. G.last_log_time = now; /* reopen log file every second */
  272. close(G.logFD);
  273. goto reopen;
  274. }
  275. } else {
  276. reopen:
  277. G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
  278. | O_NOCTTY | O_APPEND | O_NONBLOCK);
  279. if (G.logFD < 0) {
  280. /* cannot open logfile? - print to /dev/console then */
  281. int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
  282. if (fd < 0)
  283. fd = 2; /* then stderr, dammit */
  284. full_write(fd, msg, len);
  285. if (fd != 2)
  286. close(fd);
  287. return;
  288. }
  289. #if ENABLE_FEATURE_ROTATE_LOGFILE
  290. {
  291. struct stat statf;
  292. G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
  293. /* bug (mostly harmless): can wrap around if file > 4gb */
  294. G.curFileSize = statf.st_size;
  295. }
  296. #endif
  297. }
  298. fl.l_whence = SEEK_SET;
  299. fl.l_start = 0;
  300. fl.l_len = 1;
  301. fl.l_type = F_WRLCK;
  302. fcntl(G.logFD, F_SETLKW, &fl);
  303. #if ENABLE_FEATURE_ROTATE_LOGFILE
  304. if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
  305. if (G.logFileRotate) { /* always 0..99 */
  306. int i = strlen(G.logFilePath) + 3 + 1;
  307. char oldFile[i];
  308. char newFile[i];
  309. i = G.logFileRotate - 1;
  310. /* rename: f.8 -> f.9; f.7 -> f.8; ... */
  311. while (1) {
  312. sprintf(newFile, "%s.%d", G.logFilePath, i);
  313. if (i == 0) break;
  314. sprintf(oldFile, "%s.%d", G.logFilePath, --i);
  315. /* ignore errors - file might be missing */
  316. rename(oldFile, newFile);
  317. }
  318. /* newFile == "f.0" now */
  319. rename(G.logFilePath, newFile);
  320. fl.l_type = F_UNLCK;
  321. fcntl(G.logFD, F_SETLKW, &fl);
  322. close(G.logFD);
  323. goto reopen;
  324. }
  325. ftruncate(G.logFD, 0);
  326. }
  327. G.curFileSize +=
  328. #endif
  329. full_write(G.logFD, msg, len);
  330. fl.l_type = F_UNLCK;
  331. fcntl(G.logFD, F_SETLKW, &fl);
  332. }
  333. static void parse_fac_prio_20(int pri, char *res20)
  334. {
  335. const CODE *c_pri, *c_fac;
  336. if (pri != 0) {
  337. c_fac = facilitynames;
  338. while (c_fac->c_name) {
  339. if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
  340. c_fac++;
  341. continue;
  342. }
  343. /* facility is found, look for prio */
  344. c_pri = prioritynames;
  345. while (c_pri->c_name) {
  346. if (c_pri->c_val != LOG_PRI(pri)) {
  347. c_pri++;
  348. continue;
  349. }
  350. snprintf(res20, 20, "%s.%s",
  351. c_fac->c_name, c_pri->c_name);
  352. return;
  353. }
  354. /* prio not found, bail out */
  355. break;
  356. }
  357. snprintf(res20, 20, "<%d>", pri);
  358. }
  359. }
  360. /* len parameter is used only for "is there a timestamp?" check.
  361. * NB: some callers cheat and supply len==0 when they know
  362. * that there is no timestamp, short-circuiting the test. */
  363. static void timestamp_and_log(int pri, char *msg, int len)
  364. {
  365. char *timestamp;
  366. time_t now;
  367. if (len < 16 || msg[3] != ' ' || msg[6] != ' '
  368. || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
  369. ) {
  370. time(&now);
  371. timestamp = ctime(&now) + 4; /* skip day of week */
  372. } else {
  373. now = 0;
  374. timestamp = msg;
  375. msg += 16;
  376. }
  377. timestamp[15] = '\0';
  378. if (option_mask32 & OPT_small)
  379. sprintf(G.printbuf, "%s %s\n", timestamp, msg);
  380. else {
  381. char res[20];
  382. parse_fac_prio_20(pri, res);
  383. sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
  384. }
  385. /* Log message locally (to file or shared mem) */
  386. log_locally(now, G.printbuf);
  387. }
  388. static void timestamp_and_log_internal(const char *msg)
  389. {
  390. if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
  391. return;
  392. timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
  393. }
  394. /* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
  395. * embedded NULs. Split messages on each of these NULs, parse prio,
  396. * escape control chars and log each locally. */
  397. static void split_escape_and_log(char *tmpbuf, int len)
  398. {
  399. char *p = tmpbuf;
  400. tmpbuf += len;
  401. while (p < tmpbuf) {
  402. char c;
  403. char *q = G.parsebuf;
  404. int pri = (LOG_USER | LOG_NOTICE);
  405. if (*p == '<') {
  406. /* Parse the magic priority number */
  407. pri = bb_strtou(p + 1, &p, 10);
  408. if (*p == '>')
  409. p++;
  410. if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
  411. pri = (LOG_USER | LOG_NOTICE);
  412. }
  413. while ((c = *p++)) {
  414. if (c == '\n')
  415. c = ' ';
  416. if (!(c & ~0x1f) && c != '\t') {
  417. *q++ = '^';
  418. c += '@'; /* ^@, ^A, ^B... */
  419. }
  420. *q++ = c;
  421. }
  422. *q = '\0';
  423. /* Now log it */
  424. if (LOG_PRI(pri) < G.logLevel)
  425. timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
  426. }
  427. }
  428. static void quit_signal(int sig)
  429. {
  430. timestamp_and_log_internal("syslogd exiting");
  431. puts("syslogd exiting");
  432. if (ENABLE_FEATURE_IPC_SYSLOG)
  433. ipcsyslog_cleanup();
  434. kill_myself_with_sig(sig);
  435. }
  436. #ifdef SYSLOGD_MARK
  437. static void do_mark(int sig)
  438. {
  439. if (G.markInterval) {
  440. timestamp_and_log_internal("-- MARK --");
  441. alarm(G.markInterval);
  442. }
  443. }
  444. #endif
  445. /* Don't inline: prevent struct sockaddr_un to take up space on stack
  446. * permanently */
  447. static NOINLINE int create_socket(void)
  448. {
  449. struct sockaddr_un sunx;
  450. int sock_fd;
  451. char *dev_log_name;
  452. memset(&sunx, 0, sizeof(sunx));
  453. sunx.sun_family = AF_UNIX;
  454. /* Unlink old /dev/log or object it points to. */
  455. /* (if it exists, bind will fail) */
  456. strcpy(sunx.sun_path, "/dev/log");
  457. dev_log_name = xmalloc_follow_symlinks("/dev/log");
  458. if (dev_log_name) {
  459. safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
  460. free(dev_log_name);
  461. }
  462. unlink(sunx.sun_path);
  463. sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
  464. xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
  465. chmod("/dev/log", 0666);
  466. return sock_fd;
  467. }
  468. #if ENABLE_FEATURE_REMOTE_LOG
  469. static int try_to_resolve_remote(void)
  470. {
  471. if (!G.remoteAddr) {
  472. unsigned now = monotonic_sec();
  473. /* Don't resolve name too often - DNS timeouts can be big */
  474. if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
  475. return -1;
  476. G.last_dns_resolve = now;
  477. G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
  478. if (!G.remoteAddr)
  479. return -1;
  480. }
  481. return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
  482. }
  483. #endif
  484. static void do_syslogd(void) ATTRIBUTE_NORETURN;
  485. static void do_syslogd(void)
  486. {
  487. int sock_fd;
  488. #if ENABLE_FEATURE_SYSLOGD_DUP
  489. int last_sz = -1;
  490. char *last_buf;
  491. char *recvbuf = G.recvbuf;
  492. #else
  493. #define recvbuf (G.recvbuf)
  494. #endif
  495. /* Set up signal handlers */
  496. bb_signals(0
  497. + (1 << SIGINT)
  498. + (1 << SIGTERM)
  499. + (1 << SIGQUIT)
  500. , quit_signal);
  501. signal(SIGHUP, SIG_IGN);
  502. /* signal(SIGCHLD, SIG_IGN); - why? */
  503. #ifdef SYSLOGD_MARK
  504. signal(SIGALRM, do_mark);
  505. alarm(G.markInterval);
  506. #endif
  507. sock_fd = create_socket();
  508. if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
  509. ipcsyslog_init();
  510. }
  511. timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
  512. for (;;) {
  513. ssize_t sz;
  514. #if ENABLE_FEATURE_SYSLOGD_DUP
  515. last_buf = recvbuf;
  516. if (recvbuf == G.recvbuf)
  517. recvbuf = G.recvbuf + MAX_READ;
  518. else
  519. recvbuf = G.recvbuf;
  520. #endif
  521. read_again:
  522. sz = safe_read(sock_fd, recvbuf, MAX_READ - 1);
  523. if (sz < 0)
  524. bb_perror_msg_and_die("read from /dev/log");
  525. /* Drop trailing '\n' and NULs (typically there is one NUL) */
  526. while (1) {
  527. if (sz == 0)
  528. goto read_again;
  529. /* man 3 syslog says: "A trailing newline is added when needed".
  530. * However, neither glibc nor uclibc do this:
  531. * syslog(prio, "test") sends "test\0" to /dev/log,
  532. * syslog(prio, "test\n") sends "test\n\0".
  533. * IOW: newline is passed verbatim!
  534. * I take it to mean that it's syslogd's job
  535. * to make those look identical in the log files. */
  536. if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
  537. break;
  538. sz--;
  539. }
  540. #if ENABLE_FEATURE_SYSLOGD_DUP
  541. if ((option_mask32 & OPT_dup) && (sz == last_sz))
  542. if (memcmp(last_buf, recvbuf, sz) == 0)
  543. continue;
  544. last_sz = sz;
  545. #endif
  546. #if ENABLE_FEATURE_REMOTE_LOG
  547. /* We are not modifying log messages in any way before send */
  548. /* Remote site cannot trust _us_ anyway and need to do validation again */
  549. if (G.remoteAddrStr) {
  550. if (-1 == G.remoteFD) {
  551. G.remoteFD = try_to_resolve_remote();
  552. if (-1 == G.remoteFD)
  553. goto no_luck;
  554. }
  555. /* Stock syslogd sends it '\n'-terminated
  556. * over network, mimic that */
  557. recvbuf[sz] = '\n';
  558. /* send message to remote logger, ignore possible error */
  559. /* TODO: on some errors, close and set G.remoteFD to -1
  560. * so that DNS resolution and connect is retried? */
  561. sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
  562. &G.remoteAddr->u.sa, G.remoteAddr->len);
  563. no_luck: ;
  564. }
  565. #endif
  566. if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
  567. recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
  568. split_escape_and_log(recvbuf, sz);
  569. }
  570. } /* for (;;) */
  571. #undef recvbuf
  572. }
  573. int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  574. int syslogd_main(int argc ATTRIBUTE_UNUSED, char **argv)
  575. {
  576. char OPTION_DECL;
  577. INIT_G();
  578. #if ENABLE_FEATURE_REMOTE_LOG
  579. G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
  580. #endif
  581. /* do normal option parsing */
  582. opt_complementary = "=0"; /* no non-option params */
  583. getopt32(argv, OPTION_STR, OPTION_PARAM);
  584. #ifdef SYSLOGD_MARK
  585. if (option_mask32 & OPT_mark) // -m
  586. G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
  587. #endif
  588. //if (option_mask32 & OPT_nofork) // -n
  589. //if (option_mask32 & OPT_outfile) // -O
  590. if (option_mask32 & OPT_loglevel) // -l
  591. G.logLevel = xatou_range(opt_l, 1, 8);
  592. //if (option_mask32 & OPT_small) // -S
  593. #if ENABLE_FEATURE_ROTATE_LOGFILE
  594. if (option_mask32 & OPT_filesize) // -s
  595. G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
  596. if (option_mask32 & OPT_rotatecnt) // -b
  597. G.logFileRotate = xatou_range(opt_b, 0, 99);
  598. #endif
  599. #if ENABLE_FEATURE_IPC_SYSLOG
  600. if (opt_C) // -Cn
  601. G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
  602. #endif
  603. /* If they have not specified remote logging, then log locally */
  604. if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
  605. option_mask32 |= OPT_locallog;
  606. /* Store away localhost's name before the fork */
  607. G.hostname = safe_gethostname();
  608. *strchrnul(G.hostname, '.') = '\0';
  609. if (!(option_mask32 & OPT_nofork)) {
  610. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  611. }
  612. umask(0);
  613. write_pidfile("/var/run/syslogd.pid");
  614. do_syslogd();
  615. /* return EXIT_SUCCESS; */
  616. }
  617. /* Clean up. Needed because we are included from syslogd_and_logger.c */
  618. #undef G
  619. #undef GLOBALS
  620. #undef INIT_G
  621. #undef OPTION_STR
  622. #undef OPTION_DECL
  623. #undef OPTION_PARAM