callscgi: Log exec errors.
[ashd.git] / src / callscgi.c
index 21db70d..6adccc5 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);
 }
@@ -193,6 +194,7 @@ static void startlisten(void)
        for(i = 3; i < FD_SETSIZE; i++)
            close(i);
        execvp(*progspec, progspec);
+       flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
        exit(127);
     }
     close(fd);
@@ -200,7 +202,7 @@ static void startlisten(void)
 
 static void startnolisten(void)
 {
-    int i;
+    int i, fd;
     
     if((child = fork()) < 0) {
        flog(LOG_ERR, "could not fork: %s", strerror(errno));
@@ -209,7 +211,14 @@ static void startnolisten(void)
     if(child == 0) {
        for(i = 3; i < FD_SETSIZE; i++)
            close(i);
+       if((fd = open("/dev/null", O_RDONLY)) < 0) {
+           flog(LOG_ERR, "/dev/null: %s", strerror(errno));
+           exit(127);
+       }
+       dup2(fd, 0);
+       close(fd);
        execvp(*progspec, progspec);
+       flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
        exit(127);
     }
 }
@@ -262,11 +271,11 @@ retry:
            }
            break;
        }
-       if(tries++ < nolisten) {
-           sleep(1);
-           goto retry;
-       }
        if(fd < 0) {
+           if(tries++ < nolisten) {
+               sleep(1);
+               goto retry;
+           }
            flog(LOG_ERR, "could not connect to specified TCP address: %s", strerror(errno));
            exit(1);
        }
@@ -423,7 +432,7 @@ static void mkcgienv(struct hthead *req, struct charbuf *dst)
        bufaddenv(dst, "SERVER_NAME", "%s", h);
     if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
        bufaddenv(dst, "SERVER_PORT", "%s", h);
-    if(((h = getheader(req, "X-Ash-Server-Protocol")) != NULL) && !strcmp(h, "https"))
+    if(((h = getheader(req, "X-Ash-Protocol")) != NULL) && !strcmp(h, "https"))
        bufaddenv(dst, "HTTPS", "on");
     if((h = getheader(req, "X-Ash-Address")) != NULL)
        bufaddenv(dst, "REMOTE_ADDR", "%s", h);
@@ -436,7 +445,7 @@ static void mkcgienv(struct hthead *req, struct charbuf *dst)
     if((h = getheader(req, "X-Ash-File")) != NULL)
        bufaddenv(dst, "SCRIPT_FILENAME", "%s", absolutify(h));
     for(i = 0; i < req->noheaders; i++) {
-       h = sstrdup(req->headers[i][0]);
+       h = sprintf2("HTTP_%s", req->headers[i][0]);
        for(p = h; *p; p++) {
            if(isalnum(*p))
                *p = toupper(*p);
@@ -573,6 +582,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");
@@ -610,6 +624,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();