callscgi: Exit properly on SIGTERM and SIGINT.
[ashd.git] / src / callscgi.c
index c5b5499..86abb5b 100644 (file)
@@ -98,13 +98,14 @@ static char *mkanonid(void)
     
     home = getenv("HOME");
     if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK))
-       tmpl = sprintf2("%s/.ashd/sockets/scgi-a-XXXXXX");
+       tmpl = sprintf2("%s/.ashd/sockets/scgi-a-XXXXXX", home);
     else
        tmpl = sprintf2("/tmp/scgi-a-%i-XXXXXX", getuid());
     if((fd = mkstemp(tmpl)) < 0) {
        flog(LOG_ERR, "could not create anonymous socket `%s': %s", tmpl, strerror(errno));
        exit(1);
     }
+    close(fd);
     unlink(tmpl);
     return(tmpl);
 }
@@ -579,6 +580,11 @@ static void listenloop(struct muth *muth, va_list args)
     }
 }
 
+static void sigexit(int sig)
+{
+    exit(0);
+}
+
 static void usage(FILE *out)
 {
     fprintf(out, "usage: servescgi [-h] [-N RETRIES] [-i ID] [-u UNIX-PATH] [-t [HOST:]TCP-PORT] [PROGRAM [ARGS...]]\n");
@@ -616,6 +622,8 @@ int main(int argc, char **argv)
        exit(1);
     }
     signal(SIGCHLD, SIG_IGN);
+    signal(SIGINT, sigexit);
+    signal(SIGTERM, sigexit);
     mustart(listenloop, 0);
     atexit(killcuraddr);
     ioloop();