From 0381be8b6ea01aa989f6272c06612ff03c8deb8b Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Mon, 30 Apr 2018 16:23:46 +0100
Subject: [PATCH] wrap: Fix LD_PRELOAD for SteamOS libmodeswitch_inhibitor.so

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel-wrap | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/pressure-vessel-wrap b/pressure-vessel-wrap
index 04ed1f50a..396881c28 100755
--- a/pressure-vessel-wrap
+++ b/pressure-vessel-wrap
@@ -271,16 +271,6 @@ if [ -z "$interactive" ]; then
     bwrap_options+=(--new-session)
 fi
 
-# Put the caller's LD_PRELOAD and search paths back.
-if [ -n "$caller_ld_preload" ]; then
-    bwrap_options+=(--setenv LD_PRELOAD "$caller_ld_preload")
-else
-    bwrap_options+=(--unsetenv LD_PRELOAD)
-fi
-if [ -n "$caller_path" ]; then
-    bwrap_options+=(--setenv PATH "$caller_path")
-fi
-
 overrides=
 
 for t in "${multiarch_tuples[@]}"; do
@@ -613,9 +603,21 @@ fi
 # locations
 old_ifs="$IFS"
 IFS=:
+adjusted_ld_preload=
 for preload in $caller_ld_preload; do
     if [ -e "$preload" ]; then
-        bwrap_options+=(--ro-bind "$preload" "$preload")
+        case "$preload" in
+            (/usr/*|/lib*)
+                # We can't write here, so redirect them to the
+                # corresponding locations in /run/host
+                bwrap_options+=(--ro-bind "$preload" "/run/host$preload")
+                adjusted_ld_preload="${adjusted_ld_preload:+"$adjusted_ld_preload:"}/run/host$preload"
+                ;;
+            (*)
+                bwrap_options+=(--ro-bind "$preload" "$preload")
+                adjusted_ld_preload="${adjusted_ld_preload:+"$adjusted_ld_preload:"}$preload"
+                ;;
+        esac
     fi
 done
 IFS="$old_ifs"
@@ -637,6 +639,16 @@ else
 fi
 bwrap_options+=(--chdir "$PWD")
 
+# Put the caller's LD_PRELOAD and search paths back.
+if [ -n "$adjusted_ld_preload" ]; then
+    bwrap_options+=(--setenv LD_PRELOAD "$adjusted_ld_preload")
+else
+    bwrap_options+=(--unsetenv LD_PRELOAD)
+fi
+if [ -n "$caller_path" ]; then
+    bwrap_options+=(--setenv PATH "$caller_path")
+fi
+
 if "$BWRAP" --help | grep unshare-uts >/dev/null; then
     # Set a standard hostname for the container to make it easier
     # to see which shell is which
-- 
GitLab