Skip to content

A hardened Linux desktop, rebuilt from scratch, with your Windows apps built in. Free and open source.

6.0 KB
Raw Blame
#!/bin/sh
# The first start-up after NVIDIA's driver is installed is a trial
# (wildos-graphics-driver-trial.service, only while
# /var/lib/wildos/graphics-driver/trial exists).
#
# A driver that loads but cannot draw leaves a black screen with no way to
# reach Settings. So root watches for the desktop: once someone's desktop
# has drawn, the trial is over and the driver stays. If someone is signed in
# and no desktop has appeared after 90 seconds, or the PC was switched off
# during the last trial and never got that far, what the kernel and the
# window manager were doing is saved under /var/log/wildos/graphics-driver,
# the open driver is put back and the PC restarts.
#
#   graphics-driver-trial early   before the sign-in screen: count this start
#   graphics-driver-trial watch   wait for a desktop

set -u
STATE=/var/lib/wildos/graphics-driver
TRIAL=$STATE/trial
LOGS=/var/log/wildos/graphics-driver
SYSCTL=/etc/sysctl.d/30_security-misc_ptrace-disable.conf
WAIT=90

uptime_s() { cut -d. -f1 /proc/uptime; }

user_session() {
    loginctl list-sessions --no-legend 2>/dev/null | while read -r id _; do
        [ -n "$id" ] || continue
        class=$(loginctl show-session "$id" -p Class --value 2>/dev/null || true)
        type=$(loginctl show-session "$id" -p Type --value 2>/dev/null || true)
        if [ "$class" = user ] && { [ "$type" = wayland ] || [ "$type" = x11 ]; }; then
            echo "$id"
            return 0
        fi
    done
}

desktop_drawn() {
    for mark in /run/user/*/wildos-desktop-ready; do
        [ -e "$mark" ] && return 0
    done
    return 1
}

# Where a process is stuck, without needing a debugger to work: the kernel's
# own view of every thread.
threads() {
    pid=$1
    for task in /proc/"$pid"/task/*; do
        [ -d "$task" ] || continue
        echo "== thread ${task##*/} $(cat "$task/comm" 2>/dev/null)"
        grep -E '^(State|voluntary_ctxt_switches|nonvoluntary_ctxt_switches):' "$task/status" 2>/dev/null
        echo "syscall: $(cat "$task/syscall" 2>/dev/null)"
        echo "wchan: $(cat "$task/wchan" 2>/dev/null)"
        cat "$task/stack" 2>/dev/null
    done
}

capture() {
    dir=$LOGS/trial-$(date +%Y%m%d-%H%M%S)
    mkdir -p "$dir"
    {
        echo "reason: $1"
        echo "uptime: $(cat /proc/uptime)"
        echo "kernel: $(uname -r)"
        echo "cmdline: $(cat /proc/cmdline)"
        echo
        echo "--- device files"
        ls -l /dev/nvidia* /dev/dri/ 2>&1
        echo
        echo "--- modules"
        grep -E '^(nvidia|nouveau|i915|xe|amdgpu|drm)' /proc/modules
        [ -d /sys/module/nvidia_drm ] || echo "nvidia_drm is NOT loaded: NVIDIA's card has no screen"
        systemctl status wildos-nvidia-drm.service systemd-modules-load.service --no-pager -n 5 2>&1
        echo
        echo "--- driver"
        cat /proc/driver/nvidia/version 2>&1
        for gpu in /proc/driver/nvidia/gpus/*/information; do
            [ -e "$gpu" ] && cat "$gpu"
        done
        cat /sys/module/nvidia_drm/parameters/modeset /sys/module/nvidia_drm/parameters/fbdev 2>&1
        echo
        echo "--- nvidia-smi"
        timeout 15 nvidia-smi 2>&1
        echo
        echo "--- sessions"
        loginctl list-sessions 2>&1
        echo
        echo "--- processes"
        ps -eo pid,ppid,user,stat,etimes,wchan:32,args 2>&1 | grep -E 'kwin|plasmashell|sddm|Xwayland|ksplash|startplasma|nvidia' | grep -v grep
    } > "$dir/summary.txt" 2>&1

    for pid in $(pgrep -x kwin_wayland; pgrep -x plasmashell; pgrep -f '^/usr/bin/sddm-greeter'); do
        name=$(cat "/proc/$pid/comm" 2>/dev/null || echo gone)
        {
            threads "$pid"
            echo
            echo "--- open files"
            ls -l "/proc/$pid/fd" 2>&1
            echo
            echo "--- graphics libraries loaded"
            grep -o -E '/[^ ]*(nvidia|EGL|GLES|gbm|glvnd|mesa|dri)[^ ]*' "/proc/$pid/maps" 2>/dev/null | sort -u
        } > "$dir/$name-$pid.txt" 2>&1
        # A debugger's view where one is installed and allowed to attach.
        if command -v gdb >/dev/null 2>&1; then
            timeout 25 gdb -p "$pid" -batch -ex 'thread apply all bt' > "$dir/$name-$pid.gdb.txt" 2>&1
        fi
        if command -v strace >/dev/null 2>&1; then
            timeout 4 strace -f -tt -p "$pid" > "$dir/$name-$pid.strace.txt" 2>&1
        fi
    done

    # Every blocked task and every CPU's stack, into the kernel log.
    echo w > /proc/sysrq-trigger 2>/dev/null
    echo l > /proc/sysrq-trigger 2>/dev/null
    sleep 1
    dmesg > "$dir/kernel.txt" 2>&1
    journalctl -b --no-pager -o short-monotonic > "$dir/journal.txt" 2>&1
    sync
    echo "$dir"
}

go_back() {
    dir=$(capture "$1")
    /usr/libexec/wildos/settings-graphics-driver open > "$dir/go-back.txt" 2>&1
    printf '%s\n%s\n' "$1" "$dir" > "$STATE/failed"
    chmod 644 "$STATE/failed"
    rm -f "$TRIAL"
    if grep -qs "graphics driver trial" "$SYSCTL"; then
        rm -f "$SYSCTL"
    fi
    sync
    systemctl reboot
}

[ -e "$TRIAL" ] || exit 0

case "${1:-}" in
early)
    starts=$(cat "$TRIAL" 2>/dev/null || echo 0)
    case "$starts" in '' | *[!0-9]*) starts=0 ;; esac
    if [ "$starts" -ge 1 ]; then
        go_back "The last start-up with NVIDIA's driver never reached the desktop."
        exit 0
    fi
    echo $((starts + 1)) > "$TRIAL"
    sync
    ;;
watch)
    signed_in=
    while [ -e "$TRIAL" ]; do
        if desktop_drawn; then
            rm -f "$TRIAL"
            # The trial let root look inside programs; back to nobody.
            if grep -qs "graphics driver trial" "$SYSCTL"; then
                rm -f "$SYSCTL"
                sysctl -q -w kernel.yama.ptrace_scope=3 || true
            fi
            exit 0
        fi
        if [ -z "$signed_in" ] && [ -n "$(user_session)" ]; then
            signed_in=$(uptime_s)
        fi
        if [ -n "$signed_in" ] && [ $(($(uptime_s) - signed_in)) -ge "$WAIT" ]; then
            go_back "The desktop did not appear within $WAIT seconds of signing in."
            exit 0
        fi
        sleep 2
    done
    ;;
*)
    echo "usage: $0 early|watch" >&2
    exit 2
    ;;
esac