macro_rules! processes {
() => {
r#"base=${XDG_STATE_HOME:-$HOME/.local/state}/exedev/processes
# Sets n to the number in file $1, or to "" if it is missing, empty, or
# garbled, as a VM that stopped before writing it out can leave it.
num() { { n=$(cat "$1"); } 2>/dev/null; case $n in ''|*[!0-9]*) n=;; esac; }
# Prints the process in directory $1 as "PID running", "PID exited STATUS",
# or "PID lost", where PID is 0 if unknown; fails if there is none, or it is
# still starting.
state() {
[ -d "$1" ] || return
num "$1/pid"; p=${n:-0}
num "$1/exit"; if [ -n "$n" ]; then echo "$p exited $n"; return; fi
if [ "$(cat "$1/boot" 2>/dev/null)" = "$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)" ]; then
[ "$p" != 0 ] || return
if kill -0 "$(cat "$1/monitor" 2>/dev/null)" 2>/dev/null; then echo "$p running"; return; fi
num "$1/exit"; if [ -n "$n" ]; then echo "$p exited $n"; return; fi
fi
echo "$p lost"
}
"#
};
}
pub(crate) const RUN_SCRIPT: &str = r#"s=${SHELL:-/bin/sh}
command -v setsid >/dev/null 2>&1 || exec "$s" -lc "$1"
setsid "$s" -lc "$1" &
p=$!
parent() { read -r st </proc/$$/stat && st=${st##*) } && set -- $st && pp=$2; }
parent 2>/dev/null; p0=$pp
(while parent && [ "$pp" = "$p0" ]; do sleep 0.25 || sleep 1; done; kill -KILL -$p) >/dev/null 2>&1 &
w=$!
wait $p
c=$?
kill $w 2>/dev/null
exit $c"#;
pub(crate) const FLUSH_SCRIPT: &str = r#"sync -f / 2>/dev/null || sync"#;
pub(crate) const UPLOADED_COMMAND: &str = r#"c=$(cat {path}) && rm -f {path} && eval "$c""#;
pub(crate) const SETUP_PROBE: &str = r#"if [ -e /exe.dev/setup ]; then echo Setup=present; else echo Setup=absent; fi
if [ ! -d /run/systemd/system ]; then
if [ -e /etc/systemd/system/exe-setup.service ]; then echo State=booting; else echo State=unsupported; fi
exit 0
fi
props=$(systemctl show exe-setup.service -p LoadState -p ActiveState -p Job -p Result -p ExecMainCode -p ExecMainStatus -p ExecMainExitTimestampMonotonic -p ConditionResult -p ConditionTimestampMonotonic 2>/dev/null) || { echo State=booting; exit 0; }
echo "$props"
echo "SystemState=$(systemctl is-system-running 2>/dev/null)"
if [ "$(systemctl show exe-setup.service -p ActiveState --value)" = failed ]; then
echo ---
journalctl -b _SYSTEMD_UNIT=exe-setup.service _TRANSPORT=stdout -o cat --no-pager -n 50 2>&1
fi"#;
pub(crate) const WRITE_SCRIPT: &str = r#"f=$1 data=$2 mode=$3 gz=$4 parts=$5
data() { if [ -n "$parts" ]; then cat "$parts"/*; else printf %s "$data"; fi; }
case $f in */*) mkdir -p "${f%/*}" || exit;; esac
if [ -n "$gz" ]; then data | base64 -d | gzip -dc > "$f"; else data | base64 -d > "$f"; fi
c=$?
if [ -n "$parts" ]; then rm -rf "$parts"; fi
if [ $c = 0 ] && [ -n "$mode" ]; then chmod "$mode" "$f" || c=$?; fi
exit $c"#;
pub(crate) const PART_SCRIPT: &str = r#"mkdir -p "$1" && printf %s "$3" > "$1/$2""#;
pub(crate) const READ_SCRIPT: &str = r#"if [ -d "$1" ]; then exit 3; fi
if [ ! -e "$1" ]; then exit 2; fi
exec cat "$1""#;
pub(crate) const DOWNLOAD_SCRIPT: &str = r#"if [ -d "$1" ]; then printf D && exec tar -czf - -C "$1" .; fi
if [ ! -e "$1" ]; then exit 2; fi
printf F && exec cat "$1""#;
pub(crate) const UNTAR_SCRIPT: &str = r#"mkdir -p "$2" && tar -xzf "$1" -C "$2"; c=$?; rm -f "$1"; exit $c"#;
pub(crate) const SPAWN_SCRIPT: &str = concat!(
processes!(),
r#"d=$base/$1
case $(state "$d") in *" running") exit 3;; esac
rm -rf "$d" && mkdir -p "$d" || exit
cat /proc/sys/kernel/random/boot_id > "$d/boot" 2>/dev/null
printf '%s\n' "$2" > "$d/command"
setsid sh -c 'echo $$ > "$1/monitor"
setsid "${SHELL:-/bin/sh}" -lc "$2" < /dev/null > "$1/stdout" 2> "$1/stderr" &
echo $! > "$1/pid.new" && mv "$1/pid.new" "$1/pid"
wait $!
echo $? > "$1/exit.new" && mv "$1/exit.new" "$1/exit" && sync "$1/exit" "$1"' sh "$d" "$2" < /dev/null > /dev/null 2>&1 &
i=0
while [ ! -f "$d/pid" ] && [ $i -lt 100 ]; do sleep 0.05; i=$((i+1)); done
sync "$d/pid" "$d/boot" "$d/command" "$d/monitor" "$d" 2>/dev/null
cat "$d/pid""#
);
pub(crate) const STATE_SCRIPT: &str = concat!(processes!(), r#"state "$base/$1" || exit 2"#);
pub(crate) const LIST_SCRIPT: &str = concat!(
processes!(),
r#"for d in "$base"/*; do
if s=$(state "$d"); then echo "${d##*/} $s"; fi
done"#
);
pub(crate) const WAIT_SCRIPT: &str = concat!(
processes!(),
r#"d=$base/$1
while s=$(state "$d") && [ "${s#* }" = running ]; do sleep 0.5; done
case $s in
"") printf '%s missing' "$2"; exit 0;;
*" lost") printf '%s lost' "$2"; exit 0;;
esac
if [ -n "$3" ]; then cat "$d/stdout"; cat "$d/stderr" >&2; fi
exit "${s##* }""#
);
pub(crate) const LOGS_SCRIPT: &str = concat!(
processes!(),
r#"d=$base/$1
[ -f "$d/pid" ] || exit 2
if [ -n "$2" ]; then
tail -n "$2" "$d/stdout" && tail -n "$2" "$d/stderr" >&2
else
cat "$d/stdout" && cat "$d/stderr" >&2
fi"#
);
pub(crate) const KILL_SCRIPT: &str = concat!(
processes!(),
r#"s=$(state "$base/$1") || exit 2
case $s in *" running") kill -"$2" -"${s%% *}" 2>/dev/null || kill -"$2" "${s%% *}";; esac"#
);