doc: Documented htpipe.
[ashd.git] / src / htparser.c
index a738e25..e281abd 100644 (file)
@@ -40,7 +40,6 @@
 #include "htparser.h"
 
 static int plex;
-static char *pidfile = NULL;
 static int daemonize, usesyslog;
 struct mtbuf listeners;
 
@@ -333,7 +332,8 @@ static void passduplex(struct bufio *a, int afd, struct bufio *b, int bfd)
            if(ev)
                pfd[n++] = (struct selected){.fd = bfd, .ev = ev};
        }
-       sel = mblock(600, n, pfd);
+       if((sel = mblock(600, n, pfd)).ev == 0)
+           break;
        if(sel.fd == afd)
            sio = a;
        else if(sel.fd == bfd)
@@ -602,12 +602,12 @@ int main(int argc, char **argv)
 {
     int c, d;
     int i, s1;
-    char *root;
+    char *root, *pidfile, *pidtmp;
     FILE *pidout;
     struct passwd *pwent;
     
     daemonize = usesyslog = 0;
-    root = NULL;
+    root = pidfile = NULL;
     pwent = NULL;
     while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
        switch(c) {
@@ -621,16 +621,16 @@ int main(int argc, char **argv)
            usesyslog = 1;
            break;
        case 'u':
-           if((pwent = getpwnam(optarg)) == NULL) {
+           if(optarg[0] && ((pwent = getpwnam(optarg)) == NULL)) {
                flog(LOG_ERR, "could not find user %s", optarg);
                exit(1);
            }
            break;
        case 'r':
-           root = optarg;
+           root = optarg[0] ? optarg : NULL;
            break;
        case 'p':
-           pidfile = optarg;
+           pidfile = optarg[0] ? optarg : NULL;
            break;
        default:
            usage(stderr);
@@ -655,8 +655,14 @@ int main(int argc, char **argv)
     bufadd(listeners, mustart(plexwatch, plex));
     pidout = NULL;
     if(pidfile != NULL) {
-       if((pidout = fopen(pidfile, "w")) == NULL) {
-           flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
+       pidtmp = sprintf3("%s.new", pidfile);
+       if((pidout = fopen(pidtmp, "w")) == NULL) {
+           flog(LOG_ERR, "could not open %s for writing: %s", pidtmp, strerror(errno));
+           return(1);
+       }
+       if(rename(pidtmp, pidfile)) {
+           flog(LOG_ERR, "could not overwrite %s: %s", pidfile, strerror(errno));
+           unlink(pidtmp);
            return(1);
        }
     }
@@ -687,7 +693,7 @@ int main(int argc, char **argv)
     }
     if(pidout != NULL) {
        fprintf(pidout, "%i\n", getpid());
-       fclose(pidout);
+       fflush(pidout);
     }
     d = 0;
     while(!d) {
@@ -700,11 +706,17 @@ int main(int argc, char **argv)
                while(listeners.d > 0)
                    resume(listeners.b[0], 0);
                flog(LOG_INFO, "no longer listening");
+               if(pidout != NULL) {
+                   putc('\n', pidout);
+                   fflush(pidout);
+               }
            } else {
                d = 1;
            }
            break;
        }
     }
+    if(pidout != NULL)
+       ftruncate(fileno(pidout), 0);
     return(0);
 }