+exists (const char *path)
+{
+ struct stat st;
+ int err;
+
+ err = stat (path, &st);
+
+ /* Failed to stat. It either doesn't exist, or might as well not. */
+ if (err == -1)
+ return 0;
+
+ return 1;
+}
+
+/* Given "library" filename resolve it to an absolute path to an
+ * existing file as follows:
+ *
+ * 1. Look in same directory as current executable image
+ *
+ * This is to support running from the source directory, without
+ * having installed anything.
+ *
+ * 2. Look in relative path from $(foo)/$(bindir) to
+ * $(foo)/$(libdir)/fips based on $(foo) from current executable
+ * image and configured $(bindir) and $(libdir).
+ *
+ * We do this rather than looking directly at the configured
+ * $(libdir) to support cases where the application may have been
+ * moved after being installed, (in particular, we want to be
+ * particularly careful not to mix one program with a different
+ * wrapper---so this "nearest search" should most often be
+ * correct.
+ *
+ * Returns: a string talloc'ed to 'ctx'
+ */
+static char *
+resolve_path (void *ctx, const char *library)
+{
+ char *bin_path, *lib_path;
+
+ bin_path = get_bin_name (ctx);
+
+ chop_trailing_path_component (bin_path);
+
+ lib_path = talloc_asprintf(ctx, "%s/%s", bin_path, library);
+
+ if (exists (lib_path))
+ return lib_path;
+
+ talloc_free (lib_path);
+
+ lib_path = talloc_asprintf(ctx, "%s/" BINDIR_TO_LIBFIPSDIR "/%s",
+ bin_path, library);
+
+ if (exists (lib_path))
+ return lib_path;
+
+ fprintf (stderr, "Error: Failed to find library %s.\n", library);
+ fprintf (stderr, "Looked in both:\n"
+ "\t%s\n"
+ "and\n"
+ "\t%s/" BINDIR_TO_LIBFIPSDIR "\n", bin_path, bin_path);
+ exit (1);
+}
+
+/* After forking, set LD_PRELOAD to preload "library" within child
+ * environment, then exec given arguments.
+ *
+ * The "library" argument is the filename (without path) of a shared
+ * library to load. The complete path will be resolved with
+ * resolve_library_path above. */
+static int
+fork_exec_with_preload_and_wait (char * const argv[], const char *library)