* speedy, the specified number of servers is started
* right away.) The default is 4.
*
+ * -c<interval>
+ * Stale server check interval, in seconds. The worker
+ * process group will get a SIGTERM when it is no longer
+ * needed to process new requests. Ideally it would continue
+ * to serve any existing requests. The SIGTERM will arrive no
+ * earlier than <interval> after the last request arrived at
+ * the containing webserver. Default is 300.
+ *
* -D
* Debug mode. Do not actually run program. Instead, print
* out what we would do.
* ~/.cgi-fcgi-interp/<node>/
* and inside there uses these paths
* s<ident>
- * g<inum>
+ * l<ident> used to lock around garbage collection
*
* If -M<ident> is not specified then an initial substricg of the
* lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
* - check for and maybe create <base>
* - stat and lstat the <script>
* - stat the socket and check its timestamp
- * 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
+ * if it is too old, unlink it
+ * - dup stderr, mark no cloexec
+ * - run cgi-fcgi -connect SOCKET \
+ * cgi-fcgi-interp \
+ * --stage2 <was-stderr> <socket> \
+ -c<check-interval> \
+ * \
+ * <interp> <script>
+ *
+ * --stage2 does this:
+ * - dup2 <was-stderr> to fd 2
+ * - open /dev/null and expect fd 1 (and if not, close it)
+ * - become a new process group
+ * - lstat <socket> to find its inum, mtime
+ * - fork/exec <interp> <script>
+ * - periodically lstat <socket> and
+ * if inum, mtime have changed
+ * kill process group (at second iteration)
*/
#include "common.h"
#include <sys/utsname.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <sys/file.h>
#include <unistd.h>
+#include <fcntl.h>
#include <pwd.h>
#include <err.h>
-
+
#include <nettle/sha.h>
#include "myopt.h"
#define MINHEXHASH 33
static const char *interp, *ident;
-static int numservers, debugmode;
+static int numservers=4, debugmode, stage2;
+static int check_interval=300;
void diee(const char *m) {
err(127, "error: %s failed", m);
{ 0, 'g', 1, .sassignto= &ident },
{ 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
{ 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
+ { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
+ { "--stage2",0, 0, .iassignto= &stage2, .arg= 1 },
{ 0 }
};
}
static void tidy_garbage(void) {
- const char *this_garbage =
- m_asprintf("%s/%lu.%lu", run_base,
- (unsigned long)sock_stab.st_ino,
- (unsigned long)getpid());
+ /* We lock l<ident> and re-check. The effect of this is that each
+ * stale socket is removed only once. So unless multiple updates to
+ * the script happen rapidly, we can't be racing with the cgi-fcgi
+ * (which is recreating the socket */
+ int lockfd = -1;
+ int r;
+
+ const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
+
+ lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
+ if (lockfd<0) err(127,"create lock (%s)", lock_path);
+
+ r = flock(lockfd, LOCK_EX);
+ if (r) err(127,"lock lock (%s)", lock_path);
+
+ if (check_garbage()) {
+ r = unlink(socket_path);
+ if (r) {
+ if (!(errno == ENOENT))
+ err(127,"remove out-of-date socket (%s)", socket_path);
+ }
+ }
- printf("this_garb: %s\n", this_garbage);
+ r = close(lockfd);
+ if (r) errx(127,"close lock (%s)", lock_path);
}
static void shbang_opts(const char *const **argv_io,
find_socket_path();
- bool havegarbage = 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",havegarbage);
+ printf("garbage: %d\n",isgarbage);
exit(0);
}
- if (havegarbage)
+ if (isgarbage)
tidy_garbage();
exit(0);