syslogd.c 32 KB

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