[explorer/init] Clean up
This commit is contained in:
parent
21c9e3db18
commit
d895bb0e87
1 changed files with 246 additions and 153 deletions
|
@ -20,90 +20,151 @@
|
||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Returns the process name of pid 1 ( normaly the init system )
|
# Returns the name of the init system (PID 1)
|
||||||
# for example at linux this value is "init" or "systemd" in most cases
|
|
||||||
#
|
#
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Expected values:
|
# Expected values:
|
||||||
# Linux:
|
# Linux:
|
||||||
|
# Adélie Linux:
|
||||||
|
# sysvinit+openrc
|
||||||
|
# Alpine Linux:
|
||||||
|
# busybox-init+openrc
|
||||||
|
# ArchLinux:
|
||||||
|
# systemd, sysvinit
|
||||||
|
# CRUX:
|
||||||
|
# sysvinit
|
||||||
|
# Debian:
|
||||||
|
# systemd, upstart, sysvinit, openrc, ???
|
||||||
|
# Devuan:
|
||||||
|
# sysvinit, ???
|
||||||
# Gentoo:
|
# Gentoo:
|
||||||
# sysvinit, openrc-init
|
# sysvinit+openrc, openrc-init, systemd
|
||||||
|
# OpenBMC:
|
||||||
|
# systemd
|
||||||
|
# OpenWrt:
|
||||||
|
# procd, init??
|
||||||
|
# RedHat (RHEL, CentOS, Fedora, RedHat Linux, ...):
|
||||||
|
# systemd, upstart, sysvinit
|
||||||
|
# Slackware:
|
||||||
|
# sysvinit
|
||||||
|
# SuSE:
|
||||||
|
# systemd, sysvinit
|
||||||
|
# Ubuntu:
|
||||||
|
# systemd, upstart, sysvinit
|
||||||
|
#
|
||||||
# GNU:
|
# GNU:
|
||||||
# Debian:
|
# Debian:
|
||||||
# hurd-init, sysvinit
|
# hurd-init, sysvinit
|
||||||
|
#
|
||||||
|
# BSD:
|
||||||
|
# {Free,Open,Net}BSD:
|
||||||
|
# init
|
||||||
|
#
|
||||||
|
# Mac OS X:
|
||||||
|
# launchd, init
|
||||||
|
#
|
||||||
|
# Solaris/Illumos:
|
||||||
|
# smf, init
|
||||||
|
|
||||||
|
|
||||||
# [root@fedora-12 ~]# readlink /proc/1/exe
|
# [root@fedora-12 ~]# readlink /proc/1/exe
|
||||||
# /sbin/init (deleted)
|
# /sbin/init (deleted)
|
||||||
# [root@fedora-12 ~]# ls -l /proc/1/exe
|
# [root@fedora-12 ~]# ls -l /proc/1/exe
|
||||||
# lrwxrwxrwx. 1 root root 0 2020-01-30 23:00 /proc/1/exe -> /sbin/init (deleted)
|
# lrwxrwxrwx. 1 root root 0 2020-01-30 23:00 /proc/1/exe -> /sbin/init (deleted)
|
||||||
|
|
||||||
# inspired by https://stackoverflow.com/a/33266819
|
set -e
|
||||||
shreadlink() (
|
#set -x # DEBUG
|
||||||
CDPATH=
|
|
||||||
target=$1 fname= targetDir=
|
|
||||||
|
|
||||||
# Resolve potential symlinks until the ultimate target is found.
|
validate_busybox_init() {
|
||||||
while :
|
# It is quite common to use SysVinit to stack other init systemd
|
||||||
do
|
# (like OpenRC) on top of it. So we check for that, too.
|
||||||
if ! test -e "$target"
|
if stacked=$(validate_openrc)
|
||||||
then
|
then
|
||||||
printf 'ERROR: %s does not exist.\n' "'$target'" >&2
|
echo "busybox-init+${stacked}"
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Change to target dir; necessary for correct resolution of target path.
|
|
||||||
cd "$(dirname -- "$target")"
|
|
||||||
|
|
||||||
fname=$(basename -- "$target") # Extract filename.
|
|
||||||
[ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
|
|
||||||
|
|
||||||
[ -L "$fname" ] || break
|
|
||||||
|
|
||||||
# Extract [next] target path, which may be defined
|
|
||||||
# *relative* to the symlink's own directory.
|
|
||||||
# Note: We parse `ls -l` output to find the symlink target
|
|
||||||
# which is the only POSIX-compliant, albeit somewhat fragile, way.
|
|
||||||
# FIXME: Will break if one of the filenames contain ’ -> ’
|
|
||||||
target=$(ls -l "$fname" | sed -e 's/^.* -> //')
|
|
||||||
done
|
|
||||||
|
|
||||||
# Get canonical dir. path
|
|
||||||
targetDir=$(pwd -P)
|
|
||||||
|
|
||||||
# Output the ultimate target's canonical path.
|
|
||||||
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
|
|
||||||
if test "$fname" = '.'
|
|
||||||
then
|
|
||||||
printf '%s\n' "${targetDir%/}"
|
|
||||||
elif test "$fname" = '..'
|
|
||||||
then
|
|
||||||
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
|
|
||||||
# AFTER canonicalization.
|
|
||||||
printf '%s\n' "$(dirname -- "${targetDir}")"
|
|
||||||
else
|
else
|
||||||
printf '%s/%s\n' "${targetDir%/}" "$fname"
|
echo busybox-init
|
||||||
fi
|
fi
|
||||||
)
|
}
|
||||||
|
|
||||||
|
validate_hurd_init() {
|
||||||
|
# FIXME: Test me!
|
||||||
|
test -x /hurd/init || return 1
|
||||||
|
grep -q 'GNU Hurd' /hurd/init || return 1
|
||||||
|
echo hurd-init
|
||||||
|
}
|
||||||
|
|
||||||
case $(uname -s) in
|
validate_openrc() {
|
||||||
Linux|GNU)
|
test -f /run/openrc/softlevel || return 1
|
||||||
# if test -f /proc/1/comm
|
echo openrc
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_procd() {
|
||||||
|
grep -q 'procd' /sbin/procd || return 1
|
||||||
|
echo procd
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_runit() {
|
||||||
|
test -d /run/runit || return 1
|
||||||
|
echo runit
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_smf() {
|
||||||
|
# XXX: Is this the correct way??
|
||||||
|
test -f /etc/svc/volatile/svc_nonpersist.db || return 1
|
||||||
|
echo smf
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_systemd() {
|
||||||
|
# NOTE: sd_booted(3)
|
||||||
|
test -d /run/systemd/system/ || return 1
|
||||||
|
# systemctl --version | sed -e '/^systemd/!d;s/^systemd //'
|
||||||
|
echo systemd
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_sysvinit() {
|
||||||
|
test -x /sbin/init \
|
||||||
|
&& grep -q 'INIT_VERSION=sysvinit-[0-9.]*' /sbin/init \
|
||||||
|
|| return 1
|
||||||
|
|
||||||
|
# It is quite common to use SysVinit to stack other init systemd
|
||||||
|
# (like OpenRC) on top of it. So we check for that, too.
|
||||||
|
if stacked=$(validate_openrc)
|
||||||
|
then
|
||||||
|
echo "sysvinit+${stacked}"
|
||||||
|
else
|
||||||
|
echo sysvinit
|
||||||
|
fi
|
||||||
|
unset stacked
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_upstart() {
|
||||||
|
test -x "$(command -v initctl)" || return 1
|
||||||
|
case $(initctl version)
|
||||||
|
in
|
||||||
|
*'(upstart '*')')
|
||||||
|
# if type -d /etc/init
|
||||||
# then
|
# then
|
||||||
# comm_name=$(cat /proc/1/comm)
|
# # modern (DBus-based?) upstart >= 0.5
|
||||||
# else
|
# :
|
||||||
# BusyBox's versions of ps and pgrep do not support some options
|
# elif type -d /etc/events.d
|
||||||
# depending on which compile-time options have been used.
|
# then
|
||||||
# Both pgrep and ps are tried to get the command name
|
# # ancient upstart
|
||||||
# comm_name=$(
|
# :
|
||||||
# pgrep -P0 -l 2>/dev/null | awk '/^1[ \t]/ { print $2 }'
|
|
||||||
# || ps -o comm= -p 1 2>/dev/null)
|
|
||||||
# fi
|
# fi
|
||||||
|
echo upstart
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
init_exe=$(shreadlink /proc/1/exe)
|
find_init_procfs() (
|
||||||
|
# First, check if the required file in procfs exists...
|
||||||
|
test -h /proc/1/exe || return 1
|
||||||
|
|
||||||
|
# Find init executable
|
||||||
|
init_exe=$(ls -l /proc/1/exe 2>/dev/null)
|
||||||
|
init_exe=${init_exe#* -> }
|
||||||
|
|
||||||
if ! test -x "$init_exe"
|
if ! test -x "$init_exe"
|
||||||
then
|
then
|
||||||
|
@ -122,83 +183,115 @@ case $(uname -s) in
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if test "$init_exe" = '/hurd/init'
|
echo "${init_exe}"
|
||||||
then
|
)
|
||||||
# XXX: Could maybe be removed
|
|
||||||
echo hurd-init
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
comm_name=$(basename "$init_exe")
|
# BusyBox's versions of ps and pgrep do not support some options
|
||||||
case $comm_name
|
# depending on which compile-time options have been used.
|
||||||
|
|
||||||
|
find_init_pgrep() {
|
||||||
|
pgrep -P0 -fl 2>/dev/null | awk -F '[[:blank:]]' '$1 == 1 { print $2 }'
|
||||||
|
}
|
||||||
|
|
||||||
|
find_init_ps() {
|
||||||
|
case $(uname -s)
|
||||||
in
|
in
|
||||||
init)
|
Darwin|NetBSD)
|
||||||
: # handled below
|
ps -o ucomm= -p 1 2>/dev/null
|
||||||
;;
|
;;
|
||||||
systemd)
|
FreeBSD)
|
||||||
# NOTE: sd_booted(3)
|
ps -o command= -p 1 2>/dev/null | cut -d ' ' -f 1
|
||||||
if test -d /run/systemd/system/
|
;;
|
||||||
then
|
OpenBSD)
|
||||||
echo systemd
|
ps -o command -p 1 2>/dev/null | tail -n +2 | cut -d ' ' -f 1
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
# otherwise: treat like "init"
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "$comm_name"
|
ps -o comm= -p 1 2>/dev/null
|
||||||
exit 0
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
# init: it could be anything...
|
find_init() {
|
||||||
case $("$init_exe" --version 2>/dev/null | head -n 1)
|
case $(uname -s)
|
||||||
in
|
in
|
||||||
SysV*)
|
Linux|GNU|NetBSD)
|
||||||
# This is a little bit more specific than init
|
find_init_procfs || find_init_pgrep || find_init_ps
|
||||||
echo sysvinit
|
|
||||||
exit 0
|
|
||||||
;;
|
;;
|
||||||
*'GNU Hurd'*)
|
FreeBSD)
|
||||||
echo hurd-init
|
find_init_procfs || find_init_ps
|
||||||
;;
|
;;
|
||||||
*upstart*)
|
OpenBSD)
|
||||||
echo upstart
|
find_init_pgrep || find_init_ps
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
case $("$init_exe" --help 2>/dev/null | head -n 1)
|
|
||||||
in
|
|
||||||
BusyBox*)
|
|
||||||
echo busybox
|
|
||||||
exit 0
|
|
||||||
;;
|
;;
|
||||||
|
Darwin|FreeBSD|SunOS)
|
||||||
|
find_init_ps
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Don't know how to determine init." >&2
|
||||||
|
echo 'Please send a patch.' >&2
|
||||||
|
exit 1
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_by_comm_name() {
|
||||||
|
case $1
|
||||||
|
in
|
||||||
|
busybox)
|
||||||
|
validate_busybox_init
|
||||||
|
;;
|
||||||
|
init)
|
||||||
|
# FIXME: Do some more magic here!
|
||||||
echo init
|
echo init
|
||||||
;;
|
;;
|
||||||
FreeBSD|OpenBSD)
|
openrc-init)
|
||||||
ps -o comm= -p 1 2>/dev/null || true
|
validate_openrc >/dev/null && echo openrc-init
|
||||||
;;
|
;;
|
||||||
Darwin)
|
runit)
|
||||||
basename "$(ps -o comm= -p 1 2>/dev/null)"
|
validate_runit
|
||||||
;;
|
;;
|
||||||
SunOS)
|
systemd)
|
||||||
comm_name=$(ps -o comm= -p 1 2>/dev/null)
|
validate_systemd
|
||||||
if test "$(basename "$comm_name")" != 'init'
|
|
||||||
then
|
|
||||||
echo "${comm_name}"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# XXX: Is this the correct way??
|
|
||||||
if test -f /etc/svc/volatile/svc_nonpersist.db
|
|
||||||
then
|
|
||||||
echo smf
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# return a empty string as unknown value
|
# Run validate function by comm name if available.
|
||||||
echo ""
|
# Fall back to comm name if either it does not exist or
|
||||||
|
# returns non-zero.
|
||||||
|
type "validate_$1" >/dev/null && "validate_$1" || echo $1
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
try_all() {
|
||||||
|
# init: it could be anything...
|
||||||
|
# We try some approaches to gather more information about init without
|
||||||
|
# calling it! On some init systemd this triggers a reinitialisation of
|
||||||
|
# the system which we don't want (e.g. embedded systems).
|
||||||
|
|
||||||
|
validate_sysvinit || \
|
||||||
|
validate_openrc || \
|
||||||
|
validate_runit || \
|
||||||
|
validate_smf || \
|
||||||
|
validate_upstart || \
|
||||||
|
validate_hurd_init || \
|
||||||
|
echo init # fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
init=$(find_init)
|
||||||
|
|
||||||
|
if test -x "${init}"
|
||||||
|
then
|
||||||
|
case $init
|
||||||
|
in
|
||||||
|
/hurd/init)
|
||||||
|
# FIXME: Create validate function
|
||||||
|
echo hurd-init
|
||||||
;;
|
;;
|
||||||
esac
|
*/init)
|
||||||
|
try_all
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
validate_by_comm_name "$(basename "${init}")"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
validate_by_comm_name "${init}"
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in a new issue