Write the PID file as late as possible
authorAlan T. DeKok <aland@freeradius.org>
Tue, 13 Oct 2009 10:53:49 +0000 (12:53 +0200)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 20 Oct 2009 22:05:15 +0000 (00:05 +0200)
i.e. after checking the config, and after opening any sockets

src/main/radiusd.c

index d499a0a..3104d02 100644 (file)
@@ -289,30 +289,6 @@ int main(int argc, char *argv[])
        radius_pid = getpid();
 
        /*
-        *  Only write the PID file if we're running as a daemon.
-        *
-        *  And write it AFTER we've forked, so that we write the
-        *  correct PID.
-        */
-       if (dont_fork == FALSE) {
-               FILE *fp;
-
-               fp = fopen(mainconfig.pid_file, "w");
-               if (fp != NULL) {
-                       /*
-                        *      FIXME: What about following symlinks,
-                        *      and having it over-write a normal file?
-                        */
-                       fprintf(fp, "%d\n", (int) radius_pid);
-                       fclose(fp);
-               } else {
-                       radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
-                              mainconfig.pid_file, strerror(errno));
-                       exit(1);
-               }
-       }
-
-       /*
         *      If we're running as a daemon, close the default file
         *      descriptors, AFTER forking.
         */
@@ -393,6 +369,30 @@ int main(int argc, char *argv[])
        radius_stats_init(0);
 
        /*
+        *  Only write the PID file if we're running as a daemon.
+        *
+        *  And write it AFTER we've forked, so that we write the
+        *  correct PID.
+        */
+       if (dont_fork == FALSE) {
+               FILE *fp;
+
+               fp = fopen(mainconfig.pid_file, "w");
+               if (fp != NULL) {
+                       /*
+                        *      FIXME: What about following symlinks,
+                        *      and having it over-write a normal file?
+                        */
+                       fprintf(fp, "%d\n", (int) radius_pid);
+                       fclose(fp);
+               } else {
+                       radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
+                              mainconfig.pid_file, strerror(errno));
+                       exit(1);
+               }
+       }
+
+       /*
         *      Process requests until HUP or exit.
         */
        while ((rcode = radius_event_process()) == 0x80) {