if pidfile turned out to be !regular file, do not unlink it.
authorDenis Vlasenko <vda.linux@googlemail.com>
Wed, 15 Aug 2007 20:05:37 +0000 (20:05 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Wed, 15 Aug 2007 20:05:37 +0000 (20:05 -0000)
It's most probably the /dev/null.

Config.in
include/libbb.h
libbb/pidfile.c
networking/udhcp/dhcpc.c

index 54bcd9d108258ed4af04d1de957888903114fd2b..24e461eb751b348e41d62bdde8146b5af4538463 100644 (file)
--- a/Config.in
+++ b/Config.in
@@ -144,7 +144,7 @@ config FEATURE_PIDFILE
        bool "Support writing pidfiles"
        default n
        help
-         This option makes some applets (crond, syslogd and inetd) write
+         This option makes some applets (e.g. crond, syslogd, inetd) write
          a pidfile in /var/run. Some applications rely on them.
 
 config FEATURE_SUID
index fc0a0ae6cf37fd6fcb4a6be0f0bff4c81f46ecc4..d60b410ae055e0181c8c7bb93b0854d8eee811b4 100644 (file)
@@ -622,12 +622,14 @@ llist_t *llist_rev(llist_t *list);
 /* start_stop_daemon and udhcpc are special - they want
  * to create pidfiles regardless of FEATURE_PIDFILE */
 #if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
-int write_pidfile(const char *path);
-#define remove_pidfile(f) ((void)unlink(f))
+/* True only if we created pidfile which is *file*, not /dev/null etc */
+extern smallint wrote_pidfile;
+void write_pidfile(const char *path);
+#define remove_pidfile(path) do { if (wrote_pidfile) unlink(path); } while (0)
 #else
-/* Why? #defining it to 1 gives "warning: statement with no effect"... */
-static ALWAYS_INLINE int write_pidfile(const char *path) { return 1; }
-#define remove_pidfile(f) ((void)0)
+enum { wrote_pidfile = 0 };
+#define write_pidfile(path)  ((void)0)
+#define remove_pidfile(path) ((void)0)
 #endif
 
 enum {
index 64e67c322013d81d0304bb411b6829796f3dacea..cafa7891f30c2c7fbee27e9a7af73d8eb6b2b6ab 100644 (file)
 #define WANT_PIDFILE 1
 #include "libbb.h"
 
-int write_pidfile(const char *path)
+smallint wrote_pidfile;
+
+void write_pidfile(const char *path)
 {
        int pid_fd;
        char *end;
        char buf[sizeof(int)*3 + 2];
+       struct stat sb;
 
        if (!path)
-               return 1;
+               return;
        /* we will overwrite stale pidfile */
        pid_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
        if (pid_fd < 0)
-               return 0;
-       /* few bytes larger, but doesn't use stdio */
-       end = utoa_to_buf(getpid(), buf, sizeof(buf));
-       *end = '\n';
-       full_write(pid_fd, buf, end - buf + 1);
+               return;
+
+       /* path can be "/dev/null"! Test for such cases */
+       wrote_pidfile = (fstat(pid_fd, &sb) == 0) && S_ISREG(sb.st_mode);
+
+       if (wrote_pidfile) {
+               /* few bytes larger, but doesn't use stdio */
+               end = utoa_to_buf(getpid(), buf, sizeof(buf));
+               *end = '\n';
+               full_write(pid_fd, buf, end - buf + 1);
+       }
        close(pid_fd);
-       return 1;
 }
index 7cb1dfe5d4682556f78b55662b3670409dcad06a..7d38742b9d51a1338337bab15cfd73b32d61b8be 100644 (file)
@@ -546,7 +546,7 @@ int udhcpc_main(int argc, char **argv)
  ret0:
        retval = 0;
  ret:
-       if (client_config.pidfile)
+       /*if (client_config.pidfile) - remove_pidfile has it's own check */
                remove_pidfile(client_config.pidfile);
        return retval;
 }