File systemd-update-helper of Package systemd

#!/usr/bin/env bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This helper is aimed at being used by the systemd rpm macros only.
#
set -eu
set -o pipefail

# --root is accepted for testing convenience only. It redirects all state and
# unit file paths under the given directory, allowing tests to run without
# touching the real filesystem.
root=""
if [[ "${1:-}" == --root=* ]]; then
    root="${1#--root=}"
    shift
fi

command="${1:?}"
shift

command -v systemctl >/dev/null || exit 0

UPDATE_HELPER_USER_TIMEOUT_SEC=15
STATE_DIR="${root}/run/systemd/rpm"
UNIT_DIR="${root}/usr/lib/systemd"
RUNTIME_DIR="${root}/run/systemd"

do_mark_install_units() {
    mode=$1
    shift
    mkdir -p $STATE_DIR/$mode/{needs-preset,dont-disable}

    for unit in "$@" ; do
        # Clean any leftovers that might remain from a previous transaction
        # which exited abnormally.
        rm -f $STATE_DIR/$mode/*/"$unit"

        if [ ! -e $UNIT_DIR/$mode/"$unit" ]; then
            # The unit is being introduced: remember we need to apply preset on
            # this new unit regardless of whether it's a package update or
            # installation.
            touch $STATE_DIR/$mode/needs-preset/"$unit"
        fi

        # All passed units are part of a package being installed or updated and
        # therefore should not be disabled by the removal of a different package
        # part of the same rpm transaction. This can happen when the package
        # shipping the unit is being renamed (e.g. from "A" to "B"), where "B"
        # is installed first, followed by the removal of "A". In that case, "A"
        # is removed and its %preun scriptlet runs %systemd_preun on the unit.
        touch $STATE_DIR/$mode/dont-disable/"$unit"
    done
}

do_install_units() {
    mode=$1
    shift
    units=()

    for unit in "$@" ; do
        if [ -e $STATE_DIR/$mode/needs-preset/"$unit" ]; then
            rm  $STATE_DIR/$mode/needs-preset/"$unit"
            units+=("$unit")
        fi
    done

    [ ${#units[@]} -eq 0 ] && return

    case $mode in
    system)
        systemctl --no-reload preset "${units[@]}" ;;
    user)
        systemctl --no-reload preset --global "${units[@]}" ;;
    esac
}

do_remove_units() {
    mode=$1
    shift
    units=()

    for unit in "$@" ; do
        if [ ! -e $STATE_DIR/$mode/dont-disable/"$unit" ]; then
            units+=("$unit")
        fi
    done

    [ ${#units[@]} -eq 0 ] && return

    case $mode in
    system)
        if [ -d $RUNTIME_DIR/system ]; then
            systemctl --no-reload disable --now --no-warn "${units[@]}"
        else
            systemctl --no-reload disable --no-warn "${units[@]}"
        fi
        ;;
    user)
        systemctl --global disable --no-warn "${units[@]}"

        [ -d $RUNTIME_DIR/system ] || return

        users=$(systemctl list-units 'user@*' --legend=no | sed -n -r 's/.*user@([0-9]+).service.*/\1/p')
        for user in $users; do
            SYSTEMD_BUS_TIMEOUT=${UPDATE_HELPER_USER_TIMEOUT_SEC}s \
                    systemctl --user -M "$user@" disable --now --no-warn "${units[@]}" &
        done
        wait
        ;;
    esac
}

case "$command" in
    mark-install-system-units) # called from %pre (on install or upgrade)
        do_mark_install_units system "$@"
        ;;

    mark-install-user-units)
        do_mark_install_units user "$@"
        ;;

    install-system-units) # called from %post (on install or upgrade)
        do_install_units system "$@"
        ;;

    install-user-units)
        do_install_units user "$@"
        ;;

    remove-system-units) # called from %preun (on removal)
        do_remove_units system "$@"
        ;;

    remove-user-units)
        do_remove_units user "$@"
        ;;

    mark-restart-system-units) # called from %postun (on upgrade)
        [ -d $RUNTIME_DIR/system ] || exit 0

        for unit in "$@"; do
            systemctl set-property "$unit" Markers=+needs-restart &
        done
        wait
        ;;

    mark-restart-user-units)
        [ -d $RUNTIME_DIR/system ] || exit 0

        users=$(systemctl list-units 'user@*' --legend=no | sed -n -r 's/.*user@([0-9]+).service.*/\1/p')
        for user in $users; do
            for unit in "$@"; do
                SYSTEMD_BUS_TIMEOUT=${UPDATE_HELPER_USER_TIMEOUT_SEC}s \
                        systemctl --user -M "$user@" set-property "$unit" Markers=+needs-restart &
            done
        done
        wait
        ;;

    system-reload-restart|system-reload|system-restart|system-reexec) # called once from %transfiletriggerin or %transfiletriggerpostun
        if [ -n "$*" ]; then
            echo >&2 "Unexpected arguments for '$command': $*"
            exit 2
        fi

        [ -d $RUNTIME_DIR/system ] || exit 0

        if [[ "$command" =~ reexec ]]; then
            systemctl daemon-reexec
        fi

        if [[ "$command" =~ reload ]]; then
            systemctl daemon-reload
        fi

        if [[ "$command" =~ restart ]]; then
            systemctl reload-or-restart --marked
        fi
        ;;

    user-reload-restart|user-reload|user-restart|user-reexec) # called once from %transfiletriggerin or %transfiletriggerpostun
        if [ -n "$*" ]; then
            echo >&2 "Unexpected arguments for '$command': $*"
            exit 2
        fi

        [ -d $RUNTIME_DIR/system ] || exit 0

        if [[ "$command" =~ reexec|reload ]]; then
            SYSTEMD_BUS_TIMEOUT=${UPDATE_HELPER_USER_TIMEOUT_SEC}s systemctl reload "user@*.service"
        fi

        if [[ "$command" =~ restart ]]; then
            users=$(systemctl list-units 'user@*' --legend=no | sed -n -r 's/.*user@([0-9]+).service.*/\1/p')

            for user in $users; do
                SYSTEMD_BUS_TIMEOUT=${UPDATE_HELPER_USER_TIMEOUT_SEC}s \
                        systemctl --user -M "$user@" reload-or-restart --marked &
            done
            wait
        fi
        ;;

    clean-state) # called once from %transfiletriggerin

        # Any dont-disable markers left at this point were created during this
        # transaction but never consumed (i.e. no matching %preun ran), which
        # means they are from a fresh install rather than a rename. Wipe them
        # so they cannot interfere with a future removal transaction.
        rm -rf $STATE_DIR
        ;;

    *)
        echo >&2 "Unknown verb '$command'"
        exit 3
        ;;
esac
openSUSE Build Service is sponsored by