1 /* common stuff for cgi-fcgi-interp and prefork-interp */
5 const char *interp, *ident;
7 struct sha256_ctx identsc;
10 const char *run_base, *script, *socket_path, *lock_path;
11 const char *run_base_mkdir_p;
14 void common_diee(const char *m) { diee("%s", m); }
15 void common_die (const char *m) { die ("%s", m); }
17 void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
21 const char *fmt_use = fmt;
24 r = asprintf(&fmt_free, "%s: %%m", fmt);
31 vsyslog(LOG_ERR, fmt_use, al);
34 fprintf(stderr, "%s: ", our_name);
35 vfprintf(stderr,fmt,al);
36 if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
39 if (estatus) exit(estatus);
42 void usagemessage(void) { fusagemessage(stderr); }
44 void of_help(const struct cmdinfo *ci, const char *val) {
45 fusagemessage(stdout);
46 if (ferror(stdout)) diee("write usage message to stdout");
50 void of_iassign(const struct cmdinfo *ci, const char *val) {
53 errno= 0; v= strtol(val,&ep,10);
54 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
55 badusage("bad integer argument `%s' for --%s",val,ci->olong);
59 void ident_addstring(const struct cmdinfo *ci, const char *string) {
60 /* ci may be 0 and is provided so this can be .call */
61 sha256_update(&identsc,strlen(string)+1,string);
64 void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
65 const char *val = getenv(name);
67 sha256_update(&identsc,strlen(name),name); /* no nul */
68 sha256_update(&identsc,1,"=");
69 ident_addstring(0,val);
71 ident_addstring(0,name);
75 bool find_run_base_var_run(void) {
80 try = m_asprintf("%s/%lu", "/var/run/user", us);
81 r = lstat(try, &stab);
83 if (errno == ENOENT ||
87 return 0; /* oh well */
88 diee("stat /var/run/user/UID");
90 if (!S_ISDIR(stab.st_mode)) {
91 warning("%s not a directory, falling back to ~\n", try);
94 if (stab.st_uid != us) {
95 warning("%s not owned by uid %lu, falling back to ~\n", try,
99 if (stab.st_mode & 0077) {
100 warning("%s writeable by group or other, falling back to ~\n", try);
103 run_base = m_asprintf("%s/%s", try, our_name);
107 static bool find_run_base_home(void) {
113 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
115 r = uname(&ut); if (r) diee("uname(2)");
116 dot = strchr(ut.nodename, '.');
118 if (sizeof(ut.nodename) > 32)
121 run_base_mkdir_p = m_asprintf("%s/.%s", pw->pw_dir, our_name);
122 try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
127 void find_socket_path(void) {
128 struct sockaddr_un sun;
131 us = getuid(); if (us==(uid_t)-1) diee("getuid");
133 find_run_base_var_run() ||
134 find_run_base_home() ||
137 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
140 if (maxidentlen < MINHEXHASH)
141 die("base directory `%s'"
142 " leaves only %d characters for id hash"
143 " which is too little (<%d)",
144 run_base, maxidentlen, MINHEXHASH);
146 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
147 char *hexident = xmalloc(identlen + 2);
148 unsigned char bbuf[32];
151 ident_addstring(0,interp);
152 ident_addstring(0,script);
153 sha256_digest(&identsc,sizeof(bbuf),bbuf);
155 for (i=0; i<identlen; i += 2)
156 sprintf(hexident+i, "%02x", bbuf[i/2]);
158 hexident[identlen] = 0;
162 if (strlen(ident) > maxidentlen)
163 die("base directory `%s' plus ident `%s' too long"
164 " (with spare) for socket (max ident %d)\n",
165 run_base, ident, maxidentlen);
167 r = mkdir(run_base, 0700);
168 if (r && errno==ENOENT && run_base_mkdir_p) {
169 r = mkdir(run_base_mkdir_p, 0700);
170 if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
171 r = mkdir(run_base, 0700);
174 if (!(errno == EEXIST))
175 diee("mkdir %s",run_base);
178 socket_path = m_asprintf("%s/s%s",run_base,ident);
182 int acquire_lock(void) {
186 lock_path = m_asprintf("%s/l%s",run_base,ident);
188 lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
189 if (lockfd<0) diee("create lock (%s)", lock_path);
191 r = flock(lockfd, LOCK_EX);
192 if (r) diee("lock lock (%s)", lock_path);
197 static void shbang_opts(const char *const **argv_io,
198 const struct cmdinfo *cmdinfos) {
199 myopt(argv_io, cmdinfos);
201 interp = *(*argv_io)++;
202 if (!interp) badusage("need interpreter argument");
205 void process_opts(const char *const **argv_io) {
206 const char *smashedopt;
208 sha256_init(&identsc);
211 (smashedopt = (*argv_io)[1]) &&
212 smashedopt[0]=='-' &&
213 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
214 /* single argument containg all the options and <interp> */
215 *argv_io += 2; /* eat argv[0] and smashedopt */
216 const char *split_args[MAX_OPTS+1];
218 split_args[split_argc++] = (*argv_io)[0];
220 if (split_argc >= MAX_OPTS) die("too many options in combined arg");
221 split_args[split_argc++] = smashedopt;
222 if (smashedopt[0] != '-') /* never true on first iteration */
224 char *delim = strchr(smashedopt,' ');
225 if (!delim) delim = strchr(smashedopt,',');
226 if (!delim) badusage("combined arg lacks <interpreter>");
228 smashedopt = delim+1;
230 assert(split_argc <= MAX_OPTS);
231 split_args[split_argc++] = 0;
233 const char *const *split_argv = split_args;
235 shbang_opts(&split_argv, cmdinfos);
238 shbang_opts(argv_io, cmdinfos);
242 script = *(*argv_io)++;