syslogd.c 33 KB

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