2012-08-21 23:58:53 +00:00
|
|
|
{- DBus utilities
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-08-21 23:58:53 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-08-21 23:58:53 +00:00
|
|
|
-}
|
|
|
|
|
2012-10-26 23:38:27 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2019-11-21 19:38:06 +00:00
|
|
|
module Utility.DBus (
|
|
|
|
ServiceName,
|
|
|
|
listServiceNames,
|
|
|
|
callDBus,
|
|
|
|
runClient,
|
|
|
|
persistentClient,
|
|
|
|
) where
|
2012-08-21 23:58:53 +00:00
|
|
|
|
2014-04-26 23:25:05 +00:00
|
|
|
import Utility.PartialPrelude
|
2012-10-27 04:42:14 +00:00
|
|
|
import Utility.Exception
|
|
|
|
|
2012-08-21 23:58:53 +00:00
|
|
|
import DBus.Client
|
|
|
|
import DBus
|
|
|
|
import Data.Maybe
|
2012-10-26 04:02:03 +00:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception as E
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
type ServiceName = String
|
|
|
|
|
|
|
|
listServiceNames :: Client -> IO [ServiceName]
|
|
|
|
listServiceNames client = do
|
Revert "When listing DBus services, also list activatable services."
This reverts commit ef0e3ac22e219b277666cf9c6dd91ee4be0a0cae.
Sebastian thinks best to revert this:
It seems to me the reason I needed to look at activatable sockets
might actually be a networkd bug, and I was in error about patch 0001.
On my machines (without DHCP), networkd quits after configuring the
links. I thought this had to do with network activation, but that was
probably mistaken. This was obscured by my testing the change by doing
systemctl stop/start on networkd; now that I actually unplugged the
network cable, I noticed no DBus messages are triggered by this on
this machine. Your test case might have had a similar problem
(networkd quitting on idle). Might be related to [1].
On another machine (with DHCP) networkd remains active all the time,
and patch 0002 works there. You might want to revert 0001, though:
Suppose someone’s running no manager at all, so that polling would be
required. Because networkd is still listed as activable, we would
refrain from polling – by mistake, because networkd doesn’t seem to
actually go active if we listen on its bus, and it’s listed as
activable even when it’s not configured. Connectivity-related messages
will come in when stopping/starting the service, but not when
unplugging the cable.
2015-06-02 18:38:24 +00:00
|
|
|
reply <- callDBus client "ListNames" []
|
|
|
|
return $ fromMaybe [] $ fromVariant =<< headMaybe (methodReturnBody reply)
|
2012-08-21 23:58:53 +00:00
|
|
|
|
|
|
|
callDBus :: Client -> MemberName -> [Variant] -> IO MethodReturn
|
|
|
|
callDBus client name params = call_ client $
|
|
|
|
(methodCall "/org/freedesktop/DBus" "org.freedesktop.DBus" name)
|
|
|
|
{ methodCallDestination = Just "org.freedesktop.DBus"
|
|
|
|
, methodCallBody = params
|
|
|
|
}
|
2012-10-26 04:02:03 +00:00
|
|
|
|
|
|
|
{- Connects to the bus, and runs the client action.
|
|
|
|
-
|
|
|
|
- Throws a ClientError, and closes the connection if it fails to
|
|
|
|
- process an incoming message, or if the connection is lost.
|
|
|
|
- Unlike DBus's usual interface, this error is thrown at the top level,
|
|
|
|
- rather than inside the clientThreadRunner, so it can be caught, and
|
|
|
|
- runClient re-run as needed. -}
|
|
|
|
runClient :: IO (Maybe Address) -> (Client -> IO ()) -> IO ()
|
|
|
|
runClient getaddr clientaction = do
|
|
|
|
env <- getaddr
|
|
|
|
case env of
|
|
|
|
Nothing -> throwIO (clientError "runClient: unable to determine DBUS address")
|
|
|
|
Just addr -> do
|
|
|
|
{- The clientaction will set up listeners, which
|
|
|
|
- run in a different thread. We block while
|
|
|
|
- they're running, until our threadrunner catches
|
|
|
|
- a ClientError, which it will put into the MVar
|
|
|
|
- to be rethrown here. -}
|
|
|
|
mv <- newEmptyMVar
|
|
|
|
let tr = threadrunner (putMVar mv)
|
|
|
|
let opts = defaultClientOptions { clientThreadRunner = tr }
|
|
|
|
client <- connectWith opts addr
|
|
|
|
clientaction client
|
|
|
|
e <- takeMVar mv
|
|
|
|
disconnect client
|
|
|
|
throw e
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
threadrunner storeerr io = loop
|
|
|
|
where
|
|
|
|
loop = catchClientError (io >> loop) storeerr
|
2012-10-26 04:02:03 +00:00
|
|
|
|
|
|
|
{- Connects to the bus, and runs the client action.
|
|
|
|
-
|
|
|
|
- If the connection is lost, runs onretry, which can do something like
|
|
|
|
- a delay, or printing a warning, and has a state value (useful for
|
|
|
|
- exponential backoff). Once onretry returns, the connection is retried.
|
2012-10-26 23:38:27 +00:00
|
|
|
-}
|
2012-10-26 04:02:03 +00:00
|
|
|
persistentClient :: IO (Maybe Address) -> v -> (SomeException -> v -> IO v) -> (Client -> IO ()) -> IO ()
|
2012-10-26 23:38:27 +00:00
|
|
|
persistentClient getaddr v onretry clientaction =
|
2012-10-26 04:02:03 +00:00
|
|
|
{- runClient can fail with not just ClientError, but also other
|
2012-10-26 23:38:27 +00:00
|
|
|
- things, if dbus is not running. Let async exceptions through. -}
|
2012-10-27 04:42:14 +00:00
|
|
|
runClient getaddr clientaction `catchNonAsync` retry
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
retry e = do
|
|
|
|
v' <- onretry e v
|
|
|
|
persistentClient getaddr v' onretry clientaction
|
2012-10-26 04:02:03 +00:00
|
|
|
|
|
|
|
{- Catches only ClientError -}
|
|
|
|
catchClientError :: IO () -> (ClientError -> IO ()) -> IO ()
|
2012-10-27 04:42:14 +00:00
|
|
|
catchClientError io handler =
|
2012-10-26 04:02:03 +00:00
|
|
|
either handler return =<< (E.try io :: IO (Either ClientError ()))
|