refuse to start when PID file exists
[hmcfgusb] / hmland.c
index 04ee740c2855104e4b42eeb4f2c7cad221be2c72..f3353f6c1d943def551c27b731ef1e654ae26a1b 100644 (file)
--- a/hmland.c
+++ b/hmland.c
@@ -32,6 +32,8 @@
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <libusb-1.0/libusb.h>
@@ -39,6 +41,8 @@
 #include "hexdump.h"
 #include "hmcfgusb.h"
 
+#define PID_FILE "/var/run/hmland.pid"
+
 extern char *optarg;
 
 static int impersonate_hmlanif = 0;
@@ -379,7 +383,18 @@ static int comm(int fd_in, int fd_out, int master_socket)
        return 1;
 }
 
-static int socket_server(int port, int daemon)
+void sigterm_handler(int sig)
+{
+       if (unlink(PID_FILE) == -1)
+               perror("Can't remove PID file");
+
+       exit(EXIT_SUCCESS);
+}
+
+#define FLAG_DAEMON    (1 << 0)
+#define FLAG_PID_FILE  (1 << 1)
+
+static int socket_server(char *iface, int port, int flags)
 {
        struct sigaction sact;
        struct sockaddr_in sin;
@@ -387,22 +402,76 @@ static int socket_server(int port, int daemon)
        int n;
        pid_t pid;
 
-       if (daemon) {
+       if (flags & FLAG_DAEMON) {
+               FILE *pidfile = NULL;
+
+               if (flags & FLAG_PID_FILE) {
+                       int fd;
+
+                       fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
+                       if (fd == -1) {
+                               if (errno == EEXIST) {
+                                       pid_t old_pid;
+                                       pidfile = fopen(PID_FILE, "r");
+                                       if (!pidfile) {
+                                               perror("PID file " PID_FILE " already exists, already running?");
+                                               exit(EXIT_FAILURE);
+                                       }
+
+                                       if (fscanf(pidfile, "%u", &old_pid) != 1) {
+                                               fclose(pidfile);
+                                               fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
+                                               exit(EXIT_FAILURE);
+                                       }
+
+                                       fclose(pidfile);
+
+                                       fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
+                                       exit(EXIT_FAILURE);
+                               }
+                               perror("Can't create PID file " PID_FILE);
+                               exit(EXIT_FAILURE);
+                       }
+
+                       pidfile = fdopen(fd, "w");
+                       if (!pidfile) {
+                               perror("Can't reopen PID file fd");
+                               exit(EXIT_FAILURE);
+                       }
+
+                       memset(&sact, 0, sizeof(sact));
+                       sact.sa_handler = sigterm_handler;
+
+                       if (sigaction(SIGTERM, &sact, NULL) == -1) {
+                               perror("sigaction(SIGTERM)");
+                               exit(EXIT_FAILURE);
+                       }
+               }
+
                pid = fork();
                if (pid > 0) {
+                       if (pidfile) {
+                               fprintf(pidfile, "%u\n", pid);
+                               fclose(pidfile);
+                       }
+
                        printf("Daemon with PID %u started!\n", pid);
                        exit(EXIT_SUCCESS);
                } else if (pid < 0) {
                        perror("fork");
                        exit(EXIT_FAILURE);
                }
+
+               if (pidfile)
+                       fclose(pidfile);
        }
 
        memset(&sact, 0, sizeof(sact));
        sact.sa_handler = SIG_IGN;
 
        if (sigaction(SIGPIPE, &sact, NULL) == -1) {
-               perror("sigaction");
+               perror("sigaction(SIGPIPE)");
+               exit(EXIT_FAILURE);
        }
 
        impersonate_hmlanif = 1;
@@ -422,7 +491,14 @@ static int socket_server(int port, int daemon)
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_port = htons(port);
-       sin.sin_addr.s_addr = htonl(INADDR_ANY);
+       if (!iface) {
+               sin.sin_addr.s_addr = htonl(INADDR_ANY);
+       } else {
+               if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
+                       perror("inet_ntop");
+                       return EXIT_FAILURE;
+               }
+       }
 
        if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
                perror("Can't bind socket");
@@ -493,6 +569,8 @@ void hmlan_syntax(char *prog)
        fprintf(stderr, "\t-d\tdaemon mode\n");
        fprintf(stderr, "\t-h\tthis help\n");
        fprintf(stderr, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
+       fprintf(stderr, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
+       fprintf(stderr, "\t-P\tcreate PID file " PID_FILE " in daemon mode\n");
        fprintf(stderr, "\t-p n\tlisten on port n (default 1000)\n");
        fprintf(stderr, "\t-v\tverbose mode\n");
 
@@ -501,23 +579,27 @@ void hmlan_syntax(char *prog)
 int main(int argc, char **argv)
 {
        int port = 1000;
+       char *iface = NULL;
        int interactive = 0;
-       int daemon = 0;
+       int flags = 0;
        char *ep;
        int opt;
 
-       while((opt = getopt(argc, argv, "Ddhip:v")) != -1) {
+       while((opt = getopt(argc, argv, "DdhiPp:l:v")) != -1) {
                switch (opt) {
                        case 'D':
                                debug = 1;
                                verbose = 1;
                                break;
                        case 'd':
-                               daemon = 1;
+                               flags |= FLAG_DAEMON;
                                break;
                        case 'i':
                                interactive = 1;
                                break;
+                       case 'P':
+                               flags |= FLAG_PID_FILE;
+                               break;
                        case 'p':
                                port = strtoul(optarg, &ep, 10);
                                if (*ep != '\0') {
@@ -525,6 +607,9 @@ int main(int argc, char **argv)
                                        exit(EXIT_FAILURE);
                                }
                                break;
+                       case 'l':
+                               iface = optarg;
+                               break;
                        case 'v':
                                verbose = 1;
                                break;
@@ -541,6 +626,6 @@ int main(int argc, char **argv)
        if (interactive) {
                return interactive_server();
        } else {
-               return socket_server(port, daemon);
+               return socket_server(iface, port, flags);
        }
 }
Impressum, Datenschutz