summaryrefslogtreecommitdiff
path: root/bin/emacsclient
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2023-01-17 10:15:34 -0700
committerSean Whitton <spwhitton@spwhitton.name>2023-01-17 10:19:42 -0700
commit37e9061c0f8caa953b5a0adb1a3c4329930ac749 (patch)
treeef3944c6f411b607ce4eda9842f446c5ae12119d /bin/emacsclient
parent42727965c2831984e14a46c012612a06e291d0ea (diff)
downloaddotfiles-37e9061c0f8caa953b5a0adb1a3c4329930ac749.tar.gz
emacsclient wrapper: get_listener: find the youngest process
Diffstat (limited to 'bin/emacsclient')
-rwxr-xr-xbin/emacsclient18
1 files changed, 14 insertions, 4 deletions
diff --git a/bin/emacsclient b/bin/emacsclient
index c29a86ee..82d05976 100755
--- a/bin/emacsclient
+++ b/bin/emacsclient
@@ -64,7 +64,7 @@ defer () {
[ -n "$BASH_VERSION" ] \
|| { command -v bash >/dev/null && exec bash "$0" "$@" || defer "$@"; }
-for cmd in flock inotifywait perl pwdx ss; do
+for cmd in flock inotifywait pwdx ss; do
command -v $cmd >/dev/null || defer "$@"
done
@@ -80,9 +80,19 @@ server-file:,display:,parent-id:,timeout:,tramp:" -- "$@")
eval set -- "$getopt"
get_listener () {
- local socket="$1"
- local listener=$(ss -plx src "$socket" \
- | perl -wne'$tmp = $_; END { $tmp =~ /pid=(\d+)/ and print $1 }')
+ local socket=$1 listener= listener_age= rematch_listener= rematch_age=
+ # Find the youngest process listening on a socket of this name.
+ while read -r line; do
+ [[ $line =~ ,pid=([[:digit:]]+), ]] || continue
+ rematch_listener=${BASH_REMATCH[1]}
+ rematch_age="$(ps h -o etimes $rematch_listener)"
+ [[ $rematch_age =~ ^[[:blank:]]*[[:digit:]]+[[:blank:]]*$ ]] \
+ || fail "couldn't find age of process $rematch_listener"
+ if [ -z "$listener" ] || [ $rematch_age -lt $listener_age ]; then
+ listener=$rematch_listener
+ listener_age=$rematch_age
+ fi
+ done < <(ss -plx src "$socket")
if [ -z "$listener" -a -e "$socket" ]; then
# Nothing is listening: remove dangling socket.
rm "$socket"