Skip to content

Commit 6bfed60

Browse files
authored
Merge pull request #296 from MIvanchev/file-lock-cmd
Exclusive file lock during cmd execution and some bugs.
2 parents e12303a + b638699 commit 6bfed60

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

util/stumpish/stumpish

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
### STUMPwm Interactive SHell.
2222

23+
# use a busy-waiting delay of 1 second if floats are not supported by sleep
2324
DELAY=0.01
2425

2526
if ! sleep $DELAY 2>/dev/null >&2
@@ -37,11 +38,34 @@ if [ "$(echo -e foo)" = foo ]; then
3738
echo() { builtin echo -e "$@"; }
3839
fi
3940

41+
stumpwm_pid ()
42+
{
43+
local pid=$$
44+
45+
while :
46+
do
47+
if [ $pid -eq 1 ]
48+
then
49+
echo "StumpWM not found in the process tree, are you sure a graphical " 1>&2
50+
echo "session is running and StumpWM is your WM? If you think this is " 1>&2
51+
echo "a bug in stumpish, please report it." 1>&2
52+
echo 1>&2
53+
exit 1
54+
elif [ "$(cat /proc/${pid}/comm)" = "stumpwm" ]
55+
then
56+
STUMPWM_PID=$pid
57+
break
58+
else
59+
pid=$(cut -f 4 -d " " < /proc/$pid/stat)
60+
fi
61+
done
62+
}
63+
4064
wait_result ()
4165
{
4266
while true
4367
do
44-
RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8s \
68+
RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8u \
4569
STUMPWM_COMMAND_RESULT 2>/dev/null |
4670
sed -E 's/\\([[:digit:]]+)/\\0\1/g')
4771
if echo "$RESULT" | grep -v -q 'not found.$'
@@ -70,6 +94,9 @@ wait_result ()
7094

7195
send_cmd ()
7296
{
97+
(
98+
flock -n 3 || fail "Cannot obtain a file lock to exclusively talk to StumpWM."
99+
73100
local cmd="$1"
74101

75102
if [ "$cmd" = "stumpwm-quit" ]
@@ -80,9 +107,10 @@ send_cmd ()
80107
exit
81108
fi
82109

83-
xprop -root -f STUMPWM_COMMAND 8s -set STUMPWM_COMMAND "$cmd"
110+
xprop -root -f STUMPWM_COMMAND 8u -set STUMPWM_COMMAND "$cmd"
84111

85112
wait_result
113+
) 3>"${TMPDIR:-/tmp}/.stumpish.lock.$STUMPWM_PID"
86114
}
87115

88116
usage ()
@@ -138,6 +166,9 @@ fi
138166
if [ $# -gt 0 ]
139167
then
140168
[ "$1" = "--help" ] && usage
169+
170+
stumpwm_pid
171+
141172
if [ "$1" = "-e" ]
142173
then
143174
if [ $# -ne 2 ]
@@ -154,6 +185,8 @@ then
154185
send_cmd "$*"
155186
fi
156187
else
188+
stumpwm_pid
189+
157190
if [ -t 0 ]
158191
then
159192
if ! type rlwrap 2>/dev/null >&2
@@ -180,7 +213,7 @@ else
180213
tput me sgr0
181214
echo \ for a list of commands.
182215

183-
while (echo -n '> '; read -r REPLY)
216+
while { echo -n '> '; read -r REPLY; }
184217
do
185218
tput md bold
186219
tput AF setaf 2

0 commit comments

Comments
 (0)