This is mostly just a cleanup patch.  It removes some dead code and
changes around the pidfile logic a bit.  It also allows unlinking the
pid-file on exit using the atexit call.  Tested on Linux.

Thanks,
Ben

--
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com

diff --git a/libxorp/daemon.c b/libxorp/daemon.c
index 3e5ee88..718c672 100644
--- a/libxorp/daemon.c
+++ b/libxorp/daemon.c
@@ -73,13 +73,8 @@ xorp_daemonize(int nochdir, int noclose)
                return (-1);
        case 0:
                break;
-#if 0
-       default:
-               _exit(0);               /* We need to return the pid */
-#else
        default:
                return (newpid);
-#endif
        }
 
        newgrp = setsid();
diff --git a/mld6igmp/mld6igmp_vif.cc b/mld6igmp/mld6igmp_vif.cc
index 1d166a3..fa71ba1 100644
--- a/mld6igmp/mld6igmp_vif.cc
+++ b/mld6igmp/mld6igmp_vif.cc
@@ -1013,7 +1013,7 @@ Mld6igmpVif::mld6igmp_process(const IPvX& src,
     }
 
     XLOG_TRACE(mld6igmp_node().is_log_trace(),
-              "RX %s from %s to %s on vif %s",
+              "mld6igmp_process:  RX %s from %s to %s on vif %s",
               proto_message_type2ascii(message_type),
               cstring(src), cstring(dst),
               name().c_str());
diff --git a/rtrmgr/main_rtrmgr.cc b/rtrmgr/main_rtrmgr.cc
index a8cf19a..9c57ebe 100644
--- a/rtrmgr/main_rtrmgr.cc
+++ b/rtrmgr/main_rtrmgr.cc
@@ -92,7 +92,7 @@ static string syslogspec;
 static string  logfilename;
 static string  pidfilename;
 int32_t                quit_time = -1;
-static bool    daemon_mode = false;
+FILE*           pidfile = NULL;
 
 static void cleanup_and_exit(int errcode);
 
@@ -440,19 +440,22 @@ open_syslog()
        fprintf(stderr, "Failed to open syslog spec %s\n", logfilename.c_str());
 }
 
-void
-write_pidfile(pid_t thepid)
-{
-    if (pidfilename.empty()) {
-       fprintf(stderr, "Empty PID filename specified\n");
-       return;
+void handle_atexit(void) {
+    if (do_pidfile && pidfilename.size()) {
+       cout << "In rtrmgr atexit, unlinking: " << pidfilename << endl;
+       unlink(pidfilename.c_str());
     }
-    FILE *pidfile;
-    if ((pidfile = fopen(pidfilename.c_str(), "w")) != NULL) {
-       fprintf(pidfile, "%u\n", thepid);
+}
+
+void write_pidfile() {
+    if (pidfile) {
+       fprintf(pidfile, "%u\n", getpid());
+       fflush(pidfile);
        fclose(pidfile);
-    } else {
-       fprintf(stderr, "Failed to write pid file\n");
+       pidfile = NULL;
+
+       // Clean up the pid-file on exit, if possible.
+       atexit(handle_atexit);
     }
 }
 
@@ -460,7 +463,7 @@ void
 Rtrmgr::daemonize()
 {
     // If not daemonizing, do nothing.
-    if (! daemon_mode)
+    if (! _daemon_mode)
        return;
 
 #ifndef HOST_OS_WINDOWS
@@ -475,17 +478,11 @@ Rtrmgr::daemonize()
     // Make sure we open the pid file in the parent, and the
     // log file in the child. Close fds but don't chdir.
     if (newpid == 0) {
-       // We are now in the child.
-#if 0
-       if (do_logfile)
-           open_logfile();
-#endif
+       // We are now in the child, write out our pid file.
+       write_pidfile();
        return;
     }
 
-    // We are in the parent. Write the PID file and exit.
-    if (do_pidfile)
-       write_pidfile(newpid);
     _exit(0);
 #endif
 }
@@ -495,6 +492,8 @@ main(int argc, char* const argv[])
 {
     int errcode = 0;
 
+    bool daemon_mode = false;
+
     //
     // Initialize and start xlog
     //
@@ -629,10 +628,23 @@ main(int argc, char* const argv[])
        }
     }
 
-    // If not daemonizing, open the pid file now.
-    if (! daemon_mode) {
-       if (do_pidfile)
-           write_pidfile(getpid());
+    // Open this before we daemonize things, so that it's in the proper 
relative
+    // file system path.
+    if (do_pidfile) {
+       pidfile = fopen(pidfilename.c_str(), "w");
+       if (!pidfile) {
+           cerr << "ERROR:  Could not open pidfile: " << pidfilename << " 
error: "
+                << strerror(errno) << endl;
+       }
+       else {
+           if (!daemon_mode) {
+               write_pidfile();
+           }
+           // else, will write this later when we daemonize.
+       }
+    }
+    else {
+       cout << "Not doing pidfile...\n";
     }
 
     // Open the new log facility now so that all output, up to when we
_______________________________________________
Xorp-hackers mailing list
[email protected]
http://mailman.ICSI.Berkeley.EDU/mailman/listinfo/xorp-hackers

Reply via email to