do not duplicate CONFIG_PID_FILE_PATH and ".pid" strings
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 17 Mar 2019 18:47:52 +0000 (19:47 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 17 Mar 2019 18:48:39 +0000 (19:48 +0100)
   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 <vda.linux@googlemail.com>
include/libbb.h
libbb/pidfile.c
miscutils/crond.c
miscutils/watchdog.c
networking/inetd.c
networking/ntpd.c
sysklogd/klogd.c
sysklogd/syslogd.c

index 7a1e138752ca5088ad1bb24d564f9e635c95ad7a..e0d5521e489ee1b1cae419885b7bbd040f429491 100644 (file)
@@ -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
index a48dfc38b2c8f2a02c99bd42650b79bbcab867d4..a6a282562ab91dea5a694dc8bf26f606bae920a1 100644 (file)
@@ -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);
+}
index 2e36c406b8abb8fbce3f242b9001ec32d09db81f..25e5503c76ea9cfe57b9cc3a9fc4516586f84166 100644 (file)
@@ -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 */
index 1e9ecc5e84a3b51cae37c27e1bdf0238fa23b74d..86600b72fc3f67f31fa415d9489cda66803bdbaf 100644 (file)
@@ -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) {
                /*
index 8f871ee12e91367126462b52f28f45cd5d5fce1d..da65511744617f1923128b28444b2edbd830d5d0 100644 (file)
@@ -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);
index 0f474bc095a509d8da8abbbe8683e5591f1d8d5b..b2e77929e0c654c33025e013eebcd3c65fd73f1f 100644 (file)
@@ -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);
 }
 
index 76eccf1da94e59af4012e25dbf780f14a971135f..17b6ca23543d70c743a4197d85ddcbb5b68cb4ad 100644 (file)
@@ -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;
index f123594767ec9bb0543c6cfab58d0e28a884f843..d0dd1bd20bad0eac25f14a6ca2bcce6c56a5abfd 100644 (file)
@@ -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; */