syslogd.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 GPLv2 or later, see file LICENSE in this source tree.
  14. */
  15. //config:config SYSLOGD
  16. //config: bool "syslogd (14 kb)"
  17. //config: default y
  18. //config: help
  19. //config: The syslogd utility is used to record logs of all the
  20. //config: significant events that occur on a system. Every
  21. //config: message that is logged records the date and time of the
  22. //config: event, and will generally also record the name of the
  23. //config: application that generated the message. When used in
  24. //config: conjunction with klogd, messages from the Linux kernel
  25. //config: can also be recorded. This is terribly useful,
  26. //config: especially for finding what happened when something goes
  27. //config: wrong. And something almost always will go wrong if
  28. //config: you wait long enough....
  29. //config:
  30. //config:config FEATURE_ROTATE_LOGFILE
  31. //config: bool "Rotate message files"
  32. //config: default y
  33. //config: depends on SYSLOGD
  34. //config: help
  35. //config: This enables syslogd to rotate the message files
  36. //config: on his own. No need to use an external rotate script.
  37. //config:
  38. //config:config FEATURE_REMOTE_LOG
  39. //config: bool "Remote Log support"
  40. //config: default y
  41. //config: depends on SYSLOGD
  42. //config: help
  43. //config: When you enable this feature, the syslogd utility can
  44. //config: be used to send system log messages to another system
  45. //config: connected via a network. This allows the remote
  46. //config: machine to log all the system messages, which can be
  47. //config: terribly useful for reducing the number of serial
  48. //config: cables you use. It can also be a very good security
  49. //config: measure to prevent system logs from being tampered with
  50. //config: by an intruder.
  51. //config:
  52. //config:config FEATURE_SYSLOGD_DUP
  53. //config: bool "Support -D (drop dups) option"
  54. //config: default y
  55. //config: depends on SYSLOGD
  56. //config: help
  57. //config: Option -D instructs syslogd to drop consecutive messages
  58. //config: which are totally the same.
  59. //config:
  60. //config:config FEATURE_SYSLOGD_CFG
  61. //config: bool "Support syslog.conf"
  62. //config: default y
  63. //config: depends on SYSLOGD
  64. //config: help
  65. //config: Supports restricted syslogd config. See docs/syslog.conf.txt
  66. //config:
  67. //config:config FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
  68. //config: bool "Include milliseconds in timestamps"
  69. //config: default n
  70. //config: depends on SYSLOGD
  71. //config: help
  72. //config: Includes milliseconds (HH:MM:SS.mmm) in timestamp when
  73. //config: timestamps are added.
  74. //config:
  75. //config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE
  76. //config: int "Read buffer size in bytes"
  77. //config: default 256
  78. //config: range 256 20000
  79. //config: depends on SYSLOGD
  80. //config: help
  81. //config: This option sets the size of the syslog read buffer.
  82. //config: Actual memory usage increases around five times the
  83. //config: change done here.
  84. //config:
  85. //config:config FEATURE_IPC_SYSLOG
  86. //config: bool "Circular Buffer support"
  87. //config: default y
  88. //config: depends on SYSLOGD
  89. //config: help
  90. //config: When you enable this feature, the syslogd utility will
  91. //config: use a circular buffer to record system log messages.
  92. //config: When the buffer is filled it will continue to overwrite
  93. //config: the oldest messages. This can be very useful for
  94. //config: systems with little or no permanent storage, since
  95. //config: otherwise system logs can eventually fill up your
  96. //config: entire filesystem, which may cause your system to
  97. //config: break badly.
  98. //config:
  99. //config:config FEATURE_IPC_SYSLOG_BUFFER_SIZE
  100. //config: int "Circular buffer size in Kbytes (minimum 4KB)"
  101. //config: default 16
  102. //config: range 4 2147483647
  103. //config: depends on FEATURE_IPC_SYSLOG
  104. //config: help
  105. //config: This option sets the size of the circular buffer
  106. //config: used to record system log messages.
  107. //config:
  108. //config:config FEATURE_KMSG_SYSLOG
  109. //config: bool "Linux kernel printk buffer support"
  110. //config: default y
  111. //config: depends on SYSLOGD
  112. //config: help
  113. //config: When you enable this feature, the syslogd utility will
  114. //config: write system log message to the Linux kernel's printk buffer.
  115. //config: This can be used as a smaller alternative to the syslogd IPC
  116. //config: support, as klogd and logread aren't needed.
  117. //config:
  118. //config: NOTICE: Syslog facilities in log entries needs kernel 3.5+.
  119. //applet:IF_SYSLOGD(APPLET(syslogd, BB_DIR_SBIN, BB_SUID_DROP))
  120. //kbuild:lib-$(CONFIG_SYSLOGD) += syslogd_and_logger.o
  121. //usage:#define syslogd_trivial_usage
  122. //usage: "[OPTIONS]"
  123. //usage:#define syslogd_full_usage "\n\n"
  124. //usage: "System logging utility\n"
  125. //usage: IF_NOT_FEATURE_SYSLOGD_CFG(
  126. //usage: "(this version of syslogd ignores /etc/syslog.conf)\n"
  127. //usage: )
  128. //usage: "\n -n Run in foreground"
  129. //usage: IF_FEATURE_REMOTE_LOG(
  130. //usage: "\n -R HOST[:PORT] Log to HOST:PORT (default PORT:514)"
  131. //usage: "\n -L Log locally and via network (default is network only if -R)"
  132. //usage: )
  133. //usage: IF_FEATURE_IPC_SYSLOG(
  134. /* NB: -Csize shouldn't have space (because size is optional) */
  135. //usage: "\n -C[size_kb] Log to shared mem buffer (use logread to read it)"
  136. //usage: )
  137. //usage: IF_FEATURE_KMSG_SYSLOG(
  138. //usage: "\n -K Log to kernel printk buffer (use dmesg to read it)"
  139. //usage: )
  140. //usage: "\n -O FILE Log to FILE (default: /var/log/messages, stdout if -)"
  141. //usage: IF_FEATURE_ROTATE_LOGFILE(
  142. //usage: "\n -s SIZE Max size (KB) before rotation (default 200KB, 0=off)"
  143. //usage: "\n -b N N rotated logs to keep (default 1, max 99, 0=purge)"
  144. //usage: )
  145. //usage: "\n -l N Log only messages more urgent than prio N (1-8)"
  146. //usage: "\n -S Smaller output"
  147. //usage: "\n -t Strip client-generated timestamps"
  148. //usage: IF_FEATURE_SYSLOGD_DUP(
  149. //usage: "\n -D Drop duplicates"
  150. //usage: )
  151. //usage: IF_FEATURE_SYSLOGD_CFG(
  152. //usage: "\n -f FILE Use FILE as config (default:/etc/syslog.conf)"
  153. //usage: )
  154. /* //usage: "\n -m MIN Minutes between MARK lines (default 20, 0=off)" */
  155. //usage:
  156. //usage:#define syslogd_example_usage
  157. //usage: "$ syslogd -R masterlog:514\n"
  158. //usage: "$ syslogd -R 192.168.1.1:601\n"
  159. /*
  160. * Done in syslogd_and_logger.c:
  161. #include "libbb.h"
  162. #define SYSLOG_NAMES
  163. #define SYSLOG_NAMES_CONST
  164. #include <syslog.h>
  165. */
  166. #ifndef _PATH_LOG
  167. #define _PATH_LOG "/dev/log"
  168. #endif
  169. #include <sys/un.h>
  170. #include <sys/uio.h>
  171. #if ENABLE_FEATURE_REMOTE_LOG
  172. #include <netinet/in.h>
  173. #endif
  174. #if ENABLE_FEATURE_IPC_SYSLOG
  175. #include <sys/ipc.h>
  176. #include <sys/sem.h>
  177. #include <sys/shm.h>
  178. #endif
  179. #define DEBUG 0
  180. /* MARK code is not very useful, is bloat, and broken:
  181. * can deadlock if alarmed to make MARK while writing to IPC buffer
  182. * (semaphores are down but do_mark routine tries to down them again) */
  183. #undef SYSLOGD_MARK
  184. /* Write locking does not seem to be useful either */
  185. #undef SYSLOGD_WRLOCK
  186. enum {
  187. MAX_READ = CONFIG_FEATURE_SYSLOGD_READ_BUFFER_SIZE,
  188. DNS_WAIT_SEC = 2 * 60,
  189. };
  190. /* Semaphore operation structures */
  191. struct shbuf_ds {
  192. int32_t size; /* size of data - 1 */
  193. int32_t tail; /* end of message list */
  194. char data[1]; /* data/messages */
  195. };
  196. #if ENABLE_FEATURE_REMOTE_LOG
  197. typedef struct {
  198. int remoteFD;
  199. unsigned last_dns_resolve;
  200. len_and_sockaddr *remoteAddr;
  201. const char *remoteHostname;
  202. } remoteHost_t;
  203. #endif
  204. typedef struct logFile_t {
  205. const char *path;
  206. int fd;
  207. time_t last_log_time;
  208. #if ENABLE_FEATURE_ROTATE_LOGFILE
  209. unsigned size;
  210. uint8_t isRegular;
  211. #endif
  212. } logFile_t;
  213. #if ENABLE_FEATURE_SYSLOGD_CFG
  214. typedef struct logRule_t {
  215. uint8_t enabled_facility_priomap[LOG_NFACILITIES];
  216. struct logFile_t *file;
  217. struct logRule_t *next;
  218. } logRule_t;
  219. #endif
  220. /* Allows us to have smaller initializer. Ugly. */
  221. #define GLOBALS \
  222. logFile_t logFile; \
  223. /* interval between marks in seconds */ \
  224. /*int markInterval;*/ \
  225. /* level of messages to be logged */ \
  226. int logLevel; \
  227. IF_FEATURE_ROTATE_LOGFILE( \
  228. /* max size of file before rotation */ \
  229. unsigned logFileSize; \
  230. /* number of rotated message files */ \
  231. unsigned logFileRotate; \
  232. ) \
  233. IF_FEATURE_IPC_SYSLOG( \
  234. int shmid; /* ipc shared memory id */ \
  235. int s_semid; /* ipc semaphore id */ \
  236. int shm_size; \
  237. struct sembuf SMwup[1]; \
  238. struct sembuf SMwdn[3]; \
  239. ) \
  240. IF_FEATURE_SYSLOGD_CFG( \
  241. logRule_t *log_rules; \
  242. ) \
  243. IF_FEATURE_KMSG_SYSLOG( \
  244. int kmsgfd; \
  245. int primask; \
  246. )
  247. struct init_globals {
  248. GLOBALS
  249. };
  250. struct globals {
  251. GLOBALS
  252. #if ENABLE_FEATURE_REMOTE_LOG
  253. llist_t *remoteHosts;
  254. #endif
  255. #if ENABLE_FEATURE_IPC_SYSLOG
  256. struct shbuf_ds *shbuf;
  257. #endif
  258. /* localhost's name. We print only first 64 chars */
  259. char *hostname;
  260. /* We recv into recvbuf... */
  261. char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
  262. /* ...then copy to parsebuf, escaping control chars */
  263. /* (can grow x2 max) */
  264. char parsebuf[MAX_READ*2];
  265. /* ...then sprintf into printbuf, adding timestamp (15 or 19 chars),
  266. * host (64), fac.prio (20) to the message */
  267. /* (growth by: 15 + 64 + 20 + delims = ~110) */
  268. char printbuf[MAX_READ*2 + 128];
  269. };
  270. static const struct init_globals init_data = {
  271. .logFile = {
  272. .path = "/var/log/messages",
  273. .fd = -1,
  274. },
  275. #ifdef SYSLOGD_MARK
  276. .markInterval = 20 * 60,
  277. #endif
  278. .logLevel = 8,
  279. #if ENABLE_FEATURE_ROTATE_LOGFILE
  280. .logFileSize = 200 * 1024,
  281. .logFileRotate = 1,
  282. #endif
  283. #if ENABLE_FEATURE_IPC_SYSLOG
  284. .shmid = -1,
  285. .s_semid = -1,
  286. .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), /* default shm size */
  287. .SMwup = { {1, -1, IPC_NOWAIT} },
  288. .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
  289. #endif
  290. };
  291. #define G (*ptr_to_globals)
  292. #define INIT_G() do { \
  293. SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
  294. } while (0)
  295. /* Options */
  296. enum {
  297. OPTBIT_mark = 0, // -m
  298. OPTBIT_nofork, // -n
  299. OPTBIT_outfile, // -O
  300. OPTBIT_loglevel, // -l
  301. OPTBIT_small, // -S
  302. OPTBIT_timestamp, // -t
  303. IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
  304. IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
  305. IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
  306. IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
  307. IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
  308. IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
  309. IF_FEATURE_SYSLOGD_CFG( OPTBIT_cfg ,) // -f
  310. IF_FEATURE_KMSG_SYSLOG( OPTBIT_kmsg ,) // -K
  311. OPT_mark = 1 << OPTBIT_mark ,
  312. OPT_nofork = 1 << OPTBIT_nofork ,
  313. OPT_outfile = 1 << OPTBIT_outfile ,
  314. OPT_loglevel = 1 << OPTBIT_loglevel,
  315. OPT_small = 1 << OPTBIT_small ,
  316. OPT_timestamp = 1 << OPTBIT_timestamp,
  317. OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
  318. OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
  319. OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
  320. OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
  321. OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
  322. OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
  323. OPT_cfg = IF_FEATURE_SYSLOGD_CFG( (1 << OPTBIT_cfg )) + 0,
  324. OPT_kmsg = IF_FEATURE_KMSG_SYSLOG( (1 << OPTBIT_kmsg )) + 0,
  325. };
  326. #define OPTION_STR "m:nO:l:St" \
  327. IF_FEATURE_ROTATE_LOGFILE("s:" ) \
  328. IF_FEATURE_ROTATE_LOGFILE("b:" ) \
  329. IF_FEATURE_REMOTE_LOG( "R:*") \
  330. IF_FEATURE_REMOTE_LOG( "L" ) \
  331. IF_FEATURE_IPC_SYSLOG( "C::") \
  332. IF_FEATURE_SYSLOGD_DUP( "D" ) \
  333. IF_FEATURE_SYSLOGD_CFG( "f:" ) \
  334. IF_FEATURE_KMSG_SYSLOG( "K" )
  335. #define OPTION_DECL *opt_m, *opt_l \
  336. IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
  337. IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
  338. IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) \
  339. IF_FEATURE_SYSLOGD_CFG( ,*opt_f = NULL)
  340. #define OPTION_PARAM &opt_m, &(G.logFile.path), &opt_l \
  341. IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
  342. IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
  343. IF_FEATURE_REMOTE_LOG( ,&remoteAddrList) \
  344. IF_FEATURE_IPC_SYSLOG( ,&opt_C) \
  345. IF_FEATURE_SYSLOGD_CFG( ,&opt_f)
  346. #if ENABLE_FEATURE_SYSLOGD_CFG
  347. static const CODE* find_by_name(char *name, const CODE* c_set)
  348. {
  349. for (; c_set->c_name; c_set++) {
  350. if (strcmp(name, c_set->c_name) == 0)
  351. return c_set;
  352. }
  353. return NULL;
  354. }
  355. #endif
  356. static const CODE* find_by_val(int val, const CODE* c_set)
  357. {
  358. for (; c_set->c_name; c_set++) {
  359. if (c_set->c_val == val)
  360. return c_set;
  361. }
  362. return NULL;
  363. }
  364. #if ENABLE_FEATURE_SYSLOGD_CFG
  365. static void parse_syslogdcfg(const char *file)
  366. {
  367. char *t;
  368. logRule_t **pp_rule;
  369. /* tok[0] set of selectors */
  370. /* tok[1] file name */
  371. /* tok[2] has to be NULL */
  372. char *tok[3];
  373. parser_t *parser;
  374. parser = config_open2(file ? file : "/etc/syslog.conf",
  375. file ? xfopen_for_read : fopen_for_read);
  376. if (!parser)
  377. /* didn't find default /etc/syslog.conf */
  378. /* proceed as if we built busybox without config support */
  379. return;
  380. /* use ptr to ptr to avoid checking whether head was initialized */
  381. pp_rule = &G.log_rules;
  382. /* iterate through lines of config, skipping comments */
  383. while (config_read(parser, tok, 3, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
  384. char *cur_selector;
  385. logRule_t *cur_rule;
  386. /* unexpected trailing token? */
  387. if (tok[2])
  388. goto cfgerr;
  389. cur_rule = *pp_rule = xzalloc(sizeof(*cur_rule));
  390. cur_selector = tok[0];
  391. /* iterate through selectors: "kern.info;kern.!err;..." */
  392. do {
  393. const CODE *code;
  394. char *next_selector;
  395. uint8_t negated_prio; /* "kern.!err" */
  396. uint8_t single_prio; /* "kern.=err" */
  397. uint32_t facmap; /* bitmap of enabled facilities */
  398. uint8_t primap; /* bitmap of enabled priorities */
  399. unsigned i;
  400. next_selector = strchr(cur_selector, ';');
  401. if (next_selector)
  402. *next_selector++ = '\0';
  403. t = strchr(cur_selector, '.');
  404. if (!t)
  405. goto cfgerr;
  406. *t++ = '\0'; /* separate facility from priority */
  407. negated_prio = 0;
  408. single_prio = 0;
  409. if (*t == '!') {
  410. negated_prio = 1;
  411. ++t;
  412. }
  413. if (*t == '=') {
  414. single_prio = 1;
  415. ++t;
  416. }
  417. /* parse priority */
  418. if (*t == '*')
  419. primap = 0xff; /* all 8 log levels enabled */
  420. else {
  421. uint8_t priority;
  422. code = find_by_name(t, bb_prioritynames);
  423. if (!code)
  424. goto cfgerr;
  425. primap = 0;
  426. priority = code->c_val;
  427. if (priority == INTERNAL_NOPRI) {
  428. /* ensure we take "enabled_facility_priomap[fac] &= 0" branch below */
  429. negated_prio = 1;
  430. } else {
  431. priority = 1 << priority;
  432. do {
  433. primap |= priority;
  434. if (single_prio)
  435. break;
  436. priority >>= 1;
  437. } while (priority);
  438. if (negated_prio)
  439. primap = ~primap;
  440. }
  441. }
  442. /* parse facility */
  443. if (*cur_selector == '*')
  444. facmap = (1<<LOG_NFACILITIES) - 1;
  445. else {
  446. char *next_facility;
  447. facmap = 0;
  448. t = cur_selector;
  449. /* iterate through facilities: "kern,daemon.<priospec>" */
  450. do {
  451. next_facility = strchr(t, ',');
  452. if (next_facility)
  453. *next_facility++ = '\0';
  454. code = find_by_name(t, bb_facilitynames);
  455. if (!code)
  456. goto cfgerr;
  457. /* "mark" is not a real facility, skip it */
  458. if (code->c_val != INTERNAL_MARK)
  459. facmap |= 1<<(LOG_FAC(code->c_val));
  460. t = next_facility;
  461. } while (t);
  462. }
  463. /* merge result with previous selectors */
  464. for (i = 0; i < LOG_NFACILITIES; ++i) {
  465. if (!(facmap & (1<<i)))
  466. continue;
  467. if (negated_prio)
  468. cur_rule->enabled_facility_priomap[i] &= primap;
  469. else
  470. cur_rule->enabled_facility_priomap[i] |= primap;
  471. }
  472. cur_selector = next_selector;
  473. } while (cur_selector);
  474. /* check whether current file name was mentioned in previous rules or
  475. * as global logfile (G.logFile).
  476. */
  477. if (strcmp(G.logFile.path, tok[1]) == 0) {
  478. cur_rule->file = &G.logFile;
  479. goto found;
  480. }
  481. /* temporarily use cur_rule as iterator, but *pp_rule still points
  482. * to currently processing rule entry.
  483. * NOTE: *pp_rule points to the current (and last in the list) rule.
  484. */
  485. for (cur_rule = G.log_rules; cur_rule != *pp_rule; cur_rule = cur_rule->next) {
  486. if (strcmp(cur_rule->file->path, tok[1]) == 0) {
  487. /* found - reuse the same file structure */
  488. (*pp_rule)->file = cur_rule->file;
  489. cur_rule = *pp_rule;
  490. goto found;
  491. }
  492. }
  493. cur_rule->file = xzalloc(sizeof(*cur_rule->file));
  494. cur_rule->file->fd = -1;
  495. cur_rule->file->path = xstrdup(tok[1]);
  496. found:
  497. pp_rule = &cur_rule->next;
  498. }
  499. config_close(parser);
  500. return;
  501. cfgerr:
  502. bb_error_msg_and_die("error in '%s' at line %d",
  503. file ? file : "/etc/syslog.conf",
  504. parser->lineno);
  505. }
  506. #endif
  507. /* circular buffer variables/structures */
  508. #if ENABLE_FEATURE_IPC_SYSLOG
  509. #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
  510. #error Sorry, you must set the syslogd buffer size to at least 4KB.
  511. #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
  512. #endif
  513. /* our shared key (syslogd.c and logread.c must be in sync) */
  514. enum { KEY_ID = 0x414e4547 }; /* "GENA" */
  515. static void ipcsyslog_cleanup(void)
  516. {
  517. if (G.shmid != -1) {
  518. shmdt(G.shbuf);
  519. }
  520. if (G.shmid != -1) {
  521. shmctl(G.shmid, IPC_RMID, NULL);
  522. }
  523. if (G.s_semid != -1) {
  524. semctl(G.s_semid, 0, IPC_RMID, 0);
  525. }
  526. }
  527. static void ipcsyslog_init(void)
  528. {
  529. if (DEBUG)
  530. printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
  531. G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
  532. if (G.shmid == -1) {
  533. bb_simple_perror_msg_and_die("shmget");
  534. }
  535. G.shbuf = shmat(G.shmid, NULL, 0);
  536. if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
  537. bb_simple_perror_msg_and_die("shmat");
  538. }
  539. memset(G.shbuf, 0, G.shm_size);
  540. G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
  541. /*G.shbuf->tail = 0;*/
  542. /* we'll trust the OS to set initial semval to 0 (let's hope) */
  543. G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
  544. if (G.s_semid == -1) {
  545. if (errno == EEXIST) {
  546. G.s_semid = semget(KEY_ID, 2, 0);
  547. if (G.s_semid != -1)
  548. return;
  549. }
  550. bb_simple_perror_msg_and_die("semget");
  551. }
  552. }
  553. /* Write message to shared mem buffer */
  554. static void log_to_shmem(const char *msg)
  555. {
  556. int old_tail, new_tail;
  557. int len;
  558. if (semop(G.s_semid, G.SMwdn, 3) == -1) {
  559. bb_simple_perror_msg_and_die("SMwdn");
  560. }
  561. /* Circular Buffer Algorithm:
  562. * --------------------------
  563. * tail == position where to store next syslog message.
  564. * tail's max value is (shbuf->size - 1)
  565. * Last byte of buffer is never used and remains NUL.
  566. */
  567. len = strlen(msg) + 1; /* length with NUL included */
  568. again:
  569. old_tail = G.shbuf->tail;
  570. new_tail = old_tail + len;
  571. if (new_tail < G.shbuf->size) {
  572. /* store message, set new tail */
  573. memcpy(G.shbuf->data + old_tail, msg, len);
  574. G.shbuf->tail = new_tail;
  575. } else {
  576. /* k == available buffer space ahead of old tail */
  577. int k = G.shbuf->size - old_tail;
  578. /* copy what fits to the end of buffer, and repeat */
  579. memcpy(G.shbuf->data + old_tail, msg, k);
  580. msg += k;
  581. len -= k;
  582. G.shbuf->tail = 0;
  583. goto again;
  584. }
  585. if (semop(G.s_semid, G.SMwup, 1) == -1) {
  586. bb_simple_perror_msg_and_die("SMwup");
  587. }
  588. if (DEBUG)
  589. printf("tail:%d\n", G.shbuf->tail);
  590. }
  591. #else
  592. static void ipcsyslog_cleanup(void) {}
  593. static void ipcsyslog_init(void) {}
  594. void log_to_shmem(const char *msg);
  595. #endif /* FEATURE_IPC_SYSLOG */
  596. #if ENABLE_FEATURE_KMSG_SYSLOG
  597. static void kmsg_init(void)
  598. {
  599. G.kmsgfd = xopen("/dev/kmsg", O_WRONLY);
  600. /*
  601. * kernel < 3.5 expects single char printk KERN_* priority prefix,
  602. * from 3.5 onwards the full syslog facility/priority format is supported
  603. */
  604. if (get_linux_version_code() < KERNEL_VERSION(3,5,0))
  605. G.primask = LOG_PRIMASK;
  606. else
  607. G.primask = -1;
  608. }
  609. static void kmsg_cleanup(void)
  610. {
  611. if (ENABLE_FEATURE_CLEAN_UP)
  612. close(G.kmsgfd);
  613. }
  614. /* Write message to /dev/kmsg */
  615. static void log_to_kmsg(int pri, const char *msg)
  616. {
  617. /*
  618. * kernel < 3.5 expects single char printk KERN_* priority prefix,
  619. * from 3.5 onwards the full syslog facility/priority format is supported
  620. */
  621. pri &= G.primask;
  622. full_write(G.kmsgfd, G.printbuf, sprintf(G.printbuf, "<%d>%s\n", pri, msg));
  623. }
  624. #else
  625. static void kmsg_init(void) {}
  626. static void kmsg_cleanup(void) {}
  627. static void log_to_kmsg(int pri UNUSED_PARAM, const char *msg UNUSED_PARAM) {}
  628. #endif /* FEATURE_KMSG_SYSLOG */
  629. /* Print a message to the log file. */
  630. static void log_locally(time_t now, char *msg, logFile_t *log_file)
  631. {
  632. #ifdef SYSLOGD_WRLOCK
  633. struct flock fl;
  634. #endif
  635. int len = strlen(msg);
  636. /* fd can't be 0 (we connect fd 0 to /dev/log socket) */
  637. /* fd is 1 if "-O -" is in use */
  638. if (log_file->fd > 1) {
  639. /* Reopen log files every second. This allows admin
  640. * to delete the files and not worry about restarting us.
  641. * This costs almost nothing since it happens
  642. * _at most_ once a second for each file, and happens
  643. * only when each file is actually written.
  644. */
  645. if (!now)
  646. now = time(NULL);
  647. if (log_file->last_log_time != now) {
  648. log_file->last_log_time = now;
  649. close(log_file->fd);
  650. goto reopen;
  651. }
  652. }
  653. else if (log_file->fd == 1) {
  654. /* We are logging to stdout: do nothing */
  655. }
  656. else {
  657. if (LONE_DASH(log_file->path)) {
  658. log_file->fd = 1;
  659. /* log_file->isRegular = 0; - already is */
  660. } else {
  661. reopen:
  662. log_file->fd = open(log_file->path, O_WRONLY | O_CREAT
  663. | O_NOCTTY | O_APPEND | O_NONBLOCK,
  664. 0666);
  665. if (log_file->fd < 0) {
  666. /* cannot open logfile? - print to /dev/console then */
  667. int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
  668. if (fd < 0)
  669. fd = 2; /* then stderr, dammit */
  670. full_write(fd, msg, len);
  671. if (fd != 2)
  672. close(fd);
  673. return;
  674. }
  675. #if ENABLE_FEATURE_ROTATE_LOGFILE
  676. {
  677. struct stat statf;
  678. log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode));
  679. /* bug (mostly harmless): can wrap around if file > 4gb */
  680. log_file->size = statf.st_size;
  681. }
  682. #endif
  683. }
  684. }
  685. #ifdef SYSLOGD_WRLOCK
  686. fl.l_whence = SEEK_SET;
  687. fl.l_start = 0;
  688. fl.l_len = 1;
  689. fl.l_type = F_WRLCK;
  690. fcntl(log_file->fd, F_SETLKW, &fl);
  691. #endif
  692. #if ENABLE_FEATURE_ROTATE_LOGFILE
  693. if (G.logFileSize && log_file->isRegular && log_file->size > G.logFileSize) {
  694. if (G.logFileRotate) { /* always 0..99 */
  695. int i = strlen(log_file->path) + 3 + 1;
  696. char oldFile[i];
  697. char newFile[i];
  698. i = G.logFileRotate - 1;
  699. /* rename: f.8 -> f.9; f.7 -> f.8; ... */
  700. while (1) {
  701. sprintf(newFile, "%s.%d", log_file->path, i);
  702. if (i == 0) break;
  703. sprintf(oldFile, "%s.%d", log_file->path, --i);
  704. /* ignore errors - file might be missing */
  705. rename(oldFile, newFile);
  706. }
  707. /* newFile == "f.0" now */
  708. rename(log_file->path, newFile);
  709. }
  710. /* We may or may not have just renamed the file away;
  711. * if we didn't rename because we aren't keeping any backlog,
  712. * then it's time to clobber the file. If we did rename it...,
  713. * incredibly, if F and F.0 are hardlinks, POSIX _demands_
  714. * that rename returns 0 but does not remove F!!!
  715. * (hardlinked F/F.0 pair was observed after
  716. * power failure during rename()).
  717. * So ensure old file is gone in any case:
  718. */
  719. unlink(log_file->path);
  720. #ifdef SYSLOGD_WRLOCK
  721. fl.l_type = F_UNLCK;
  722. fcntl(log_file->fd, F_SETLKW, &fl);
  723. #endif
  724. close(log_file->fd);
  725. goto reopen;
  726. }
  727. /* TODO: what to do on write errors ("disk full")? */
  728. len = full_write(log_file->fd, msg, len);
  729. if (len > 0)
  730. log_file->size += len;
  731. #else
  732. full_write(log_file->fd, msg, len);
  733. #endif
  734. #ifdef SYSLOGD_WRLOCK
  735. fl.l_type = F_UNLCK;
  736. fcntl(log_file->fd, F_SETLKW, &fl);
  737. #endif
  738. }
  739. static void parse_fac_prio_20(int pri, char *res20)
  740. {
  741. const CODE *c_pri, *c_fac;
  742. c_fac = find_by_val(LOG_FAC(pri) << 3, bb_facilitynames);
  743. if (c_fac) {
  744. c_pri = find_by_val(LOG_PRI(pri), bb_prioritynames);
  745. if (c_pri) {
  746. snprintf(res20, 20, "%s.%s", c_fac->c_name, c_pri->c_name);
  747. return;
  748. }
  749. }
  750. snprintf(res20, 20, "<%d>", pri);
  751. }
  752. /* len parameter is used only for "is there a timestamp?" check.
  753. * NB: some callers cheat and supply len==0 when they know
  754. * that there is no timestamp, short-circuiting the test. */
  755. static void timestamp_and_log(int pri, char *msg, int len)
  756. {
  757. char *timestamp = NULL;
  758. time_t now;
  759. /* Jan 18 00:11:22 msg... */
  760. /* 01234567890123456 */
  761. if (len >= 16 && msg[3] == ' ' && msg[6] == ' '
  762. && msg[9] == ':' && msg[12] == ':' && msg[15] == ' '
  763. ) {
  764. if (!(option_mask32 & OPT_timestamp)) {
  765. /* use message timestamp */
  766. timestamp = msg;
  767. now = 0;
  768. }
  769. msg += 16;
  770. }
  771. #if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
  772. if (!timestamp) {
  773. struct timeval tv;
  774. xgettimeofday(&tv);
  775. now = tv.tv_sec;
  776. timestamp = ctime(&now) + 4; /* skip day of week */
  777. /* overwrite year by milliseconds, zero terminate */
  778. sprintf(timestamp + 15, ".%03u", (unsigned)tv.tv_usec / 1000u);
  779. } else {
  780. timestamp[15] = '\0';
  781. }
  782. #else
  783. if (!timestamp) {
  784. time(&now);
  785. timestamp = ctime(&now) + 4; /* skip day of week */
  786. }
  787. timestamp[15] = '\0';
  788. #endif
  789. if (option_mask32 & OPT_kmsg) {
  790. log_to_kmsg(pri, msg);
  791. return;
  792. }
  793. if (option_mask32 & OPT_small)
  794. sprintf(G.printbuf, "%s %s\n", timestamp, msg);
  795. else {
  796. char res[20];
  797. parse_fac_prio_20(pri, res);
  798. sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
  799. }
  800. /* Log message locally (to file or shared mem) */
  801. #if ENABLE_FEATURE_SYSLOGD_CFG
  802. {
  803. bool match = 0;
  804. logRule_t *rule;
  805. uint8_t facility = LOG_FAC(pri);
  806. uint8_t prio_bit = 1 << LOG_PRI(pri);
  807. for (rule = G.log_rules; rule; rule = rule->next) {
  808. if (rule->enabled_facility_priomap[facility] & prio_bit) {
  809. log_locally(now, G.printbuf, rule->file);
  810. match = 1;
  811. }
  812. }
  813. if (match)
  814. return;
  815. }
  816. #endif
  817. if (LOG_PRI(pri) < G.logLevel) {
  818. #if ENABLE_FEATURE_IPC_SYSLOG
  819. if ((option_mask32 & OPT_circularlog) && G.shbuf) {
  820. log_to_shmem(G.printbuf);
  821. return;
  822. }
  823. #endif
  824. log_locally(now, G.printbuf, &G.logFile);
  825. }
  826. }
  827. static void timestamp_and_log_internal(const char *msg)
  828. {
  829. /* -L, or no -R */
  830. if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
  831. return;
  832. timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
  833. }
  834. /* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
  835. * embedded NULs. Split messages on each of these NULs, parse prio,
  836. * escape control chars and log each locally. */
  837. static void split_escape_and_log(char *tmpbuf, int len)
  838. {
  839. char *p = tmpbuf;
  840. tmpbuf += len;
  841. while (p < tmpbuf) {
  842. char c;
  843. char *q = G.parsebuf;
  844. int pri = (LOG_USER | LOG_NOTICE);
  845. if (*p == '<') {
  846. /* Parse the magic priority number */
  847. pri = bb_strtou(p + 1, &p, 10);
  848. if (*p == '>')
  849. p++;
  850. if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
  851. pri = (LOG_USER | LOG_NOTICE);
  852. }
  853. while ((c = *p++)) {
  854. if (c == '\n')
  855. c = ' ';
  856. if (!(c & ~0x1f) && c != '\t') {
  857. *q++ = '^';
  858. c += '@'; /* ^@, ^A, ^B... */
  859. }
  860. *q++ = c;
  861. }
  862. *q = '\0';
  863. /* Now log it */
  864. timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
  865. }
  866. }
  867. #ifdef SYSLOGD_MARK
  868. static void do_mark(int sig)
  869. {
  870. if (G.markInterval) {
  871. timestamp_and_log_internal("-- MARK --");
  872. alarm(G.markInterval);
  873. }
  874. }
  875. #endif
  876. static int create_socket(void)
  877. {
  878. struct sockaddr_un sunx;
  879. int sock_fd;
  880. char *dev_log_name;
  881. memset(&sunx, 0, sizeof(sunx));
  882. sunx.sun_family = AF_UNIX;
  883. /* Unlink old /dev/log or object it points to. */
  884. /* (if it exists, bind will fail) */
  885. strcpy(sunx.sun_path, _PATH_LOG);
  886. dev_log_name = xmalloc_follow_symlinks(_PATH_LOG);
  887. if (dev_log_name) {
  888. safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
  889. free(dev_log_name);
  890. }
  891. unlink(sunx.sun_path);
  892. sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
  893. xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
  894. chmod(_PATH_LOG, 0666);
  895. return sock_fd;
  896. }
  897. #if ENABLE_FEATURE_REMOTE_LOG
  898. static int try_to_resolve_remote(remoteHost_t *rh)
  899. {
  900. if (!rh->remoteAddr) {
  901. unsigned now = monotonic_sec();
  902. /* Don't resolve name too often - DNS timeouts can be big */
  903. if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC)
  904. return -1;
  905. rh->last_dns_resolve = now;
  906. rh->remoteAddr = host2sockaddr(rh->remoteHostname, 514);
  907. if (!rh->remoteAddr)
  908. return -1;
  909. }
  910. return xsocket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
  911. }
  912. #endif
  913. /* By doing init in a separate function we decrease stack usage
  914. * in main loop.
  915. */
  916. static int NOINLINE syslogd_init(char **argv)
  917. {
  918. int opts;
  919. int fd;
  920. char OPTION_DECL;
  921. #if ENABLE_FEATURE_REMOTE_LOG
  922. llist_t *remoteAddrList = NULL;
  923. #endif
  924. /* No non-option params */
  925. opts = getopt32(argv, "^"OPTION_STR"\0""=0", OPTION_PARAM);
  926. #if ENABLE_FEATURE_REMOTE_LOG
  927. while (remoteAddrList) {
  928. remoteHost_t *rh = xzalloc(sizeof(*rh));
  929. rh->remoteHostname = llist_pop(&remoteAddrList);
  930. rh->remoteFD = -1;
  931. rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
  932. llist_add_to(&G.remoteHosts, rh);
  933. }
  934. #endif
  935. #ifdef SYSLOGD_MARK
  936. if (opts & OPT_mark) // -m
  937. G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
  938. #endif
  939. //if (opts & OPT_nofork) // -n
  940. //if (opts & OPT_outfile) // -O
  941. if (opts & OPT_loglevel) // -l
  942. G.logLevel = xatou_range(opt_l, 1, 8);
  943. //if (opts & OPT_small) // -S
  944. #if ENABLE_FEATURE_ROTATE_LOGFILE
  945. if (opts & OPT_filesize) // -s
  946. G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
  947. if (opts & OPT_rotatecnt) // -b
  948. G.logFileRotate = xatou_range(opt_b, 0, 99);
  949. #endif
  950. #if ENABLE_FEATURE_IPC_SYSLOG
  951. if (opt_C) // -Cn
  952. G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
  953. #endif
  954. /* If they have not specified remote logging, then log locally */
  955. if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
  956. option_mask32 |= OPT_locallog;
  957. #if ENABLE_FEATURE_SYSLOGD_CFG
  958. parse_syslogdcfg(opt_f);
  959. #endif
  960. /* Store away localhost's name before the fork */
  961. G.hostname = safe_gethostname();
  962. *strchrnul(G.hostname, '.') = '\0';
  963. fd = create_socket();
  964. if (opts & OPT_circularlog)
  965. ipcsyslog_init();
  966. if (opts & OPT_kmsg)
  967. kmsg_init();
  968. if (!(opts & OPT_nofork)) {
  969. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  970. }
  971. xmove_fd(fd, STDIN_FILENO);
  972. /* Set up signal handlers (so that they interrupt read()) */
  973. signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
  974. signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
  975. //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
  976. signal(SIGHUP, SIG_IGN);
  977. #ifdef SYSLOGD_MARK
  978. signal(SIGALRM, do_mark);
  979. alarm(G.markInterval);
  980. #endif
  981. return opts;
  982. }
  983. int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  984. int syslogd_main(int argc UNUSED_PARAM, char **argv)
  985. {
  986. int opts;
  987. #if ENABLE_FEATURE_REMOTE_LOG
  988. llist_t *item;
  989. #endif
  990. #if ENABLE_FEATURE_SYSLOGD_DUP
  991. int last_sz = -1;
  992. char *last_buf;
  993. char *recvbuf;
  994. #else
  995. #define recvbuf (G.recvbuf)
  996. #endif
  997. INIT_G();
  998. opts = syslogd_init(argv);
  999. timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
  1000. write_pidfile_std_path_and_ext("syslogd");
  1001. #if ENABLE_FEATURE_SYSLOGD_DUP
  1002. recvbuf = G.recvbuf;
  1003. #endif
  1004. while (!bb_got_signal) {
  1005. ssize_t sz;
  1006. #if ENABLE_FEATURE_SYSLOGD_DUP
  1007. last_buf = recvbuf;
  1008. if (recvbuf == G.recvbuf)
  1009. recvbuf = G.recvbuf + MAX_READ;
  1010. else
  1011. recvbuf = G.recvbuf;
  1012. #endif
  1013. read_again:
  1014. sz = read(STDIN_FILENO, recvbuf, MAX_READ - 1);
  1015. if (sz < 0) {
  1016. if (!bb_got_signal)
  1017. bb_perror_msg("read from %s", _PATH_LOG);
  1018. break;
  1019. }
  1020. /* Drop trailing '\n' and NULs (typically there is one NUL) */
  1021. while (1) {
  1022. if (sz == 0)
  1023. goto read_again;
  1024. /* man 3 syslog says: "A trailing newline is added when needed".
  1025. * However, neither glibc nor uclibc do this:
  1026. * syslog(prio, "test") sends "test\0" to /dev/log,
  1027. * syslog(prio, "test\n") sends "test\n\0".
  1028. * IOW: newline is passed verbatim!
  1029. * I take it to mean that it's syslogd's job
  1030. * to make those look identical in the log files. */
  1031. if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
  1032. break;
  1033. sz--;
  1034. }
  1035. #if ENABLE_FEATURE_SYSLOGD_DUP
  1036. if ((opts & OPT_dup) && (sz == last_sz))
  1037. if (memcmp(last_buf, recvbuf, sz) == 0)
  1038. continue;
  1039. last_sz = sz;
  1040. #endif
  1041. #if ENABLE_FEATURE_REMOTE_LOG
  1042. /* Stock syslogd sends it '\n'-terminated
  1043. * over network, mimic that */
  1044. recvbuf[sz] = '\n';
  1045. /* We are not modifying log messages in any way before send */
  1046. /* Remote site cannot trust _us_ anyway and need to do validation again */
  1047. for (item = G.remoteHosts; item != NULL; item = item->link) {
  1048. remoteHost_t *rh = (remoteHost_t *)item->data;
  1049. if (rh->remoteFD == -1) {
  1050. rh->remoteFD = try_to_resolve_remote(rh);
  1051. if (rh->remoteFD == -1)
  1052. continue;
  1053. }
  1054. /* Send message to remote logger.
  1055. * On some errors, close and set remoteFD to -1
  1056. * so that DNS resolution is retried.
  1057. */
  1058. if (sendto(rh->remoteFD, recvbuf, sz+1,
  1059. MSG_DONTWAIT | MSG_NOSIGNAL,
  1060. &(rh->remoteAddr->u.sa), rh->remoteAddr->len) == -1
  1061. ) {
  1062. switch (errno) {
  1063. case ECONNRESET:
  1064. case ENOTCONN: /* paranoia */
  1065. case EPIPE:
  1066. close(rh->remoteFD);
  1067. rh->remoteFD = -1;
  1068. free(rh->remoteAddr);
  1069. rh->remoteAddr = NULL;
  1070. }
  1071. }
  1072. }
  1073. #endif
  1074. if (!ENABLE_FEATURE_REMOTE_LOG || (opts & OPT_locallog)) {
  1075. recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
  1076. split_escape_and_log(recvbuf, sz);
  1077. }
  1078. } /* while (!bb_got_signal) */
  1079. timestamp_and_log_internal("syslogd exiting");
  1080. remove_pidfile_std_path_and_ext("syslogd");
  1081. ipcsyslog_cleanup();
  1082. if (opts & OPT_kmsg)
  1083. kmsg_cleanup();
  1084. kill_myself_with_sig(bb_got_signal);
  1085. #undef recvbuf
  1086. }
  1087. /* Clean up. Needed because we are included from syslogd_and_logger.c */
  1088. #undef DEBUG
  1089. #undef SYSLOGD_MARK
  1090. #undef SYSLOGD_WRLOCK
  1091. #undef G
  1092. #undef GLOBALS
  1093. #undef INIT_G
  1094. #undef OPTION_STR
  1095. #undef OPTION_DECL
  1096. #undef OPTION_PARAM