chiark / gitweb /
cgi-fcgi-interp: new garbage collection approach, wip implementation
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 25 Mar 2016 00:58:21 +0000 (00:58 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 29 Mar 2016 18:32:51 +0000 (19:32 +0100)
cprogs/cgi-fcgi-interp.c

index 28ec126..40cedb4 100644 (file)
@@ -410,7 +410,25 @@ static void shbang_opts(const char *const **argv_io,
 }
 
 int main(int argc, const char *const *argv) {
-  const char *smashedopt;
+  const char *smashedopt, *us;
+
+  us = argv[0];
+
+  if (argv>=4 && !strcmp(argv[1],"--stage2")) {
+    ++argv;
+    stage2 = 1;
+
+    int stderrfd = atoi(*++argv);
+    r = dup2(stderrfd, 2);
+    assert(r==2);
+
+    r = open("/dev/null",O_WRONLY);
+    if (r<0) err(127,"open /dev/null as stdout");
+    if (r>=3) close(r);
+    else if (r!=) errx(127,"open /dev/null gave bad fd %d",r);
+
+    sock_path = *++argv;
+  }
 
   if (argc>=2 &&
       (smashedopt = argv[1]) &&
@@ -449,20 +467,45 @@ int main(int argc, const char *const *argv) {
   if (!script) errx(127,"need script argument");
   if (*argv) errx(127,"too many arguments");
 
-  find_socket_path();
+  if (!stage2) {
+    
+    find_socket_path();
 
-  bool isgarbage = check_garbage();
+    bool isgarbage = check_garbage();
+
+    if (debugmode) {
+      printf("socket: %s\n",socket_path);
+      printf("interp: %s\n",interp);
+      printf("script: %s\n",script);
+      printf("garbage: %d\n",isgarbage);
+      exit(0);
+    }
 
-  if (debugmode) {
-    printf("socket: %s\n",socket_path);
-    printf("interp: %s\n",interp);
-    printf("script: %s\n",script);
-    printf("garbage: %d\n",isgarbage);
-    exit(0);
+    if (isgarbage)
+      tidy_garbage();
+
+    execlp("cgi-fcgi",
+          "cgi-fcti", "-connect", socket_path,
+          us, "--stage2",
+          m_asprintf("-c%d", check_interval),
+          m_asprintf("%d", copy_stderr), socket_path,
+          interp, script,
+          (char*)0);
+    err(127,"exec cgi-fcgi");
+    
+  } else { /*stage2*/
+
+    become_pgrp();
+    check_socket_baseline();
+    spawn_script();
+    
+  r = lstat(socket_path, &sock_stab);
+  if (r) {
+    if ((errno == ENOENT))
+      return 0; /* well, no garbage then */
+    err(127,"stat socket (%s)",socket_path);
   }
 
-  if (isgarbage)
-    tidy_garbage();
 
-  exit(0);
+  }
 }