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 stabs_same_inode(struct stat *a, struct stat *b) {
75 return (a->st_dev == b->st_dev &&
76 a->st_ino == b->st_ino);
79 bool find_run_base_var_run(void) {
84 try = m_asprintf("%s/%lu", "/var/run/user", us);
85 r = lstat(try, &stab);
87 if (errno == ENOENT ||
91 return 0; /* oh well */
92 diee("stat /var/run/user/UID");
94 if (!S_ISDIR(stab.st_mode)) {
95 warning("%s not a directory, falling back to ~\n", try);
98 if (stab.st_uid != us) {
99 warning("%s not owned by uid %lu, falling back to ~\n", try,
103 if (stab.st_mode & 0077) {
104 warning("%s writeable by group or other, falling back to ~\n", try);
107 run_base = m_asprintf("%s/%s", try, our_name);
111 static bool find_run_base_home(void) {
117 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
119 r = uname(&ut); if (r) diee("uname(2)");
120 dot = strchr(ut.nodename, '.');
122 if (sizeof(ut.nodename) > 32)
125 run_base_mkdir_p = m_asprintf("%s/.%s", pw->pw_dir, our_name);
126 try = m_asprintf("%s/%s", run_base_mkdir_p, ut.nodename);
131 void find_socket_path(void) {
132 struct sockaddr_un sun;
135 us = getuid(); if (us==(uid_t)-1) diee("getuid");
137 find_run_base_var_run() ||
138 find_run_base_home() ||
141 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
144 if (maxidentlen < MINHEXHASH)
145 die("base directory `%s'"
146 " leaves only %d characters for id hash"
147 " which is too little (<%d)",
148 run_base, maxidentlen, MINHEXHASH);
150 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
151 char *hexident = xmalloc(identlen + 2);
152 unsigned char bbuf[32];
155 ident_addstring(0,interp);
157 ident_addstring(0,script);
158 sha256_digest(&identsc,sizeof(bbuf),bbuf);
160 for (i=0; i<identlen; i += 2)
161 sprintf(hexident+i, "%02x", bbuf[i/2]);
163 hexident[identlen] = 0;
167 if (strlen(ident) > maxidentlen)
168 die("base directory `%s' plus ident `%s' too long"
169 " (with spare) for socket (max ident %d)\n",
170 run_base, ident, maxidentlen);
172 r = mkdir(run_base, 0700);
173 if (r && errno==ENOENT && run_base_mkdir_p) {
174 r = mkdir(run_base_mkdir_p, 0700);
175 if (r) diee("mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
176 r = mkdir(run_base, 0700);
179 if (!(errno == EEXIST))
180 diee("mkdir %s",run_base);
183 socket_path = m_asprintf("%s/s%s",run_base,ident);
187 int acquire_lock(void) {
191 lock_path = m_asprintf("%s/l%s",run_base,ident);
193 lockfd = open(lock_path, O_CREAT|O_RDWR, 0600);
194 if (lockfd<0) diee("create lock (%s)", lock_path);
196 r = flock(lockfd, LOCK_EX);
197 if (r) diee("lock lock (%s)", lock_path);
202 static void shbang_opts(const char *const **argv_io,
203 const struct cmdinfo *cmdinfos) {
204 myopt(argv_io, cmdinfos);
206 interp = *(*argv_io)++;
207 if (!interp) badusage("need interpreter argument");
210 void process_opts(const char *const **argv_io) {
211 const char *smashedopt;
213 sha256_init(&identsc);
217 (smashedopt = (*argv_io)[1]) &&
218 smashedopt[0]=='-' &&
219 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
220 /* single argument containg all the options and <interp> */
221 *argv_io += 2; /* eat argv[0] and smashedopt */
222 const char *split_args[MAX_OPTS+1];
224 split_args[split_argc++] = (*argv_io)[0];
226 if (split_argc >= MAX_OPTS) die("too many options in combined arg");
227 split_args[split_argc++] = smashedopt;
228 if (smashedopt[0] != '-') /* never true on first iteration */
230 char *delim = strchr(smashedopt,' ');
231 if (!delim) delim = strchr(smashedopt,',');
232 if (!delim) badusage("combined arg lacks <interpreter>");
234 smashedopt = delim+1;
236 assert(split_argc <= MAX_OPTS);
237 split_args[split_argc++] = 0;
239 const char *const *split_argv = split_args;
241 shbang_opts(&split_argv, cmdinfos);
245 badusage("no script argument (expected after combined #! options)");
247 shbang_opts(argv_io, cmdinfos);
251 script = *(*argv_io)++;