syslogd.c 33 KB

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