* 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.
* - 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
+ * - dup2 stderr, mark no cloexec
+ * - run cgi-fcgi -connect SOCKET \
+ * cgi-fcgi-interp --stage2 <was-stderr> <socket> <interp> <script>
+ *
+ * -E<was-stderr> 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"
static uid_t us;
static const char *run_base, *script, *socket_path;
+static struct stat sock_stab;
static bool find_run_base_var_run(void) {
struct stat stab;
((tvp)->tv_sec cmp (uvp)->tv_sec))
#endif /*timespeccmp*/
+
+
static bool stab_isnewer(const struct stat *a, const struct stat *b) {
- return 0;
+#ifdef st_mtime
+ return timespeccmp(&a->st_mtim, &b->st_mtim, >);
+#else
+ return a->st_mtime > &b->st_mtime;
+#endif
}
static bool check_garbage(void) {
- struct stat sock_stab, script_stab;
+ struct stat script_stab;
int r;
r = lstat(script, &script_stab);
return 0;
}
+static void tidy_1_garbage(const char *leaf) {
+ int r;
+ struct sockaddr_un sun;
+
+ int fd = -1;
+
+ memset(&sun,0,sizeof(sun));
+ sun.sun_family = AF_UNIX;
+ r = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", run_base, leaf);
+ if (r >= sizeof(sun_path))
+ goto cannot;
+
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd<0) err("create socket for tidying garbage");
+
+ r = fcntl(fd, F_SETFL, O_NONBLOCK);
+ if (r<0) err("set garbage socket nonblocking");
+
+ r = connect(fd, &sun, sizeof(sun));
+ if (r) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
+ goto cannot;
+ if (errno != EINPROGRESS)
+ err("connect to garbage socket (%s)", sun.sun_path);
+ /* well, EINPROGRESS, let's just carry on and hope write works */
+ }
+
+ r = write(
+
+
+static void tidy_garbage(void) {
+ const char *this_garbage =
+ m_asprintf("%s/g%lu.%lu", run_base,
+ (unsigned long)sock_stab.st_ino,
+ (unsigned long)getpid());
+
+ r = rename(socket_path, this_garbage);
+ if (r) {
+ if (!(errno == ENOENT))
+ err("rename socket from old runner (from %s to %s)",
+ socket_path, this_garbage);
+ }
+
+ DIR *d = opendir(run_base);
+ if (!d) err("open run directory (%d) to clean up garbage", run_base);
+ struct dirent *de;
+ while ((errno = 0, de = readdir(d))) {
+ if (de->d_name[0] != 'g')
+ continue;
+ tidy_1_garbage(de->d_name);
+ }
+ if (errno) err("read run directory (%d) to clean up garbage", run_base);
+ /*
+ (void)r;
+ if (r) {
+
+ printf("this_garb: %s\n", this_garbage);
+}
+
static void shbang_opts(const char *const **argv_io,
const struct cmdinfo *cmdinfos) {
myopt(argv_io, cmdinfos);
find_socket_path();
- check_garbage();
+ bool havegarbage = 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);
+ exit(0);
}
+ if (havegarbage)
+ tidy_garbage();
+
exit(0);
}