chiark / gitweb /
cgi-fcgi-perl: wip, convert to err.h; do mkdir
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 22 Mar 2016 00:40:36 +0000 (00:40 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 22 Mar 2016 00:40:36 +0000 (00:40 +0000)
cprogs/cgi-fcgi-perl.c

index 617f8f00ddd9da401b395fb372f44698ea50479a..58a858ae41fede971bc4372001c2427a2ff222e9 100644 (file)
@@ -73,6 +73,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 #include <pwd.h>
+#include <err.h>
 
 #include <nettle/sha.h>
 
@@ -87,8 +88,7 @@ static const char *ident;
 static int numservers;
 
 void diee(const char *m) {
-  fprintf(stderr,"cgi-fcgi-: error: %s failed: %s\n", m, strerror(errno));
-  exit(127);
+  err(127, "error: %s failed", m);
 }
 
 static void fusagemessage(FILE *f) {
@@ -129,16 +129,16 @@ static bool find_run_base_var_run(void) {
     diee("stat /var/run/user/UID");
   }
   if (!S_ISDIR(stab.st_mode)) {
-    fprintf(stderr,"%s not a directory, falling back to ~\n", try);
+    warnx("%s not a directory, falling back to ~\n", try);
     return 0;
   }
   if (stab.st_uid != us) {
-    fprintf(stderr,"%s not owned by uid %lu, falling back to ~\n",
-           try, (unsigned long)us);
+    warnx("%s not owned by uid %lu, falling back to ~\n", try,
+         (unsigned long)us);
     return 0;
   }
   if (stab.st_mode & 0077) {
-    fprintf(stderr,"%s writeable by group or other, falling back to ~\n",try);
+    warnx("%s writeable by group or other, falling back to ~\n", try);
     return 0;
   }
   run_base = m_asprintf("%s/%s", try, "cgi-fcgi-perl");
@@ -166,6 +166,7 @@ static bool find_run_base_home(void) {
 
 static void find_socket_path(void) {
   struct sockaddr_un sun;
+  int r;
 
   us = getuid();  if (us==(uid_t)-1) diee("getuid");
 
@@ -176,13 +177,11 @@ static void find_socket_path(void) {
   int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
 
   if (!ident) {
-    if (maxidentlen < MINHEXHASH) {
-      fprintf(stderr,"cgi-fcgi-perl: base directory `%s'"
-             " leaves only %d characters for command name hash"
-             " which is too little (<%d)\n",
-             run_base, maxidentlen, MINHEXHASH);
-      exit(127);
-    }
+    if (maxidentlen < MINHEXHASH)
+      errx(127,"cgi-fcgi-perl: base directory `%s'"
+          " leaves only %d characters for command name hash"
+          " which is too little (<%d)",
+          run_base, maxidentlen, MINHEXHASH);
 
     int identlen = maxidentlen > 64 ? 64 : maxidentlen;
     char *hexident = xmalloc(identlen + 2);
@@ -201,11 +200,15 @@ static void find_socket_path(void) {
     ident = hexident;
   }
 
-  if (strlen(ident) > maxidentlen) {
-    fprintf(stderr,"cgi-fgci-perl: base directory `%s'"
-           " plus ident `%s' too long (with spare) for socket\n",
-           run_base, ident);
-    exit(127);
+  if (strlen(ident) > maxidentlen)
+    errx(127, "base directory `%s' plus ident `%s' too long"
+        " (with spare) for socket (max ident %d)\n",
+        run_base, ident, maxidentlen);
+
+  r = mkdir(run_base, 0700);
+  if (r) {
+    if (!(errno == EEXIST))
+      err(127,"mkdir %s",run_base);
   }
 
   socket_path = m_asprintf("%s/g%s",run_base,ident);
@@ -215,10 +218,8 @@ int main(int argc, const char *const *argv) {
   myopt(&argv, cmdinfos);
 
   command = *argv++;
-  if (!command || *argv) {
-    fprintf(stderr,"wrong number of arguments\n");
-    exit(127);
-  }
+  if (!command) errx(127,"need command argument");
+  if (*argv) errx(127,"too many arguments");
 
   find_socket_path();