From 36bd86d0a83f252a4e43df6638d6563304571711 Mon Sep 17 00:00:00 2001 From: merakor Date: Mon, 10 Aug 2020 08:21:57 +0000 Subject: as_root(): use 'env' for setting variables. FossilOrigin-Name: 1cfe280b0c731ed14d08c3e89f8eef0ee2202e9dbd9d5aeca2587dd4427f8124 --- src/cpt-lib | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cpt-lib b/src/cpt-lib index 3218fab..df3f92d 100644 --- a/src/cpt-lib +++ b/src/cpt-lib @@ -75,10 +75,24 @@ as_root() { # 'doas' or 'su'. Hurrah for choice. [ "$uid" = 0 ] || log "Using '${su:-su}' (to become ${user:=root})" + # We are exporting package manager variables, so that we still have the same + # repository paths / access to the same cache directories etc. + set -- HOME="$HOME" \ + XDG_CACHE_HOME="$XDG_CACHE_HOME" \ + CPT_PATH="$CPT_PATH" \ + CPT_FORCE="$CPT_FORCE" \ + CPT_HOOK="$CPT_HOOK" \ + CPT_ROOT="$CPT_ROOT" \ + CPT_CHOICE="$CPT_CHOICE" \ + CPT_CACHE="$CPT_CACHE" \ + CPT_TMPDIR="$CPT_TMPDIR" \ + CPT_HOOK="$CPT_HOOK" \ + CPT_PID="$CPT_PID" \ + "$@" + case ${su##*/} in - sudo) sudo -E -u "$user" -- "$@" ;; - doas) doas -u "$user" -- "$@" ;; - su) su -pc "env USER=$user $* <&3" "$user" 3<&0