From 9c5bbeb04c525eda79b13e25721e806742244235 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 19 Dec 2019 13:32:30 +0200
Subject: [PATCH] Fix issue with ControlMaster sockets pruning.

The prune_ssh_sockets shell function would always delete the
ControlMaster sockets (the open file handlers would have a random suffix
and the check would always show that the socket wasn't open by any
process). Instead add a prefix to the ControlMaster sockets and ignore
them when pruning. Those sockets are always deleted when the process
exists. Also, mark the vim filetype for the ssh config fragments as
sshconfig.
---
 .bashrc                    | 16 ++++++++++------
 .ssh/config.d/20_endless   |  1 +
 .ssh/config.d/20_github    |  2 ++
 .ssh/config.d/20_shore     |  1 +
 .ssh/config.d/30_localhost |  1 +
 .ssh/config.d/90_ec2       |  1 +
 Documents/bin/forward      |  2 +-
 7 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/.bashrc b/.bashrc
index c025c9e..9e968d5 100644
--- a/.bashrc
+++ b/.bashrc
@@ -293,11 +293,14 @@ flatpak_kill () {
     fi
 }
 
-__prune_docker_remote () {
-    for i in ~/.ssh/docker_*
-    do
-        [ ! -e "$i" ] || lsof -t "$i" >/dev/null || rm "$i"
-    done
+# shellcheck disable=SC2120
+prune_ssh_sockets () {
+    ( [ "${1:-}" != '-f' ] && [ "${1:-}" != '--force' ] ) || killall -v ssh || true
+    find ~/.ssh/ \
+        -maxdepth 1 \
+        -type s \
+        \! -name 'cm_*.sock' \
+        -execdir sh -c 'lsof -t "$1" >/dev/null || rm "$1"' _ {} \;
 }
 
 __prompt () {
@@ -383,4 +386,5 @@ then
     ! command -v direnv > /dev/null || eval "$(direnv hook bash)"
 fi
 
-__prune_docker_remote
+# shellcheck disable=SC2119
+prune_ssh_sockets
diff --git a/.ssh/config.d/20_endless b/.ssh/config.d/20_endless
index 7c26027..068810d 100644
--- a/.ssh/config.d/20_endless
+++ b/.ssh/config.d/20_endless
@@ -1,4 +1,5 @@
 
+# vim:ft=sshconfig
 #Endless
 
 Host appupdates.endlessm.com irc.endlessm.com status.endlessm.com
diff --git a/.ssh/config.d/20_github b/.ssh/config.d/20_github
index 198ef69..d6f9e06 100644
--- a/.ssh/config.d/20_github
+++ b/.ssh/config.d/20_github
@@ -1,3 +1,5 @@
+
+# vim:ft=sshconfig
 # github.com
 
 Host github.com
diff --git a/.ssh/config.d/20_shore b/.ssh/config.d/20_shore
index cd1fadd..7ea02c9 100644
--- a/.ssh/config.d/20_shore
+++ b/.ssh/config.d/20_shore
@@ -1,4 +1,5 @@
 
+# vim:ft=sshconfig
 # shore.co.il infrastructure
 
 Host cgit.shore.co.il host01.shore.co.il wdr4300.shore.co.il xbmc.shore.co.il
diff --git a/.ssh/config.d/30_localhost b/.ssh/config.d/30_localhost
index 8c88ab9..e290116 100644
--- a/.ssh/config.d/30_localhost
+++ b/.ssh/config.d/30_localhost
@@ -1,4 +1,5 @@
 
+# vim:ft=sshconfig
 # localhost
 
 Host localhost
diff --git a/.ssh/config.d/90_ec2 b/.ssh/config.d/90_ec2
index 6b7ed8a..2e3df24 100644
--- a/.ssh/config.d/90_ec2
+++ b/.ssh/config.d/90_ec2
@@ -1,4 +1,5 @@
 
+# vim:ft=sshconfig
 # Disable SSH host key checking for EC2 instances.
 
 Host  *.compute-1.amazonaws.com
diff --git a/Documents/bin/forward b/Documents/bin/forward
index a75ecd7..d14272a 100755
--- a/Documents/bin/forward
+++ b/Documents/bin/forward
@@ -7,7 +7,7 @@ usage () {
 
 host="$1" || { usage >&2; exit 1; }
 forward="$2" || { usage >&2; exit 1; }
-alias ssh='ssh -fnNTS ~/.ssh/%C.sock'
+alias ssh='ssh -fnNTS ~/.ssh/cm_%C.sock'
 
 # shellcheck disable=SC2029
 ssh -o ControlMaster=auto \
-- 
GitLab