Skip to content

Instantly share code, notes, and snippets.

@ze-
Last active February 27, 2017 15:56
Show Gist options
  • Save ze-/eb39764bc242d6167390 to your computer and use it in GitHub Desktop.
Save ze-/eb39764bc242d6167390 to your computer and use it in GitHub Desktop.
bastard alarm
In general, please pre-test and adjust before reliance. In particular, note
potential sensitivity to the more restrictive $PATH cron often has over your
standard login shell, as well as the config variables (especially mpopts) at
the beginning of bastard_alarm.sh. I personally call it indirectly through
a helper script that sets a suitable PATH for it and other things, as well
as turns on my monitor and keeps it on until a nap script shuts it off, with
'xset dpms force on -dpms s reset', stops soothing background sounds started
by a nap script, etc...
bastard_alarm.sh:
A hardcore wakeup alarm.
It's meant to be launched by cron, a delayed commandline, sensor
condition triggers, etc.
Its user-interaction is strictly through the system's idle timer (like a
screensaver).
Once executed it plays alarm noise.
If the user comes active, the alarm pauses and stays paused.
If the user goes idle, the alarm resumes.
Only after the user has stayed active for several minutes (cumulative) does
the script shut down.
This forces time to become awake enough not go back to bed or fall asleep
where you're sitting.
Depends on mpv or mplayer, xidles, and optionally fftest to vibrate a gamepad.
(fftest came from the 'joystick' package on my distro, apparently a part of
the linuxconsole project: http://sourceforge.net/projects/linuxconsole/)
baspoof.sh:
Flawed attempt at a process commandline spoofer. NOT IN USABLE CONDITION!
Since any true *NIX nerd can destroy a bastard_alarm.sh process in their
sleep, this is a launch-wrapper script to make it harder.
It sanitizes the list of running processes and selects one randomly, then
launches a script (i.e. bastard_alarm.sh) disguised as that other process.
This ensures pkill/killall can't find/name it, and makes it impossible to
grep or otherwise easily find it in a reasonably populated ps list.
Unfortunately this doesn't stop you from shutting down or unplugging your
computer, but if you're the sort of person to need baspoof.sh then you
probably wouldn't dream of doing such a thing. ;)
Update 2014/09/03: Fix dubious sanitation that can cause random alarm
failure. Unfortunately reduces the candidates a lot, may revise further.
Update 2014/09/05: Another fix for dubious sanitation. However, baspoof.sh
hasn't really been tested all that well and clearly has some issues, so
is no longer recommended for use until a method of generating guaranteed
safe spoof canditates is implemented.
xidles.c:
Gives system idle time, in seconds, via the X11 XScreenSaver facility.
Depended upon by bastard_alarm.sh.
Compile with the given gcc commandline, and either place somewhere in your
$PATH or edit its calls in bastard_alarm.sh to point to its location.
This one's not my code and I don't know where it came from, but I think it
was public domain. If it's yours, or you know, let me know for any credit,
corrections, removal, etc.
TODO: Is there a kernel-level or cross-platform facility to replace this?
Update 2014/09/12: Only fixing build commandline to include -lX11. Thanks
to Kat for reporting.
#!/bin/bash
# some config
mpcommand="mpv" # mplayer / mplayer2 / mpv, kinda depends on one as is, but could be adapted to other things
mpopts="--af-pre channels=6:[0-0,0-1,0-2,0-3,0-4,0-5] --ao jack:name=mpv_alarm,alsa:device=lame" # special options
# [optional] crude sequence for force-feedback device:
# mine does a long low rumble with a few short/hard ones overlayed (seemingly independent, additive)
# play around with fftest to find appropriate numbers and timings for a desired effect
# XXX MAKE SURE YOU echo -1 AT THE END OR RISK POSSIBLE SYSTEM LOCKUP FROM BUGGY JOYSTICK DRIVERS!! XXX
#ffseq="echo 0 && sleep 2 && echo 5 && sleep 6 && echo 5 && sleep 6 && echo 5 && sleep 6 && echo -1"
# option defaults
idletime=4 # seconds of inactivity before alarm unpauses
endtime=5 # minutes of cumulative activity before alarm quits
#joydev="/dev/input/dragonrise" # [optional] reliable dev file for force-feedback device, must work with fftest
alarmdir="${HOME}/.alarms" # [optional] directory with alarm sound file(s), alternative to /dev/urandom noise
# these generally shouldn't need changing
mpinput="${HOME}/.$mpcommand/alarminput" # command input fifo, controls play/pause/quit
# mpv/mplayer2/mplayer version-specific option forms, also the commands in alarm_pause():
mpopts+=" --input-file=$mpinput" # mpopts+=" -input file=$mpinput"
mpopts+=" --no-lirc --no-joystick" # mpopts+=" -no-lirc -no-joystick" # no quitting with js/remote!
mpopts+=" --really-quiet --loop=inf" # mpopts+=" -really-quiet -loop -1" # no term output, loop forever.
quitcontrol="/tmp/$(uuidgen).quit" # control tempfile to propagate quit condition to forked (child) loops
ffinput="/tmp/$(uuidgen).ff" # command input tempfile to control force-feedback
while getopts :i:e:j:a: OPT;do
case "$OPT" in
i) idletime="$OPTARG" ;;
e) endtime="$OPTARG" ;;
j) joydev="$OPTARG" ;;
a) alarmdir="$OPTARG" ;;
:) echo "missing value for option: -$OPTARG" >/dev/stderr; exit 1 ;;
?) echo "invalid option: -$OPTARG" >/dev/stderr ;;
esac
done
pause=0
activetime=0
function alarm_init() {
mkfifo "$mpinput" 2>/dev/null
echo 1 >"$ffinput"
if [[ $(ls "$alarmdir"/ 2>/dev/null | wc -l ) -gt 0 ]];then # if we have alt alarm sound(s)
until [[ -e "$quitcontrol" ]];do # dying is no excuse to quit
$mpcommand $mpopts "$alarmdir"/* >/dev/null 2>&1
done &
else # default to /dev/urandom noise
until [[ -e "$quitcontrol" ]];do
$mpcommand $mpopts -demuxer rawaudio /dev/urandom >/dev/null 2>&1
done &
fi
[[ -c "$joydev" ]] && until [[ -e "$quitcontrol" ]];do # if we've got a valid joystick device
grep -s -q 1 "$ffinput" && ( $ffseq ) | fftest "$joydev" >/dev/null 2>&1 && sleep 1
done &
}
function alarm_pause() {
[[ $1 -eq 1 ]] && [[ $pause -eq 0 ]] && echo "set pause yes" >"$mpinput" && echo 0 >"$ffinput" && pause=1
[[ $1 -eq 0 ]] && [[ $pause -eq 1 ]] && echo "set pause no" >"$mpinput" && echo 1 >"$ffinput" && pause=0
}
function alarm_end() {
touch "$quitcontrol"
echo quit >"$mpinput"
echo 0 >"$ffinput"
sleep 1
rm -f "$quitcontrol" "$mpinput" "$ffinput"
}
function cond_pause() {
[[ $(xidles) -ge $idletime ]] && return 1
((activetime++))
return 0
}
function cond_end() {
[[ $activetime -gt $((endtime*60*4)) ]] && return 0 # *4 for ~1/4s loops
return 1
}
alarm_init
until cond_end;do
if cond_pause; then alarm_pause 1
else alarm_pause 0
fi
sleep 0.25
done
alarm_end
/* gcc -o xidles xidles.c -lm -lXss -lX11 */
#include <stdio.h>
#include <X11/extensions/scrnsaver.h>
#include <math.h>
main() {
XScreenSaverInfo *info = XScreenSaverAllocInfo();
Display *display = XOpenDisplay(0);
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
printf("%u\n", (int)ceil((float)info->idle/1000));
}
# This is probably broken, possibly dangerous. Consider it as only for academic interest.
#!/bin/bash
payload=$(which bastard_alarm.sh) # full path to script to run, 'which' works if the file is executable and in $PATH
export PATH=.:${PATH}
IFS=$'\n'
mkdir -p ~/.baspoof/
pses=($(ps ax -o command | tail -n +2 | grep -v -e '\[' -e '^-' -e '/' -e '^\w* -\( \|$\)' -e grep | grep ' '))
psnum=$((RANDOM/(32767/${#pses[@]})))
bastard="${pses[$psnum]%% *}"
bastard2="${pses[$psnum]#* }"
bastard2="${bastard2/-}"
ln -s /bin/bash ~/.baspoof/$bastard
ln -s "$payload" ~/.baspoof/"$bastard2"
cd ~/.baspoof/
$bastard "$bastard2" &
sleep 1
rm $bastard -- "$bastard2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment