[PATCH] libwine: Fully dereference the /proc/self/exe symbolic link.

Francois Gouget fgouget at codeweavers.com
Sun Sep 1 21:20:40 CDT 2019


Linux will do it for us but not NetBSD. That is, if foo is an executable
that prints the path /proc/self/exe points to, on Linux one gets:

$ ./foo
/tmp/foo
$ ln -s foo bar
$ ln -s bar babar
$ /tmp/babar
/tmp/foo

But on NetBSD one gets instead:

$ ./foo
/tmp/./foo
$ ln -s foo bar
$ ln -s bar babar
$ /tmp/babar
/tmp/babar

Fully dereferencing /proc/self/exe is necessary to be able to run both
32 and 64 bit executables from the build tree.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---

Using realpath() simplifies the code quite a bit and letting it allocate 
the memory itself seems to be part of POSIX.1-2008. But let me know if 
this self-allocating feature should be avoided or if realpath() should 
be avoided altogether (though note we're already using it in a couple of 
places).

 libs/wine/config.c | 31 +++++++------------------------
 1 file changed, 7 insertions(+), 24 deletions(-)

diff --git a/libs/wine/config.c b/libs/wine/config.c
index ab5e3816dc5..f4fda02bf95 100644
--- a/libs/wine/config.c
+++ b/libs/wine/config.c
@@ -164,33 +164,16 @@ static char *get_runtime_libdir(void)
 /* read a symlink and return its directory */
 static char *symlink_dirname( const char *name )
 {
-    char *p, *buffer, *absdir = NULL;
-    int ret, size;
+    char *p, *fullpath = NULL;
 
-    for (size = 256; ; size *= 2)
+    fullpath = realpath( name, NULL );
+    if (fullpath)
     {
-        if (!(buffer = malloc( size ))) return NULL;
-        if ((ret = readlink( name, buffer, size )) == -1) break;
-        if (ret != size)
-        {
-            buffer[ret] = 0;
-            if (!(p = strrchr( buffer, '/' ))) break;
-            if (p == buffer) p++;
-            *p = 0;
-            if (buffer[0] == '/') return buffer;
-            /* make it absolute */
-            absdir = xmalloc( strlen(name) + strlen(buffer) + 1 );
-            strcpy( absdir, name );
-            if (!(p = strrchr( absdir, '/' ))) break;
-            strcpy( p + 1, buffer );
-            free( buffer );
-            return absdir;
-        }
-        free( buffer );
+        p = strrchr( fullpath, '/' );
+        if (p == fullpath) p++;
+        if (p) *p = 0;
     }
-    free( buffer );
-    free( absdir );
-    return NULL;
+    return fullpath;
 }
 
 /* return the directory that contains the main exe at run-time */
-- 
2.20.1




More information about the wine-devel mailing list