From 583ff24fced887fb05718b6cae568d13b51bcdfe Mon Sep 17 00:00:00 2001
From: elliotkendall <elliotkendall@gmail.com>
Date: Fri, 6 Jun 2014 12:24:40 -0700
Subject: [PATCH] Only open the pid file if we're going to use it

---
 fanout.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/fanout.c b/fanout.c
index 9af474f..af312c4 100644
--- a/fanout.c
+++ b/fanout.c
@@ -158,7 +158,7 @@ int main (int argc, char *argv[])
     socklen_t optlen = sizeof(optval);
     u_int listen_backlog = 25;
     u_int max_events = 25;
-    FILE *pidfile = NULL;
+    char *pidfilename;
     server_start_time = (long)time (NULL);
     char buffer[1025];
 
@@ -215,9 +215,7 @@ int main (int argc, char *argv[])
                         break;
                     // pidfile
                     case 3:
-                        if ((pidfile = fopen (optarg, "w+")) == NULL) {
-                            fanout_error ("ERROR cannot open pidfile");
-                        }
+                        pidfilename = optarg;
                         break;
                     //daemon
                     case 4:
@@ -398,9 +396,14 @@ xit\n");
            we can exit the parent process. */
         if (pid > 0) {
             /* Write pid to file */
+            FILE *pidfile = NULL;
+            pidfile = fopen (pidfilename, "w+");
             if (pidfile != NULL) {
                 fprintf (pidfile, "%d\n", (int) pid);
                 fclose (pidfile);
+            } else {
+                fanout_error ("ERROR cannot open pidfile");
+                exit (EXIT_FAILURE);
             }
             exit (EXIT_SUCCESS);
         }
-- 
GitLab