2 * "Interpreter" that you can put in #! like this
3 * #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
4 * #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
7 * cgi-fcgi-interp.[ch] - Convenience wrapper for cgi-fcgi
9 * Copyright 2016 Ian Jackson
10 * Copyright 1982,1986,1993 The Regents of the University of California
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public
23 * License along with this file; if not, consult the Free Software
24 * Foundation's website at www.fsf.org, or the GNU Project website at
27 * See below for a BSD 3-clause notice regarding timespeccmp.
30 * The result is a program which looks, when executed via the #!
31 * line, like a CGI program. But the script inside will be executed
32 * via <interpreter> in an fcgi context.
37 * The real interpreter to use. Eg "perl". Need not
38 * be an absolute path; will be fed to execvp.
41 * Add <ident-info> to the unique identifying information for
42 * this fcgi program. May be repeated; order is significant.
44 * -E<ident-info-env-var>
45 * Look <ident-info-env-var> up in the environment and add
46 * <ident-info-env-var>=<value> as if specified with -G. If
47 * the variable is unset in the environment, it is as if
48 * -G<ident-info-env-var> was specified.
51 * Use <ident> rather than hex(sha256(<interp>\0<script>\0))
52 * as the basename of the leafname of the fcgi rendezvous
53 * socket. If <ident> contains only hex digit characters it
54 * ought to be no more than 32 characters. <ident> should
55 * not contain spaces or commas (see below).
58 * Start <numservers> instances of the program. This
59 * determines the maximum concurrency. (Note that unlike
60 * speedy, the specified number of servers is started
61 * right away.) The default is 4.
64 * Stale server check interval, in seconds. The worker
65 * process group will get a SIGTERM when it is no longer
66 * needed to process new requests. Ideally it would continue
67 * to serve any existing requests. The SIGTERM will arrive no
68 * earlier than <interval> after the last request arrived at
69 * the containing webserver. Default is 300.
72 * Debug mode. Do not actually run program. Instead, print
73 * out what we would do.
75 * <options> and <interpreter> can be put into a single argument
76 * to cgi-fcgi-interp, separated by spaces or commas. <interpreter>
79 * cgi-fcgi-interp automatically expires old sockets, including
80 * ones where the named script is out of date.
83 * Uses one of two directories
84 * /var/run/user/<UID>/cgi-fcgi-interp/
85 * ~/.cgi-fcgi-interp/<node>/
86 * and inside there uses these paths
88 * l<ident> used to lock around garbage collection
90 * If -M<ident> is not specified then an initial substring of the
91 * lowercase hex of the sha256 of <interp>\0<script>\0 is
92 * used. The substring is chosen so that the whole path is 10 bytes
93 * shorter than sizeof(sun_path). But always at least 33 characters.
95 * <node> is truncated at the first `.' and after the first 32
99 * - see if /var/run/user exists
100 * if so, lstat /var/run/user/<UID> and check that
101 * we own it and it's X700; if not, fail
102 * if it's ok then <base> is /var/run/user/<UID>
103 * otherwise, look for and maybe create ~/.cgi-fcgi-interp
104 * (where ~ is HOME or from getpwuid)
105 * and then <base> is ~/.cgi-fcgi-interp/<node>
106 * - calculate pathname (checking <ident> length is OK)
107 * - check for and maybe create <base>
108 * - stat and lstat the <script>
109 * - stat the socket and check its timestamp
110 * if it is too old, unlink it
111 * - dup stderr, mark no cloexec
112 * - set CHIARKUTILS_CGIFCGIINTERP_STAGE2=<stderr-copy-fd>
113 * - run cgi-fcgi -connect SOCKET <script>
115 * When CHIARKUTILS_CGIFCGIINTERP_STAGE2 is set, --stage2 does this:
116 * - dup2 <was-stderr> to fd 2
117 * - open /dev/null and expect fd 1 (and if not, close it)
118 * - become a new process group
119 * - lstat <socket> to find its inum, mtime
120 * - fork/exec <interp> <script>
121 * - periodically lstat <interp> and <script> and
122 * if mtime is newer than our start time
123 * kill process group (at second iteration)
136 #include <sys/types.h>
137 #include <sys/stat.h>
138 #include <sys/utsname.h>
139 #include <sys/socket.h>
141 #include <sys/file.h>
147 #include <sys/wait.h>
150 #include <nettle/sha.h>
154 #define MINHEXHASH 33
155 #define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
157 static const char *interp, *ident;
158 static int numservers=4, debugmode;
159 static int check_interval=300;
161 static struct sha256_ctx identsc;
164 static const char *stage2;
166 static void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
170 const char *fmt_use = fmt;
173 r = asprintf(&fmt_free, "%s: %%m", fmt);
180 vsyslog(LOG_ERR, fmt_use, al);
183 fputs("cgi-fcgi-interp: ",stderr);
184 vfprintf(stderr,fmt,al);
185 if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
188 if (estatus) exit(estatus);
191 #define DEF_MSG(func, attrs, estatus, errnoval, after) \
192 static void func(const char *fmt, ...) \
193 __attribute__((format(printf,1,2))) attrs; \
194 static void func(const char *fmt, ...) { \
197 vmsgcore(estatus,errnoval,fmt,al); \
201 DEF_MSG(warninge, /*empty*/, 0, errno, { });
202 DEF_MSG(warning , /*empty*/, 0, 0, { });
204 #define DEF_DIE(func, errnoval) \
205 DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
210 void common_diee(const char *m) { diee("%s", m); }
211 void common_die (const char *m) { die ("%s", m); }
213 static void fusagemessage(FILE *f) {
214 fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
217 void usagemessage(void) { fusagemessage(stderr); }
219 static void of_help(const struct cmdinfo *ci, const char *val) {
220 fusagemessage(stdout);
221 if (ferror(stdout)) diee("write usage message to stdout");
225 static void of_iassign(const struct cmdinfo *ci, const char *val) {
228 errno= 0; v= strtol(val,&ep,10);
229 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
230 badusage("bad integer argument `%s' for --%s",val,ci->olong);
234 static void ident_addstring(const struct cmdinfo *ci, const char *string) {
235 /* ci may be 0 and is provided so this can be .call */
236 sha256_update(&identsc,strlen(string)+1,string);
239 static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
240 const char *val = getenv(name);
242 sha256_update(&identsc,strlen(name),name); /* no nul */
243 sha256_update(&identsc,1,"=");
244 ident_addstring(0,val);
246 ident_addstring(0,name);
252 static const struct cmdinfo cmdinfos[]= {
253 { "help", 0, .call=of_help },
254 { 0, 'g', 1, .sassignto= &ident },
255 { 0, 'G', 1, .call= ident_addstring },
256 { 0, 'E', 1, .call= off_ident_addenv },
257 { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
258 { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
259 { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
264 static const char *run_base, *script, *socket_path;
265 static const char *run_base_mkdir_p;
266 static int stderr_copy;
268 static bool find_run_base_var_run(void) {
273 try = m_asprintf("%s/%lu", "/var/run/user", us);
274 r = lstat(try, &stab);
276 if (errno == ENOENT ||
280 return 0; /* oh well */
281 diee("stat /var/run/user/UID");
283 if (!S_ISDIR(stab.st_mode)) {
284 warning("%s not a directory, falling back to ~\n", try);
287 if (stab.st_uid != us) {
288 warning("%s not owned by uid %lu, falling back to ~\n", try,
292 if (stab.st_mode & 0077) {
293 warning("%s writeable by group or other, falling back to ~\n", try);
296 run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
300 static bool find_run_base_home(void) {
306 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
308 r = uname(&ut); if (r) diee("uname(2)");
309 dot = strchr(ut.nodename, '.');
311 if (sizeof(ut.nodename) > 32)
314 run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
315 try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
320 static void find_socket_path(void) {
321 struct sockaddr_un sun;
324 us = getuid(); if (us==(uid_t)-1) diee("getuid");
326 find_run_base_var_run() ||
327 find_run_base_home() ||
330 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
333 if (maxidentlen < MINHEXHASH)
334 die("base directory `%s'"
335 " leaves only %d characters for id hash"
336 " which is too little (<%d)",
337 run_base, maxidentlen, MINHEXHASH);
339 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
340 char *hexident = xmalloc(identlen + 2);
341 unsigned char bbuf[32];
344 ident_addstring(0,interp);
345 ident_addstring(0,script);
346 sha256_digest(&identsc,sizeof(bbuf),bbuf);
348 for (i=0; i<identlen; i += 2)
349 sprintf(hexident+i, "%02x", bbuf[i/2]);
351 hexident[identlen] = 0;
355 if (strlen(ident) > maxidentlen)
356 die("base directory `%s' plus ident `%s' too long"
357 " (with spare) for socket (max ident %d)\n",
358 run_base, ident, maxidentlen);
360 r = mkdir(run_base, 0700);
361 if (r && errno==ENOENT && run_base_mkdir_p) {
362 r = mkdir(run_base_mkdir_p, 0700);
363 if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
364 r = mkdir(run_base, 0700);
367 if (!(errno == EEXIST))
368 diee("mkdir %s",run_base);
371 socket_path = m_asprintf("%s/s%s",run_base,ident);
375 * Regarding the macro timespeccmp:
377 * Copyright (c) 1982, 1986, 1993
378 * The Regents of the University of California. All rights reserved.
380 * Redistribution and use in source and binary forms, with or without
381 * modification, are permitted provided that the following conditions
383 * 1. Redistributions of source code must retain the above copyright
384 * notice, this list of conditions and the following disclaimer.
385 * 2. Redistributions in binary form must reproduce the above copyright
386 * notice, this list of conditions and the following disclaimer in the
387 * documentation and/or other materials provided with the distribution.
388 * 4. Neither the name of the University nor the names of its contributors
389 * may be used to endorse or promote products derived from this software
390 * without specific prior written permission.
392 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
393 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
394 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
395 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
396 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
397 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
398 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
399 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
400 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
401 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
404 * @(#)time.h 8.5 (Berkeley) 5/4/95
405 * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
408 #define timespeccmp(tvp, uvp, cmp) \
409 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
410 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
411 ((tvp)->tv_sec cmp (uvp)->tv_sec))
412 #endif /*timespeccmp*/
418 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
420 fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
421 (unsigned long)a->st_mtim.tv_sec,
422 (unsigned long)a->st_mtim.tv_nsec,
423 (unsigned long)b->st_mtim.tv_sec,
424 (unsigned long)b->st_mtim.tv_nsec);
425 return timespeccmp(&a->st_mtim, &b->st_mtim, >);
428 static void stab_mtimenow(struct stat *out) {
429 int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
430 if (r) diee("(stage2) clock_gettime");
432 fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
433 (unsigned long)out->st_mtim.tv_sec,
434 (unsigned long)out->st_mtim.tv_nsec);
437 #else /* !defined(st_mtime) */
439 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
441 fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
442 (unsigned long)a->st_mtime,
443 (unsigned long)b->st_mtime);
444 return a->st_mtime > &b->st_mtime;
447 static void stab_mtimenow(struct stat *out) {
448 out->st_mtime = time(NULL);
449 if (baseline_time.st_mtime == (time_t)-1) diee("(stage2) time()");
451 fprintf(stderr,"stab_mtimenow mtime %lu\n",
452 (unsigned long)out->st_mtime);
455 #endif /* !defined(st_mtime) */
457 static bool check_garbage_vs(const struct stat *started) {
458 struct stat script_stab;
461 r = lstat(script, &script_stab);
462 if (r) diee("lstat script (%s)",script);
464 if (stab_isnewer(&script_stab, started))
467 if (S_ISLNK(script_stab.st_mode)) {
468 r = stat(script, &script_stab);
469 if (r) diee("stat script (%s0",script);
471 if (stab_isnewer(&script_stab, started))
478 static bool check_garbage(void) {
479 struct stat sock_stab;
482 r = lstat(socket_path, &sock_stab);
484 if ((errno == ENOENT))
485 return 0; /* well, no garbage then */
486 diee("stat socket (%s)",socket_path);
489 return check_garbage_vs(&sock_stab);
492 static void tidy_garbage(void) {
493 /* We lock l<ident> and re-check. The effect of this is that each
494 * stale socket is removed only once. So unless multiple updates to
495 * the script happen rapidly, we can't be racing with the cgi-fcgi
496 * (which is recreating the socket */
500 const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
502 lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
503 if (lockfd<0) diee("create lock (%s)", lock_path);
505 r = flock(lockfd, LOCK_EX);
506 if (r) diee("lock lock (%s)", lock_path);
508 if (check_garbage()) {
509 r = unlink(socket_path);
511 if (!(errno == ENOENT))
512 diee("remove out-of-date socket (%s)", socket_path);
517 if (r) diee("close lock (%s)", lock_path);
520 static void make_stderr_copy(void) {
521 stderr_copy = dup(2);
522 if (stderr_copy < 0) diee("dup stderr (for copy for stage2)");
525 static void prep_stage2(void) {
528 const char *stage2_val = m_asprintf("%d", stderr_copy);
529 r = setenv(STAGE2_VAR, stage2_val, 1);
530 if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
533 static void shbang_opts(const char *const **argv_io,
534 const struct cmdinfo *cmdinfos) {
535 myopt(argv_io, cmdinfos);
537 interp = *(*argv_io)++;
538 if (!interp) badusage("need interpreter argument");
541 /* stage2 predeclarations */
542 static void record_baseline_time(void);
543 static void become_pgrp(void);
544 static void setup_handlers(void);
545 static void spawn_script(void);
546 static void queue_alarm(void);
547 static void start_logging(void);
548 static void await_something(void);
550 int main(int argc, const char *const *argv) {
551 const char *smashedopt;
554 stage2 = getenv(STAGE2_VAR);
556 int stderrfd = atoi(stage2);
559 r = dup2(stderrfd, 2);
562 r = open("/dev/null",O_WRONLY);
563 if (r<0) diee("open /dev/null as stdout");
565 else if (r!=1) die("open /dev/null for stdout gave bad fd %d",r);
568 if (r) diee("close saved stderr fd");
571 sha256_init(&identsc);
574 (smashedopt = argv[1]) &&
575 smashedopt[0]=='-' &&
576 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
577 /* single argument containg all the options and <interp> */
578 argv += 2; /* eat argv[0] and smashedopt */
579 const char *split_args[MAX_OPTS+1];
581 split_args[split_argc++] = argv[0];
583 if (split_argc >= MAX_OPTS) die("too many options in combined arg");
584 split_args[split_argc++] = smashedopt;
585 if (smashedopt[0] != '-') /* never true on first iteration */
587 char *delim = strchr(smashedopt,' ');
588 if (!delim) delim = strchr(smashedopt,',');
589 if (!delim) badusage("combined arg lacks <interpreter>");
591 smashedopt = delim+1;
593 assert(split_argc <= MAX_OPTS);
594 split_args[split_argc++] = 0;
596 const char *const *split_argv = split_args;
598 shbang_opts(&split_argv, cmdinfos);
600 if (!split_argv) badusage("combined arg too many non-option arguments");
602 shbang_opts(&argv, cmdinfos);
606 if (!script) badusage("need script argument");
612 bool isgarbage = check_garbage();
615 printf("socket: %s\n",socket_path);
616 printf("interp: %s\n",interp);
617 printf("script: %s\n",script);
618 printf("garbage: %d\n",isgarbage);
629 "cgi-fcgi", "-connect", socket_path,
631 m_asprintf("%d", numservers),
633 diee("exec cgi-fcgi");
637 record_baseline_time();
651 /* It is most convenient to handle the recheck timeout, as well as
652 * child death, in signal handlers. Our signals all block each other,
653 * and the main program has signals blocked except in sigsuspend, so
654 * we don't need to worry about async-signal-safety, or errno. */
656 static struct stat baseline_time;
657 static pid_t script_child, stage2_pgrp;
658 static bool out_of_date;
661 static void record_baseline_time(void) {
662 stab_mtimenow(&baseline_time);
665 static void become_pgrp(void) {
668 stage2_pgrp = getpid();
671 if (r) diee("(stage2) setpgid");
674 static void atexit_handler(void) {
677 sighandler_t sigr = signal(SIGTERM,SIG_IGN);
678 if (sigr == SIG_ERR) warninge("(stage2) signal(SIGTERM,SIG_IGN)");
680 r = killpg(stage2_pgrp,SIGTERM);
681 if (r) warninge("(stage) killpg failed");
684 static void alarm_handler(int dummy) {
687 exit(0); /* transfers control to atexit_handler */
689 out_of_date = check_garbage_vs(&baseline_time);
693 static void child_handler(int dummy) {
696 pid_t got = waitpid(-1, &status, WNOHANG);
697 if (got == (pid_t)-1) diee("(stage2) waitpid");
698 if (got != script_child) {
699 warning("(stage2) waitpid got status %d for unknown child [%lu]",
700 status, (unsigned long)got);
703 if (WIFEXITED(status)) {
704 int v = WEXITSTATUS(status);
705 if (v) warning("program failed with error exit status %d", v);
707 } else if (WIFSIGNALED(status)) {
708 int s = WTERMSIG(status);
709 warning("program died due to fatal signal %s%s",
710 strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
711 assert(status & 0xff);
714 die("program failed with crazy wait status %#x", status);
720 static void setup_handlers(void) {
724 r = atexit(atexit_handler);
725 if (r) diee("(stage2) atexit");
727 sigemptyset(&sa.sa_mask);
728 sigaddset(&sa.sa_mask, SIGALRM);
729 sigaddset(&sa.sa_mask, SIGCHLD);
732 r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
733 if (r) diee("(stage2) sigprocmask(SIG_BLOCK,)");
735 sa.sa_handler = alarm_handler;
736 r = sigaction(SIGALRM, &sa, 0);
737 if (r) diee("(stage2) sigaction SIGALRM");
739 sa.sa_flags |= SA_NOCLDSTOP;
740 sa.sa_handler = child_handler;
741 r = sigaction(SIGCHLD, &sa, 0);
742 if (r) diee("(stage2) sigaction SIGCHLD");
745 static void spawn_script(void) {
750 if (r) diee("(stage2) pipe");
752 script_child = fork();
753 if (script_child == (pid_t)-1) diee("(stage2) fork");
755 r = close(errpipes[0]);
756 if (r) diee("(stage2 child) close errpipes[0]");
758 r = dup2(errpipes[1], 2);
759 if (r != 2) diee("(stage2 child) dup2 stderr");
762 interp, script, (char*)0);
763 diee("(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
766 r = close(errpipes[1]);
767 if (r) diee("(stage2) close errpipes[1]");
769 errpipe = errpipes[0];
770 r = fcntl(errpipe, F_SETFL, O_NONBLOCK);
771 if (r) diee("(stage2) set errpipe nonblocking");
774 static void queue_alarm(void) {
775 alarm(check_interval);
778 static void start_logging(void) {
781 openlog(script, LOG_NOWAIT|LOG_PID, LOG_USER);
784 if (r!=2) diee("dup2 stdout to stderr");
787 static void errpipe_readable(void) {
788 static char buf[1024];
791 /* %: does not contain newlines
795 /* %%%%%%%%%%%__________________ */
799 int avail = sizeof(buf) - pending;
800 ssize_t got = read(errpipe, buf+pending, avail);
802 if (errno==EINTR) continue;
803 else if (errno==EWOULDBLOCK || errno==EAGAIN) return;
804 else diee("(stage2) errpipe read");
807 warning("program closed its stderr fd");
811 int scanned = pending;
815 const char *newline = memchr(buf+scanned, '\n', pending-scanned);
818 printupto = newline-buf;
820 } else if (!eaten && pending==sizeof(buf)) { /* overflow */
826 syslog(LOG_ERR,"stderr: %.*s", printupto-eaten, buf+eaten);
831 memmove(buf, buf+eaten, pending);
835 static void await_something(void) {
844 FD_SET(errpipe, &rfds);
845 r = pselect(errpipe+1, &rfds,0,0, 0, &mask);
847 if (errno != EINTR) diee("(stage2) sigsuspend");
851 assert(FD_ISSET(errpipe, &rfds));