Have a generic pidfile API.
[apps/pfixtools.git] / main-srsd.c
index 8d703af..79f9e19 100644 (file)
@@ -228,8 +228,6 @@ void usage(void)
 
 int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec)
 {
-    int exitcode = EXIT_SUCCESS;
-
     if (start_listener(port_enc, false) < 0)
         return EXIT_FAILURE;
     if (start_listener(port_dec, true) < 0)
@@ -243,8 +241,7 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec)
         if (n < 0) {
             if (errno != EAGAIN && errno != EINTR) {
                 UNIXERR("epoll_wait");
-                exitcode = EXIT_FAILURE;
-                break;
+                return EXIT_FAILURE;
             }
             continue;
         }
@@ -318,7 +315,7 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec)
         }
     }
 
-    return exitcode;
+    return EXIT_SUCCESS;
 }
 
 static srs_t *srs_read_secrets(const char *sfile)
@@ -369,7 +366,6 @@ int main(int argc, char *argv[])
     int port_dec = DEFAULT_DECODER_PORT;
     const char *pidfile = NULL;
 
-    FILE *f = NULL;
     int res;
     srs_t *srs;
 
@@ -403,13 +399,9 @@ int main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    if (pidfile) {
-        f = fopen(pidfile, "w");
-        if (!f) {
-            syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
-        }
-        fprintf(f, "%d\n", getpid());
-        fflush(f);
+    if (pidfile_open(pidfile) < 0) {
+        syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
+        return EXIT_FAILURE;
     }
 
     if (!unsafe && drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
@@ -422,19 +414,8 @@ int main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    if (f) {
-        rewind(f);
-        ftruncate(fileno(f), 0);
-        fprintf(f, "%d\n", getpid());
-        fflush(f);
-    }
+    pidfile_refresh();
     res = main_loop(srs, argv[optind], port_enc, port_dec);
-    if (f) {
-        rewind(f);
-        ftruncate(fileno(f), 0);
-        fclose(f);
-        f = NULL;
-    }
     syslog(LOG_INFO, "Stopping...");
     return res;
 }