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] - C helpers common to the whole of chiark-utils
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>
148 #include <sys/wait.h>
150 #include <nettle/sha.h>
154 #define die common_die
155 #define diee common_diee
157 #define MINHEXHASH 33
158 #define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
160 static const char *interp, *ident;
161 static int numservers=4, debugmode;
162 static int check_interval=300;
164 static struct sha256_ctx identsc;
168 void diee(const char *m) {
169 err(127, "error: %s failed", m);
172 static void fusagemessage(FILE *f) {
173 fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
176 void usagemessage(void) { fusagemessage(stderr); }
178 static void of_help(const struct cmdinfo *ci, const char *val) {
179 fusagemessage(stdout);
180 if (ferror(stdout)) diee("write usage message to stdout");
184 static void of_iassign(const struct cmdinfo *ci, const char *val) {
187 errno= 0; v= strtol(val,&ep,10);
188 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
189 badusage("bad integer argument `%s' for --%s",val,ci->olong);
193 static void ident_addstring(const struct cmdinfo *ci, const char *string) {
194 /* ci may be 0 and is provided so this can be .call */
195 sha256_update(&identsc,strlen(string)+1,string);
198 static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
199 const char *val = getenv(name);
201 sha256_update(&identsc,strlen(name),name); /* no nul */
202 sha256_update(&identsc,1,"=");
203 ident_addstring(0,val);
205 ident_addstring(0,name);
211 static const struct cmdinfo cmdinfos[]= {
212 { "help", 0, .call=of_help },
213 { 0, 'g', 1, .sassignto= &ident },
214 { 0, 'G', 1, .call= ident_addstring },
215 { 0, 'E', 1, .call= off_ident_addenv },
216 { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
217 { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
218 { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
223 static const char *run_base, *script, *socket_path;
224 static const char *run_base_mkdir_p;
225 static int stderr_copy;
227 static bool find_run_base_var_run(void) {
232 try = m_asprintf("%s/%lu", "/var/run/user", us);
233 r = lstat(try, &stab);
235 if (errno == ENOENT ||
239 return 0; /* oh well */
240 diee("stat /var/run/user/UID");
242 if (!S_ISDIR(stab.st_mode)) {
243 warnx("%s not a directory, falling back to ~\n", try);
246 if (stab.st_uid != us) {
247 warnx("%s not owned by uid %lu, falling back to ~\n", try,
251 if (stab.st_mode & 0077) {
252 warnx("%s writeable by group or other, falling back to ~\n", try);
255 run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
259 static bool find_run_base_home(void) {
265 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
267 r = uname(&ut); if (r) diee("uname(2)");
268 dot = strchr(ut.nodename, '.');
270 if (sizeof(ut.nodename) > 32)
273 run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
274 try = m_asprintf("%/%s", run_base_mkdir_p, ut.nodename);
279 static void find_socket_path(void) {
280 struct sockaddr_un sun;
283 us = getuid(); if (us==(uid_t)-1) diee("getuid");
285 find_run_base_var_run() ||
286 find_run_base_home() ||
289 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
292 if (maxidentlen < MINHEXHASH)
293 errx(127,"base directory `%s'"
294 " leaves only %d characters for id hash"
295 " which is too little (<%d)",
296 run_base, maxidentlen, MINHEXHASH);
298 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
299 char *hexident = xmalloc(identlen + 2);
300 unsigned char bbuf[32];
303 ident_addstring(0,interp);
304 ident_addstring(0,script);
305 sha256_digest(&identsc,sizeof(bbuf),bbuf);
307 for (i=0; i<identlen; i += 2)
308 sprintf(hexident+i, "%02x", bbuf[i/2]);
310 hexident[identlen] = 0;
314 if (strlen(ident) > maxidentlen)
315 errx(127, "base directory `%s' plus ident `%s' too long"
316 " (with spare) for socket (max ident %d)\n",
317 run_base, ident, maxidentlen);
319 r = mkdir(run_base, 0700);
320 if (r && errno==ENOENT && run_base_mkdir_p) {
321 r = mkdir(run_base_mkdir_p, 0700);
322 if (r) err(127,"mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
323 r = mkdir(run_base, 0700);
326 if (!(errno == EEXIST))
327 err(127,"mkdir %s",run_base);
330 socket_path = m_asprintf("%s/s%s",run_base,ident);
334 * Regarding the macro timespeccmp:
336 * Copyright (c) 1982, 1986, 1993
337 * The Regents of the University of California. All rights reserved.
339 * Redistribution and use in source and binary forms, with or without
340 * modification, are permitted provided that the following conditions
342 * 1. Redistributions of source code must retain the above copyright
343 * notice, this list of conditions and the following disclaimer.
344 * 2. Redistributions in binary form must reproduce the above copyright
345 * notice, this list of conditions and the following disclaimer in the
346 * documentation and/or other materials provided with the distribution.
347 * 4. Neither the name of the University nor the names of its contributors
348 * may be used to endorse or promote products derived from this software
349 * without specific prior written permission.
351 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
352 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
353 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
354 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
355 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
356 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
357 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
359 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
360 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
363 * @(#)time.h 8.5 (Berkeley) 5/4/95
364 * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
367 #define timespeccmp(tvp, uvp, cmp) \
368 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
369 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
370 ((tvp)->tv_sec cmp (uvp)->tv_sec))
371 #endif /*timespeccmp*/
377 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
379 fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
380 (unsigned long)a->st_mtim.tv_sec,
381 (unsigned long)a->st_mtim.tv_nsec,
382 (unsigned long)b->st_mtim.tv_sec,
383 (unsigned long)b->st_mtim.tv_nsec);
384 return timespeccmp(&a->st_mtim, &b->st_mtim, >);
387 static void stab_mtimenow(struct stat *out) {
388 int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
389 if (r) err(127,"(stage2) clock_gettime");
391 fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
392 (unsigned long)out->st_mtim.tv_sec,
393 (unsigned long)out->st_mtim.tv_nsec);
396 #else /* !defined(st_mtime) */
398 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
400 fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
401 (unsigned long)a->st_mtime,
402 (unsigned long)b->st_mtime);
403 return a->st_mtime > &b->st_mtime;
406 static void stab_mtimenow(struct stat *out) {
407 out->st_mtime = time(NULL);
408 if (baseline_time.st_mtime == (time_t)-1) err(127,"(stage2) time()");
410 fprintf(stderr,"stab_mtimenow mtime %lu\n",
411 (unsigned long)out->st_mtime);
414 #endif /* !defined(st_mtime) */
416 static bool check_garbage_vs(const struct stat *started) {
417 struct stat script_stab;
420 r = lstat(script, &script_stab);
421 if (r) err(127,"lstat script (%s)",script);
423 if (stab_isnewer(&script_stab, started))
426 if (S_ISLNK(script_stab.st_mode)) {
427 r = stat(script, &script_stab);
428 if (r) err(127,"stat script (%s0",script);
430 if (stab_isnewer(&script_stab, started))
437 static bool check_garbage(void) {
438 struct stat sock_stab;
441 r = lstat(socket_path, &sock_stab);
443 if ((errno == ENOENT))
444 return 0; /* well, no garbage then */
445 err(127,"stat socket (%s)",socket_path);
448 return check_garbage_vs(&sock_stab);
451 static void tidy_garbage(void) {
452 /* We lock l<ident> and re-check. The effect of this is that each
453 * stale socket is removed only once. So unless multiple updates to
454 * the script happen rapidly, we can't be racing with the cgi-fcgi
455 * (which is recreating the socket */
459 const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
461 lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
462 if (lockfd<0) err(127,"create lock (%s)", lock_path);
464 r = flock(lockfd, LOCK_EX);
465 if (r) err(127,"lock lock (%s)", lock_path);
467 if (check_garbage()) {
468 r = unlink(socket_path);
470 if (!(errno == ENOENT))
471 err(127,"remove out-of-date socket (%s)", socket_path);
476 if (r) errx(127,"close lock (%s)", lock_path);
479 static void make_stderr_copy(void) {
480 stderr_copy = dup(2);
481 if (stderr_copy < 0) err(127,"dup stderr (for copy for stage2)");
484 static void prep_stage2(void) {
487 const char *stage2_val = m_asprintf("%d", stderr_copy);
488 r = setenv(STAGE2_VAR, stage2_val, 1);
489 if (r) err(127,"set %s (to announce to stage2)", STAGE2_VAR);
492 static void shbang_opts(const char *const **argv_io,
493 const struct cmdinfo *cmdinfos) {
494 myopt(argv_io, cmdinfos);
496 interp = *(*argv_io)++;
497 if (!interp) errx(127,"need interpreter argument");
500 /* stage2 predeclarations */
501 static void record_baseline_time(void);
502 static void become_pgrp(void);
503 static void setup_handlers(void);
504 static void spawn_script(void);
505 static void queue_alarm(void);
506 static void await_something(void);
508 int main(int argc, const char *const *argv) {
509 const char *smashedopt;
512 stage2 = getenv(STAGE2_VAR);
514 int stderrfd = atoi(stage2);
517 r = dup2(stderrfd, 2);
520 r = open("/dev/null",O_WRONLY);
521 if (r<0) err(127,"open /dev/null as stdout");
523 else if (r!=1) errx(127,"open /dev/null for stdout gave bad fd %d",r);
526 sha256_init(&identsc);
529 (smashedopt = argv[1]) &&
530 smashedopt[0]=='-' &&
531 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
532 /* single argument containg all the options and <interp> */
533 argv += 2; /* eat argv[0] and smashedopt */
534 const char *split_args[MAX_OPTS+1];
536 split_args[split_argc++] = argv[0];
538 if (split_argc >= MAX_OPTS) errx(127,"too many options in combined arg");
539 split_args[split_argc++] = smashedopt;
540 if (smashedopt[0] != '-') /* never true on first iteration */
542 char *delim = strchr(smashedopt,' ');
543 if (!delim) delim = strchr(smashedopt,',');
545 errx(127,"combined arg lacks <interpreter>");
547 smashedopt = delim+1;
549 assert(split_argc <= MAX_OPTS);
550 split_args[split_argc++] = 0;
552 const char *const *split_argv = split_args;
554 shbang_opts(&split_argv, cmdinfos);
556 if (!split_argv) errx(127,"combined arg too many non-option arguments");
558 shbang_opts(&argv, cmdinfos);
562 if (!script) errx(127,"need script argument");
563 if (*argv) errx(127,"too many arguments");
569 bool isgarbage = check_garbage();
572 printf("socket: %s\n",socket_path);
573 printf("interp: %s\n",interp);
574 printf("script: %s\n",script);
575 printf("garbage: %d\n",isgarbage);
586 "cgi-fcgi", "-connect", socket_path,
588 m_asprintf("%d", numservers),
590 err(127,"exec cgi-fcgi");
594 record_baseline_time();
607 /* It is most convenient to handle the recheck timeout, as well as
608 * child death, in signal handlers. Our signals all block each other,
609 * and the main program has signals blocked except in sigsuspend, so
610 * we don't need to worry about async-signal-safety, or errno. */
612 static struct stat baseline_time;
613 static pid_t script_child, stage2_pgrp;
614 static bool out_of_date;
616 static void record_baseline_time(void) {
617 stab_mtimenow(&baseline_time);
620 static void become_pgrp(void) {
623 stage2_pgrp = getpid();
626 if (r) err(127,"(stage2) setpgid");
629 static void atexit_handler(void) {
632 sighandler_t sigr = signal(SIGTERM,SIG_IGN);
633 if (sigr == SIG_ERR) warn("(stage2) signal(SIGTERM,SIG_IGN)");
635 r = killpg(stage2_pgrp,SIGTERM);
636 if (r) warn("(stage) killpg failed");
639 static void alarm_handler(int dummy) {
642 exit(0); /* transfers control to atexit_handler */
644 out_of_date = check_garbage_vs(&baseline_time);
648 static void child_handler(int dummy) {
651 pid_t got = waitpid(-1, &status, WNOHANG);
652 if (got == (pid_t)-1) err(127,"(stage2) waitpid");
653 if (got != script_child) {
654 warn("(stage2) waitpid got status %d for unknown child [%lu]",
655 status, (unsigned long)got);
658 if (WIFEXITED(status)) {
659 int v = WEXITSTATUS(status);
660 if (v) warn("program failed with error exit status %d", v);
662 } else if (WIFSIGNALED(status)) {
663 int s = WTERMSIG(status);
664 err(status & 0xff, "program died due to fatal signal %s%s",
665 strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
667 err(127, "program failed with crazy wait status %#x", status);
673 static void setup_handlers(void) {
677 r = atexit(atexit_handler);
678 if (r) err(127,"(stage2) atexit");
680 sigemptyset(&sa.sa_mask);
681 sigaddset(&sa.sa_mask, SIGALRM);
682 sigaddset(&sa.sa_mask, SIGCHLD);
685 r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
686 if (r) err(127,"(stage2) sigprocmask(SIG_BLOCK,)");
688 sa.sa_handler = alarm_handler;
689 r = sigaction(SIGALRM, &sa, 0);
690 if (r) err(127,"(stage2) sigaction SIGALRM");
692 sa.sa_flags |= SA_NOCLDSTOP;
693 sa.sa_handler = child_handler;
694 r = sigaction(SIGCHLD, &sa, 0);
695 if (r) err(127,"(stage2) sigaction SIGCHLD");
698 static void spawn_script(void) {
699 script_child = fork();
700 if (script_child == (pid_t)-1) err(127,"(stage2) fork");
703 interp, script, (char*)0);
704 err(127,"(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
708 static void queue_alarm(void) {
709 alarm(check_interval);
712 static void await_something(void) {
718 r = sigsuspend(&mask);
720 if (errno != EINTR) err(127,"(stage2) sigsuspend");