1 /* common stuff for cgi-fcgi-interp and prefork-interp */
5 const char *interp, *ident, *script, *socket_path, *lock_path;
7 struct sha256_ctx identsc;
10 static const char *run_base;
11 static const char *run_base_mkdir_p;
13 void common_diee(const char *m) { diee("%s", m); }
14 void common_die (const char *m) { die ("%s", m); }
16 void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al) {
20 const char *fmt_use = fmt;
23 r = asprintf(&fmt_free, "%s: %%m", fmt);
30 vsyslog(LOG_ERR, fmt_use, al);
33 fprintf(stderr, "%s: ", our_name);
34 vfprintf(stderr,fmt,al);
35 if (errnoval!=-1) fprintf(stderr,": %s",strerror(errnoval));
38 if (estatus) exit(estatus);
41 void usagemessage(void) { fusagemessage(stderr); }
43 void of_help(const struct cmdinfo *ci, const char *val) {
44 fusagemessage(stdout);
45 if (ferror(stdout)) diee("write usage message to stdout");
49 void of_iassign(const struct cmdinfo *ci, const char *val) {
52 errno= 0; v= strtol(val,&ep,10);
53 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
54 badusage("bad integer argument `%s' for --%s",val,ci->olong);
58 void ident_addstring(const struct cmdinfo *ci, const char *string) {
59 /* ci may be 0 and is provided so this can be .call */
60 sha256_update(&identsc,strlen(string)+1,string);
63 void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
64 const char *val = getenv(name);
66 sha256_update(&identsc,strlen(name),name); /* no nul */
67 sha256_update(&identsc,1,"=");
68 ident_addstring(0,val);
70 ident_addstring(0,name);
74 bool find_run_base_var_run(void) {
79 try = m_asprintf("%s/%lu", "/var/run/user", us);
80 r = lstat(try, &stab);
82 if (errno == ENOENT ||
86 return 0; /* oh well */
87 diee("stat /var/run/user/UID");
89 if (!S_ISDIR(stab.st_mode)) {
90 warning("%s not a directory, falling back to ~\n", try);
93 if (stab.st_uid != us) {
94 warning("%s not owned by uid %lu, falling back to ~\n", try,
98 if (stab.st_mode & 0077) {
99 warning("%s writeable by group or other, falling back to ~\n", try);
102 run_base = m_asprintf("%s/%s", try, our_name);
106 static bool find_run_base_home(void) {
112 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
114 r = uname(&ut); if (r) diee("uname(2)");
115 dot = strchr(ut.nodename, '.');
117 if (sizeof(ut.nodename) > 32)
120 run_base_mkdir_p = m_asprintf("%s/.%s", pw->pw_dir, our_name);
121 try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
126 void find_socket_path(void) {
127 struct sockaddr_un sun;
130 us = getuid(); if (us==(uid_t)-1) diee("getuid");
132 find_run_base_var_run() ||
133 find_run_base_home() ||
136 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
139 if (maxidentlen < MINHEXHASH)
140 die("base directory `%s'"
141 " leaves only %d characters for id hash"
142 " which is too little (<%d)",
143 run_base, maxidentlen, MINHEXHASH);
145 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
146 char *hexident = xmalloc(identlen + 2);
147 unsigned char bbuf[32];
150 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);
239 badusage("no script argument (expected after combined #! options)");
241 shbang_opts(argv_io, cmdinfos);
245 script = *(*argv_io)++;