/*
* "Interpreter" that you can put in #! like this
* #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
- * #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
+ *
+ * Usages:
+ * cgi-fcgi-interp [<option> ..] <interpreter> <script> [<ignored> ...]
+ * cgi-fcgi-interp [<option>,..],<interpreter> <script> [<ignored> ...]
+ * cgi-fcgi-interp '[<option> ..] <interpreter>' <script> [<ignored> ...]
*/
/*
* cgi-fcgi-interp.[ch] - Convenience wrapper for cgi-fcgi
* kill process group (at second iteration)
*/
-#include "common.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <stdbool.h>
-#include <assert.h>
-#include <limits.h>
-
-#include <sys/types.h>
-#include <sys/stat.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 <time.h>
-#include <signal.h>
-#include <sys/wait.h>
-#include <syslog.h>
-
-#include <nettle/sha.h>
-
-#include "myopt.h"
-
-#define MINHEXHASH 33
-#define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
-
-static const char *interp, *ident;
-static int numservers=4, debugmode;
-static int check_interval=300;
+#include "prefork.h"
-static struct sha256_ctx identsc;
+#define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
-static bool logging;
static const char *stage2;
-static void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
- int r;
-
- if (logging) {
- const char *fmt_use = fmt;
- char *fmt_free = 0;
- if (errnoval) {
- r = asprintf(&fmt_free, "%s: %%m", fmt);
- if (r) {
- fmt_free = 0;
- } else {
- fmt_use = fmt_free;
- }
- }
- vsyslog(LOG_ERR, fmt_use, al);
- free(fmt_free);
- } else {
- fputs("cgi-fcgi-interp: ",stderr);
- vfprintf(stderr,fmt,al);
- if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
- fputc('\n',stderr);
- }
- if (estatus) exit(estatus);
-}
-
-#define DEF_MSG(func, attrs, estatus, errnoval, after) \
- static void func(const char *fmt, ...) \
- __attribute__((format(printf,1,2))) attrs; \
- static void func(const char *fmt, ...) { \
- va_list al; \
- va_start(al,fmt); \
- vmsgcore(estatus,errnoval,fmt,al); \
- after \
- }
-
-DEF_MSG(warninge, /*empty*/, 0, errno, { });
-DEF_MSG(warning , /*empty*/, 0, 0, { });
-
-#define DEF_DIE(func, errnoval) \
- DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
-
-DEF_DIE(diee, errno)
-DEF_DIE(die, 0)
-
-void common_diee(const char *m) { diee("%s", m); }
-void common_die (const char *m) { die ("%s", m); }
-
-static void fusagemessage(FILE *f) {
- fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
-}
-
-void usagemessage(void) { fusagemessage(stderr); }
-
-static void of_help(const struct cmdinfo *ci, const char *val) {
- fusagemessage(stdout);
- if (ferror(stdout)) diee("write usage message to stdout");
- exit(0);
-}
-
-static void of_iassign(const struct cmdinfo *ci, const char *val) {
- long v;
- char *ep;
- errno= 0; v= strtol(val,&ep,10);
- if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
- badusage("bad integer argument `%s' for --%s",val,ci->olong);
- *ci->iassignto = v;
-}
-
-static void ident_addstring(const struct cmdinfo *ci, const char *string) {
- /* ci may be 0 and is provided so this can be .call */
- sha256_update(&identsc,strlen(string)+1,string);
-}
-
-static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
- const char *val = getenv(name);
- if (val) {
- sha256_update(&identsc,strlen(name),name); /* no nul */
- sha256_update(&identsc,1,"=");
- ident_addstring(0,val);
- } else {
- ident_addstring(0,name);
- }
-}
+const char our_name[] = "cgi-fcgi-interp";
-#define MAX_OPTS 5
-
-static const struct cmdinfo cmdinfos[]= {
+const struct cmdinfo cmdinfos[]= {
{ "help", 0, .call=of_help },
{ 0, 'g', 1, .sassignto= &ident },
{ 0, 'G', 1, .call= ident_addstring },
{ 0 }
};
-static uid_t us;
-static const char *run_base, *script, *socket_path;
-static const char *run_base_mkdir_p;
-static int stderr_copy;
-
-static bool find_run_base_var_run(void) {
- struct stat stab;
- char *try;
- int r;
-
- try = m_asprintf("%s/%lu", "/var/run/user", us);
- r = lstat(try, &stab);
- if (r<0) {
- if (errno == ENOENT ||
- errno == ENOTDIR ||
- errno == EACCES ||
- errno == EPERM)
- return 0; /* oh well */
- diee("stat /var/run/user/UID");
- }
- if (!S_ISDIR(stab.st_mode)) {
- warning("%s not a directory, falling back to ~\n", try);
- return 0;
- }
- if (stab.st_uid != us) {
- warning("%s not owned by uid %lu, falling back to ~\n", try,
- (unsigned long)us);
- return 0;
- }
- if (stab.st_mode & 0077) {
- warning("%s writeable by group or other, falling back to ~\n", try);
- return 0;
- }
- run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
- return 1;
-}
-
-static bool find_run_base_home(void) {
- struct passwd *pw;
- struct utsname ut;
- char *dot, *try;
- int r;
-
- pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
-
- r = uname(&ut); if (r) diee("uname(2)");
- dot = strchr(ut.nodename, '.');
- if (dot) *dot = 0;
- if (sizeof(ut.nodename) > 32)
- ut.nodename[32] = 0;
-
- run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
- try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
- run_base = try;
- return 1;
-}
-
-static void find_socket_path(void) {
- struct sockaddr_un sun;
- int r;
-
- us = getuid(); if (us==(uid_t)-1) diee("getuid");
-
- find_run_base_var_run() ||
- find_run_base_home() ||
- (abort(),0);
-
- int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
-
- if (!ident) {
- if (maxidentlen < MINHEXHASH)
- die("base directory `%s'"
- " leaves only %d characters for id hash"
- " which is too little (<%d)",
- run_base, maxidentlen, MINHEXHASH);
-
- int identlen = maxidentlen > 64 ? 64 : maxidentlen;
- char *hexident = xmalloc(identlen + 2);
- unsigned char bbuf[32];
- int i;
-
- ident_addstring(0,interp);
- ident_addstring(0,script);
- sha256_digest(&identsc,sizeof(bbuf),bbuf);
-
- for (i=0; i<identlen; i += 2)
- sprintf(hexident+i, "%02x", bbuf[i/2]);
-
- hexident[identlen] = 0;
- ident = hexident;
- }
-
- if (strlen(ident) > maxidentlen)
- die("base directory `%s' plus ident `%s' too long"
- " (with spare) for socket (max ident %d)\n",
- run_base, ident, maxidentlen);
-
- r = mkdir(run_base, 0700);
- if (r && errno==ENOENT && run_base_mkdir_p) {
- r = mkdir(run_base_mkdir_p, 0700);
- if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
- r = mkdir(run_base, 0700);
- }
- if (r) {
- if (!(errno == EEXIST))
- diee("mkdir %s",run_base);
- }
-
- socket_path = m_asprintf("%s/s%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*/
-
-
-
-#ifdef st_mtime
-
-static bool stab_isnewer(const struct stat *a, const struct stat *b) {
- if (debugmode)
- fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
- (unsigned long)a->st_mtim.tv_sec,
- (unsigned long)a->st_mtim.tv_nsec,
- (unsigned long)b->st_mtim.tv_sec,
- (unsigned long)b->st_mtim.tv_nsec);
- return timespeccmp(&a->st_mtim, &b->st_mtim, >);
-}
-
-static void stab_mtimenow(struct stat *out) {
- int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
- if (r) diee("(stage2) clock_gettime");
- if (debugmode)
- fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
- (unsigned long)out->st_mtim.tv_sec,
- (unsigned long)out->st_mtim.tv_nsec);
-}
-
-#else /* !defined(st_mtime) */
-
-static bool stab_isnewer(const struct stat *a, const struct stat *b) {
- if (debugmode)
- fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
- (unsigned long)a->st_mtime,
- (unsigned long)b->st_mtime);
- return a->st_mtime > b->st_mtime;
-}
-
-static void stab_mtimenow(struct stat *out) {
- out->st_mtime = time(NULL);
- if (out->st_mtime == (time_t)-1) diee("(stage2) time()");
- if (debugmode)
- fprintf(stderr,"stab_mtimenow mtime %lu\n",
- (unsigned long)out->st_mtime);
-}
-
-#endif /* !defined(st_mtime) */
-
-static bool check_garbage_vs(const struct stat *started) {
- struct stat script_stab;
- int r;
-
- r = lstat(script, &script_stab);
- if (r) diee("lstat script (%s)",script);
-
- if (stab_isnewer(&script_stab, started))
- return 1;
-
- if (S_ISLNK(script_stab.st_mode)) {
- r = stat(script, &script_stab);
- if (r) diee("stat script (%s0",script);
-
- if (stab_isnewer(&script_stab, started))
- return 1;
- }
-
- return 0;
-}
-
-static bool check_garbage(void) {
- struct stat sock_stab;
- int r;
-
- r = lstat(socket_path, &sock_stab);
- if (r) {
- if ((errno == ENOENT))
- return 0; /* well, no garbage then */
- diee("stat socket (%s)",socket_path);
- }
-
- return check_garbage_vs(&sock_stab);
+void fusagemessage(FILE *f) {
+ fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
}
-static void tidy_garbage(void) {
- /* 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) diee("create lock (%s)", lock_path);
-
- r = flock(lockfd, LOCK_EX);
- if (r) diee("lock lock (%s)", lock_path);
-
- if (check_garbage()) {
- r = unlink(socket_path);
- if (r) {
- if (!(errno == ENOENT))
- diee("remove out-of-date socket (%s)", socket_path);
- }
- }
-
- r = close(lockfd);
- if (r) diee("close lock (%s)", lock_path);
-}
+static int stderr_copy;
static void make_stderr_copy(void) {
stderr_copy = dup(2);
if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
}
-static void shbang_opts(const char *const **argv_io,
- const struct cmdinfo *cmdinfos) {
- myopt(argv_io, cmdinfos);
-
- interp = *(*argv_io)++;
- if (!interp) badusage("need interpreter argument");
-}
-
/* stage2 predeclarations */
static void record_baseline_time(void);
static void become_pgrp(void);
static void start_logging(void);
static void await_something(void);
-int main(int argc, const char *const *argv) {
- const char *smashedopt;
+int main(int unused_argc, const char *const *argv) {
int r;
stage2 = getenv(STAGE2_VAR);
if (r) diee("close saved stderr fd");
}
- sha256_init(&identsc);
-
- if (argc>=2 &&
- (smashedopt = argv[1]) &&
- smashedopt[0]=='-' &&
- (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
- /* single argument containg all the options and <interp> */
- argv += 2; /* eat argv[0] and smashedopt */
- const char *split_args[MAX_OPTS+1];
- int split_argc = 0;
- split_args[split_argc++] = argv[0];
- for (;;) {
- if (split_argc >= MAX_OPTS) die("too many options in combined arg");
- split_args[split_argc++] = smashedopt;
- if (smashedopt[0] != '-') /* never true on first iteration */
- break;
- char *delim = strchr(smashedopt,' ');
- if (!delim) delim = strchr(smashedopt,',');
- if (!delim) badusage("combined arg lacks <interpreter>");
- *delim = 0;
- smashedopt = delim+1;
- }
- assert(split_argc <= MAX_OPTS);
- split_args[split_argc++] = 0;
-
- const char *const *split_argv = split_args;
-
- shbang_opts(&split_argv, cmdinfos);
- /* sets interp */
- if (!split_argv) badusage("combined arg too many non-option arguments");
- } else {
- shbang_opts(&argv, cmdinfos);
- }
-
- script = *argv++;
- if (!script) badusage("need script argument");
+ process_opts(&argv);
if (!stage2) {