[PATCH xserver] xfree86: search for modules "breadth-first"

Luca Boccassi luca.boccassi at gmail.com
Sun May 8 20:55:21 UTC 2016


Depending on the order of files returned by readdir(), subdirs might
be searched before the current directory.
This causes the wrong libglx.so to be used in some configurations,
including with the NVIDIA stack on Debian with Bumblebee, as the
correct libglx is located in /usr/lib/nvidia (passed to Xorg via
-modulepath), but the wrong libglx is used instead from a subdir.
In FindModuleInSubdir function in hw/xfree86/loader/loadmod.c search
the current directory first before recursing into the subdirectories.

Signed-off-by: Luca Boccassi <luca.boccassi at gmail.com>
---

Tested on Debian Sid with Intel integrated graphics and Nvidia driver
via Bumblebee.

---
 hw/xfree86/loader/loadmod.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
index 702d4e7..7478aba 100644
--- a/hw/xfree86/loader/loadmod.c
+++ b/hw/xfree86/loader/loadmod.c
@@ -401,12 +401,9 @@ FindModuleInSubdir(const char *dirpath, const char *module)
         snprintf(tmpBuf, PATH_MAX, "%s%s/", dirpath, direntry->d_name);
         /* the stat with the appended / fails for normal files,
            and works for sub dirs fine, looks a bit strange in strace
-           but does seem to work */
-        if ((stat(tmpBuf, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode)) {
-            if ((ret = FindModuleInSubdir(tmpBuf, module)))
-                break;
+           but does seem to work. Search current directory first. */
+        if ((stat(tmpBuf, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
             continue;
-        }
 
 #ifdef __CYGWIN__
         snprintf(tmpBuf, PATH_MAX, "cyg%s.dll", module);
@@ -443,6 +440,26 @@ FindModuleInSubdir(const char *dirpath, const char *module)
     }
 
     closedir(dir);
+    if (ret)
+        return ret;
+
+    dir = opendir(dirpath);
+    if (!dir)
+        return NULL;
+
+    /* Search subdirectories last. This is especially important when
+       the user sets -modulepath, as it is clear that the intention is to
+       have it searched first, before lookinto into subdirectories. */
+    while ((direntry = readdir(dir))) {
+        if (direntry->d_name[0] == '.')
+            continue;
+        snprintf(tmpBuf, PATH_MAX, "%s%s/", dirpath, direntry->d_name);
+        if ((stat(tmpBuf, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode) &&
+                (ret = FindModuleInSubdir(tmpBuf, module)))
+            break;
+    }
+
+    closedir(dir);
     return ret;
 }
 
-- 
2.1.4



More information about the xorg-devel mailing list