module System.Proc.Bindings.Tab.Config
  ( TableConfig (..)

    -- * Table filters
  , Filter (..)

    -- * Binary flags
  , Flags
  , flagsAsInt

    -- ** Constructing flags
  , noFlags
  , fillMem
  , fillCom
  , fillEnv
  , fillUser
  , fillGroup
  , fillStatus
  , fillStat
  , fillArg
  , fillCGroup
  , fillSupGroup
  , fillOOM
  , fillNS
  , fillSystemd
  , fillLxc
  , fillLooseTasks
  , editCGroupAsSingleVector
  , editCmdlineAsSingleVector
  , editEnvironAsSingleVector

    -- ** Accessors
  , flagMem
  , flagCom
  , flagEnv
  , flagUser
  , flagGroup
  , flagStatus
  , flagStat
  , flagArg
  , flagCGroup
  , flagSupGroup
  , flagOOM
  , flagNS
  , flagSystemd
  , flagLxc
  , flagLooseTasks
  , flagEditCGroupSingleVector
  , flagEditCmdlineSingleVector
  , flagEditEnvironSingleVector

    -- * Util
  , branchTableConfig
  , openTableConfig
  )
where

import Control.Exception
import Foreign
import Foreign.C.Types
import GHC.Stack
import System.Posix.Types
import System.Proc.Bindings.C.Utils
import System.Proc.Bindings.Flags.C

{- | This type represents our preference of what processes we want information about,
and what information we want about them. It'll be translated into a list of arguments
to be passed to @openproc@ ('System.Proc.Bindings.Tab.openProcTab') or
@readproctab@ ('System.Proc.Bindings.openAllProcs').
-}
data TableConfig = TableConfig
  { TableConfig -> Flags
binaryFlags :: Flags
  , TableConfig -> Filter
tableFilter :: Filter
  }
  deriving (Int -> TableConfig -> ShowS
[TableConfig] -> ShowS
TableConfig -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TableConfig] -> ShowS
$cshowList :: [TableConfig] -> ShowS
show :: TableConfig -> String
$cshow :: TableConfig -> String
showsPrec :: Int -> TableConfig -> ShowS
$cshowsPrec :: Int -> TableConfig -> ShowS
Show, TableConfig -> TableConfig -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TableConfig -> TableConfig -> Bool
$c/= :: TableConfig -> TableConfig -> Bool
== :: TableConfig -> TableConfig -> Bool
$c== :: TableConfig -> TableConfig -> Bool
Eq)

{- | Case analysis on 'TableConfig'. The three continuation arguments correspond to the three
constructors of 'Filter'.

Handles memory management and makes sure we pass the correct argument to
@openproc@ or @readproctab@.
-}
branchTableConfig ::
  HasCallStack =>
  (CInt -> IO a) ->
  (CInt -> Ptr CPid -> IO a) ->
  (CInt -> Ptr CUid -> CInt -> IO a) ->
  (TableConfig -> IO a)
branchTableConfig :: forall a.
HasCallStack =>
(CInt -> IO a)
-> (CInt -> Ptr CPid -> IO a)
-> (CInt -> Ptr CUid -> CInt -> IO a)
-> TableConfig
-> IO a
branchTableConfig CInt -> IO a
simple CInt -> Ptr CPid -> IO a
onPids CInt -> Ptr CUid -> CInt -> IO a
onUids (TableConfig Flags
flags Filter
tabFilter) = do
  HasCallStack => String -> IO ()
xprintf String
"branchTableConfig"
  case Filter
tabFilter of
    Filter
NoFilter -> CInt -> IO a
simple CInt
flagsInt
    ByPids [CPid]
pids ->
      forall a b. Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
withArray0 CPid
0 [CPid]
pids forall a b. (a -> b) -> a -> b
$ \Ptr CPid
pidPtr -> do
        HasCallStack => String -> IO ()
xprintf forall a b. (a -> b) -> a -> b
$ String
"PID pointer: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Ptr CPid
pidPtr
        CInt -> Ptr CPid -> IO a
onPids CInt
flagsInt Ptr CPid
pidPtr
    ByUids [CUid]
uids ->
      forall a b. Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
withArrayLen [CUid]
uids forall a b. (a -> b) -> a -> b
$ \Int
nuid Ptr CUid
uidPtr -> do
        HasCallStack => String -> IO ()
xprintf forall a b. (a -> b) -> a -> b
$ String
"UID pointer: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Ptr CUid
uidPtr
        CInt -> Ptr CUid -> CInt -> IO a
onUids CInt
flagsInt Ptr CUid
uidPtr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nuid)
  where
    flagsInt :: CInt
flagsInt = Flags -> CInt
flagsAsInt Flags
flags

{- | Same as 'branchTableConfig', but without the bracketing.

The @IO ()@ return value is a cleanup function to free any memory that was allocated for
@openproc@, e.g an array of @uid_t@s or @pid_t@s.
-}
openTableConfig ::
  (CInt -> IO a) ->
  (CInt -> Ptr CPid -> IO a) ->
  (CInt -> Ptr CUid -> CInt -> IO a) ->
  (TableConfig -> IO (a, IO ()))
openTableConfig :: forall a.
(CInt -> IO a)
-> (CInt -> Ptr CPid -> IO a)
-> (CInt -> Ptr CUid -> CInt -> IO a)
-> TableConfig
-> IO (a, IO ())
openTableConfig CInt -> IO a
simple CInt -> Ptr CPid -> IO a
onPids CInt -> Ptr CUid -> CInt -> IO a
onUids (TableConfig Flags
flags Filter
tabFilter) = do
  case Filter
tabFilter of
    Filter
NoFilter -> do
      a
a <- CInt -> IO a
simple CInt
flagsInt
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
    ByPids [CPid]
pids -> do
      Ptr CPid
pidPtr <- forall a. Storable a => a -> [a] -> IO (Ptr a)
newArray0 CPid
0 [CPid]
pids
      a
a <- CInt -> Ptr CPid -> IO a
onPids CInt
flagsInt Ptr CPid
pidPtr forall a b. IO a -> IO b -> IO a
`onException` forall a. Ptr a -> IO ()
free Ptr CPid
pidPtr
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, forall a. Ptr a -> IO ()
free Ptr CPid
pidPtr)
    ByUids [CUid]
uids -> do
      Ptr CUid
uidPtr <- forall a. Storable a => [a] -> IO (Ptr a)
newArray [CUid]
uids
      let nuid :: CInt
nuid = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [CUid]
uids
      a
a <- CInt -> Ptr CUid -> CInt -> IO a
onUids CInt
flagsInt Ptr CUid
uidPtr CInt
nuid forall a b. IO a -> IO b -> IO a
`onException` forall a. Ptr a -> IO ()
free Ptr CUid
uidPtr
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, forall a. Ptr a -> IO ()
free Ptr CUid
uidPtr)
  where
    flagsInt :: CInt
flagsInt = Flags -> CInt
flagsAsInt Flags
flags

------------------------------------------------------------
-- Table filters

{- | Which processes do we want to read information about?

This will determine which extra arguments we pass to @openproc@ or @readproctab@.
-}
data Filter
  = -- | Read all processes
    NoFilter
  | -- | Filter process by user IDs
    ByUids [CUid]
  | -- | Filter process by process IDs
    ByPids [CPid]
  deriving (Int -> Filter -> ShowS
[Filter] -> ShowS
Filter -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Filter] -> ShowS
$cshowList :: [Filter] -> ShowS
show :: Filter -> String
$cshow :: Filter -> String
showsPrec :: Int -> Filter -> ShowS
$cshowsPrec :: Int -> Filter -> ShowS
Show, Filter -> Filter -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Filter -> Filter -> Bool
$c/= :: Filter -> Filter -> Bool
== :: Filter -> Filter -> Bool
$c== :: Filter -> Filter -> Bool
Eq)

------------------------------------------------------------
-- Binary flags

-- | Only read 'System.Proc.Bindings.taskId's.
noFlags :: Flags
noFlags :: Flags
noFlags = forall a. Monoid a => a
mempty

-- | Read @\/proc\/#\/statm@.
fillMem :: Flags
fillMem :: Flags
fillMem = Flags
noFlags {flagMem :: Bool
flagMem = Bool
True}

-- | Read @\/proc\/#\/cmdline@.
fillCom :: Flags
fillCom :: Flags
fillCom = Flags
noFlags {flagCom :: Bool
flagCom = Bool
True}

-- | Read @\/proc\/#\/environ@.
fillEnv :: Flags
fillEnv :: Flags
fillEnv = Flags
noFlags {flagEnv :: Bool
flagEnv = Bool
True}

-- | Resolve user id number -> user name
fillUser :: Flags
fillUser :: Flags
fillUser = Flags
noFlags {flagUser :: Bool
flagUser = Bool
True}

-- | Resolve group id number -> group name
fillGroup :: Flags
fillGroup :: Flags
fillGroup = Flags
noFlags {flagGroup :: Bool
flagGroup = Bool
True}

-- | Read @\/proc\/#\/status@.
fillStatus :: Flags
fillStatus :: Flags
fillStatus = Flags
noFlags {flagStatus :: Bool
flagStatus = Bool
True}

-- | Read @\/proc\/#\/stat@.
fillStat :: Flags
fillStat :: Flags
fillStat = Flags
noFlags {flagStat :: Bool
flagStat = Bool
True}

-- | Read @\/proc\/#\/cmdline@. Seems to be treated identically to 'fillCom'.
fillArg :: Flags
fillArg :: Flags
fillArg = Flags
noFlags {flagArg :: Bool
flagArg = Bool
True}

-- | Read @\/proc\/#\/cgroup@.
fillCGroup :: Flags
fillCGroup :: Flags
fillCGroup = Flags
noFlags {flagCGroup :: Bool
flagCGroup = Bool
True}

-- | Resolve supplementary group id -> group name
fillSupGroup :: Flags
fillSupGroup :: Flags
fillSupGroup = Flags
noFlags {flagSupGroup :: Bool
flagSupGroup = Bool
True}

-- | Read @\/proc\/#\/oom_{score,adj}@.
fillOOM :: Flags
fillOOM :: Flags
fillOOM = Flags
noFlags {flagOOM :: Bool
flagOOM = Bool
True}

-- | Read @\/proc\/#\/ns/@.
fillNS :: Flags
fillNS :: Flags
fillNS = Flags
noFlags {flagNS :: Bool
flagNS = Bool
True}

-- | Read systemd information.
fillSystemd :: Flags
fillSystemd :: Flags
fillSystemd = Flags
noFlags {flagSystemd :: Bool
flagSystemd = Bool
True}

-- | Read LXC name, if possible.
fillLxc :: Flags
fillLxc :: Flags
fillLxc = Flags
noFlags {flagLxc :: Bool
flagLxc = Bool
True}

-- | Treat threads as if they were processes.
fillLooseTasks :: Flags
fillLooseTasks :: Flags
fillLooseTasks = Flags
noFlags {flagLooseTasks :: Bool
flagLooseTasks = Bool
True}

-- | Edit @cgroup@ as a single vector.
editCGroupAsSingleVector :: Flags
editCGroupAsSingleVector :: Flags
editCGroupAsSingleVector = Flags
noFlags {flagEditCGroupSingleVector :: Bool
flagEditCGroupSingleVector = Bool
True}

-- | Edit @cmdline@ as a single vector.
editCmdlineAsSingleVector :: Flags
editCmdlineAsSingleVector :: Flags
editCmdlineAsSingleVector = Flags
noFlags {flagEditCmdlineSingleVector :: Bool
flagEditCmdlineSingleVector = Bool
True}

-- | Edit @environ@ as a single vector.
editEnvironAsSingleVector :: Flags
editEnvironAsSingleVector :: Flags
editEnvironAsSingleVector = Flags
noFlags {flagEditEnvironSingleVector :: Bool
flagEditEnvironSingleVector = Bool
True}