* "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.
* speedy, the specified number of servers is started
* right away.) The default is 4.
*
+ * -D
+ * Debug mode. Do not actually run program. Instead, print
+ * out what we would do.
+ *
* <options> and <interpreter> can be put into a single argument
* to cgi-fcgi-interp, separated by spaces or commas. <interpreter>
* must come last.
* - 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
*/
#include <errno.h>
#include <stdbool.h>
#include <assert.h>
+#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MINHEXHASH 33
static const char *interp, *ident;
-static int numservers;
+static int numservers, debugmode;
void diee(const char *m) {
err(127, "error: %s failed", m);
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;
+}
+
#define MAX_OPTS 5
static const struct cmdinfo cmdinfos[]= {
{ "help", 0, .call= of_help },
{ 0, 'g', 1, .sassignto= &ident },
- { 0, 'M', 1, .iassignto= &numservers },
+ { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
+ { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
{ 0 }
};
static uid_t us;
-static const char *run_base, *command, *socket_path;
+static const char *run_base, *script, *socket_path;
static bool find_run_base_var_run(void) {
struct stat stab;
if (!ident) {
if (maxidentlen < MINHEXHASH)
errx(127,"base directory `%s'"
- " leaves only %d characters for command name hash"
+ " leaves only %d characters for id hash"
" which is too little (<%d)",
run_base, maxidentlen, MINHEXHASH);
sha256_init(&sc);
sha256_update(&sc,strlen(interp)+1,interp);
- sha256_update(&sc,strlen(command)+1,command);
+ sha256_update(&sc,strlen(script)+1,script);
sha256_digest(&sc,sizeof(bbuf),bbuf);
for (i=0; i<identlen; i += 2)
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))
err(127,"stat socket (%s)",socket_path);
}
- r = lstat(command, &cmd_stab);
- if (r) err(127,"lstat command (%s)",command);
+ 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;
}
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) errx(127,"too many options in combined arg");
split_args[split_argc++] = smashedopt;
shbang_opts(&argv, cmdinfos);
}
- command = *argv++;
- if (!command) errx(127,"need command argument");
+ script = *argv++;
+ if (!script) errx(127,"need script argument");
if (*argv) errx(127,"too many arguments");
find_socket_path();
check_garbage();
- printf("socket: %s\n",socket_path);
- printf("interp: %s\n",interp);
- printf("command: %s\n",command);
+ if (debugmode) {
+ printf("socket: %s\n",socket_path);
+ printf("interp: %s\n",interp);
+ printf("script: %s\n",script);
+ }
exit(0);
}