syslogd.c 32 KB

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