]> git.zerfleddert.de Git - hmcfgusb/commitdiff
refuse to start when PID file exists
authorMichael Gernoth <michael@gernoth.net>
Sun, 16 Jun 2013 12:36:30 +0000 (14:36 +0200)
committerMichael Gernoth <michael@gernoth.net>
Sun, 16 Jun 2013 12:36:30 +0000 (14:36 +0200)
hmland.c

index 519e02b997fac6c5c0114b4cf78e07c1be79b0a1..f3353f6c1d943def551c27b731ef1e654ae26a1b 100644 (file)
--- a/hmland.c
+++ b/hmland.c
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.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>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <libusb-1.0/libusb.h>
@@ -405,14 +406,36 @@ static int socket_server(char *iface, int port, int flags)
                FILE *pidfile = NULL;
 
                if (flags & FLAG_PID_FILE) {
                FILE *pidfile = NULL;
 
                if (flags & FLAG_PID_FILE) {
-                       mode_t old_umask;
-
-                       old_umask = umask(022);
-                       pidfile = fopen(PID_FILE, "w");
-                       umask(old_umask);
+                       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) {
                        if (!pidfile) {
-                               perror("Can't create PID file " PID_FILE);
+                               perror("Can't reopen PID file fd");
                                exit(EXIT_FAILURE);
                        }
 
                                exit(EXIT_FAILURE);
                        }
 
Impressum, Datenschutz