2012-08-21 23:58:53 +00:00
|
|
|
{- git-annex assistant network connection watcher, using dbus
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-08-21 23:58:53 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-08-21 23:58:53 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Assistant.Threads.NetWatcher where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-08-22 18:32:17 +00:00
|
|
|
import Assistant.Sync
|
2012-08-21 23:58:53 +00:00
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import qualified Types.Remote as Remote
|
2013-07-26 20:53:50 +00:00
|
|
|
import Assistant.DaemonStatus
|
2013-11-21 21:49:56 +00:00
|
|
|
import Utility.NotificationBroadcaster
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
#if WITH_DBUS
|
2014-12-29 21:25:59 +00:00
|
|
|
import Assistant.RemoteControl
|
2012-08-21 23:58:53 +00:00
|
|
|
import Utility.DBus
|
|
|
|
import DBus.Client
|
|
|
|
import DBus
|
|
|
|
#else
|
2014-02-25 18:49:27 +00:00
|
|
|
#ifdef linux_HOST_OS
|
2012-08-21 23:58:53 +00:00
|
|
|
#warning Building without dbus support; will poll for network connection changes
|
|
|
|
#endif
|
2014-02-25 18:49:27 +00:00
|
|
|
#endif
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread :: NamedThread
|
2012-08-21 23:58:53 +00:00
|
|
|
#if WITH_DBUS
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread = thread dbusThread
|
2012-09-06 18:56:04 +00:00
|
|
|
#else
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherThread = thread noop
|
2012-08-21 23:58:53 +00:00
|
|
|
#endif
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
2013-01-26 06:09:33 +00:00
|
|
|
thread = namedThread "NetWatcher"
|
2012-09-06 18:56:04 +00:00
|
|
|
|
|
|
|
{- This is a fallback for when dbus cannot be used to detect
|
|
|
|
- network connection changes, but it also ensures that
|
|
|
|
- any networked remotes that may have not been routable for a
|
|
|
|
- while (despite the local network staying up), are synced with
|
2013-08-24 17:49:04 +00:00
|
|
|
- periodically.
|
|
|
|
-
|
2016-11-14 18:26:20 +00:00
|
|
|
- Note that it does not signal the RemoteControl, because it doesn't
|
|
|
|
- know that the network has changed.
|
2013-08-24 17:49:04 +00:00
|
|
|
-}
|
2012-10-29 06:21:04 +00:00
|
|
|
netWatcherFallbackThread :: NamedThread
|
2013-01-26 06:09:33 +00:00
|
|
|
netWatcherFallbackThread = namedThread "NetWatcherFallback" $
|
2012-10-29 06:21:04 +00:00
|
|
|
runEvery (Seconds 3600) <~> handleConnection
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
#if WITH_DBUS
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
dbusThread :: Assistant ()
|
|
|
|
dbusThread = do
|
|
|
|
handleerr <- asIO2 onerr
|
2012-10-30 21:14:26 +00:00
|
|
|
runclient <- asIO1 go
|
2012-10-29 06:21:04 +00:00
|
|
|
liftIO $ persistentClient getSystemAddress () handleerr runclient
|
|
|
|
where
|
|
|
|
go client = ifM (checkNetMonitor client)
|
|
|
|
( do
|
2014-04-12 21:58:19 +00:00
|
|
|
callback <- asIO1 connchange
|
|
|
|
liftIO $ do
|
|
|
|
listenNMConnections client callback
|
2015-06-02 14:01:57 +00:00
|
|
|
listenNDConnections client callback
|
2014-04-12 21:58:19 +00:00
|
|
|
listenWicdConnections client callback
|
2012-10-29 06:21:04 +00:00
|
|
|
, do
|
|
|
|
liftAnnex $
|
|
|
|
warning "No known network monitor available through dbus; falling back to polling"
|
|
|
|
)
|
2014-04-12 21:58:19 +00:00
|
|
|
connchange False = do
|
|
|
|
debug ["detected network disconnection"]
|
2014-04-12 20:32:59 +00:00
|
|
|
sendRemoteControl LOSTNET
|
2014-04-12 21:58:19 +00:00
|
|
|
connchange True = do
|
|
|
|
debug ["detected network connection"]
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection
|
2014-04-08 19:23:50 +00:00
|
|
|
sendRemoteControl RESUME
|
2012-10-29 06:21:04 +00:00
|
|
|
onerr e _ = do
|
|
|
|
liftAnnex $
|
|
|
|
warning $ "lost dbus connection; falling back to polling (" ++ show e ++ ")"
|
|
|
|
{- Wait, in hope that dbus will come back -}
|
|
|
|
liftIO $ threadDelaySeconds (Seconds 60)
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
{- Examine the list of services connected to dbus, to see if there
|
|
|
|
- are any we can use to monitor network connections. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
checkNetMonitor :: Client -> Assistant Bool
|
2012-08-21 23:58:53 +00:00
|
|
|
checkNetMonitor client = do
|
2015-06-02 14:01:57 +00:00
|
|
|
running <- liftIO $ filter (`elem` manager_addresses)
|
2012-08-21 23:58:53 +00:00
|
|
|
<$> listServiceNames client
|
|
|
|
case running of
|
|
|
|
[] -> return False
|
|
|
|
(service:_) -> do
|
2012-10-29 06:21:04 +00:00
|
|
|
debug [ "Using running DBUS service"
|
2012-08-21 23:58:53 +00:00
|
|
|
, service
|
|
|
|
, "to monitor network connection events."
|
|
|
|
]
|
|
|
|
return True
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
2015-06-02 14:01:57 +00:00
|
|
|
manager_addresses = [networkmanager, networkd, wicd]
|
2012-10-29 06:21:04 +00:00
|
|
|
networkmanager = "org.freedesktop.NetworkManager"
|
2015-06-02 14:01:57 +00:00
|
|
|
networkd = "org.freedesktop.network1"
|
2012-10-29 06:21:04 +00:00
|
|
|
wicd = "org.wicd.daemon"
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2015-06-02 14:01:57 +00:00
|
|
|
{- Listens for systemd-networkd connections and diconnections.
|
|
|
|
-
|
|
|
|
- Connection example (once fully connected):
|
|
|
|
- [Variant {"OperationalState": Variant "routable"}]
|
|
|
|
-
|
|
|
|
- Disconnection example:
|
|
|
|
- [Variant {"OperationalState": Variant _}]
|
|
|
|
-}
|
|
|
|
listenNDConnections :: Client -> (Bool -> IO ()) -> IO ()
|
|
|
|
listenNDConnections client setconnected =
|
|
|
|
void $ addMatch client matcher
|
|
|
|
$ \event -> mapM_ handleevent
|
|
|
|
(map dictionaryItems $ mapMaybe fromVariant $ signalBody event)
|
|
|
|
where
|
|
|
|
matcher = matchAny
|
|
|
|
{ matchInterface = Just "org.freedesktop.DBus.Properties"
|
|
|
|
, matchMember = Just "PropertiesChanged"
|
|
|
|
}
|
|
|
|
operational_state_key = toVariant ("OperationalState" :: String)
|
|
|
|
routable = toVariant $ toVariant ("routable" :: String)
|
|
|
|
handleevent m = case lookup operational_state_key m of
|
|
|
|
Just state -> if state == routable
|
|
|
|
then setconnected True
|
|
|
|
else setconnected False
|
|
|
|
Nothing -> noop
|
|
|
|
|
2014-04-12 21:58:19 +00:00
|
|
|
{- Listens for NetworkManager connections and diconnections.
|
|
|
|
-
|
|
|
|
- Connection example (once fully connected):
|
|
|
|
- [Variant {"ActivatingConnection": Variant (ObjectPath "/"), "PrimaryConnection": Variant (ObjectPath "/org/freedesktop/NetworkManager/ActiveConnection/34"), "State": Variant 70}]
|
|
|
|
-
|
|
|
|
- Disconnection example:
|
|
|
|
- [Variant {"ActiveConnections": Variant []}]
|
|
|
|
-}
|
|
|
|
listenNMConnections :: Client -> (Bool -> IO ()) -> IO ()
|
|
|
|
listenNMConnections client setconnected =
|
2014-04-26 23:18:33 +00:00
|
|
|
void $ addMatch client matcher
|
2014-08-08 17:55:09 +00:00
|
|
|
$ \event -> mapM_ handleevent
|
2014-04-26 23:18:33 +00:00
|
|
|
(map dictionaryItems $ mapMaybe fromVariant $ signalBody event)
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
matcher = matchAny
|
2014-04-12 21:58:19 +00:00
|
|
|
{ matchInterface = Just "org.freedesktop.NetworkManager"
|
2012-10-29 06:21:04 +00:00
|
|
|
, matchMember = Just "PropertiesChanged"
|
|
|
|
}
|
2014-04-12 21:58:19 +00:00
|
|
|
nm_active_connections_key = toVariant ("ActiveConnections" :: String)
|
|
|
|
nm_activatingconnection_key = toVariant ("ActivatingConnection" :: String)
|
|
|
|
noconnections = Just $ toVariant $ toVariant ([] :: [ObjectPath])
|
|
|
|
rootconnection = Just $ toVariant $ toVariant $ objectPath_ "/"
|
2014-08-08 17:55:09 +00:00
|
|
|
handleevent m
|
2014-04-12 21:58:19 +00:00
|
|
|
| lookup nm_active_connections_key m == noconnections =
|
|
|
|
setconnected False
|
|
|
|
| lookup nm_activatingconnection_key m == rootconnection =
|
|
|
|
setconnected True
|
|
|
|
| otherwise = noop
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2014-04-12 22:36:48 +00:00
|
|
|
{- Listens for Wicd connections and disconnections.
|
|
|
|
-
|
|
|
|
- Connection example:
|
|
|
|
- ConnectResultsSent:
|
|
|
|
- Variant "success"
|
|
|
|
-
|
2023-03-14 02:39:16 +00:00
|
|
|
- Disconnection example:
|
2014-04-12 22:36:48 +00:00
|
|
|
- StatusChanged
|
2023-03-14 02:39:16 +00:00
|
|
|
- [Variant 0, Variant [Variant ""]]
|
2014-04-12 22:36:48 +00:00
|
|
|
-}
|
2014-04-12 21:58:19 +00:00
|
|
|
listenWicdConnections :: Client -> (Bool -> IO ()) -> IO ()
|
2014-04-12 22:36:48 +00:00
|
|
|
listenWicdConnections client setconnected = do
|
2014-04-26 23:18:33 +00:00
|
|
|
match connmatcher $ \event ->
|
2012-08-21 23:58:53 +00:00
|
|
|
when (any (== wicd_success) (signalBody event)) $
|
2014-04-12 22:36:48 +00:00
|
|
|
setconnected True
|
2014-08-08 17:55:09 +00:00
|
|
|
match statusmatcher $ \event -> handleevent (signalBody event)
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
2014-04-12 22:36:48 +00:00
|
|
|
connmatcher = matchAny
|
2012-10-29 06:21:04 +00:00
|
|
|
{ matchInterface = Just "org.wicd.daemon"
|
|
|
|
, matchMember = Just "ConnectResultsSent"
|
|
|
|
}
|
2014-04-12 22:36:48 +00:00
|
|
|
statusmatcher = matchAny
|
|
|
|
{ matchInterface = Just "org.wicd.daemon"
|
|
|
|
, matchMember = Just "StatusChanged"
|
|
|
|
}
|
2012-10-29 06:21:04 +00:00
|
|
|
wicd_success = toVariant ("success" :: String)
|
2014-04-12 22:36:48 +00:00
|
|
|
wicd_disconnected = toVariant [toVariant ("" :: String)]
|
2014-08-08 17:55:09 +00:00
|
|
|
handleevent status
|
2014-04-12 22:36:48 +00:00
|
|
|
| any (== wicd_disconnected) status = setconnected False
|
|
|
|
| otherwise = noop
|
2014-04-26 23:18:33 +00:00
|
|
|
match matcher a =
|
|
|
|
void $ addMatch client matcher a
|
2012-08-21 23:58:53 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
handleConnection :: Assistant ()
|
2013-11-21 21:49:56 +00:00
|
|
|
handleConnection = do
|
|
|
|
liftIO . sendNotification . networkConnectedNotifier =<< getDaemonStatus
|
2016-11-14 18:26:20 +00:00
|
|
|
reconnectRemotes =<< networkRemotes
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2013-07-26 20:53:50 +00:00
|
|
|
{- Network remotes to sync with. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
networkRemotes :: Assistant [Remote]
|
2013-07-26 20:53:50 +00:00
|
|
|
networkRemotes = filter (isNothing . Remote.localpath) . syncRemotes
|
|
|
|
<$> getDaemonStatus
|