2020-08-12 19:17:53 +00:00
|
|
|
{- External remote protocol async extension.
|
|
|
|
-
|
2020-08-13 19:49:43 +00:00
|
|
|
- Copyright 2020 Joey Hess <id@joeyh.name>
|
2020-08-12 19:17:53 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2020-08-12 19:17:53 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-08-12 19:17:53 +00:00
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
module Remote.External.AsyncExtension (runRelayToExternalAsync) where
|
2020-08-12 19:17:53 +00:00
|
|
|
|
2020-08-12 19:54:30 +00:00
|
|
|
import Common
|
2020-12-02 18:57:43 +00:00
|
|
|
import Annex
|
2020-08-13 19:49:43 +00:00
|
|
|
import Messages
|
2020-08-12 19:17:53 +00:00
|
|
|
import Remote.External.Types
|
2020-12-02 18:57:43 +00:00
|
|
|
import qualified Utility.SimpleProtocol as Proto
|
2020-08-12 19:17:53 +00:00
|
|
|
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Concurrent.STM.TBMChan
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
|
|
|
|
-- | Starts a thread that will handle all communication with the external
|
|
|
|
-- process. The input ExternalState communicates directly with the external
|
|
|
|
-- process.
|
2020-12-02 18:57:43 +00:00
|
|
|
runRelayToExternalAsync :: External -> ExternalState -> (Annex () -> IO ()) -> IO ExternalAsyncRelay
|
|
|
|
runRelayToExternalAsync external st annexrunner = do
|
2020-08-13 19:49:43 +00:00
|
|
|
jidmap <- newTVarIO M.empty
|
|
|
|
sendq <- newSendQueue
|
2020-08-14 00:18:06 +00:00
|
|
|
nextjid <- newTVarIO (JobId 1)
|
2020-11-30 17:03:47 +00:00
|
|
|
sender <- async $ sendloop st sendq
|
2020-12-02 18:57:43 +00:00
|
|
|
receiver <- async $ receiveloop external st jidmap sendq sender annexrunner
|
2020-08-12 20:25:53 +00:00
|
|
|
return $ ExternalAsyncRelay $ do
|
2020-08-13 19:49:43 +00:00
|
|
|
receiveq <- newReceiveQueue
|
2020-08-14 18:24:46 +00:00
|
|
|
jid <- atomically $ do
|
|
|
|
jid@(JobId n) <- readTVar nextjid
|
|
|
|
let !jid' = JobId (succ n)
|
|
|
|
writeTVar nextjid jid'
|
|
|
|
modifyTVar' jidmap $ M.insert jid receiveq
|
|
|
|
return jid
|
2020-08-12 19:54:30 +00:00
|
|
|
return $ ExternalState
|
2020-08-13 19:49:43 +00:00
|
|
|
{ externalSend = \msg ->
|
|
|
|
atomically $ writeTBMChan sendq
|
2020-08-14 18:24:46 +00:00
|
|
|
(toAsyncWrapped msg, jid)
|
2020-08-13 19:49:43 +00:00
|
|
|
, externalReceive = atomically (readTBMChan receiveq)
|
2020-08-12 20:25:53 +00:00
|
|
|
-- This shuts down the whole relay.
|
2020-11-30 17:03:47 +00:00
|
|
|
, externalShutdown = shutdown external st sendq sender receiver
|
2023-03-14 02:39:16 +00:00
|
|
|
-- These three TMVars are shared among all
|
2020-08-12 19:17:53 +00:00
|
|
|
-- ExternalStates that use this relay; they're
|
|
|
|
-- common state about the external process.
|
|
|
|
, externalPrepared = externalPrepared st
|
|
|
|
, externalConfig = externalConfig st
|
|
|
|
, externalConfigChanges = externalConfigChanges st
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
type ReceiveQueue = TBMChan String
|
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
type SendQueue = TBMChan (AsyncWrapped, JobId)
|
2020-08-13 19:49:43 +00:00
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
type JidMap = TVar (M.Map JobId ReceiveQueue)
|
2020-08-14 00:18:06 +00:00
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
newReceiveQueue :: IO ReceiveQueue
|
|
|
|
newReceiveQueue = newTBMChanIO 10
|
|
|
|
|
|
|
|
newSendQueue :: IO SendQueue
|
|
|
|
newSendQueue = newTBMChanIO 10
|
|
|
|
|
2020-12-02 18:57:43 +00:00
|
|
|
receiveloop :: External -> ExternalState -> JidMap -> SendQueue -> Async () -> (Annex () -> IO ()) -> IO ()
|
|
|
|
receiveloop external st jidmap sendq sendthread annexrunner = externalReceive st >>= \case
|
2020-08-13 19:49:43 +00:00
|
|
|
Just l -> case parseMessage l :: Maybe AsyncMessage of
|
2020-08-14 00:18:06 +00:00
|
|
|
Just (AsyncMessage jid msg) ->
|
|
|
|
M.lookup jid <$> readTVarIO jidmap >>= \case
|
2020-08-14 18:24:46 +00:00
|
|
|
Just c -> do
|
2020-08-14 00:18:06 +00:00
|
|
|
atomically $ writeTBMChan c msg
|
2020-12-02 18:57:43 +00:00
|
|
|
receiveloop external st jidmap sendq sendthread annexrunner
|
2020-08-14 00:18:06 +00:00
|
|
|
Nothing -> protoerr "unknown job number"
|
2020-08-14 18:24:46 +00:00
|
|
|
Nothing -> case parseMessage l :: Maybe ExceptionalMessage of
|
2020-08-15 18:33:18 +00:00
|
|
|
Just _ -> do
|
2020-08-14 18:24:46 +00:00
|
|
|
-- ERROR is relayed to all listeners
|
|
|
|
m <- readTVarIO jidmap
|
2020-08-15 18:33:18 +00:00
|
|
|
forM_ (M.elems m) $ \c ->
|
2020-08-14 18:24:46 +00:00
|
|
|
atomically $ writeTBMChan c l
|
2020-12-02 18:57:43 +00:00
|
|
|
receiveloop external st jidmap sendq sendthread annexrunner
|
2020-08-14 18:24:46 +00:00
|
|
|
Nothing -> protoerr "unexpected non-async message"
|
2020-08-13 20:11:38 +00:00
|
|
|
Nothing -> closeandshutdown
|
2020-08-12 19:17:53 +00:00
|
|
|
where
|
2020-08-13 20:11:38 +00:00
|
|
|
protoerr s = do
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
annexrunner $ warning $ "async external special remote protocol error: " <> s
|
2020-08-13 20:11:38 +00:00
|
|
|
closeandshutdown
|
|
|
|
|
|
|
|
closeandshutdown = do
|
2020-11-30 17:03:47 +00:00
|
|
|
dummy <- async noop
|
|
|
|
shutdown external st sendq sendthread dummy True
|
2020-08-14 00:18:06 +00:00
|
|
|
m <- atomically $ readTVar jidmap
|
2020-08-14 18:24:46 +00:00
|
|
|
forM_ (M.elems m) (atomically . closeTBMChan)
|
2020-08-13 19:49:43 +00:00
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
sendloop :: ExternalState -> SendQueue -> IO ()
|
|
|
|
sendloop st sendq = atomically (readTBMChan sendq) >>= \case
|
|
|
|
Just (wrappedmsg, jid) -> do
|
2020-08-13 19:49:43 +00:00
|
|
|
case wrappedmsg of
|
|
|
|
AsyncWrappedRemoteResponse msg ->
|
2020-08-14 18:24:46 +00:00
|
|
|
externalSend st $ wrapjid msg jid
|
|
|
|
AsyncWrappedRequest msg ->
|
|
|
|
externalSend st $ wrapjid msg jid
|
|
|
|
AsyncWrappedExceptionalMessage msg ->
|
2020-08-14 00:18:06 +00:00
|
|
|
externalSend st msg
|
|
|
|
AsyncWrappedAsyncMessage msg ->
|
2020-08-13 19:49:43 +00:00
|
|
|
externalSend st msg
|
2020-08-14 18:24:46 +00:00
|
|
|
sendloop st sendq
|
2020-08-13 19:49:43 +00:00
|
|
|
Nothing -> return ()
|
|
|
|
where
|
2020-08-14 00:18:06 +00:00
|
|
|
wrapjid msg jid = AsyncMessage jid $ unwords $ Proto.formatMessage msg
|
2020-08-13 19:49:43 +00:00
|
|
|
|
2020-11-30 17:03:47 +00:00
|
|
|
shutdown :: External -> ExternalState -> SendQueue -> Async () -> Async () -> Bool -> IO ()
|
|
|
|
shutdown external st sendq sendthread receivethread b = do
|
|
|
|
-- Receive thread is normally blocked reading from a handle.
|
|
|
|
-- That can block closing the handle, so it needs to be canceled.
|
|
|
|
cancel receivethread
|
|
|
|
-- Cleanly shutdown the send thread as well, allowing it to finish
|
|
|
|
-- writing anything that was buffered.
|
|
|
|
atomically $ closeTBMChan sendq
|
|
|
|
wait sendthread
|
2020-08-13 19:49:43 +00:00
|
|
|
r <- atomically $ do
|
|
|
|
r <- tryTakeTMVar (externalAsync external)
|
|
|
|
putTMVar (externalAsync external)
|
|
|
|
UncheckedExternalAsync
|
|
|
|
return r
|
|
|
|
case r of
|
|
|
|
Just (ExternalAsync _) -> externalShutdown st b
|
|
|
|
_ -> noop
|