2 * "Interpreter" that you can put in #! like this
3 * #!/usr/bin/cgi-fcgi-perl [<options>]
5 * The result is a program which looks, when executed via the #!
6 * line, like a CGI program. But the perl script inside will
7 * be executed via /usr/bin/perl in fcgi.
12 * Use <ident> rather than hex(sha256(<script>))
13 * as the basename of the leafname of the fcgi rendezvous
14 * socket. If <ident> contains only hex digit characters it
15 * ought to be no more than 32 characters.
18 * Start <numservers> instances of the program. This
19 * determines the maximum concurrency. (Note that unlike
20 * speedy, the specified number of servers is started
21 * right away.) The default is 4.
23 * cgi-fcgi-perl automatically expires old sockets, including
24 * ones where the named perl script is out of date.
28 * Uses one of two directories
29 * /var/run/user/<UID>/cgi-fcgi-perl/
30 * ~/.cgi-fcgi-perl/<node>/
31 * and inside there uses these paths
35 * If -M<ident> is not specified then an initial substricg of the
36 * lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
37 * used. The substring is chosen so that the whole path is 10 bytes
38 * shorter than sizeof(sun_path). But always at least 33 characters.
40 * <node> is truncated at the first `.' and after the first 32
44 * - see if /var/run/user exists
45 * if so, lstat /var/run/user/<UID> and check that
46 * we own it and it's X700; if not, fail
47 * if it's ok then <base> is /var/run/user/<UID>
48 * otherwise, look for and maybe create ~/.cgi-fcgi-perl
49 * (where ~ is HOME or from getpwuid)
50 * and then <base> is ~/.cgi-fcgi-perl/<node>
51 * - calculate pathname (checking <ident> length is OK)
52 * - check for and maybe create <base>
53 * - stat and lstat the <script>
54 * - stat the socket and check its timestamp
55 * if it is too hold, rename it to g<inum> (where
56 * <inum> is in decimal)
57 * and run garbage collection
58 * - run cgi-fcgi -connect SOCKET SCRIPT
69 #include <sys/types.h>
71 #include <sys/utsname.h>
72 #include <sys/socket.h>
77 #include <nettle/sha.h>
81 #define die common_die
82 #define diee common_diee
86 static const char *ident;
87 static int numservers;
89 void diee(const char *m) {
90 fprintf(stderr,"cgi-fcgi-: error: %s failed: %s\n", m, strerror(errno));
94 static void fusagemessage(FILE *f) {
95 fprintf(f, "usage: #!/usr/bin/cgi-fcgi-perl [<options>]\n");
98 void usagemessage(void) { fusagemessage(stderr); }
100 static void of_help(const struct cmdinfo *ci, const char *val) {
101 fusagemessage(stdout);
102 if (ferror(stdout)) diee("write usage message to stdout");
106 static const struct cmdinfo cmdinfos[]= {
107 { "help", 0, .call= of_help },
108 { 0, 'g', 1, .sassignto= &ident },
109 { 0, 'M', 1, .iassignto= &numservers },
114 static const char *run_base, *command, *socket_path;
116 static bool find_run_base_var_run(void) {
121 try = m_asprintf("%s/%lu", "/var/run/user", us);
122 r = lstat(try, &stab);
124 if (errno == ENOENT ||
128 return 0; /* oh well */
129 diee("stat /var/run/user/UID");
131 if (!S_ISDIR(stab.st_mode)) {
132 fprintf(stderr,"%s not a directory, falling back to ~\n", try);
135 if (stab.st_uid != us) {
136 fprintf(stderr,"%s not owned by uid %lu, falling back to ~\n",
137 try, (unsigned long)us);
140 if (stab.st_mode & 0077) {
141 fprintf(stderr,"%s writeable by group or other, falling back to ~\n",try);
144 run_base = m_asprintf("%s/%s", try, "cgi-fcgi-perl");
148 static bool find_run_base_home(void) {
154 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
156 r = uname(&ut); if (r) diee("uname(2)");
157 dot = strchr(ut.nodename, '.');
159 if (sizeof(ut.nodename) > 32)
162 try = m_asprintf("%s/%s/%s", pw->pw_dir, ".cgi-fcgi-perl", ut.nodename);
167 static void find_socket_path(void) {
168 struct sockaddr_un sun;
170 us = getuid(); if (us==(uid_t)-1) diee("getuid");
172 find_run_base_var_run() ||
173 find_run_base_home() ||
176 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
179 if (maxidentlen < MINHEXHASH) {
180 fprintf(stderr,"cgi-fcgi-perl: base directory `%s'"
181 " leaves only %d characters for command name hash"
182 " which is too little (<%d)\n",
183 run_base, maxidentlen, MINHEXHASH);
187 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
188 char *hexident = xmalloc(identlen + 2);
189 struct sha256_ctx sc;
190 unsigned char bbuf[32];
194 sha256_update(&sc,strlen(command)+1,command);
195 sha256_digest(&sc,sizeof(bbuf),bbuf);
197 for (i=0; i<identlen; i += 2)
198 sprintf(hexident+i, "%02x", bbuf[i/2]);
200 hexident[identlen] = 0;
204 if (strlen(ident) > maxidentlen) {
205 fprintf(stderr,"cgi-fgci-perl: base directory `%s'"
206 " plus ident `%s' too long (with spare) for socket\n",
211 socket_path = m_asprintf("%s/g%s",run_base,ident);
214 int main(int argc, const char *const *argv) {
215 myopt(&argv, cmdinfos);
218 if (!command || *argv) {
219 fprintf(stderr,"wrong number of arguments\n");
225 printf(">%s<\n",socket_path);