+static void free_files(struct saved_file *s)
+{
+ while (s) {
+ struct saved_file *next = s->next;
+ free(s->contents);
+ free(s);
+ s = next;
+ }
+}
+
+static void free_call(struct failtest_call *call)
+{
+ /* We don't do this in cleanup: needed even for failed opens. */
+ if (call->type == FAILTEST_OPEN)
+ free((char *)call->u.open.pathname);
+ free(call->backtrace);
+ tlist_del_from(&history, call, list);
+ free(call);
+}
+
+/* Free up memory, so valgrind doesn't report leaks. */
+static void free_everything(void)
+{
+ struct failtest_call *i;
+
+ while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
+ free_call(i);
+
+ failtable_clear(&failtable);
+}
+
+static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
+{
+ struct failtest_call *i;
+
+ /* For children, we don't care if they "failed" the testing. */
+ if (control_fd != -1)
+ status = 0;
+
+ if (forced_cleanup) {
+ /* We didn't actually do final operation: remove it. */
+ i = tlist_tail(&history, struct failtest_call, list);
+ free_call(i);
+ }
+
+ /* Cleanup everything, in reverse order. */
+ tlist_for_each_rev(&history, i, list) {
+ if (!i->cleanup)
+ continue;
+ if (!forced_cleanup) {
+ printf("Leak at %s:%u: --failpath=%s\n",
+ i->file, i->line, failpath_string());
+ status = 1;
+ }
+ i->cleanup(&i->u);
+ }
+
+ free_everything();
+ tell_parent(SUCCESS);
+ exit(status);
+}
+