2022-05-21 14:52:58 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
if [ "$(id -u)" -eq 0 ]; then
|
2022-07-10 21:13:45 +00:00
|
|
|
SUDO=
|
|
|
|
else
|
|
|
|
SUDO=sudo
|
2022-05-21 14:52:58 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-10 21:13:45 +00:00
|
|
|
echo "Actually Portable Executable (APE) Installer" >&2
|
|
|
|
echo "Author: Justine Tunney <jtunney@gmail.com>" >&2
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# INSTALL APE LOADER SYSTEMWIDE
|
|
|
|
|
2023-07-11 11:29:33 +00:00
|
|
|
if [ -f o/depend ] && make -j8 o//ape; then
|
|
|
|
echo "successfully recompiled ape loader" >&2
|
2022-07-10 21:13:45 +00:00
|
|
|
elif [ -d build/bootstrap ]; then
|
|
|
|
# if make isn't being used then it's unlikely the user changed the sources
|
|
|
|
# in that case the prebuilt binaries should be completely up-to-date
|
|
|
|
echo "using prebuilt ape loader from cosmo repo" >&2
|
|
|
|
mkdir -p o//ape || exit
|
|
|
|
cp -af build/bootstrap/ape.elf o//ape/ape.elf || exit
|
|
|
|
cp -af build/bootstrap/ape.macho o//ape/ape.macho || exit
|
2022-05-21 14:52:58 +00:00
|
|
|
else
|
2022-07-10 21:13:45 +00:00
|
|
|
echo "no cosmopolitan libc repository here" >&2
|
|
|
|
echo "fetching ape loader from justine.lol" >&2
|
2022-05-21 14:52:58 +00:00
|
|
|
mkdir -p o//ape || exit
|
2022-07-10 21:13:45 +00:00
|
|
|
if command -v wget >/dev/null 2>&1; then
|
|
|
|
wget -qO o//ape/ape.elf https://justine.lol/ape.elf || exit
|
|
|
|
wget -qO o//ape/ape.macho https://justine.lol/ape.macho || exit
|
|
|
|
else
|
|
|
|
curl -Rso o//ape/ape.elf https://justine.lol/ape.elf || exit
|
|
|
|
curl -Rso o//ape/ape.macho https://justine.lol/ape.macho || exit
|
|
|
|
fi
|
|
|
|
chmod +x o//ape/ape.elf || exit
|
|
|
|
chmod +x o//ape/ape.macho || exit
|
2022-05-21 14:52:58 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-10 21:13:45 +00:00
|
|
|
if [ "$(uname -s)" = "Darwin" ]; then
|
|
|
|
if ! [ /usr/bin/ape -nt o//ape/ape.macho ]; then
|
|
|
|
echo >&2
|
|
|
|
echo "installing o//ape/ape.elf to /usr/bin/ape" >&2
|
|
|
|
echo "$SUDO mv -f o//ape/ape.elf /usr/bin/ape" >&2
|
|
|
|
$SUDO cp -f o//ape/ape.macho /usr/bin/ape || exit
|
|
|
|
echo "done" >&2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if ! [ /usr/bin/ape -nt o//ape/ape.elf ]; then
|
|
|
|
echo >&2
|
|
|
|
echo "installing o//ape/ape.elf to /usr/bin/ape" >&2
|
|
|
|
echo "$SUDO mv -f o//ape/ape.elf /usr/bin/ape" >&2
|
|
|
|
$SUDO cp -f o//ape/ape.elf /usr/bin/ape || exit
|
|
|
|
echo "done" >&2
|
|
|
|
fi
|
2022-05-21 14:52:58 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-10 21:13:45 +00:00
|
|
|
################################################################################
|
|
|
|
# REGISTER APE LOADER WITH BINFMT_MISC TOO (LINUX-ONLY)
|
|
|
|
|
|
|
|
if [ x"$(uname -s)" = xLinux ]; then
|
|
|
|
|
|
|
|
if [ -e /proc/sys/fs/binfmt_misc/APE ]; then
|
|
|
|
echo >&2
|
|
|
|
echo it looks like APE is already registered with binfmt_misc >&2
|
|
|
|
echo To reinstall please run ape/apeuninstall.sh first >&2
|
|
|
|
echo please check that it is mapped to ape not /bin/sh >&2
|
|
|
|
echo cat /proc/sys/fs/binfmt_misc/APE >&2
|
|
|
|
cat /proc/sys/fs/binfmt_misc/APE >&2
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -e /proc/sys/fs/binfmt_misc ]; then
|
|
|
|
echo >&2
|
|
|
|
echo loading binfmt_misc into your kernel >&2
|
|
|
|
echo you may need to edit configs to persist across reboot >&2
|
|
|
|
echo $SUDO modprobe binfmt_misc >&2
|
|
|
|
$SUDO modprobe binfmt_misc || exit
|
|
|
|
echo done >&2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -e /proc/sys/fs/binfmt_misc/register ]; then
|
|
|
|
echo >&2
|
|
|
|
echo mounting binfmt_misc into your kernel >&2
|
|
|
|
echo you may need to edit configs to persist across reboot >&2
|
|
|
|
echo $SUDO mount -t binfmt_misc none /proc/sys/fs/binfmt_misc >&2
|
|
|
|
$SUDO mount -t binfmt_misc none /proc/sys/fs/binfmt_misc || exit
|
|
|
|
echo done >&2
|
|
|
|
fi
|
2022-05-21 14:52:58 +00:00
|
|
|
|
|
|
|
echo >&2
|
2022-07-10 21:13:45 +00:00
|
|
|
echo registering APE with binfmt_misc >&2
|
2022-05-21 14:52:58 +00:00
|
|
|
echo you may need to edit configs to persist across reboot >&2
|
2022-07-10 21:13:45 +00:00
|
|
|
echo '$SUDO sh -c "echo '"'"':APE:M::MZqFpD::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2
|
|
|
|
$SUDO sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit
|
Introduce new `jartsr='` ape prefix
Files beginning with "MZqFpD" are Actually Portable Executables,
which have a printf statement in the first 4096 bytes with octal
codes that specify the ELF header. APE also specifies `jartsr='`
as an alternative prefix, intended for binaries that do not want
to be identified as Windows executables. Like the \177ELF magic,
all these prefixes decode as x86 jump instructions that could be
used for 16-bit bootloaders or 32-bit / 64-bit flat executables.
Most importantly they provide a fallback path for Thompson shell
copmatible command interpreters, which do not require a shebang,
e.g. bash, zsh, fish, bourne, almquist, etc. Please note that in
order to meet the requirements of POSIX.1, the single quote must
be followed by a newline character, before any null bytes occur.
See also: https://www.austingroupbugs.net/view.php?id=1250
2022-11-11 04:58:48 +00:00
|
|
|
echo '$SUDO sh -c "echo '"'"':APE-jart:M::jartsr::/usr/bin/ape:'"'"' >/proc/sys/fs/binfmt_misc/register"' >&2
|
|
|
|
$SUDO sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" || exit
|
2022-05-21 14:52:58 +00:00
|
|
|
echo done >&2
|
2022-07-10 21:13:45 +00:00
|
|
|
|
2022-07-13 10:08:16 +00:00
|
|
|
if [ x"$(cat /proc/sys/fs/binfmt_misc/status)" = xdisabled ]; then
|
|
|
|
echo >&2
|
|
|
|
echo enabling binfmt_misc >&2
|
|
|
|
echo you may need to edit configs to persist across reboot >&2
|
|
|
|
echo $SUDO sh -c 'echo 1 >/proc/sys/fs/binfmt_misc/status' >&2
|
|
|
|
$SUDO sh -c 'echo 1 >/proc/sys/fs/binfmt_misc/status' || exit
|
|
|
|
echo done >&2
|
|
|
|
fi
|
|
|
|
|
2022-05-21 14:52:58 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-10 21:13:45 +00:00
|
|
|
################################################################################
|
|
|
|
|
|
|
|
{
|
|
|
|
echo
|
|
|
|
echo "------------------------------------------------------------------"
|
|
|
|
echo
|
|
|
|
echo "APE INSTALL COMPLETE"
|
|
|
|
echo
|
|
|
|
echo "If you decide to uninstall APE later on"
|
|
|
|
echo "you may do so using ape/apeuninstall.sh"
|
|
|
|
echo
|
|
|
|
echo "Enjoy your APE loader (>'.')>"
|
|
|
|
echo
|
|
|
|
} >&2
|