aboutsummaryrefslogtreecommitdiff
path: root/sysklogd/syslogd.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysklogd/syslogd.c')
-rw-r--r--sysklogd/syslogd.c164
1 files changed, 85 insertions, 79 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 6ddfd771a..83b5c0cf6 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -13,7 +13,7 @@
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 */ 14 */
15//config:config SYSLOGD 15//config:config SYSLOGD
16//config: bool "syslogd (13 kb)" 16//config: bool "syslogd (14 kb)"
17//config: default y 17//config: default y
18//config: help 18//config: help
19//config: The syslogd utility is used to record logs of all the 19//config: The syslogd utility is used to record logs of all the
@@ -1002,20 +1002,71 @@ static int try_to_resolve_remote(remoteHost_t *rh)
1002} 1002}
1003#endif 1003#endif
1004 1004
1005static void do_syslogd(void) NORETURN; 1005/* By doing init in a separate function we decrease stack usage
1006static void do_syslogd(void) 1006 * in main loop.
1007 */
1008static int NOINLINE syslogd_init(char **argv)
1007{ 1009{
1010 int opts;
1011 char OPTION_DECL;
1008#if ENABLE_FEATURE_REMOTE_LOG 1012#if ENABLE_FEATURE_REMOTE_LOG
1009 llist_t *item; 1013 llist_t *remoteAddrList = NULL;
1010#endif 1014#endif
1011#if ENABLE_FEATURE_SYSLOGD_DUP 1015
1012 int last_sz = -1; 1016 /* No non-option params */
1013 char *last_buf; 1017 opts = getopt32(argv, "^"OPTION_STR"\0""=0", OPTION_PARAM);
1014 char *recvbuf = G.recvbuf; 1018#if ENABLE_FEATURE_REMOTE_LOG
1015#else 1019 while (remoteAddrList) {
1016#define recvbuf (G.recvbuf) 1020 remoteHost_t *rh = xzalloc(sizeof(*rh));
1021 rh->remoteHostname = llist_pop(&remoteAddrList);
1022 rh->remoteFD = -1;
1023 rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
1024 llist_add_to(&G.remoteHosts, rh);
1025 }
1026#endif
1027
1028#ifdef SYSLOGD_MARK
1029 if (opts & OPT_mark) // -m
1030 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
1031#endif
1032 //if (opts & OPT_nofork) // -n
1033 //if (opts & OPT_outfile) // -O
1034 if (opts & OPT_loglevel) // -l
1035 G.logLevel = xatou_range(opt_l, 1, 8);
1036 //if (opts & OPT_small) // -S
1037#if ENABLE_FEATURE_ROTATE_LOGFILE
1038 if (opts & OPT_filesize) // -s
1039 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
1040 if (opts & OPT_rotatecnt) // -b
1041 G.logFileRotate = xatou_range(opt_b, 0, 99);
1042#endif
1043#if ENABLE_FEATURE_IPC_SYSLOG
1044 if (opt_C) // -Cn
1045 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
1046#endif
1047 /* If they have not specified remote logging, then log locally */
1048 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
1049 option_mask32 |= OPT_locallog;
1050#if ENABLE_FEATURE_SYSLOGD_CFG
1051 parse_syslogdcfg(opt_f);
1017#endif 1052#endif
1018 1053
1054 /* Store away localhost's name before the fork */
1055 G.hostname = safe_gethostname();
1056 *strchrnul(G.hostname, '.') = '\0';
1057
1058 xmove_fd(create_socket(), STDIN_FILENO);
1059
1060 if (opts & OPT_circularlog)
1061 ipcsyslog_init();
1062
1063 if (opts & OPT_kmsg)
1064 kmsg_init();
1065
1066 if (!(opts & OPT_nofork)) {
1067 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
1068 }
1069
1019 /* Set up signal handlers (so that they interrupt read()) */ 1070 /* Set up signal handlers (so that they interrupt read()) */
1020 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo); 1071 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
1021 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); 1072 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
@@ -1025,17 +1076,33 @@ static void do_syslogd(void)
1025 signal(SIGALRM, do_mark); 1076 signal(SIGALRM, do_mark);
1026 alarm(G.markInterval); 1077 alarm(G.markInterval);
1027#endif 1078#endif
1028 xmove_fd(create_socket(), STDIN_FILENO); 1079 return opts;
1080}
1029 1081
1030 if (option_mask32 & OPT_circularlog) 1082int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1031 ipcsyslog_init(); 1083int syslogd_main(int argc UNUSED_PARAM, char **argv)
1084{
1085 int opts;
1086#if ENABLE_FEATURE_REMOTE_LOG
1087 llist_t *item;
1088#endif
1089#if ENABLE_FEATURE_SYSLOGD_DUP
1090 int last_sz = -1;
1091 char *last_buf;
1092 char *recvbuf;
1093#else
1094#define recvbuf (G.recvbuf)
1095#endif
1032 1096
1033 if (option_mask32 & OPT_kmsg) 1097 INIT_G();
1034 kmsg_init(); 1098 opts = syslogd_init(argv);
1035 1099
1036 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER); 1100 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
1037 write_pidfile_std_path_and_ext("syslogd"); 1101 write_pidfile_std_path_and_ext("syslogd");
1038 1102
1103#if ENABLE_FEATURE_SYSLOGD_DUP
1104 recvbuf = G.recvbuf;
1105#endif
1039 while (!bb_got_signal) { 1106 while (!bb_got_signal) {
1040 ssize_t sz; 1107 ssize_t sz;
1041 1108
@@ -1070,7 +1137,7 @@ static void do_syslogd(void)
1070 sz--; 1137 sz--;
1071 } 1138 }
1072#if ENABLE_FEATURE_SYSLOGD_DUP 1139#if ENABLE_FEATURE_SYSLOGD_DUP
1073 if ((option_mask32 & OPT_dup) && (sz == last_sz)) 1140 if ((opts & OPT_dup) && (sz == last_sz))
1074 if (memcmp(last_buf, recvbuf, sz) == 0) 1141 if (memcmp(last_buf, recvbuf, sz) == 0)
1075 continue; 1142 continue;
1076 last_sz = sz; 1143 last_sz = sz;
@@ -1111,7 +1178,7 @@ static void do_syslogd(void)
1111 } 1178 }
1112 } 1179 }
1113#endif 1180#endif
1114 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { 1181 if (!ENABLE_FEATURE_REMOTE_LOG || (opts & OPT_locallog)) {
1115 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */ 1182 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
1116 split_escape_and_log(recvbuf, sz); 1183 split_escape_and_log(recvbuf, sz);
1117 } 1184 }
@@ -1120,73 +1187,12 @@ static void do_syslogd(void)
1120 timestamp_and_log_internal("syslogd exiting"); 1187 timestamp_and_log_internal("syslogd exiting");
1121 remove_pidfile_std_path_and_ext("syslogd"); 1188 remove_pidfile_std_path_and_ext("syslogd");
1122 ipcsyslog_cleanup(); 1189 ipcsyslog_cleanup();
1123 if (option_mask32 & OPT_kmsg) 1190 if (opts & OPT_kmsg)
1124 kmsg_cleanup(); 1191 kmsg_cleanup();
1125 kill_myself_with_sig(bb_got_signal); 1192 kill_myself_with_sig(bb_got_signal);
1126#undef recvbuf 1193#undef recvbuf
1127} 1194}
1128 1195
1129int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1130int syslogd_main(int argc UNUSED_PARAM, char **argv)
1131{
1132 int opts;
1133 char OPTION_DECL;
1134#if ENABLE_FEATURE_REMOTE_LOG
1135 llist_t *remoteAddrList = NULL;
1136#endif
1137
1138 INIT_G();
1139
1140 /* No non-option params */
1141 opts = getopt32(argv, "^"OPTION_STR"\0""=0", OPTION_PARAM);
1142#if ENABLE_FEATURE_REMOTE_LOG
1143 while (remoteAddrList) {
1144 remoteHost_t *rh = xzalloc(sizeof(*rh));
1145 rh->remoteHostname = llist_pop(&remoteAddrList);
1146 rh->remoteFD = -1;
1147 rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
1148 llist_add_to(&G.remoteHosts, rh);
1149 }
1150#endif
1151
1152#ifdef SYSLOGD_MARK
1153 if (opts & OPT_mark) // -m
1154 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
1155#endif
1156 //if (opts & OPT_nofork) // -n
1157 //if (opts & OPT_outfile) // -O
1158 if (opts & OPT_loglevel) // -l
1159 G.logLevel = xatou_range(opt_l, 1, 8);
1160 //if (opts & OPT_small) // -S
1161#if ENABLE_FEATURE_ROTATE_LOGFILE
1162 if (opts & OPT_filesize) // -s
1163 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
1164 if (opts & OPT_rotatecnt) // -b
1165 G.logFileRotate = xatou_range(opt_b, 0, 99);
1166#endif
1167#if ENABLE_FEATURE_IPC_SYSLOG
1168 if (opt_C) // -Cn
1169 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
1170#endif
1171 /* If they have not specified remote logging, then log locally */
1172 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
1173 option_mask32 |= OPT_locallog;
1174#if ENABLE_FEATURE_SYSLOGD_CFG
1175 parse_syslogdcfg(opt_f);
1176#endif
1177
1178 /* Store away localhost's name before the fork */
1179 G.hostname = safe_gethostname();
1180 *strchrnul(G.hostname, '.') = '\0';
1181
1182 if (!(opts & OPT_nofork)) {
1183 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
1184 }
1185
1186 do_syslogd();
1187 /* return EXIT_SUCCESS; */
1188}
1189
1190/* Clean up. Needed because we are included from syslogd_and_logger.c */ 1196/* Clean up. Needed because we are included from syslogd_and_logger.c */
1191#undef DEBUG 1197#undef DEBUG
1192#undef SYSLOGD_MARK 1198#undef SYSLOGD_MARK