The script was in the tree untracked; reviewed before it went in: - pam_permit was written to the host's /etc/pam.d/dtlogin before the trap that puts it back was set: a run killed in between left the graphical login open without a password. The trap comes first now, and only a run that switched puts the file back. - the PAM backup was taken once (2026-11-15) and put back over every newer file; it is taken from the file as it is, each run. - restore removed $HOME/.dt before copying the backup back; the session's copy is set aside now, and the backup is made whole or not at all. .gitignore: /.hermes/ (an agent's local plans) and /www/screenshots/ (raw shots; the kept ones are www/img/).
331 lines
11 KiB
Bash
Executable File
331 lines
11 KiB
Bash
Executable File
#!/bin/sh
|
|
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
#
|
|
# mcde-desktop.sh -- start MCDE on a private Xvfb display through dtlogin,
|
|
# log in, and control the desktop to take targeted screenshots
|
|
#
|
|
# usage: tools/mcde-desktop.sh [COMMANDS_FILE]
|
|
#
|
|
# Starts dtlogin (as root) on a private Xvfb display, logs in as $USER
|
|
# (pam_permit is used during the run, so no password is needed), waits for
|
|
# the desktop, then runs the control commands from COMMANDS_FILE (or stdin
|
|
# if none / if "-" is given) and tears everything down on exit.
|
|
#
|
|
# Control commands (one per line; lines starting with # are ignored):
|
|
# wait SECONDS sleep
|
|
# shot OUT.png screenshot of the whole screen
|
|
# shot OUT.png X Y W H screenshot of a region (ImageMagick crop)
|
|
# type TEXT type TEXT into the focused window
|
|
# key KEYSYM [KEYSYM ...] press keys (via tools/mcde-xkey.sh)
|
|
# click X Y [BUTTON] click at (X,Y) (default button 1)
|
|
# move X Y move the pointer
|
|
# run PROGRAM [ARG ...] launch PROGRAM from the dev install in the
|
|
# session (background)
|
|
# focus NAME focus the first window whose name contains NAME
|
|
# close close the focused window (Alt+F4)
|
|
# exit stop processing (teardown follows)
|
|
#
|
|
# Environment:
|
|
# MCDE_DESK_DISPLAY X display to use (default :93)
|
|
# MCDE_DESK_USER user to log in as (default $USER)
|
|
# MCDE_DESK_WAIT seconds to wait for the desktop after login (default 8)
|
|
# MCDE_DESK_SETUP shell command run in $HOME before login (optional)
|
|
# MCDE_DESK_EXTRA program started beside the session (optional)
|
|
# MCDE_THUMB_DEBUG etc. forwarded as in mcde-shot.sh
|
|
#
|
|
# A new home is NOT created: the session runs as $USER and uses its real
|
|
# $HOME, so the dev build under the tree is reachable. $HOME/.dt is backed
|
|
# up to build/mcde-desktop/home.dt.bak before the run and restored on exit.
|
|
|
|
set -u
|
|
|
|
TOP=$(cd "$(dirname "$0")/.." && pwd)
|
|
R=$TOP/build/mcde-root
|
|
[ -x "$R/dt/bin/dtlogin" ] || { echo "no $R: make mcde-linux MCDE_DIRS=install" >&2; exit 1; }
|
|
|
|
DISP=${MCDE_DESK_DISPLAY:-:93}
|
|
USERN=${MCDE_DESK_USER:-${USER:-lotherk}}
|
|
WAIT=${MCDE_DESK_WAIT:-8}
|
|
CMDS=${1:-}
|
|
|
|
WORK=$TOP/build/mcde-desktop
|
|
mkdir -p "$WORK"
|
|
CFG=$WORK/cfg
|
|
rm -rf "$CFG"
|
|
mkdir -p "$CFG"
|
|
cp -r "$R/dt/config/"* "$CFG/" 2>/dev/null
|
|
|
|
# Xservers: launch Xvfb ourselves on $DISP
|
|
cat > "$CFG/Xservers" <<EOF
|
|
${DISP} Local local@none /usr/bin/Xvfb ${DISP} -screen 0 1280x800x24 -nolisten tcp
|
|
EOF
|
|
|
|
# Xconfig: no authorization, no server grab, XDMCP off
|
|
cat > "$CFG/Xconfig" <<EOF
|
|
Dtlogin.errorLogFile: $WORK/Xerrors
|
|
Dtlogin.pidFile: $WORK/Xpid
|
|
Dtlogin.accessFile: $CFG/Xaccess
|
|
Dtlogin.servers: $CFG/Xservers
|
|
Dtlogin*resources: %L/Xresources
|
|
Dtlogin*startup: Xstartup
|
|
Dtlogin*reset: Xreset
|
|
Dtlogin*setup: Xsetup
|
|
Dtlogin*failsafeClient: Xfailsafe
|
|
Dtlogin.exportList: NLSPATH LANG
|
|
Dtlogin*userPath: /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:$R/dt/bin
|
|
Dtlogin*environment: FONTCONFIG_FILE=$R/dt/config/fonts.conf
|
|
Dtlogin*authorize: False
|
|
Dtlogin*grabServer: False
|
|
Dtlogin*openTimeout: 15
|
|
Dtlogin*serverAttempts: 3
|
|
Dtlogin*wakeupInterval: 20
|
|
Dtlogin.requestPort: 0
|
|
EOF
|
|
# Xaccess already copied from config; ensure it exists
|
|
[ -f "$CFG/Xaccess" ] || cp "$R/dt/config/Xaccess" "$CFG/Xaccess" 2>/dev/null
|
|
|
|
# PAM: pam_permit for this run, the host's own file put back afterwards.
|
|
# The trap is set BEFORE the switch: a run killed in between used to leave the
|
|
# host's graphical login open without a password. The backup is taken from
|
|
# the file as it is now, every run -- a backup kept from the first run put an
|
|
# old file back over a newer one -- unless the file is our pam_permit, left by
|
|
# a killed run, and then the backup of that run is the original.
|
|
PAM=/etc/pam.d/dtlogin
|
|
PAMBAK=/etc/pam.d/dtlogin.orig.makronix
|
|
HOME_BAK=$WORK/home.dt.bak
|
|
switched=
|
|
restored=
|
|
|
|
restore() {
|
|
[ -n "$restored" ] && return 0
|
|
restored=yes
|
|
[ -n "$switched" ] && sudo cp "$PAMBAK" "$PAM"
|
|
# $HOME/.dt as it was before the run; the session's copy is set aside in
|
|
# $WORK/home.dt.session, never removed before the backup is back
|
|
if [ -d "$HOME_BAK" ]; then
|
|
rm -rf "$WORK/home.dt.session"
|
|
[ -d "$HOME/.dt" ] && mv "$HOME/.dt" "$WORK/home.dt.session"
|
|
cp -a "$HOME_BAK" "$HOME/.dt" && rm -rf "$HOME_BAK"
|
|
fi
|
|
# kill dtlogin / Xvfb
|
|
if [ -n "${DTLOGIN_PID:-}" ]; then
|
|
sudo kill "$DTLOGIN_PID" 2>/dev/null
|
|
fi
|
|
# kill any leftover Xvfb on $DISP
|
|
pids=$(pgrep -f "Xvfb ${DISP}" 2>/dev/null)
|
|
[ -n "$pids" ] && sudo kill $pids 2>/dev/null
|
|
sleep 1
|
|
}
|
|
trap restore EXIT
|
|
trap 'restore; exit 1' HUP INT TERM
|
|
|
|
if grep -q pam_permit "$PAM" 2>/dev/null; then
|
|
[ -f "$PAMBAK" ] || { echo "$PAM is pam_permit and $PAMBAK is missing: fix $PAM by hand" >&2; exit 1; }
|
|
else
|
|
sudo cp "$PAM" "$PAMBAK" || { echo "cannot back up $PAM" >&2; exit 1; }
|
|
fi
|
|
switched=yes
|
|
sudo tee "$PAM" >/dev/null <<'PAMEOF'
|
|
auth required pam_permit.so
|
|
account required pam_permit.so
|
|
session required pam_permit.so
|
|
password required pam_permit.so
|
|
PAMEOF
|
|
|
|
# back up $HOME/.dt (a backup left by a killed run is the original: kept)
|
|
if [ -d "$HOME/.dt" ] && [ ! -d "$HOME_BAK" ]; then
|
|
# whole or not at all: a half copy would be put back over $HOME/.dt
|
|
rm -rf "$HOME_BAK.tmp"
|
|
cp -a "$HOME/.dt" "$HOME_BAK.tmp" && mv "$HOME_BAK.tmp" "$HOME_BAK" ||
|
|
{ echo "cannot back up $HOME/.dt" >&2; exit 1; }
|
|
fi
|
|
|
|
# environment for launched programs
|
|
export DISPLAY=$DISP
|
|
export HOME=$HOME
|
|
PATH=$R/dt/bin:$PATH
|
|
LD_LIBRARY_PATH=$R/dt/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
|
|
XFILESEARCHPATH=$R/dt/app-defaults/%L/%N:$R/dt/app-defaults/C/%N
|
|
DTDATABASESEARCHPATH=$R/dt/appconfig/types/%L,$R/dt/appconfig/types/C
|
|
TTPATH=$R/dt/appconfig/tttypes
|
|
DTSCREENSAVERLIST="StartDtscreenSwarm StartDtscreenQix StartDtscreenFlame StartDtscreenHop StartDtscreenImage StartDtscreenLife StartDtscreenRotor StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank"
|
|
export PATH LD_LIBRARY_PATH XFILESEARCHPATH DTDATABASESEARCHPATH TTPATH DTSCREENSAVERLIST
|
|
|
|
Session() {
|
|
env -i DISPLAY=$DISPLAY HOME=$HOME PATH=$PATH LANG=${MCDE_LANG:-C.UTF-8} \
|
|
LD_LIBRARY_PATH=$LD_LIBRARY_PATH XFILESEARCHPATH=$XFILESEARCHPATH \
|
|
TTPATH=$TTPATH DTDATABASESEARCHPATH=$DTDATABASESEARCHPATH \
|
|
DTSCREENSAVERLIST="$DTSCREENSAVERLIST" \
|
|
MCDE_THUMB_DEBUG="${MCDE_THUMB_DEBUG:-}" \
|
|
MCDE_DESK_DEBUG="${MCDE_DESK_DEBUG:-}" \
|
|
MCDE_PARENTAL_DEBUG="${MCDE_PARENTAL_DEBUG:-}" \
|
|
MCDE_DEFAULTS_DEBUG="${MCDE_DEFAULTS_DEBUG:-}" "$@"
|
|
}
|
|
|
|
# start dtlogin
|
|
sudo "$R/dt/bin/dtlogin" -config "$CFG/Xconfig" -nodaemon -debug 1 \
|
|
> "$WORK/dtlogin.log" 2>&1 &
|
|
DTLOGIN_PID=$!
|
|
# wait for greeter
|
|
i=0
|
|
while [ $i -lt 60 ]; do
|
|
grep -q 'Greeter started' "$WORK/dtlogin.log" 2>/dev/null && break
|
|
sleep 1
|
|
i=$((i + 1))
|
|
done
|
|
grep -q 'Greeter started' "$WORK/dtlogin.log" 2>/dev/null || { echo "greeter did not start" >&2; exit 1; }
|
|
sleep 4
|
|
|
|
# find entry window id (the username text field)
|
|
find_entry() {
|
|
DISPLAY=$DISP xwininfo -root -tree 2>/dev/null | awk '/222x23/ {print $1; exit}'
|
|
}
|
|
|
|
UENTRY=$(find_entry)
|
|
if [ -z "$UENTRY" ]; then
|
|
echo "username entry not found" >&2
|
|
exit 1
|
|
fi
|
|
# click the username field at its known position, then type
|
|
env DISPLAY=$DISP xdotool mousemove 551 394 click 1
|
|
sleep 0.3
|
|
env DISPLAY=$DISP xdotool type --delay 40 "$USERN" 2>/dev/null
|
|
sleep 0.3
|
|
env DISPLAY=$DISP xdotool key Return 2>/dev/null
|
|
sleep 2
|
|
# password challenge: the greeter grabs the keyboard for the no-echo field.
|
|
# Retry a few times: the grab may not be active yet on the first try.
|
|
i=0
|
|
while [ $i -lt 5 ]; do
|
|
env DISPLAY=$DISP xdotool type --delay 40 "x" 2>/dev/null
|
|
sleep 0.3
|
|
env DISPLAY=$DISP xdotool key Return 2>/dev/null
|
|
sleep 2
|
|
if ! DISPLAY=$DISP xdotool search --name login_shell >/dev/null 2>&1; then break; fi
|
|
i=$((i + 1))
|
|
done
|
|
|
|
# wait for the greeter to go away (login_shell window disappears)
|
|
i=0
|
|
while [ $i -lt 30 ]; do
|
|
if ! DISPLAY=$DISP xdotool search --name login_shell >/dev/null 2>&1; then break; fi
|
|
sleep 1
|
|
i=$((i + 1))
|
|
done
|
|
sleep "$WAIT"
|
|
|
|
# verify a session actually started: dtlogin log must show Verify() + Client started
|
|
if ! grep -q "Verify(): $USERN" "$WORK/dtlogin.log" 2>/dev/null; then
|
|
echo "login failed; see $WORK/dtlogin.log" >&2
|
|
exit 1
|
|
fi
|
|
if ! grep -q "Client started" "$WORK/dtlogin.log" 2>/dev/null; then
|
|
echo "session did not start; see $WORK/dtlogin.log" >&2
|
|
exit 1
|
|
fi
|
|
echo "mcde-desktop: desktop up on $DISP (user $USERN)" >&2
|
|
|
|
# helper: run a shell command in the session env
|
|
run_session() {
|
|
# shellcheck disable=SC2086
|
|
DISPLAY=$DISP HOME=$HOME PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
|
|
XFILESEARCHPATH=$XFILESEARCHPATH DTDATABASESEARCHPATH=$DTDATABASESEARCHPATH \
|
|
TTPATH=$TTPATH DTSCREENSAVERLIST="$DTSCREENSAVERLIST" "$@"
|
|
}
|
|
|
|
# command processor
|
|
process_line() {
|
|
line=$1
|
|
# strip leading/trailing whitespace
|
|
line=$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
|
[ -z "$line" ] && return 0
|
|
case "$line" in
|
|
\#*) return 0 ;;
|
|
esac
|
|
set -- $line
|
|
cmd=$1
|
|
shift
|
|
case "$cmd" in
|
|
wait)
|
|
sleep "${1:-1}"
|
|
;;
|
|
shot)
|
|
out=$1; shift
|
|
if [ $# -ge 4 ]; then
|
|
x=$1; y=$2; w=$3; h=$4
|
|
env DISPLAY=$DISP import -window root -crop "${w}x${h}+${x}+${y}" "$out" 2>/dev/null
|
|
else
|
|
env DISPLAY=$DISP import -window root "$out" 2>/dev/null
|
|
fi
|
|
echo "shot: $out"
|
|
;;
|
|
type)
|
|
# rest of line as text
|
|
text=${line#type }
|
|
text=${text# }
|
|
env DISPLAY=$DISP xdotool type --delay 30 "$text" 2>/dev/null
|
|
;;
|
|
key)
|
|
# shellcheck disable=SC2086
|
|
env DISPLAY=$DISP sh "$TOP/tools/mcde-xkey.sh" "$@" 2>/dev/null
|
|
;;
|
|
click)
|
|
x=$1; y=$2; btn=${3:-1}
|
|
env DISPLAY=$DISP xdotool mousemove "$x" "$y" click "$btn" 2>/dev/null
|
|
;;
|
|
move)
|
|
env DISPLAY=$DISP xdotool mousemove "$1" "$2" 2>/dev/null
|
|
;;
|
|
run)
|
|
# launch in session env, background
|
|
prog=$1; shift
|
|
case "$prog" in
|
|
*/*) ;;
|
|
*) prog=$R/dt/bin/$prog ;;
|
|
esac
|
|
Session "$prog" "$@" >> "$WORK/run.log" 2>&1 &
|
|
echo "run: $prog $*"
|
|
;;
|
|
focus)
|
|
name=$1
|
|
w=$(env DISPLAY=$DISP xdotool search --onlyvisible --name "$name" 2>/dev/null | head -1)
|
|
if [ -n "$w" ]; then
|
|
env DISPLAY=$DISP xdotool windowactivate "$w" 2>/dev/null
|
|
env DISPLAY=$DISP xdotool windowfocus "$w" 2>/dev/null
|
|
echo "focus: $name ($w)"
|
|
else
|
|
echo "focus: no window matching '$name'" >&2
|
|
fi
|
|
;;
|
|
close)
|
|
env DISPLAY=$DISP xdotool key alt+F4 2>/dev/null
|
|
;;
|
|
exit)
|
|
return 1
|
|
;;
|
|
*)
|
|
echo "unknown command: $cmd" >&2
|
|
;;
|
|
esac
|
|
return 0
|
|
}
|
|
|
|
# read commands
|
|
if [ -n "$CMDS" ] && [ "$CMDS" != "-" ]; then
|
|
if [ ! -r "$CMDS" ]; then
|
|
echo "cannot read commands file: $CMDS" >&2
|
|
exit 2
|
|
fi
|
|
while IFS= read -r line; do
|
|
process_line "$line" || break
|
|
done < "$CMDS"
|
|
else
|
|
# stdin
|
|
while IFS= read -r line; do
|
|
process_line "$line" || break
|
|
done
|
|
fi
|
|
|
|
echo "mcde-desktop: done" >&2
|
|
exit 0
|