Simplify sigint handler.
[apps/pfixtools.git] / common.c
index d3147ff..d00461b 100644 (file)
--- a/common.c
+++ b/common.c
@@ -47,18 +47,9 @@ static FILE *pidfile = NULL;
 
 void common_sighandler(int sig)
 {
-    static time_t lastintr = 0;
-    time_t now = time(NULL);
-
     switch (sig) {
       case SIGINT:
-        if (sigint) {
-            if (now - lastintr >= 1)
-                break;
-        } else {
-            lastintr = now;
-            sigint   = true;
-        }
+        sigint = true;
         return;
 
       case SIGHUP:
@@ -88,7 +79,7 @@ static int setnonblock(int sock)
     return 0;
 }
 
-int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
+int tcp_bind(const struct sockaddr *addr, socklen_t len)
 {
     int sock;
 
@@ -128,17 +119,32 @@ int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
         return -1;
     }
 
-    if (setnonblock(sock)) {
+    return sock;
+}
+
+int tcp_listen(const struct sockaddr *addr, socklen_t len)
+{
+    int sock = tcp_bind(addr, len);
+    if (listen(sock, 0) < 0) {
+        UNIXERR("bind");
         close(sock);
         return -1;
     }
+    return sock;
+}
 
+int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
+{
+    int sock = tcp_bind(addr, len);
+    if (setnonblock(sock)) {
+        close(sock);
+        return -1;
+    }
     if (listen(sock, 0) < 0) {
         UNIXERR("bind");
         close(sock);
         return -1;
     }
-
     return sock;
 }
 
@@ -159,6 +165,20 @@ int accept_nonblock(int fd)
     return sock;
 }
 
+int xwrite(int fd, const char *s, size_t l)
+{
+    while (l > 0) {
+        int nb = write(fd, s, l);
+        if (nb < 0) {
+            if (errno == EINTR || errno == EAGAIN)
+                continue;
+            return -1;
+        }
+        l -= nb;
+    }
+    return 0;
+}
+
 int daemon_detach(void)
 {
     pid_t pid;