+struct saved_file {
+ struct saved_file *next;
+ int fd;
+ void *contents;
+ off_t off, len;
+};
+
+static struct saved_file *save_file(struct saved_file *next, int fd)
+{
+ struct saved_file *s = malloc(sizeof(*s));
+
+ s->next = next;
+ s->fd = fd;
+ s->off = lseek(fd, 0, SEEK_CUR);
+ /* Special file? Erk... */
+ assert(s->off != -1);
+ s->len = lseek(fd, 0, SEEK_END);
+ lseek(fd, 0, SEEK_SET);
+ s->contents = malloc(s->len);
+ if (read(fd, s->contents, s->len) != s->len)
+ err(1, "Failed to save %zu bytes", (size_t)s->len);
+ lseek(fd, s->off, SEEK_SET);
+ return s;
+}
+
+/* We have little choice but to save and restore open files: mmap means we
+ * can really intercept changes in the child.
+ *
+ * We could do non-mmap'ed files on demand, however. */
+static struct saved_file *save_files(void)