From 663764ea8b9e547d1e422a7e7a335415405945ab Mon Sep 17 00:00:00 2001 From: Debian Live user Date: Sat, 29 Apr 2023 06:30:25 -0400 Subject: normalize file locations --- Makefile | 2 +- firestart | 39 ------------------- firestartx | 58 --------------------------- src/firefox-io-slayer | 95 +++++++++++++++++++++++++++++++++++++++++++++ src/firefox-io-slayer-redux | 95 --------------------------------------------- src/firestart | 40 +++++++++++++++++++ src/firestartx | 58 +++++++++++++++++++++++++++ src/ioslay@.service | 3 +- 8 files changed, 196 insertions(+), 194 deletions(-) delete mode 100755 firestart delete mode 100755 firestartx create mode 100755 src/firefox-io-slayer delete mode 100755 src/firefox-io-slayer-redux create mode 100755 src/firestart create mode 100755 src/firestartx diff --git a/Makefile b/Makefile index 923d3a8..f3d4b3f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BINDIR = /usr/local/bin -BINARIES = firestart firestartx $(addprefix src/,sliceweasel.lib.sh firefox-io-slayer-redux ioslay-firefox) +BINARIES = $(addprefix src/,firestart firestartx sliceweasel.lib.sh firefox-io-slayer ioslay-firefox) ifeq ($(shell id -u),0) # Prevent running as root diff --git a/firestart b/firestart deleted file mode 100755 index 2557903..0000000 --- a/firestart +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - -UNIT=firefox@$DISPLAY - -if systemctl --user is-active "$UNIT" >/dev/null -then - # Ask the existing firefox to open a new window. - - # Firefox does not include an official API to do this in a way that - # is not racy. If we merely do this: - # - # exec firefox "$@" - # - # ...then the unit could become inactive after our check, in which - # case this launches a new firefox outside the container! - # - # So instead, we launch the same command using a systemd container - # that prevents Firefox from forking off a new process. - # - # Well, actually, we let it fork off _one_ process, since otherwise - # it segfaults. But this _does_ happen to prevent it starting a new - # firefox instance! Firefox safely segfaults if it is limited this - # way while trying to start a new instance. - # - # A more "correct" approach is available here: - # https://github.com/ayosec/findfox - exec systemd-run \ - --user \ - --property Environment="DISPLAY=$DISPLAY" \ - --property Environment=XAUTHORITY="$XAUTHORITY" \ - --property TasksMax=2 \ - --wait \ - --pipe \ - -q \ - -- /bin/sh -c 'exec firefox "$@"' sh "$@" -else - systemctl --user reset-failed "$UNIT" 2>/dev/null - systemctl --user start "$UNIT" -fi diff --git a/firestartx b/firestartx deleted file mode 100755 index 636fc84..0000000 --- a/firestartx +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash - -get_new_display() -{ - for ((n=1; n<99; ++n)) - do - if [ ! -e /tmp/.X$n-lock ] - then - NEW_DISPLAY=:$n - return - fi - done - false -} - -get_new_display || exit - -if [ $# = 0 ] -then - set -- firestart -fi - -# TITLE=$* # lol, startx doesn't support arguments with spaces -TITLE=$1 -TITLE= # blank title documents the screen grab keys in the title - -if [ "$1" != firestart ] -then - USE_XINIT=y -fi - -if [ "$USE_XINIT" ] -then - set -x - case "$1" in - /*) ;; - *) f1=$(which "$1") || exit - shift - set -- "$f1" "$@" - ;; - esac - xinit "$@" -- $(which Xephyr) "$NEW_DISPLAY" -resizeable -p 0 -terminate ${TITLE:+ -title "$TITLE"} -else - set -x - dpi=$(xrdb -query | sed -n -e 's/^Xft.dpi:[ \t]*//p') - startx -- "$(which Xephyr)" "$NEW_DISPLAY" \ - -resizeable \ - -p 0 \ - -terminate \ - ${TITLE:+ -title "$TITLE"} \ - ${dpi:+ -dpi "$dpi"} \ - & - if [ $# -gt 0 ] - then - DISPLAY=$NEW_DISPLAY "$@" & - fi - wait -fi diff --git a/src/firefox-io-slayer b/src/firefox-io-slayer new file mode 100755 index 0000000..fe54e37 --- /dev/null +++ b/src/firefox-io-slayer @@ -0,0 +1,95 @@ +#!/bin/bash + +DISPLAY=$1 + +if [ -e /usr/lib/bash/sleep ] +then + enable -f /usr/lib/bash/sleep sleep +fi + +export NOTICE=y +noticeLOG() { [ "$NOTICE" ] || return; echo "Notice: $*" >&2; } +debugLOG() { [ "$DBG" ] || return; echo "Debug: $*" >&2; } + +. sliceweasel.lib.sh + +vkill() +{ + if [ $# = 0 ] + then + return + fi + ( + if [ "$(id -u)" = 0 ] + then sudo= + else sudo=sudo + fi + set -x + ps u "$@" + $sudo kill "$@" + ) +} + +slay_slayer() +{ + if [ "$ioslay" ] + then + children=$(for pid in $ioslay; do pgrep -P $ioslay; done) + grandchildren=$(for pid in $children; do pgrep -P $pid; done) + vkill $ioslay $children $grandchildren + fi +} + +group_procs=$(get_firefox_cgroup_procs) + +ioslay= +lastprocs= +SIGNALLED= +trap 'SIGNALLED=y' SIGINT SIGTERM SIGHUP +while [ ! "$SIGNALLED" ] +do + if ! [ -e "$group_procs" ] + then + [ "$warned" ] || echo "Warning: firefox not running or cgroup not found" >&2 + warned=y + else + if [ "$warned" ] + then + echo "Found firefox cgroup: $group_procs" >&2 + warned= + fi + read -N 1000100 procs < "$group_procs" + if [ "$procs" ] + then + set -- + for pid in $procs + do + read comm < /proc/$pid/comm + case "$comm" in + 'Isolated Web Content' | 'Web Content' | 'Isolated Web Co') + set -- "$@" "$pid" + debugLOG "accept /proc/$pid/comm $comm" + ;; + *) + debugLOG "reject /proc/$pid/comm $comm" + esac + done + + # echo "pids: ($*|$(echo $procs))" >&2 + if [ "$lastargs" != "$*" ] + then + slay_slayer + wait $ioslay + if [ $# -gt 0 ] + then + ioslay-firefox "$@" & + ioslay=$! + noticeLOG "Launched ioslay-firefox[$ioslay] $*" + fi + fi + lastargs=$* + fi + fi + sleep 1 +done +slay_slayer diff --git a/src/firefox-io-slayer-redux b/src/firefox-io-slayer-redux deleted file mode 100755 index fe54e37..0000000 --- a/src/firefox-io-slayer-redux +++ /dev/null @@ -1,95 +0,0 @@ -#!/bin/bash - -DISPLAY=$1 - -if [ -e /usr/lib/bash/sleep ] -then - enable -f /usr/lib/bash/sleep sleep -fi - -export NOTICE=y -noticeLOG() { [ "$NOTICE" ] || return; echo "Notice: $*" >&2; } -debugLOG() { [ "$DBG" ] || return; echo "Debug: $*" >&2; } - -. sliceweasel.lib.sh - -vkill() -{ - if [ $# = 0 ] - then - return - fi - ( - if [ "$(id -u)" = 0 ] - then sudo= - else sudo=sudo - fi - set -x - ps u "$@" - $sudo kill "$@" - ) -} - -slay_slayer() -{ - if [ "$ioslay" ] - then - children=$(for pid in $ioslay; do pgrep -P $ioslay; done) - grandchildren=$(for pid in $children; do pgrep -P $pid; done) - vkill $ioslay $children $grandchildren - fi -} - -group_procs=$(get_firefox_cgroup_procs) - -ioslay= -lastprocs= -SIGNALLED= -trap 'SIGNALLED=y' SIGINT SIGTERM SIGHUP -while [ ! "$SIGNALLED" ] -do - if ! [ -e "$group_procs" ] - then - [ "$warned" ] || echo "Warning: firefox not running or cgroup not found" >&2 - warned=y - else - if [ "$warned" ] - then - echo "Found firefox cgroup: $group_procs" >&2 - warned= - fi - read -N 1000100 procs < "$group_procs" - if [ "$procs" ] - then - set -- - for pid in $procs - do - read comm < /proc/$pid/comm - case "$comm" in - 'Isolated Web Content' | 'Web Content' | 'Isolated Web Co') - set -- "$@" "$pid" - debugLOG "accept /proc/$pid/comm $comm" - ;; - *) - debugLOG "reject /proc/$pid/comm $comm" - esac - done - - # echo "pids: ($*|$(echo $procs))" >&2 - if [ "$lastargs" != "$*" ] - then - slay_slayer - wait $ioslay - if [ $# -gt 0 ] - then - ioslay-firefox "$@" & - ioslay=$! - noticeLOG "Launched ioslay-firefox[$ioslay] $*" - fi - fi - lastargs=$* - fi - fi - sleep 1 -done -slay_slayer diff --git a/src/firestart b/src/firestart new file mode 100755 index 0000000..5cc1c92 --- /dev/null +++ b/src/firestart @@ -0,0 +1,40 @@ +#!/bin/bash + +UNIT=firefox@$DISPLAY + +if systemctl --user is-active "$UNIT" >/dev/null +then + echo "Attemping to contact existing Firefox instance." >&2 + # Ask the existing firefox to open a new window. + + # Firefox does not include an official API to do this in a way that + # is not racy. If we merely do this: + # + # exec firefox "$@" + # + # ...then the unit could become inactive after our check, in which + # case this launches a new firefox outside the container! + # + # So instead, we launch the same command using a systemd container + # that prevents Firefox from forking off a new process. + # + # Well, actually, we let it fork off _one_ process, since otherwise + # it segfaults. But this _does_ happen to prevent it starting a new + # firefox instance! Firefox safely segfaults if it is limited this + # way while trying to start a new instance. + # + # A more "correct" approach is available here: + # https://github.com/ayosec/findfox + exec systemd-run \ + --user \ + --property Environment="DISPLAY=$DISPLAY" \ + --property Environment=XAUTHORITY="$XAUTHORITY" \ + --property TasksMax=2 \ + --wait \ + --pipe \ + -q \ + -- /bin/sh -c 'exec firefox "$@"' sh "$@" +else + systemctl --user reset-failed "$UNIT" 2>/dev/null + systemctl --user start "$UNIT" +fi diff --git a/src/firestartx b/src/firestartx new file mode 100755 index 0000000..636fc84 --- /dev/null +++ b/src/firestartx @@ -0,0 +1,58 @@ +#!/bin/bash + +get_new_display() +{ + for ((n=1; n<99; ++n)) + do + if [ ! -e /tmp/.X$n-lock ] + then + NEW_DISPLAY=:$n + return + fi + done + false +} + +get_new_display || exit + +if [ $# = 0 ] +then + set -- firestart +fi + +# TITLE=$* # lol, startx doesn't support arguments with spaces +TITLE=$1 +TITLE= # blank title documents the screen grab keys in the title + +if [ "$1" != firestart ] +then + USE_XINIT=y +fi + +if [ "$USE_XINIT" ] +then + set -x + case "$1" in + /*) ;; + *) f1=$(which "$1") || exit + shift + set -- "$f1" "$@" + ;; + esac + xinit "$@" -- $(which Xephyr) "$NEW_DISPLAY" -resizeable -p 0 -terminate ${TITLE:+ -title "$TITLE"} +else + set -x + dpi=$(xrdb -query | sed -n -e 's/^Xft.dpi:[ \t]*//p') + startx -- "$(which Xephyr)" "$NEW_DISPLAY" \ + -resizeable \ + -p 0 \ + -terminate \ + ${TITLE:+ -title "$TITLE"} \ + ${dpi:+ -dpi "$dpi"} \ + & + if [ $# -gt 0 ] + then + DISPLAY=$NEW_DISPLAY "$@" & + fi + wait +fi diff --git a/src/ioslay@.service b/src/ioslay@.service index 1a00bd4..11db192 100644 --- a/src/ioslay@.service +++ b/src/ioslay@.service @@ -1,5 +1,6 @@ [Unit] Description = Kill firefox when it spins the disk +Requires = firefox@%I [Service] -ExecStart = /usr/local/bin/firefox-io-slayer-redux %I +ExecStart = /usr/local/bin/firefox-io-slayer %I -- cgit v1.2.3