aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-06-13 16:16:37 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-06-13 16:24:56 +0200
commit02378ce20c6d2df062357b6d60fc440609d203be (patch)
tree10a011ca71b56bca23e8e10762fef1703ced544d
parentc1d7507a4d36bbeba88b09e8f0269fe81e1cb33f (diff)
downloadbusybox-w32-02378ce20c6d2df062357b6d60fc440609d203be.tar.gz
busybox-w32-02378ce20c6d2df062357b6d60fc440609d203be.tar.bz2
busybox-w32-02378ce20c6d2df062357b6d60fc440609d203be.zip
syslogd: decrease stack usage, ~50 bytes
function old new delta syslogd_init - 1007 +1007 syslogd_main 1619 636 -983 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 1007/-983) Total: 24 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--sysklogd/syslogd.c156
1 files changed, 81 insertions, 75 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 6ddfd771a..20034e969 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -1002,20 +1002,63 @@ 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 }
1017#endif 1026#endif
1018 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);
1052#endif
1053
1054 /* Store away localhost's name before the fork */
1055 G.hostname = safe_gethostname();
1056 *strchrnul(G.hostname, '.') = '\0';
1057
1058 if (!(opts & OPT_nofork)) {
1059 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
1060 }
1061
1019 /* Set up signal handlers (so that they interrupt read()) */ 1062 /* Set up signal handlers (so that they interrupt read()) */
1020 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo); 1063 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
1021 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); 1064 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
@@ -1027,15 +1070,39 @@ static void do_syslogd(void)
1027#endif 1070#endif
1028 xmove_fd(create_socket(), STDIN_FILENO); 1071 xmove_fd(create_socket(), STDIN_FILENO);
1029 1072
1030 if (option_mask32 & OPT_circularlog) 1073 if (opts & OPT_circularlog)
1031 ipcsyslog_init(); 1074 ipcsyslog_init();
1032 1075
1033 if (option_mask32 & OPT_kmsg) 1076 if (opts & OPT_kmsg)
1034 kmsg_init(); 1077 kmsg_init();
1035 1078
1079 return opts;
1080}
1081
1082int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
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
1096
1097 INIT_G();
1098 opts = syslogd_init(argv);
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