X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fhtparser.c;h=e281abd85701d9b11bea7e9aa7bd9323b6b0d2ff;hb=595adb9922885c2a05bc6917ee8f8f02f496e618;hp=d10121cd6a2bb0e6af60a3b36e032ab468b9b002;hpb=bcad6b0c48d516ddc920b52f06083ceaa242e1ca;p=ashd.git diff --git a/src/htparser.c b/src/htparser.c index d10121c..e281abd 100644 --- a/src/htparser.c +++ b/src/htparser.c @@ -40,7 +40,6 @@ #include "htparser.h" static int plex; -static char *pidfile = NULL; static int daemonize, usesyslog; struct mtbuf listeners; @@ -603,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) { @@ -622,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); @@ -656,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); } } @@ -688,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) { @@ -701,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); }