forked from dmp1ce/list-all-systemd-timers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-all-systemd-timers.hs
executable file
·75 lines (64 loc) · 2.02 KB
/
list-all-systemd-timers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env stack
-- stack script --resolver lts-13.21
{-# OPTIONS_GHC -Werror -Wall #-}
{- Print all systemd timers, including system-wide and all user timers. -}
module Main where
import System.Process
import System.Posix.User
import System.Posix.Types
import System.Exit
type Username = String
data User = Root | User { username :: Username
, uid :: Int
}
type Command = String
main :: IO ()
main = do
uid' <- getRealUserID
if uid' == CUid 0
then listTimers
else do
putStrLn "Sorry, not root user.\n\
\Please use sudo or su to become \
\root user before running this script."
exitFailure
listTimers :: IO ()
listTimers = do
putStrLn "System timers:"
callCommand $ createListTimersCommandString Root
mapM_ (\x -> do
putStrLn $ "\nTimers for " ++ (username x) ++ ":"
callCommand $ createListTimersCommandString x
) =<< getDbusUserList
createListTimersCommandString :: User -> Command
createListTimersCommandString Root = "systemctl --no-pager list-timers"
createListTimersCommandString (User name uid') =
"su " ++ name
++ " -c 'XDG_RUNTIME_DIR=/run/user/" ++ (show uid')
++ " systemctl --no-pager --user list-timers'"
-- This is intended to get every 'User' who has an active dbus daemon running.
getDbusUserList :: IO [User]
getDbusUserList = do
uids <- uidsWithRunningDbusDaemon
mapM (\x -> do
e <- getUserEntryForID $ toEnum x
pure $ User (userName e) x
) uids
uidsWithRunningDbusDaemon :: IO [Int]
uidsWithRunningDbusDaemon = do
o <- readProcess
"systemctl"
["--no-legend", "--full", "list-units", "--state=active", "user@*.service"]
""
pure $ parseSystemctlOutputForUIDs o
parseSystemctlOutputForUIDs :: String -> [Int]
parseSystemctlOutputForUIDs s = (read . p) <$> lines s
where
p :: String -> String
p ('@':xs) = p' xs
p (_:xs) = p xs
p "" = ""
p' :: String -> String
p' ('.':_) = ""
p' (x:xs) = x:(p' xs)
p' "" = ""