From: Denys Vlasenko Date: Sun, 17 Mar 2019 18:47:52 +0000 (+0100) Subject: do not duplicate CONFIG_PID_FILE_PATH and ".pid" strings X-Git-Tag: 1_31_0~162 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5059653882dbd86e3bbf48389f9f81b0fac8cd0a;p=oweals%2Fbusybox.git do not duplicate CONFIG_PID_FILE_PATH and ".pid" strings text data bss dec hex filename 981737 485 7296 989518 f194e busybox_old 981704 485 7296 989485 f192d busybox_unstripped Signed-off-by: Denys Vlasenko --- diff --git a/include/libbb.h b/include/libbb.h index 7a1e13875..e0d5521e4 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1298,9 +1298,13 @@ llist_t *llist_find_str(llist_t *first, const char *str) FAST_FUNC; /* True only if we created pidfile which is *file*, not /dev/null etc */ extern smallint wrote_pidfile; void write_pidfile(const char *path) FAST_FUNC; +void write_pidfile_std_path_and_ext(const char *path) FAST_FUNC; +void remove_pidfile_std_path_and_ext(const char *path) FAST_FUNC; #define remove_pidfile(path) do { if (wrote_pidfile) unlink(path); } while (0) #else enum { wrote_pidfile = 0 }; +#define write_pidfile_std_path_and_ext(path) ((void)0) +#define remove_pidfile_std_path_and_ext(path) ((void)0) #define write_pidfile(path) ((void)0) #define remove_pidfile(path) ((void)0) #endif diff --git a/libbb/pidfile.c b/libbb/pidfile.c index a48dfc38b..a6a282562 100644 --- a/libbb/pidfile.c +++ b/libbb/pidfile.c @@ -38,3 +38,21 @@ void FAST_FUNC write_pidfile(const char *path) } close(pid_fd); } + +void FAST_FUNC write_pidfile_std_path_and_ext(const char *name) +{ + char buf[sizeof(CONFIG_PID_FILE_PATH) + 64]; + + snprintf(buf, sizeof(buf), CONFIG_PID_FILE_PATH"/%s.pid", name); + write_pidfile(buf); +} + +void FAST_FUNC remove_pidfile_std_path_and_ext(const char *name) +{ + char buf[sizeof(CONFIG_PID_FILE_PATH) + 64]; + + if (!wrote_pidfile) + return; + snprintf(buf, sizeof(buf), CONFIG_PID_FILE_PATH"/%s.pid", name); + unlink(buf); +} diff --git a/miscutils/crond.c b/miscutils/crond.c index 2e36c406b..25e5503c7 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -1056,7 +1056,7 @@ int crond_main(int argc UNUSED_PARAM, char **argv) log8("crond (busybox "BB_VER") started, log level %d", G.log_level); rescan_crontab_dir(); - write_pidfile(CONFIG_PID_FILE_PATH "/crond.pid"); + write_pidfile_std_path_and_ext("crond"); #if ENABLE_FEATURE_CROND_SPECIAL_TIMES if (touch_reboot_file()) start_jobs(START_ME_REBOOT); /* start @reboot entries, if any */ diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c index 1e9ecc5e8..86600b72f 100644 --- a/miscutils/watchdog.c +++ b/miscutils/watchdog.c @@ -64,7 +64,7 @@ static void shutdown_watchdog(void) static void shutdown_on_signal(int sig UNUSED_PARAM) { - remove_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid"); + remove_pidfile_std_path_and_ext("watchdog"); shutdown_watchdog(); _exit(EXIT_SUCCESS); } @@ -136,7 +136,7 @@ int watchdog_main(int argc UNUSED_PARAM, char **argv) stimer_duration, htimer_duration * 1000); #endif - write_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid"); + write_pidfile_std_path_and_ext("watchdog"); while (1) { /* diff --git a/networking/inetd.c b/networking/inetd.c index 8f871ee12..da6551174 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -1207,7 +1207,7 @@ static void clean_up_and_exit(int sig UNUSED_PARAM) if (ENABLE_FEATURE_CLEAN_UP) close(sep->se_fd); } - remove_pidfile(CONFIG_PID_FILE_PATH "/inetd.pid"); + remove_pidfile_std_path_and_ext("inetd"); exit(EXIT_SUCCESS); } @@ -1256,7 +1256,7 @@ int inetd_main(int argc UNUSED_PARAM, char **argv) setgroups(1, &gid); } - write_pidfile(CONFIG_PID_FILE_PATH "/inetd.pid"); + write_pidfile_std_path_and_ext("inetd"); /* never fails under Linux (except if you pass it bad arguments) */ getrlimit(RLIMIT_NOFILE, &rlim_ofile); diff --git a/networking/ntpd.c b/networking/ntpd.c index 0f474bc09..b2e77929e 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -2641,7 +2641,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) */ cnt = G.peer_cnt * (INITIAL_SAMPLES + 1); - write_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid"); + write_pidfile_std_path_and_ext("ntpd"); while (!bb_got_signal) { llist_t *item; @@ -2814,7 +2814,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) } } /* while (!bb_got_signal) */ - remove_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid"); + remove_pidfile_std_path_and_ext("ntpd"); kill_myself_with_sig(bb_got_signal); } diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 76eccf1da..17b6ca235 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -231,7 +231,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) syslog(LOG_NOTICE, "klogd started: %s", bb_banner); - write_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid"); + write_pidfile_std_path_and_ext("klogd"); used = 0; while (!bb_got_signal) { @@ -295,7 +295,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv) klogd_close(); syslog(LOG_NOTICE, "klogd: exiting"); - remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid"); + remove_pidfile_std_path_and_ext("klogd"); if (bb_got_signal) kill_myself_with_sig(bb_got_signal); return EXIT_FAILURE; diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index f12359476..d0dd1bd20 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -1098,7 +1098,7 @@ static void do_syslogd(void) } /* while (!bb_got_signal) */ timestamp_and_log_internal("syslogd exiting"); - remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); + remove_pidfile_std_path_and_ext("syslogd"); ipcsyslog_cleanup(); if (option_mask32 & OPT_kmsg) kmsg_cleanup(); @@ -1164,7 +1164,7 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv) } //umask(0); - why?? - write_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); + write_pidfile_std_path_and_ext("syslogd"); do_syslogd(); /* return EXIT_SUCCESS; */