3 # Script to install host system binaries along with required libraries.
5 # Copyright (C) 2012-2017 Jo-Philipp Wich <jo@mein.io>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 cp ${VERBOSE:+-v} -L "$1" "$2" ||
{
25 echo "cp($1 $2) failed" >&2
31 mv ${VERBOSE:+-v} "$1" "$2" ||
{
32 echo "mv($1 $2) failed" >&2
38 mkdir
${VERBOSE:+-v} -p "$1" ||
{
39 echo "mkdir($1) failed" >&2
45 ln ${VERBOSE:+-v} -sf "$1" "$2" ||
{
46 echo "ln($1 $2) failed" >&2
52 local base
="$(readlink -f "$1")"
53 local dest
="$(readlink -f "$2")"
56 [ -d "$base" ] || base
="${base%/*}"
57 [ -d "$dest" ] || dest
="${dest%/*}"
62 echo "$up/${base#$dest/}"
74 cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" -
79 int mangle_arg0(int argc, char **argv, char **env) {
80 char *arg0 = getenv("RUNAS_ARG0");
84 unsetenv("RUNAS_ARG0");
87 unsetenv("LD_PRELOAD");
92 __attribute__((section("__DATA,__mod_init_func")))
94 __attribute__((section(".init_array")))
96 static void *mangle_arg0_constructor = &mangle_arg0;
100 echo "compiling preload library failed" >&2
106 _cp
"$1" "$1.patched"
107 sed -i -e 's,/\(usr\|lib\|etc\)/,/###/,g' "$1.patched"
109 if "$1.patched" 2>&1 |
grep -q -- --library-path; then
110 _mv
"$1.patched" "$1"
112 echo "binary patched ${1##*/} not executable, using original" >&2
118 _cp
"$1" "$1.patched"
119 sed -i -e 's,/usr/\(\(lib\|share\)/locale\),/###/\1,g' "$1.patched"
121 if "$1.patched" 2>&1 |
grep -q -- GNU
; then
122 _mv
"$1.patched" "$1"
124 echo "binary patched ${1##*/} not executable, using original" >&2
129 for LDD
in ${PATH//://ldd }/ldd
; do
130 "$LDD" --version >/dev
/null
2>/dev
/null
&& break
134 [ -n "$LDD" -a -x "$LDD" ] || LDD
=
137 [ -n "$BIN" -a -n "$DIR" ] ||
{
138 echo "Usage: $0 <destdir> <executable> ..." >&2
142 [ ! -d "$DIR/lib" ] && {
145 _ln
"../lib" "$DIR/usr/lib"
148 [ ! -x "$DIR/lib/runas.so" ] && {
149 _runas_so
"$DIR/lib/runas.so"
154 [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" |
grep -sqE "ELF.*(executable|interpreter|uses shared libs)" && {
155 for token
in $
("$LDD" "$BIN" 2>/dev
/null
); do
156 case "$token" in */*.so
*)
157 dest
="$DIR/lib/${token##*/}"
161 */ld-
*.so
*) LDSO
="${token##*/}" ;;
164 [ -f "$token" -a ! -f "$dest" ] && {
168 */ld-
*.so
*) _patch_ldso
"$dest" ;;
169 */libc.so
.6) _patch_glibc
"$dest" ;;
176 # is a dynamically linked executable
177 if [ -n "$LDSO" ]; then
178 echo "Bundling ${BIN##*/}"
180 RUNDIR
="$(readlink -f "$BIN")"; RUNDIR
="${RUNDIR%/*}"
181 RUN
="${LDSO#ld-}"; RUN
="run-${RUN%%.so*}.sh"
182 REL
="$(_relpath "$DIR/lib
" "$BIN")"
184 _mv
"$BIN" "$RUNDIR/.${BIN##*/}.bin"
188 dir="\$(dirname "\$0")"
189 export RUNAS_ARG0="\$0"
190 export LD_PRELOAD="\${LD_PRELOAD:+\$LD_PRELOAD:}\$dir/${REL:+$REL/}runas.so"
191 exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@"
194 chmod ${VERBOSE:+-v} 0755 "$BIN"