chiark / gitweb /
cgi-fcgi-interp: new garbage collection approach, wip compile
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index fb6dfe3ba04f3c66b0fdf9f937c918392c96a20e..8e126a9a70d5ae041079169f1b92498124184424 100644 (file)
@@ -191,6 +191,7 @@ static const struct cmdinfo cmdinfos[]= {
 
 static uid_t us;
 static const char *run_base, *script, *socket_path;
+static int stderr_copy;
 
 static bool find_run_base_var_run(void) {
   struct stat stab;
@@ -408,6 +409,11 @@ static void tidy_garbage(void) {
   if (r) errx(127,"close lock (%s)", lock_path);
 }
 
+static void make_stderr_copy(void) {
+  stderr_copy = dup(2);
+  if (stderr_copy < 0) err(127,"dup stderr (for copy for stage2)");
+}
+
 static void shbang_opts(const char *const **argv_io,
                        const struct cmdinfo *cmdinfos) {
   myopt(argv_io, cmdinfos);
@@ -416,12 +422,20 @@ static void shbang_opts(const char *const **argv_io,
   if (!interp) errx(127,"need interpreter argument");
 }
 
+/* stage2 predeclarations */
+static void record_baseline_time(void);
+static void become_pgrp(void);
+static void setup_handlers(void);
+static void spawn_script(void);
+static void queue_alarm(void);
+
 int main(int argc, const char *const *argv) {
   const char *smashedopt, *us;
+  int r;
 
   us = argv[0];
 
-  if (argv>=4 && !strcmp(argv[1],"--stage2")) {
+  if (argc>=4 && !strcmp(argv[1],"--stage2")) {
     ++argv;
     stage2 = 1;
 
@@ -432,9 +446,9 @@ int main(int argc, const char *const *argv) {
     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);
+    else if (r!=1) errx(127,"open /dev/null for stdout gave bad fd %d",r);
 
-    sock_path = *++argv;
+    socket_path = *++argv;
   }
 
   if (argc>=2 &&
@@ -491,18 +505,20 @@ int main(int argc, const char *const *argv) {
     if (isgarbage)
       tidy_garbage();
 
+    make_stderr_copy();
+
     execlp("cgi-fcgi",
           "cgi-fcti", "-connect", socket_path,
           us, "--stage2",
           m_asprintf("-c%d", check_interval),
-          m_asprintf("%d", copy_stderr), socket_path,
+          m_asprintf("%d", stderr_copy), socket_path,
           interp, script,
           (char*)0);
     err(127,"exec cgi-fcgi");
     
   } else { /*stage2*/
 
-    check_baseline_time();
+    record_baseline_time();
     become_pgrp();
     setup_handlers();
     spawn_script();
@@ -524,7 +540,7 @@ static struct stab baseline_time;
 static pid_t script_child, stage2_pgrp;
 static bool out_of_date;
 
-void check_baseline_time(void) {
+static void record_baseline_time(void) {
 #ifdef st_mtime
   int r = clock_gettime(CLOCK_REALTIME, &baselime_time.st_mtim);
   if (r) err(127,"(stage2) clock_gettime");
@@ -543,6 +559,25 @@ static void become_pgrp(void) {
   if (r) err(127,"(stage2) setpgid");
 }
 
+static void atexit_handler(void) {
+  int r;
+
+  sighandler_t sigr = signal(SIGTERM,SIG_IGN);
+  if (sigr == SIG_ERR) warn("(stage2) signal(SIGTERM,SIG_IGN)");
+
+  r = killpg(stage2_pgrp,SIGTERM);
+  if (r) warn("(stage) killpg failed");
+}
+
+static void alarm_handler(int dummy) {
+  if (out_of_date)
+    /* second timeout */
+    exit(0); /* transfers control to atexit_handler */
+
+  out_of_date = check_garbage_vs(&baseline_time);
+  queue_alarm();
+}
+
 static void setup_handlers(void) {
   struct sigaction sa;
   int r;
@@ -568,25 +603,6 @@ static void setup_handlers(void) {
   if (r) err(127,"(stage2) sigaction SIGCHLD");
 }
 
-static void atexit_handler(void) {
-  int r;
-
-  sighandler_t sigr = signal(SIGTERM,SIG_IGN);
-  if (sigr == SIG_ERR) warn("(stage2) signal(SIGTERM,SIG_IGN)");
-
-  r = killpg(stage2_pgrp,SIGTERM);
-  if (r) warn("(stage) killpg failed");
-}
-
-static void alarm_handler(int dummy) {
-  if (out_of_date)
-    /* second timeout */
-    exit(0); /* transfers control to atexit_handler */
-
-  out_of_date = check_garbage_vs(&baseline_time);
-  queue_alarm();
-}
-
 static void spawn_script(void) {
   script_child = fork();
   if (script_child == (pid_t)-1) err(127,"(stage2) fork");