2 * "Interpreter" that you can put in #! like this
3 * #!/usr/bin/prefork-interp [<options>] <interpreter>
6 * prefork-interp [<option> ..] <interpreter> [<script> [<args> ...]]
7 * prefork-interp [<option>,..],<interpreter> <script> [<args> ...]
8 * prefork-interp '[<option> ..] <interpreter>' <script> [<args> ...]
10 * Options must specify argument laundering mode.
11 * Currently the only mode supported is:
12 * -U unlaundered: setup and executor both get all arguments and env vars
13 * ident covers only env vars specified with -E
14 * ident covers only arguments interpreter and (if present) script
18 * client (C wrapper) connects to server
19 * (including reading ack byte)
21 * === acquire lock ===
22 * makes new listening socket
23 * makes first-instance socketpair
25 * forks watcher and awaits
26 * forks setup (script, sock fds indicated in env)
27 * fd0, fd1, fd2: from-outer
28 * other fd: call(client-end)(fake)
29 * reaps setup (and reports error)
30 * (implicitly releases lock)
32 * watcher fd[012]: watcher pipes
33 * starts watch on socket path
34 * sets stderr to line buffered
35 * sets stdin to nonblocking
37 * when socket stat changes, quit
39 * setup (pre-exec) fd0: null,
40 * fd[12]: fd2-from-outer
41 * env fds: listener, call(server-end)(fake),
42 * watcher read, watcher write
44 * possibly clean env, argv
46 * setup (script) runs initialisation parts of the script
47 * at prefork establishment point:
48 * setup (pm) [1] opens syslog
52 * server (pm) [1] [fd0: null],
53 * [fd[12]: fd2-from-outer]
54 * right away, forks init monitor
55 * [2] closes outer caller fds and call(fake)
56 * [server (pm)] fd[012]: null
57 * other fds: listener, syslog
58 * runs in loop accepting and forking,
59 * reaping and limiting children (incl init monitor)
60 * reports failures of monitors to syslog
62 * [client (C wrapper)] if client connect succeeds:
63 * now fd: call(client-end)
64 * sends message with: cmdline, env
67 * [server (script)] accepts, forks subseq monitor
69 * monitor [1] [fd0: null]
70 * (init [fd[12]: init: fd2-from-outer; subseq: null]
71 * or errors: init: fd2; subseq: syslog
72 * subseq) other fds: syslog, call(server-end)
74 * receives args, env, fds
77 * executor sorts out fds:
78 * fd0, fd1, fd2: from-outer
79 * close fds: call(server-end)
80 * retained fds: syslog
83 * runs main part of script
86 * [monitor] [fd[012]: null]
87 * [fd[12]: init: fd2-from-outer; subseq: null]
88 * [errors: init: fd2; subseq: syslog]
90 * reports status via socket
92 * [client (C wrapper)] [fd0, fd1, fd2: from-outer]
93 * [other fd: call(client-end)]
94 * receives status, exits appropriately
95 * (if was bad signal, reports to stderr, exits 127)
98 #include <arpa/inet.h>
102 const char our_name[] = "prefork-interp";
104 static struct sockaddr_un sockaddr_sun;
105 static FILE *call_sock;
107 #define ACK_BYTE '\n'
109 static const char *const *executor_argv;
111 static const char header_magic[4] = "PFI\n";
113 void fusagemessage(FILE *f) {
114 fprintf(f, "usage: #!/usr/bin/prefork-interp [<options>]\n");
117 static int laundering;
119 const struct cmdinfo cmdinfos[]= {
121 { 0, 'U', 0, .iassignto= &laundering, .arg= 'U' },
125 void ident_addinit(void) {
126 char ident_magic[1] = { 0 };
127 sha256_update(&identsc, sizeof(ident_magic), ident_magic);
130 static void propagate_exit_status(int status, const char *what) {
133 if (WIFEXITED(status)) {
137 if (WIFSIGNALED(status)) {
138 int sig = WTERMSIG(status);
139 const char *signame = strsignal(sig);
140 if (signame == 0) signame = "unknown signal";
142 if (! WCOREDUMP(status) &&
149 sa.sa_handler = SIG_DFL;
150 r = sigaction(sig, &sa, 0);
151 if (r) diee("failed to reset signal handler while propagating %s",
156 sigaddset(&sset, sig);
157 r = sigprocmask(SIG_UNBLOCK, &sset, 0);
158 if (r) diee("failed to reset signal block while propagating %s",
162 die("unexpectedly kept running after raising (to propagate) %s",
166 die("%s failed due to signal %d %s%s", what, sig, signame,
167 WCOREDUMP(status) ? " (core dumped)" : "");
170 die("%s failed with weird wait status %d 0x%x", what, status, status);
173 static __attribute((noreturn)) void die_data_overflow(void) {
174 die("cannot handle data with length >2^32");
177 static void prepare_data(size_t *len, char **buf,
178 const void *data, size_t dl) {
180 if (dl >= SIZE_MAX - *len)
185 memcpy(*buf, data, dl);
190 static void prepare_length(size_t *len, char **buf, size_t dl_sz) {
191 if (dl_sz > UINT32_MAX) die_data_overflow();
192 uint32_t dl = htonl(dl_sz);
193 prepare_data(len, buf, &dl, sizeof(dl));
196 static void prepare_string(size_t *len, char **buf, const char *s) {
197 size_t sl = strlen(s);
198 prepare_data(len, buf, s, sl+1);
201 static void prepare_message(size_t *len, char **buf) {
204 const char *const *p = (void*)environ;
207 prepare_string(len, buf, s);
210 prepare_string(len, buf, "");
214 prepare_string(len, buf, s);
217 static void send_fd(int payload_fd) {
218 int via_fd = fileno(call_sock);
221 struct cmsghdr align;
222 char buf[CMSG_SPACE(sizeof(payload_fd))];
232 iov.iov_base = &dummy_byte;
238 msg.msg_control = cmsg_buf.buf;
239 msg.msg_controllen = sizeof(cmsg_buf.buf);
241 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
242 cmsg->cmsg_level = SOL_SOCKET;
243 cmsg->cmsg_type = SCM_RIGHTS;
244 cmsg->cmsg_len = CMSG_LEN(sizeof(payload_fd));
245 *(int*)CMSG_DATA(cmsg) = payload_fd;
247 msg.msg_controllen = sizeof(cmsg_buf.buf);
250 ssize_t r = sendmsg(via_fd, &msg, 0);
252 if (errno == EINTR) continue;
260 static void send_request(void) {
261 // Sending these first makes it easier for the script to
262 // use buffered IO for the message.
268 prepare_message(&len, 0);
270 char *m = xmalloc(len + 4);
272 prepare_length(0, &p, len);
273 prepare_message(0, &p);
274 assert(p == m + len + 4);
276 ssize_t sr = fwrite(p, len, 1, call_sock);
277 if (sr != 1) diee("write request (buffer)");
279 if (fflush(call_sock)) diee("write request");
282 static FILE *call_sock_from_fd(int fd) {
285 FILE *call_sock = fdopen(fd, "r+");
286 if (!call_sock) diee("fdopen socket");
288 r = setvbuf(call_sock, 0, _IONBF, 0);
289 if (r) die("setvbuf socket");
294 static bool was_eof(FILE *call_sock) {
295 return feof(call_sock) || errno==ECONNRESET;
299 static int protocol_read_maybe(void *data, size_t sz) {
301 size_t sr = fread(data, sz, 1, call_sock);
303 if (was_eof(call_sock)) return -1;
304 diee("read() on monitor call socket (%zd)", sz);
309 static void protocol_read(void *data, size_t sz) {
310 if (protocol_read_maybe(data, sz) < 0)
311 die("monitor process quit unexpectedly");
314 // Returns 0 if OK, error msg if peer was garbage.
315 static const char *read_greeting(void) {
316 char got_magic[sizeof(header_magic)];
318 if (protocol_read_maybe(&got_magic, sizeof(got_magic)) < 0)
319 return "initial monitor process quit";
321 if (memcmp(got_magic, header_magic, sizeof(header_magic)))
322 die("got unexpected protocol magic 0x%02x%02x%02x%02x",
323 got_magic[0], got_magic[1], got_magic[2], got_magic[3]);
326 protocol_read(&xdata_len, sizeof(xdata_len));
327 void *xdata = xmalloc(xdata_len);
328 protocol_read(xdata, xdata_len);
333 // Returns: call(client-end), or 0 to mean "is garbage"
334 // find_socket_path must have been called
335 static FILE *connect_existing(void) {
339 fd = socket(AF_UNIX, SOCK_STREAM, 0);
340 if (fd==-1) diee("socket() for client");
342 socklen_t salen = sizeof(sockaddr_sun);
343 r = connect(fd, (const struct sockaddr*)&sockaddr_sun, salen);
345 if (errno==ECONNREFUSED || errno==ENOENT) goto x_garbage;
346 diee("connect() %s", socket_path);
349 call_sock = call_sock_from_fd(fd);
358 if (call_sock) { fclose(call_sock); call_sock=0; }
359 if (fd >= 0) close(fd);
363 static __attribute__((noreturn))
364 void become_setup(int sfd, int fake_pair[2]) {
366 int call_fd = fake_pair[1];
368 int null_0 = open("/dev/null", O_RDONLY); if (null_0 < 0) diee("open null");
369 if (dup2(null_0, 0)) diee("dup2 /dev/null onto stdin");
370 if (dup2(2, 1) != 1) die("dup2 stderr onto stdout");
372 // Extension could work like this:
374 // We advertise a new protocol (perhaps one which is nearly entirely
375 // different after the connect) by putting a name for it comma-separated
376 // next to "v1". Simple extension can be done by having the script
377 // side say something about it in the ack xdata, which we currently ignore.
378 putenv(m_asprintf("PREFORK_INTERP=v1 %d,%d %s",
379 sfd, call_fd, socket_path));
381 execvp(executor_argv[0], (char**)executor_argv);
382 diee("execute %s", executor_argv[0]);
385 static void connect_or_spawn(void) {
388 call_sock = connect_existing();
389 if (call_sock) return;
391 int lockfd = acquire_lock();
392 call_sock = connect_existing();
393 if (call_sock) { close(lockfd); return; }
395 // We must start a fresh one, and we hold the lock
397 r = unlink(socket_path);
398 if (r<0 && errno!=ENOENT)
399 diee("failed to remove stale socket %s", socket_path);
402 r = socketpair(AF_UNIX, SOCK_STREAM, 0, fake_pair);
403 if (r<0) diee("socketpair() for fake initial connection");
405 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
406 if (sfd<0) diee("socket() for new listener");
408 socklen_t salen = sizeof(sockaddr_sun);
409 r= bind(sfd, (const struct sockaddr*)&sockaddr_sun, salen);
410 if (r<0) diee("bind() on new listener");
412 // We never want callers to get ECONNREFUSED. But:
413 // There is a race here: from my RTFM they may get ECONNREFUSED
414 // if they try between our bind() and listen(). But if they do, they'll
415 // acquire the lock (serialising with us) and retry, and then it will work.
416 r = listen(sfd, INT_MAX);
417 if (r<0) diee("listen() for new listener");
419 pid_t setup_pid = fork();
420 if (setup_pid == (pid_t)-1) diee("fork for spawn setup");
421 if (!setup_pid) become_setup(sfd, fake_pair);
425 call_sock = call_sock_from_fd(fake_pair[0]);
428 pid_t got = waitpid(setup_pid, &status, 0);
429 if (got == (pid_t)-1) diee("waitpid setup [%ld]", (long)setup_pid);
430 if (got != setup_pid) diee("waitpid setup [%ld] gave [%ld]!",
431 (long)setup_pid, (long)got);
432 if (status != 0) propagate_exit_status(status, "setup");
434 const char *emsg = read_greeting();
435 if (emsg) die("setup failed: %s", emsg);
441 static void make_executor_argv(const char *const *argv) {
442 switch (laundering) {
444 default: die("need -U (specifying unlaundered argument handling)");
448 #define EACH_NEW_ARG(EACH) { \
449 arg = interp; { EACH } \
450 if ((arg = script)) { EACH } \
451 const char *const *walk = argv; \
452 while ((arg = *walk++)) { EACH } \
456 EACH_NEW_ARG( (void)arg; count++; );
458 const char **out = calloc(count, sizeof(char*));
459 executor_argv = (const char* const*)out;
460 if (!executor_argv) diee("allocate for arguments");
462 EACH_NEW_ARG( *out++ = arg; );
466 int main(int argc_unused, const char *const *argv) {
473 // which ought to be passed on to the actual executor.
474 make_executor_argv(argv);
477 FILLZERO(sockaddr_sun);
478 sockaddr_sun.sun_family = AF_UNIX;
479 assert(strlen(socket_path) <= sizeof(sockaddr_sun.sun_path));
480 strncpy(sockaddr_sun.sun_path, socket_path, sizeof(sockaddr_sun.sun_path));
484 // We're committed now, send the request (or bail out)
488 protocol_read(&status, sizeof(status));
490 status = ntohl(status);
491 if (status > INT_MAX) die("status 0x%lx does not fit in an int",
492 (unsigned long)status);
494 propagate_exit_status(status, "invocation");