]> git.cworth.org Git - glaze/commitdiff
Teach libglaze to look for wrapper library in current working directory
authorCarl Worth <cworth@cworth.org>
Mon, 23 Sep 2013 15:16:27 +0000 (08:16 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 23 Sep 2013 15:16:27 +0000 (08:16 -0700)
Previously, with libfoo.so in the current directory, the following
command would fail:

glaze --wrapper=libfoo.so <program> <args>

And to fix things, the user would have to explicitly name the current
working directory such as:

glaze --wrapper=./libfoo.so <program> <args>

That seems like a silly requirement, (and is an artefact if the logic
of the dynamic loader).

To allow the convenient usage (without "./") we teach libglaze to
explicitly look for the wrapper in the current working directory, and
if found, to resolve its absolute path.

libglaze.c

index 8377d846a149321640e8b3dfc0bcae3c0d3ed1dc..17c473657f132b61bade34319ec268b9ce8f3c15 100644 (file)
@@ -197,20 +197,39 @@ read_process_output_one_line (const char *program)
  * If "wrapper" is an absolute path, return it directly.
  *
  * Otherwise, ("wrapper" is relative), look for an existing file named
- * "wrapper" in the same directory as the currently executing binary,
- * (as determined by /proc/self/exe). If that file exists, return its
- * path.
+ * "wrapper" in the following locations in order:
+ *
+ *     1. The current working directory
+ *
+ *     2. The same directory as the currently executing binary, (as
+ *         determined by /proc/self/exe).
+ *
+ * If either of those files exist, return its path.
  *
  * Otherwise, return the original, relative "wrapper".
  */
 static const char *
 resolve_wrapper_path (void *ctx, const char *wrapper)
 {
-       char *bin_path, *lib_path;
+       char *cwd_path, *bin_path, *lib_path;
 
+       /* Return absolute wrapper path immediately. */
        if (*wrapper == '/')
                return wrapper;
 
+       /* Look for wrapper in current working directory. */
+       cwd_path = get_current_dir_name ();
+
+       lib_path = talloc_asprintf (ctx, "%s/%s", cwd_path, wrapper);
+
+       free (cwd_path);
+
+       if (exists (lib_path))
+               return lib_path;
+
+       talloc_free (lib_path);
+
+       /* Look for wrapper next to current executable. */
        bin_path = get_bin_name (ctx);
 
        chop_trailing_path_component (bin_path);
@@ -224,6 +243,7 @@ resolve_wrapper_path (void *ctx, const char *wrapper)
 
        talloc_free (lib_path);
 
+       /* Allow relative wrapper path to remain as-is. */
        return wrapper;
 }