chiark / gitweb /
cgi-fcgi-interp: wip check_garbage
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index f2ba0bdb1a98174c9c22e4788cf85b50a51c575c..c08ba9da457d0e153bf52c2c04c1121487c2235e 100644 (file)
@@ -2,7 +2,28 @@
  * "Interpreter" that you can put in #! like this
  *   #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
  *   #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
+ */
+/*
+ * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
+ *
+ * Copyright 2016 Ian Jackson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this file; if not, consult the Free Software
+ * Foundation's website at www.fsf.org, or the GNU Project website at
+ * www.gnu.org.
+ */
+/*
  * The result is a program which looks, when executed via the #!
  * line, like a CGI program.  But the script inside will be executed
  * via <interpreter> in an fcgi context.
@@ -66,8 +87,8 @@
  *  - check for and maybe create <base>
  *  - stat and lstat the <script>
  *  - stat the socket and check its timestamp
- *       if it is too hold, rename it to g<inum> (where
- *       <inum> is in decimal)
+ *       if it is too old, rename it to g<inum>.<pid> (where
+ *       <inum> and <pid> are in decimal)
  *       and run garbage collection
  *  - run  cgi-fcgi -connect SOCKET SCRIPT
  */
@@ -243,10 +264,17 @@ static void find_socket_path(void) {
   socket_path = m_asprintf("%s/g%s",run_base,ident);
 }  
 
+static bool stab_isnewer(const struct stat *a, const struct stat *b) {
+  return 0;
+}
+
 static bool check_garbage(void) {
-  struct stat sock_stab, cmd_stab;
+  struct stat sock_stab, script_stab;
   int r;
 
+  r = lstat(script, &script_stab);
+  if (r) err(127,"lstat script (%s)",script);
+
   r = lstat(socket_path, &sock_stab);
   if (r) {
     if ((errno == ENOENT))
@@ -254,8 +282,16 @@ static bool check_garbage(void) {
     err(127,"stat socket (%s)",socket_path);
   }
 
-  r = lstat(script, &cmd_stab);
-  if (r) err(127,"lstat script (%s)",script);
+  if (stab_isnewer(&script_stab, &sock_stab))
+    return 1;
+
+  if (S_ISLNK(script_stab.st_mode)) {
+    r = stat(script, &script_stab);
+    if (r) err(127,"stat script (%s0",script);
+
+    if (stab_isnewer(&script_stab, &sock_stab))
+      return 1;
+  }
 
   return 0;
 }