#!/bin/sh # ANODE session autostart runner (i3-style directives + drop-ins + XDG autostart, strict POSIX sh) set -eu : "${XDG_CONFIG_HOME:=$HOME/.config}" : "${XDG_CONFIG_DIRS:=/etc/xdg}" : "${XDG_CURRENT_DESKTOP:=ANODE}" : "${ANODE_PREFIX:=/usr/local}" : "${ANODE_XDG_AUTOSTART:=1}" SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" CONF_DIR="$XDG_CONFIG_HOME/anode" MODE="start" EXPLICIT_CFG="" PROFILE="${ANODE_SESSION_PROFILE:-}" ENABLE_XDG="$ANODE_XDG_AUTOSTART" DRY_RUN=0 usage() { cat <<'EOF' Usage: anode-autostart [start|reload|list|check] [OPTIONS] Commands: start Run exec, exec_once, exec_always, autostart.d/, and XDG entries (default) reload Run only exec_always directives (used on WM reload/restart) list Print resolved autostart entries without executing them check Validate declarative autostart files and includes (exit 0 on valid) Options: --config FILE Use FILE instead of ~/.config/anode/autostart --profile NAME Load session profile overrides from autostart.NAME --no-xdg Skip XDG /etc/xdg/autostart and ~/.config/autostart/*.desktop --dry-run Print actions that would run without executing them -h, --help Show this help EOF } while [ $# -gt 0 ]; do case "$1" in start|reload|list|check) MODE="$1" shift ;; --config) [ $# -ge 2 ] || { printf 'anode-autostart: --config requires a path\n' >&2; exit 2; } EXPLICIT_CFG="$2" shift 2 ;; --config=*) EXPLICIT_CFG="${1#--config=}" shift ;; --profile) [ $# -ge 2 ] || { printf 'anode-autostart: --profile requires a name\n' >&2; exit 2; } PROFILE="$2" shift 2 ;; --profile=*) PROFILE="${1#--profile=}" shift ;; --no-xdg) ENABLE_XDG=0 shift ;; --dry-run) DRY_RUN=1 shift ;; -h|--help) usage exit 0 ;; *) printf 'anode-autostart: unknown argument: %s\n' "$1" >&2 exit 2 ;; esac done if [ "$MODE" = "list" ]; then DRY_RUN=1 fi find_sys_file() { _rel="$1" for _base in \ "$ANODE_PREFIX/share/anode" \ "$SCRIPT_DIR/../share/anode" \ "$SCRIPT_DIR/../data" do if [ -r "$_base/$_rel" ]; then printf '%s\n' "$_base/$_rel" return 0 fi done return 1 } PRIMARY_CFG="" if [ -n "$EXPLICIT_CFG" ]; then if [ ! -r "$EXPLICIT_CFG" ]; then printf 'anode-autostart: config file not readable: %s\n' "$EXPLICIT_CFG" >&2 exit 1 fi PRIMARY_CFG="$EXPLICIT_CFG" elif [ -r "$CONF_DIR/autostart" ]; then PRIMARY_CFG="$CONF_DIR/autostart" else PRIMARY_CFG="$(find_sys_file autostart || true)" fi PROFILE_CFG="" if [ -n "$PROFILE" ]; then if [ -r "$CONF_DIR/autostart.$PROFILE" ]; then PROFILE_CFG="$CONF_DIR/autostart.$PROFILE" else PROFILE_CFG="$(find_sys_file "autostart.$PROFILE" || true)" fi fi MY_UID="$(id -u)" SELF_PID="$$" LAUNCHED_BASES=":" norm_display() { case "${1:-}" in *.0) printf '%s\n' "${1%.0}" ;; *) printf '%s\n' "${1:-}" ;; esac } is_polkit_agent_base() { case "${1:-}" in lxpolkit|polkit-gnome-authentication-agent-1|polkit-mate-authentication-agent-1|polkit-kde-authentication-agent-1|xfce-polkit|lxqt-policykit-agent|mate-polkit) return 0 ;; esac return 1 } is_generic_wrapper_base() { case "${1:-}" in sh|bash|dash|ksh|zsh|ash|env|nohup|setsid|st|xterm|urxvt|alacritty|kitty|python|python3|perl|ruby|true|false|echo|printf) return 0 ;; esac return 1 } polkit_agent_running_on_display() { _cands="$( (ps -eo uid=,pid=,comm= 2>/dev/null || ps -ax -o uid=,pid=,comm= 2>/dev/null || true) | awk -v myuid="$MY_UID" -v self="$SELF_PID" ' $1 == myuid && $2 > 1 && $2 != self { c = $3 sub(/^.*\//, "", c) if (c == "lxpolkit" || c == "polkit-gnome-authentication-agent-1" || c == "polkit-mate-authentication-agent-1" || c == "polkit-kde-authentication-agent-1" || c == "xfce-polkit" || c == "lxqt-policykit-agent" || c == "mate-polkit") { print $2 } } ' )" [ -n "${_cands:-}" ] || return 1 if [ -n "${DISPLAY:-}" ] && [ -d "/proc/$SELF_PID" ]; then _cur_disp="$(norm_display "$DISPLAY")" for _cpid in $_cands; do [ -r "/proc/$_cpid/environ" ] || continue _pdisp="$(tr '\0' '\n' < "/proc/$_cpid/environ" 2>/dev/null | awk 'substr($0, 1, 8) == "DISPLAY=" { print substr($0, 9); exit }' || true)" [ -n "${_pdisp:-}" ] || continue if [ "$(norm_display "$_pdisp")" = "$_cur_disp" ]; then return 0 fi done return 1 fi return 0 } polkit_session_is_invalid() { [ "${ANODE_FORCE_POLKIT_AGENT:-0}" = "1" ] && return 1 command -v loginctl >/dev/null 2>&1 || return 1 _has_sess="$(loginctl list-sessions 2>/dev/null | awk ' NF >= 2 && $1 != "SESSION" && $1 !~ /listed\.$/ && $2 ~ /^[0-9]+$/ { print $1; exit } ' || true)" [ -n "${_has_sess:-}" ] || return 1 _sid="" if [ -r "/proc/$SELF_PID/cgroup" ]; then _sid="$(awk -F/ ' { s = $NF if (s ~ /^session-.*\.scope$/) { sub(/^session-/, "", s) sub(/\.scope$/, "", s) if (s != "") { print s; exit } } else if ($1 ~ /^0:/ && s != "" && s !~ /\.(slice|service|scope)$/) { print s exit } } ' "/proc/$SELF_PID/cgroup" 2>/dev/null || true)" fi if [ -z "${_sid:-}" ]; then _sid="${XDG_SESSION_ID:-}" fi [ -n "${_sid:-}" ] || return 1 if ! loginctl show-session "$_sid" >/dev/null 2>&1; then return 0 fi return 1 } should_skip_launch() { _kind="$1" _base="$2" [ -n "${_base:-}" ] || return 1 if is_polkit_agent_base "$_base"; then case "$LAUNCHED_BASES" in *":$_base:"*) return 0 ;; esac if polkit_agent_running_on_display; then return 0 fi if polkit_session_is_invalid; then return 0 fi return 1 fi case "$_kind" in exec_once) case "$LAUNCHED_BASES" in *":$_base:"*) return 0 ;; esac if is_proc_running "$_base"; then return 0 fi ;; desktop) if ! is_generic_wrapper_base "$_base"; then case "$LAUNCHED_BASES" in *":$_base:"*) return 0 ;; esac if is_proc_running "$_base"; then return 0 fi fi ;; esac return 1 } # Extract executable basename from a shell command line for exec_once tracking cmd_exec_base() { awk -v raw="$1" 'BEGIN { n = split(raw, a, /[ \t]+/) tok = "" for (i = 1; i <= n; i++) { if (a[i] == "") continue if (a[i] == "exec" || a[i] == "nohup" || a[i] == "env") continue if (a[i] ~ /^[A-Za-z_][A-Za-z0-9_]*=/) continue tok = a[i] break } gsub(/^["'\'']|["'\'']$/, "", tok) sub(/^.*\//, "", tok) print tok }' } is_proc_running() { _tgt="$1" [ -n "$_tgt" ] || return 1 _self="$$" _my_uid="$(id -u)" _cands="$( (ps -eo uid=,pid=,comm=,args= 2>/dev/null || ps -ax -o uid=,pid=,comm=,args= 2>/dev/null || true) | awk -v tgt="$_tgt" -v self="$_self" -v myuid="$_my_uid" ' { uid = $1; pid = $2; comm = $3; arg1 = $4 if (uid != myuid || pid == self) next sub(/^.*\//, "", comm) sub(/^.*\//, "", arg1) if (comm == tgt || arg1 == tgt) { print pid } } ' )" [ -n "${_cands:-}" ] || return 1 _cur_sid="${XDG_SESSION_ID:-}" _cur_dbus="${DBUS_SESSION_BUS_ADDRESS:-}" for _cpid in $_cands; do if [ -r "/proc/$_cpid/environ" ] && { [ -n "$_cur_sid" ] || [ -n "$_cur_dbus" ]; }; then _penv="$(tr '\0' '\n' < "/proc/$_cpid/environ" 2>/dev/null || true)" _p_sid="$(printf '%s\n' "$_penv" | awk 'substr($0, 1, 15) == "XDG_SESSION_ID=" { print substr($0, 16); exit }')" _p_dbus="$(printf '%s\n' "$_penv" | awk 'substr($0, 1, 25) == "DBUS_SESSION_BUS_ADDRESS=" { print substr($0, 26); exit }')" if [ -n "$_cur_sid" ] && [ -n "${_p_sid:-}" ] && [ "$_p_sid" != "$_cur_sid" ]; then continue fi if [ -n "$_cur_dbus" ] && [ -n "${_p_dbus:-}" ] && [ "$_p_dbus" != "$_cur_dbus" ]; then continue fi return 0 fi return 0 done return 1 } launch_cmd() { _cmd="$1" (sh -c "$_cmd" /dev/null 2>&1 &) } run_declarative_files() { _strict=0 [ "$MODE" = "check" ] && _strict=1 if [ -z "$PRIMARY_CFG" ] && [ -z "$PROFILE_CFG" ]; then return 0 fi _awk_err=0 _parsed="$(awk -v conf_dir="$CONF_DIR" -v home_dir="$HOME" -v strict="$_strict" ' function trim(s) { gsub(/^[ \t\r\n]+|[ \t\r\n]+$/, "", s) return s } function resolve_inc(p, base_dir, out) { gsub(/^["'\'']|["'\'']$/, "", p) if (p ~ /^~\//) { out = home_dir "/" substr(p, 3) } else if (p ~ /^\//) { out = p } else { out = base_dir "/" p } return out } function parse_file(fpath, base_dir, depth, nlines, i, raw, s, verb, rest, inc_p, inc_dir, rc) { if (depth > 8) { printf "anode-autostart: include depth exceeded at %s\n", fpath > "/dev/stderr" had_err = 1 return } if (fpath in visited) { printf "anode-autostart: skipping cyclic/duplicate include: %s\n", fpath > "/dev/stderr" return } visited[fpath] = 1 nlines = 0 while ((rc = (getline raw < fpath)) > 0) { file_lines[fpath, ++nlines] = raw } close(fpath) if (rc < 0 && nlines == 0) { printf "anode-autostart: cannot read include file: %s\n", fpath > "/dev/stderr" if (strict) had_err = 1 return } for (i = 1; i <= nlines; i++) { s = trim(file_lines[fpath, i]) if (s == "" || substr(s, 1, 1) == "#") continue if (s ~ /^[A-Za-z0-9_]+[ \t]+/) { match(s, /^[A-Za-z0-9_]+/) verb = substr(s, 1, RLENGTH) rest = trim(substr(s, RLENGTH + 1)) } else { verb = s rest = "" } if (verb == "exec" || verb == "exec_once" || verb == "exec_always") { if (rest ~ /^--no-startup-id([ \t]|$)/) { rest = trim(substr(rest, 16)) } if (rest == "") { printf "anode-autostart: %s:%d: missing command for %s\n", fpath, i, verb > "/dev/stderr" had_err = 1 continue } printf "%s\t%s\n", verb, rest } else if (verb == "include") { if (rest == "") { printf "anode-autostart: %s:%d: missing path for include\n", fpath, i > "/dev/stderr" had_err = 1 continue } inc_p = resolve_inc(rest, base_dir) inc_dir = inc_p if (inc_dir ~ /\//) { sub(/\/[^\/]*$/, "", inc_dir) if (inc_dir == "") inc_dir = "/" } else { inc_dir = base_dir } parse_file(inc_p, inc_dir, depth + 1) } else { printf "anode-autostart: %s:%d: unknown directive: %s\n", fpath, i, verb > "/dev/stderr" had_err = 1 } } } BEGIN { had_err = 0 for (fi = 1; fi < ARGC; fi++) { fp = ARGV[fi] ARGV[fi] = "" if (fp != "") { bdir = fp if (bdir ~ /\//) { sub(/\/[^\/]*$/, "", bdir) if (bdir == "") bdir = "/" } else { bdir = conf_dir } parse_file(fp, bdir, 0) } } if (strict && had_err) exit 1 exit 0 } ' "$PRIMARY_CFG" "$PROFILE_CFG")" || _awk_err=1 if [ "$_awk_err" != "0" ] && [ "$MODE" = "check" ]; then return 1 fi if [ "$MODE" = "check" ]; then return 0 fi _tab="$(printf '\t')" while IFS="$_tab" read -r _kind _cmd; do [ -n "${_kind:-}" ] || continue case "$MODE:$_kind" in reload:exec|reload:exec_once) continue ;; esac _base="" if [ "$DRY_RUN" != "1" ]; then _base="$(cmd_exec_base "$_cmd")" if should_skip_launch "$_kind" "$_base"; then continue fi fi if [ "$DRY_RUN" = "1" ]; then printf '[%s] %s\n' "$_kind" "$_cmd" else launch_cmd "$_cmd" if [ -n "$_base" ] && ! is_generic_wrapper_base "$_base"; then LAUNCHED_BASES="${LAUNCHED_BASES}${_base}:" fi fi done <