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>
147 #include <sys/wait.h>
149 #include <nettle/sha.h>
153 #define MINHEXHASH 33
154 #define STAGE2_VAR "CHIARKUTILS_CGIFCGIINTERP_STAGE2"
156 static const char *interp, *ident;
157 static int numservers=4, debugmode;
158 static int check_interval=300;
160 static struct sha256_ctx identsc;
162 static const char *stage2;
164 static void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
165 fputs("cgi-fcgi-interp: ",stderr);
166 vfprintf(stderr,fmt,al);
167 if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
169 if (estatus) exit(estatus);
172 #define DEF_MSG(func, attrs, estatus, errnoval, after) \
173 static void func(const char *fmt, ...) \
174 __attribute__((format(printf,1,2))) attrs; \
175 static void func(const char *fmt, ...) { \
178 vmsgcore(estatus,errnoval,fmt,al); \
182 DEF_MSG(warninge, /*empty*/, 0, errno, { });
183 DEF_MSG(warning , /*empty*/, 0, 0, { });
185 #define DEF_DIE(func, errnoval) \
186 DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
191 void common_diee(const char *m) { diee("%s", m); }
192 void common_die (const char *m) { die ("%s", m); }
194 static void fusagemessage(FILE *f) {
195 fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
198 void usagemessage(void) { fusagemessage(stderr); }
200 static void of_help(const struct cmdinfo *ci, const char *val) {
201 fusagemessage(stdout);
202 if (ferror(stdout)) diee("write usage message to stdout");
206 static void of_iassign(const struct cmdinfo *ci, const char *val) {
209 errno= 0; v= strtol(val,&ep,10);
210 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
211 badusage("bad integer argument `%s' for --%s",val,ci->olong);
215 static void ident_addstring(const struct cmdinfo *ci, const char *string) {
216 /* ci may be 0 and is provided so this can be .call */
217 sha256_update(&identsc,strlen(string)+1,string);
220 static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
221 const char *val = getenv(name);
223 sha256_update(&identsc,strlen(name),name); /* no nul */
224 sha256_update(&identsc,1,"=");
225 ident_addstring(0,val);
227 ident_addstring(0,name);
233 static const struct cmdinfo cmdinfos[]= {
234 { "help", 0, .call=of_help },
235 { 0, 'g', 1, .sassignto= &ident },
236 { 0, 'G', 1, .call= ident_addstring },
237 { 0, 'E', 1, .call= off_ident_addenv },
238 { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
239 { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
240 { 0, 'c', 1, .call=of_iassign, .iassignto= &check_interval },
245 static const char *run_base, *script, *socket_path;
246 static const char *run_base_mkdir_p;
247 static int stderr_copy;
249 static bool find_run_base_var_run(void) {
254 try = m_asprintf("%s/%lu", "/var/run/user", us);
255 r = lstat(try, &stab);
257 if (errno == ENOENT ||
261 return 0; /* oh well */
262 diee("stat /var/run/user/UID");
264 if (!S_ISDIR(stab.st_mode)) {
265 warning("%s not a directory, falling back to ~\n", try);
268 if (stab.st_uid != us) {
269 warning("%s not owned by uid %lu, falling back to ~\n", try,
273 if (stab.st_mode & 0077) {
274 warning("%s writeable by group or other, falling back to ~\n", try);
277 run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
281 static bool find_run_base_home(void) {
287 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
289 r = uname(&ut); if (r) diee("uname(2)");
290 dot = strchr(ut.nodename, '.');
292 if (sizeof(ut.nodename) > 32)
295 run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
296 try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
301 static void find_socket_path(void) {
302 struct sockaddr_un sun;
305 us = getuid(); if (us==(uid_t)-1) diee("getuid");
307 find_run_base_var_run() ||
308 find_run_base_home() ||
311 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
314 if (maxidentlen < MINHEXHASH)
315 die("base directory `%s'"
316 " leaves only %d characters for id hash"
317 " which is too little (<%d)",
318 run_base, maxidentlen, MINHEXHASH);
320 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
321 char *hexident = xmalloc(identlen + 2);
322 unsigned char bbuf[32];
325 ident_addstring(0,interp);
326 ident_addstring(0,script);
327 sha256_digest(&identsc,sizeof(bbuf),bbuf);
329 for (i=0; i<identlen; i += 2)
330 sprintf(hexident+i, "%02x", bbuf[i/2]);
332 hexident[identlen] = 0;
336 if (strlen(ident) > maxidentlen)
337 die("base directory `%s' plus ident `%s' too long"
338 " (with spare) for socket (max ident %d)\n",
339 run_base, ident, maxidentlen);
341 r = mkdir(run_base, 0700);
342 if (r && errno==ENOENT && run_base_mkdir_p) {
343 r = mkdir(run_base_mkdir_p, 0700);
344 if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
345 r = mkdir(run_base, 0700);
348 if (!(errno == EEXIST))
349 diee("mkdir %s",run_base);
352 socket_path = m_asprintf("%s/s%s",run_base,ident);
356 * Regarding the macro timespeccmp:
358 * Copyright (c) 1982, 1986, 1993
359 * The Regents of the University of California. All rights reserved.
361 * Redistribution and use in source and binary forms, with or without
362 * modification, are permitted provided that the following conditions
364 * 1. Redistributions of source code must retain the above copyright
365 * notice, this list of conditions and the following disclaimer.
366 * 2. Redistributions in binary form must reproduce the above copyright
367 * notice, this list of conditions and the following disclaimer in the
368 * documentation and/or other materials provided with the distribution.
369 * 4. Neither the name of the University nor the names of its contributors
370 * may be used to endorse or promote products derived from this software
371 * without specific prior written permission.
373 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
374 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
375 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
376 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
377 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
378 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
379 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
380 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
381 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
382 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385 * @(#)time.h 8.5 (Berkeley) 5/4/95
386 * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
389 #define timespeccmp(tvp, uvp, cmp) \
390 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
391 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
392 ((tvp)->tv_sec cmp (uvp)->tv_sec))
393 #endif /*timespeccmp*/
399 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
401 fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
402 (unsigned long)a->st_mtim.tv_sec,
403 (unsigned long)a->st_mtim.tv_nsec,
404 (unsigned long)b->st_mtim.tv_sec,
405 (unsigned long)b->st_mtim.tv_nsec);
406 return timespeccmp(&a->st_mtim, &b->st_mtim, >);
409 static void stab_mtimenow(struct stat *out) {
410 int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
411 if (r) diee("(stage2) clock_gettime");
413 fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
414 (unsigned long)out->st_mtim.tv_sec,
415 (unsigned long)out->st_mtim.tv_nsec);
418 #else /* !defined(st_mtime) */
420 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
422 fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
423 (unsigned long)a->st_mtime,
424 (unsigned long)b->st_mtime);
425 return a->st_mtime > &b->st_mtime;
428 static void stab_mtimenow(struct stat *out) {
429 out->st_mtime = time(NULL);
430 if (baseline_time.st_mtime == (time_t)-1) diee("(stage2) time()");
432 fprintf(stderr,"stab_mtimenow mtime %lu\n",
433 (unsigned long)out->st_mtime);
436 #endif /* !defined(st_mtime) */
438 static bool check_garbage_vs(const struct stat *started) {
439 struct stat script_stab;
442 r = lstat(script, &script_stab);
443 if (r) diee("lstat script (%s)",script);
445 if (stab_isnewer(&script_stab, started))
448 if (S_ISLNK(script_stab.st_mode)) {
449 r = stat(script, &script_stab);
450 if (r) diee("stat script (%s0",script);
452 if (stab_isnewer(&script_stab, started))
459 static bool check_garbage(void) {
460 struct stat sock_stab;
463 r = lstat(socket_path, &sock_stab);
465 if ((errno == ENOENT))
466 return 0; /* well, no garbage then */
467 diee("stat socket (%s)",socket_path);
470 return check_garbage_vs(&sock_stab);
473 static void tidy_garbage(void) {
474 /* We lock l<ident> and re-check. The effect of this is that each
475 * stale socket is removed only once. So unless multiple updates to
476 * the script happen rapidly, we can't be racing with the cgi-fcgi
477 * (which is recreating the socket */
481 const char *lock_path = m_asprintf("%s/l%s",run_base,ident);
483 lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
484 if (lockfd<0) diee("create lock (%s)", lock_path);
486 r = flock(lockfd, LOCK_EX);
487 if (r) diee("lock lock (%s)", lock_path);
489 if (check_garbage()) {
490 r = unlink(socket_path);
492 if (!(errno == ENOENT))
493 diee("remove out-of-date socket (%s)", socket_path);
498 if (r) diee("close lock (%s)", lock_path);
501 static void make_stderr_copy(void) {
502 stderr_copy = dup(2);
503 if (stderr_copy < 0) diee("dup stderr (for copy for stage2)");
506 static void prep_stage2(void) {
509 const char *stage2_val = m_asprintf("%d", stderr_copy);
510 r = setenv(STAGE2_VAR, stage2_val, 1);
511 if (r) diee("set %s (to announce to stage2)", STAGE2_VAR);
514 static void shbang_opts(const char *const **argv_io,
515 const struct cmdinfo *cmdinfos) {
516 myopt(argv_io, cmdinfos);
518 interp = *(*argv_io)++;
519 if (!interp) badusage("need interpreter argument");
522 /* stage2 predeclarations */
523 static void record_baseline_time(void);
524 static void become_pgrp(void);
525 static void setup_handlers(void);
526 static void spawn_script(void);
527 static void queue_alarm(void);
528 static void await_something(void);
530 int main(int argc, const char *const *argv) {
531 const char *smashedopt;
534 stage2 = getenv(STAGE2_VAR);
536 int stderrfd = atoi(stage2);
539 r = dup2(stderrfd, 2);
542 r = open("/dev/null",O_WRONLY);
543 if (r<0) diee("open /dev/null as stdout");
545 else if (r!=1) die("open /dev/null for stdout gave bad fd %d",r);
548 sha256_init(&identsc);
551 (smashedopt = argv[1]) &&
552 smashedopt[0]=='-' &&
553 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
554 /* single argument containg all the options and <interp> */
555 argv += 2; /* eat argv[0] and smashedopt */
556 const char *split_args[MAX_OPTS+1];
558 split_args[split_argc++] = argv[0];
560 if (split_argc >= MAX_OPTS) die("too many options in combined arg");
561 split_args[split_argc++] = smashedopt;
562 if (smashedopt[0] != '-') /* never true on first iteration */
564 char *delim = strchr(smashedopt,' ');
565 if (!delim) delim = strchr(smashedopt,',');
566 if (!delim) badusage("combined arg lacks <interpreter>");
568 smashedopt = delim+1;
570 assert(split_argc <= MAX_OPTS);
571 split_args[split_argc++] = 0;
573 const char *const *split_argv = split_args;
575 shbang_opts(&split_argv, cmdinfos);
577 if (!split_argv) badusage("combined arg too many non-option arguments");
579 shbang_opts(&argv, cmdinfos);
583 if (!script) badusage("need script argument");
584 if (*argv) badusage("too many arguments");
590 bool isgarbage = check_garbage();
593 printf("socket: %s\n",socket_path);
594 printf("interp: %s\n",interp);
595 printf("script: %s\n",script);
596 printf("garbage: %d\n",isgarbage);
607 "cgi-fcgi", "-connect", socket_path,
609 m_asprintf("%d", numservers),
611 diee("exec cgi-fcgi");
615 record_baseline_time();
628 /* It is most convenient to handle the recheck timeout, as well as
629 * child death, in signal handlers. Our signals all block each other,
630 * and the main program has signals blocked except in sigsuspend, so
631 * we don't need to worry about async-signal-safety, or errno. */
633 static struct stat baseline_time;
634 static pid_t script_child, stage2_pgrp;
635 static bool out_of_date;
637 static void record_baseline_time(void) {
638 stab_mtimenow(&baseline_time);
641 static void become_pgrp(void) {
644 stage2_pgrp = getpid();
647 if (r) diee("(stage2) setpgid");
650 static void atexit_handler(void) {
653 sighandler_t sigr = signal(SIGTERM,SIG_IGN);
654 if (sigr == SIG_ERR) warninge("(stage2) signal(SIGTERM,SIG_IGN)");
656 r = killpg(stage2_pgrp,SIGTERM);
657 if (r) warninge("(stage) killpg failed");
660 static void alarm_handler(int dummy) {
663 exit(0); /* transfers control to atexit_handler */
665 out_of_date = check_garbage_vs(&baseline_time);
669 static void child_handler(int dummy) {
672 pid_t got = waitpid(-1, &status, WNOHANG);
673 if (got == (pid_t)-1) diee("(stage2) waitpid");
674 if (got != script_child) {
675 warning("(stage2) waitpid got status %d for unknown child [%lu]",
676 status, (unsigned long)got);
679 if (WIFEXITED(status)) {
680 int v = WEXITSTATUS(status);
681 if (v) warning("program failed with error exit status %d", v);
683 } else if (WIFSIGNALED(status)) {
684 int s = WTERMSIG(status);
685 warning("program died due to fatal signal %s%s",
686 strsignal(s), WCOREDUMP(status) ? " (core dumped" : "");
687 assert(status & 0xff);
690 die("program failed with crazy wait status %#x", status);
696 static void setup_handlers(void) {
700 r = atexit(atexit_handler);
701 if (r) diee("(stage2) atexit");
703 sigemptyset(&sa.sa_mask);
704 sigaddset(&sa.sa_mask, SIGALRM);
705 sigaddset(&sa.sa_mask, SIGCHLD);
708 r = sigprocmask(SIG_BLOCK, &sa.sa_mask, 0);
709 if (r) diee("(stage2) sigprocmask(SIG_BLOCK,)");
711 sa.sa_handler = alarm_handler;
712 r = sigaction(SIGALRM, &sa, 0);
713 if (r) diee("(stage2) sigaction SIGALRM");
715 sa.sa_flags |= SA_NOCLDSTOP;
716 sa.sa_handler = child_handler;
717 r = sigaction(SIGCHLD, &sa, 0);
718 if (r) diee("(stage2) sigaction SIGCHLD");
721 static void spawn_script(void) {
722 script_child = fork();
723 if (script_child == (pid_t)-1) diee("(stage2) fork");
726 interp, script, (char*)0);
727 diee("(stage2) exec interpreter (`%s', for `%s')\n",interp,script);
731 static void queue_alarm(void) {
732 alarm(check_interval);
735 static void await_something(void) {
741 r = sigsuspend(&mask);
743 if (errno != EINTR) diee("(stage2) sigsuspend");