chiark / gitweb /
cgi-fcgi-interp: Import timespeccmp from FreeBSD
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index 9df31034aa8da607297ea67dff618b86c5c5e755..1d88f50294997bc5cced3d4c3abc839bfa29cb79 100644 (file)
@@ -7,6 +7,7 @@
  * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
  *
  * Copyright 2016 Ian Jackson
+ * Copyright 1982,1986,1993 The Regents of the University of California
  *
  * 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
@@ -22,6 +23,8 @@
  * 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.
+ *
+ * See below for a BSD 3-clause notice regarding timespeccmp.
  */
 /*
  * The result is a program which looks, when executed via the #!
@@ -87,8 +90,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
  */
@@ -264,10 +267,57 @@ static void find_socket_path(void) {
   socket_path = m_asprintf("%s/g%s",run_base,ident);
 }  
 
+/*
+ * Regarding the macro timespeccmp:
+ *
+ * Copyright (c) 1982, 1986, 1993
+ *      The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *      @(#)time.h      8.5 (Berkeley) 5/4/95
+ * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
+ */
+#ifndef timespeccmp
+#define timespeccmp(tvp, uvp, cmp)                                      \
+        (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
+            ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
+            ((tvp)->tv_sec cmp (uvp)->tv_sec))
+#endif /*timespeccmp*/
+
+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))
@@ -275,8 +325,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;
 }