stop cached ssh connection w/o needing to look up host and port

Turns out that with -O stop -S socketfile, ssh does not need the real
hostname, or port to be specificed. This is because it simply talks to the
ssh behind the socket and tells it to stop. So, can eliminate the
conversion back from a socketfile to host and port. Which will allow using
shorter filenames for sockets in the future.
This commit is contained in:
Joey Hess 2013-07-21 14:14:54 -04:00
parent 002de3e547
commit c6a020ad1f

View file

@ -51,14 +51,15 @@ sshInfo (host, port) = go =<< sshCacheDir
go (Just dir) = do go (Just dir) = do
let socketfile = dir </> hostport2socket host port let socketfile = dir </> hostport2socket host port
if valid_unix_socket_path socketfile if valid_unix_socket_path socketfile
then return (Just socketfile, cacheparams socketfile) then return (Just socketfile, sshConnectionCachingParams socketfile)
else do else do
socketfile' <- liftIO $ relPathCwdToFile socketfile socketfile' <- liftIO $ relPathCwdToFile socketfile
if valid_unix_socket_path socketfile' if valid_unix_socket_path socketfile'
then return (Just socketfile', cacheparams socketfile') then return (Just socketfile', sshConnectionCachingParams socketfile')
else return (Nothing, []) else return (Nothing, [])
cacheparams :: FilePath -> [CommandParam]
cacheparams socketfile = sshConnectionCachingParams :: FilePath -> [CommandParam]
sshConnectionCachingParams socketfile =
[ Param "-S", Param socketfile [ Param "-S", Param socketfile
, Params "-o ControlMaster=auto -o ControlPersist=yes" , Params "-o ControlMaster=auto -o ControlPersist=yes"
] ]
@ -116,14 +117,13 @@ sshCleanup = go =<< sshCacheDir
stopssh socketfile stopssh socketfile
#endif #endif
stopssh socketfile = do stopssh socketfile = do
let (host, port) = socket2hostport socketfile let params = sshConnectionCachingParams socketfile
(_, params) <- sshInfo (host, port)
-- "ssh -O stop" is noisy on stderr even with -q -- "ssh -O stop" is noisy on stderr even with -q
void $ liftIO $ catchMaybeIO $ void $ liftIO $ catchMaybeIO $
withQuietOutput createProcessSuccess $ withQuietOutput createProcessSuccess $
proc "ssh" $ toCommand $ proc "ssh" $ toCommand $
[ Params "-O stop" [ Params "-O stop"
] ++ params ++ [Param host] ] ++ params ++ [Param "any"]
-- Cannot remove the lock file; other processes may -- Cannot remove the lock file; other processes may
-- be waiting on our exclusive lock to use it. -- be waiting on our exclusive lock to use it.
@ -131,13 +131,6 @@ hostport2socket :: String -> Maybe Integer -> FilePath
hostport2socket host Nothing = host hostport2socket host Nothing = host
hostport2socket host (Just port) = host ++ "!" ++ show port hostport2socket host (Just port) = host ++ "!" ++ show port
socket2hostport :: FilePath -> (String, Maybe Integer)
socket2hostport socket
| null p = (h, Nothing)
| otherwise = (h, readish p)
where
(h, p) = separate (== '!') $ takeFileName socket
socket2lock :: FilePath -> FilePath socket2lock :: FilePath -> FilePath
socket2lock socket = socket ++ lockExt socket2lock socket = socket ++ lockExt