]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Keep flock on pidfile open for while running.
authorRoy Marples <roy@marples.name>
Sun, 13 May 2007 13:46:39 +0000 (13:46 +0000)
committerRoy Marples <roy@marples.name>
Sun, 13 May 2007 13:46:39 +0000 (13:46 +0000)
client.c
dhcpcd.c

index d32f6298130c87a0a7585f5086fdbbe4b6f6bf22..a76c654f49a9d50f8efd35a9906ab4836744088e 100644 (file)
--- a/client.c
+++ b/client.c
@@ -124,9 +124,6 @@ static bool daemonise (int *pidfd)
                return (false);
        }
        
-       close (*pidfd);
-       *pidfd = -1;
-
        return (true);
 }
 
@@ -696,6 +693,11 @@ eexit:
        if (buffer)
                free (buffer);
 
+       if (*pidfd != -1) {
+               close (*pidfd);
+               *pidfd = -1;
+       }
+
        logger (LOG_INFO, "exiting");
 
        /* Unlink our pidfile */
index 09e4b43e51302738fe63f5eb6373efee462774c1..86f2ab7cdf8e1eed9b41cf1b8c9088702936292d 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -389,12 +389,12 @@ int main(int argc, char **argv)
        }
 
        if ((pid = read_pid (options.pidfile)) > 0 && kill (pid, 0) == 0) {
-               logger (LOG_ERR, ""PACKAGE" already running (%s)", options.pidfile);
+               logger (LOG_ERR, ""PACKAGE" already running on pid %d (%s)",
+                               pid, options.pidfile);
                exit (EXIT_FAILURE);
        }
 
-       pidfd = open (options.pidfile,
-                                 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0660);
+       pidfd = open (options.pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
        if (pidfd == -1) {
                logger (LOG_ERR, "open `%s': %s", options.pidfile, strerror (errno));
                exit (EXIT_FAILURE);
@@ -406,6 +406,11 @@ int main(int argc, char **argv)
                exit (EXIT_FAILURE);
        }
 
+       /* dhcpcd.sh should not interhit this fd */
+       if ((i = fcntl (pidfd, F_GETFD, 0)) < 0 ||
+               fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) < 0)
+                       logger (LOG_ERR, "fcntl: %s", strerror (errno));
+
        logger (LOG_INFO, PACKAGE " " VERSION " starting");
        if (dhcp_run (&options, &pidfd)) {
                if (pidfd > -1)