syslogd.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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"
  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, 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, 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, facilitynames);
  733. if (c_fac) {
  734. c_pri = find_by_val(LOG_PRI(pri), 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, -R can occur multiple times */
  1009. opt_complementary = "=0" IF_FEATURE_REMOTE_LOG(":R::");
  1010. opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
  1011. #if ENABLE_FEATURE_REMOTE_LOG
  1012. while (remoteAddrList) {
  1013. remoteHost_t *rh = xzalloc(sizeof(*rh));
  1014. rh->remoteHostname = llist_pop(&remoteAddrList);
  1015. rh->remoteFD = -1;
  1016. rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
  1017. llist_add_to(&G.remoteHosts, rh);
  1018. }
  1019. #endif
  1020. #ifdef SYSLOGD_MARK
  1021. if (opts & OPT_mark) // -m
  1022. G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
  1023. #endif
  1024. //if (opts & OPT_nofork) // -n
  1025. //if (opts & OPT_outfile) // -O
  1026. if (opts & OPT_loglevel) // -l
  1027. G.logLevel = xatou_range(opt_l, 1, 8);
  1028. //if (opts & OPT_small) // -S
  1029. #if ENABLE_FEATURE_ROTATE_LOGFILE
  1030. if (opts & OPT_filesize) // -s
  1031. G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
  1032. if (opts & OPT_rotatecnt) // -b
  1033. G.logFileRotate = xatou_range(opt_b, 0, 99);
  1034. #endif
  1035. #if ENABLE_FEATURE_IPC_SYSLOG
  1036. if (opt_C) // -Cn
  1037. G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
  1038. #endif
  1039. /* If they have not specified remote logging, then log locally */
  1040. if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
  1041. option_mask32 |= OPT_locallog;
  1042. #if ENABLE_FEATURE_SYSLOGD_CFG
  1043. parse_syslogdcfg(opt_f);
  1044. #endif
  1045. /* Store away localhost's name before the fork */
  1046. G.hostname = safe_gethostname();
  1047. *strchrnul(G.hostname, '.') = '\0';
  1048. if (!(opts & OPT_nofork)) {
  1049. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  1050. }
  1051. //umask(0); - why??
  1052. write_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid");
  1053. do_syslogd();
  1054. /* return EXIT_SUCCESS; */
  1055. }
  1056. /* Clean up. Needed because we are included from syslogd_and_logger.c */
  1057. #undef DEBUG
  1058. #undef SYSLOGD_MARK
  1059. #undef SYSLOGD_WRLOCK
  1060. #undef G
  1061. #undef GLOBALS
  1062. #undef INIT_G
  1063. #undef OPTION_STR
  1064. #undef OPTION_DECL
  1065. #undef OPTION_PARAM