Merge pull request #126 from X0rg/request

Improvements for darling script
This commit is contained in:
Luboš Doležel 2016-02-26 21:35:32 +01:00
commit 2b561eca79

View File

@ -24,6 +24,19 @@ DARLING_PREFIX=$(realpath "$SCRIPTPATH/..")
selfmtime=$(stat -c %Y "$DARLING_PREFIX/bin/darling")
check_prefix() {
if [ ! -d "$DPREFIX" -o ! -f "$DPREFIX/.update-timestamp" ]; then
setup_prefix "$DPREFIX"
else
updatets=$(cat $DPREFIX/.update-timestamp)
if [ "$updatets" != "disable" -a "$updatets" -lt $selfmtime ]; then
setup_prefix "$DPREFIX"
fi
fi
}
setup_prefix() {
prefix_pkg="/tmp/prefix.$$.pkg"
@ -87,12 +100,56 @@ install_pkg() {
rm -rf "$temp"
}
is_sudo_allowed() {
if type -p sudo &> /dev/null; then
sudo -nl "$@" &> /dev/null && return 0
fi
return 1
}
launch_with_su() {
if [ "$(id -u)" == 0 ]; then
"$@"
elif is_sudo_allowed; then
sudo "$@"
else
local cmd=$(printf '%q ' "$@")
su --shell=$BASH --command "$cmd"
fi
}
darling_check() {
# Returning values:
# 0: module is loaded, file permissions are good
# 1: module is loaded, file permissions are wrong
# 2: module is not loaded
grep -q darling_mach "/proc/modules"
if [ $? -eq 0 ]; then
[ -r "/dev/mach" -a -w "/dev/mach" ] && return 0 || return 1
else
return 2
fi
}
darling_load() {
modprobe darling-mach
if $(darling_check); then
>&2 echo "Module is already loaded."
else
>&2 echo -n "Loading module... "
launch_with_su modprobe darling-mach && >&2 echo "Done." || >&2 echo "Fail."
sleep 1 # Wait for module loading
launch_with_su chmod a+rw "/dev/mach"
fi
}
darling_unload() {
rmmod darling-mach
if $(darling_check); then
>&2 echo -n "Unloading module... "
launch_with_su rmmod darling-mach && >&2 echo "Done." || >&2 echo "Fail."
else
>&2 echo "Module is not loaded, so it can't be unloaded."
fi
}
if [ $# -eq 0 ]; then
@ -105,6 +162,8 @@ if [ $# -eq 0 ]; then
>&2 echo -e "\tdarling shell\t\t\tStart bash shell in prefix"
#>&2 echo -e "\tdarling hdiutil\t\t\tMount DMG disk images"
#>&2 echo -e "\tdarling pkgutil\t\t\tInstall PKG packages"
>&2 echo -e "\tdarling load\t\t\tLoad kernel module"
>&2 echo -e "\tdarling unload\t\t\tUnload kernel module"
>&2 echo
>&2 echo "The prefix is specified by the DPREFIX environment variable."
>&2 echo "The default DPREFIX is \$HOME/.darling"
@ -117,15 +176,9 @@ if [ -z "$DPREFIX" ]; then
export DPREFIX="$HOME/.darling"
fi
if [ ! -d "$DPREFIX" -o ! -f "$DPREFIX/.update-timestamp" ]; then
setup_prefix "$DPREFIX"
else
updatets=$(cat $DPREFIX/.update-timestamp)
if [ "$updatets" != "disable" -a "$updatets" -lt $selfmtime ]; then
setup_prefix "$DPREFIX"
fi
if ! $(darling_check) && [ "$1" != "load" ] && [ "$1" != "unload" ]; then
2>&1 echo "Cannot open /dev/mach, running 'darling load'."
darling_load
fi
#dyld_path="${0%darling}dyld"
@ -133,6 +186,8 @@ dyld_path="${DARLING_PREFIX}/bin/dyld"
case "$1" in
"shell")
check_prefix
if [ $# -gt 1 ]; then
exec "${dyld_path}" "${DPREFIX}/bin/bash" -c "${*:2}"
else
@ -148,22 +203,13 @@ case "$1" in
# exit 1
# ;;
"load")
if [ "$(id -u)" != 0 ]; then
2>&1 "You need to be root for this command."
exit 1
fi
darling_load
;;
"unload")
if [ "$(id -u)" != 0 ]; then
2>&1 "You need to be root for this command."
exit 1
fi
darling_unload
;;
*)
check_prefix
exec "${dyld_path}" "$1" "${@:2}"
;;
esac