#!/bin/bash
#
# cagefs        Startup script for CageFS

solo_marker="/etc/cloudlinux-edition-solo"
container_marker="/etc/cloudlinux-container"
cagefs_bin="/usr/sbin/cagefsctl"
lvectl_bin="/usr/sbin/lvectl"

if [ -e "$solo_marker" ] || [ -e "$container_marker" ]; then
    lvectl_bin="/bin/true"
fi


function run {
  local command="$@"
  $command

  local retval=$?
  if [ $retval -ne 0 ]; then
      echo "Command execution failed: ${command}"
      return $retval
  fi
}

start() {
    run $cagefs_bin --mount-skel || return $?
    run $lvectl_bin destroy all --force || return $?

    if [ -e "$solo_marker" ] || [ -e "$container_marker" ]; then
        run /usr/sbin/cagefsctl --delete-namespaces --wait-lock || return $?
    fi

    sleep 1
    run $cagefs_bin --remove-unused-mount-points --wait-lock || return $?
    if [ -e "$solo_marker" ] || [ -e "$container_marker" ]; then
        run /usr/sbin/cagefsctl --create-namespaces --wait-lock || return $?
    fi

    run $lvectl_bin apply all --force
    local retval=$?
    # CLOS-7971: the binaries the kernel enters into an account's LVE and
    # cage at exec (podman, registered by cagefsctl --hook-install) are kept
    # in lvectl's configuration; nothing else loads them at boot.
    $lvectl_bin load-binaries > /dev/null 2>&1
    # CLOS-7971: arm the per-user manager jail where pam_lve and kmod-lve
    # have caught up with cagefs since the last upgrade (no-op otherwise)
    # CLOS-7971: the managers of the lingering accounts: logind may have
    # started them before the cage was ready (boot), and the destroy above
    # killed the jailed ones; their Restart= gives up after five tries in
    # two minutes. Bring them back only when the arming says the jail stands
    # (a no-op for a running one; refused where the CLOS-4517 mask keeps
    # managers off): otherwise the mask the arming kept has just stopped
    # them, and an instance file would start them again past it.
    local linger uid
    if $cagefs_bin --without-lock --arm-user-manager-jail > /dev/null 2>&1; then
        for linger in /var/lib/systemd/linger/*; do
            [ -e "$linger" ] || continue
            uid=$(id -u "${linger##*/}" 2>/dev/null) || continue
            [ "$uid" != 0 ] || continue
            systemctl reset-failed "user@$uid.service" > /dev/null 2>&1
            systemctl start --no-block "user@$uid.service" > /dev/null 2>&1
        done
    fi
    rm -f /usr/share/cagefs/need.remount
    return $retval
}

restart() {
    start
}

stop() {
    run $cagefs_bin --unmount-skel || return $?
    run $lvectl_bin destroy all || return $?
    sleep 1

    if [ -e "$solo_marker" ] || [ -e "$container_marker" ]; then
       run /usr/sbin/cagefsctl --delete-namespaces --wait-lock || return $?
    fi

    run $cagefs_bin --remove-unused-mount-points --wait-lock || return $?

    run $lvectl_bin apply all || return $?
    return 0
}


RETVAL=0

# See how we were called.
case "$1" in
    start)
        start
        RETVAL=$?
        ;;
    restart)
        if [ -e /usr/share/cagefs/skip.cagefs.restart ]; then
            rm -f /usr/share/cagefs/skip.cagefs.restart
            exit 0
        fi
        restart
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|restart|stop}"
        exit 1
esac

exit $RETVAL
