module System.Proc.Bindings.Tab
  ( ProcTab
  , withProcTab
  , openProcTab
  , closeProcTab
  , ProcTabInfo (..)
  , getProcTabInfo
  , readProcTabInfo
  , module System.Proc.Bindings.Tab.Config
  )
where

import Control.Monad
import Foreign.C.Types
import Foreign.Ptr
import GHC.Stack
import System.Posix.Types
import System.Proc.Bindings.C
import System.Proc.Bindings.Error
import System.Proc.Bindings.Tab.C
import System.Proc.Bindings.Tab.Config
import System.Proc.Bindings.Tab.Internal

-- | Read information about a 'ProcTab'.
getProcTabInfo :: HasCallStack => ProcTab -> IO ProcTabInfo
getProcTabInfo :: HasCallStack => ProcTab -> IO ProcTabInfo
getProcTabInfo (UnsafeProcTab Ptr ProcTabC
ptr Ptr Proc
_) = HasCallStack => Ptr ProcTabC -> IO ProcTabInfo
fromProcTabC Ptr ProcTabC
ptr

-- | Bracketed access to a 'ProcTab'. The internal pointers will be freed after use.
withProcTab :: HasCallStack => TableConfig -> (ProcTab -> IO a) -> IO (Either ProcError a)
withProcTab :: forall a.
HasCallStack =>
TableConfig -> (ProcTab -> IO a) -> IO (Either ProcError a)
withProcTab TableConfig
cfg ProcTab -> IO a
f = do
  Ptr Proc
procPtr <- IO (Ptr Proc)
callocProc
  forall a.
HasCallStack =>
TableConfig -> (Ptr ProcTabC -> IO a) -> IO (Either ProcError a)
withProcTabPtr TableConfig
cfg forall a b. (a -> b) -> a -> b
$ \Ptr ProcTabC
procTabPtr ->
    ProcTab -> IO a
f forall a b. (a -> b) -> a -> b
$ Ptr ProcTabC -> Ptr Proc -> ProcTab
UnsafeProcTab Ptr ProcTabC
procTabPtr Ptr Proc
procPtr

{- | Open a 'ProcTab' according to a 'TableConfig'. It is the caller's responsibility
to free the internal 'Ptr's once they're done with it.

It's safer to use 'withProcTab', which will automatically clean up all the pointers.

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.
-}
openProcTab :: HasCallStack => TableConfig -> IO (Either ProcError ProcTab, IO ())
openProcTab :: HasCallStack => TableConfig -> IO (Either ProcError ProcTab, IO ())
openProcTab TableConfig
cfg = do
  (Ptr ProcTabC
procTabPtr, IO ()
closer) <- HasCallStack => TableConfig -> IO (Ptr ProcTabC, IO ())
openProcTabPtr TableConfig
cfg
  if Ptr ProcTabC
procTabPtr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr
    then forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. a -> Either a b
Left HasCallStack => ProcError
nullPtrError, IO ()
closer)
    else do
      Ptr Proc
procPtr <- IO (Ptr Proc)
callocProc
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Ptr ProcTabC -> Ptr Proc -> ProcTab
UnsafeProcTab Ptr ProcTabC
procTabPtr Ptr Proc
procPtr, IO ()
closer)

-- | Free the internal 'Ptr's of a 'ProcTab', if they're not null.
closeProcTab :: HasCallStack => ProcTab -> IO ()
closeProcTab :: HasCallStack => ProcTab -> IO ()
closeProcTab (UnsafeProcTab Ptr ProcTabC
procTabPtr Ptr Proc
procPtr) = do
  Ptr ProcTabC -> IO ()
closeProcTabC Ptr ProcTabC
procTabPtr
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Ptr Proc
procPtr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr) forall a b. (a -> b) -> a -> b
$ Ptr Proc -> IO ()
freeProc Ptr Proc
procPtr

{- | Information about a 'ProcTab'. Most of this information is just a copy
of the arguments passed to @openproc@ or @readproctab@.

This in an "escape hatch": while we have to be careful managing the pointers
underlying a 'ProcTab', a 'ProcTabInfo' is just a normal Haskell datatype without
any of that stuff going on, so you can do what you want with it.
-}
data ProcTabInfo = ProcTabInfo
  { ProcTabInfo -> Bool
didFake :: Bool
  , ProcTabInfo -> [CPid]
pids :: [CPid]
  , ProcTabInfo -> [CUid]
uids :: [CUid]
  , ProcTabInfo -> CUInt
flags :: CUInt
  , ProcTabInfo -> FilePath
path :: FilePath
  }
  deriving (Int -> ProcTabInfo -> ShowS
[ProcTabInfo] -> ShowS
ProcTabInfo -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [ProcTabInfo] -> ShowS
$cshowList :: [ProcTabInfo] -> ShowS
show :: ProcTabInfo -> FilePath
$cshow :: ProcTabInfo -> FilePath
showsPrec :: Int -> ProcTabInfo -> ShowS
$cshowsPrec :: Int -> ProcTabInfo -> ShowS
Show)

fromProcTabC :: HasCallStack => Ptr ProcTabC -> IO ProcTabInfo
fromProcTabC :: HasCallStack => Ptr ProcTabC -> IO ProcTabInfo
fromProcTabC Ptr ProcTabC
ptr = do
  Bool
didFake <- Ptr ProcTabC -> IO Bool
readProcTabDidFake Ptr ProcTabC
ptr
  [CPid]
pids <- Ptr ProcTabC -> IO [CPid]
readProcTabPids Ptr ProcTabC
ptr
  [CUid]
uids <- Ptr ProcTabC -> IO [CUid]
readProcTabUids Ptr ProcTabC
ptr
  CUInt
flags <- Ptr ProcTabC -> IO CUInt
readProcTabFlags Ptr ProcTabC
ptr
  FilePath
path <- Ptr ProcTabC -> IO FilePath
readProcTabPath Ptr ProcTabC
ptr
  forall (f :: * -> *) a. Applicative f => a -> f a
pure ProcTabInfo {Bool
FilePath
[CUid]
[CPid]
CUInt
path :: FilePath
flags :: CUInt
uids :: [CUid]
pids :: [CPid]
didFake :: Bool
path :: FilePath
flags :: CUInt
uids :: [CUid]
pids :: [CPid]
didFake :: Bool
..}

{- | For convenience: read 'ProcTabInfo' according to a 'TableConfig', skipping all the C stuff
in the middle.
-}
readProcTabInfo :: HasCallStack => TableConfig -> IO (Either ProcError ProcTabInfo)
readProcTabInfo :: HasCallStack => TableConfig -> IO (Either ProcError ProcTabInfo)
readProcTabInfo TableConfig
cfg =
  forall a.
HasCallStack =>
TableConfig -> (Ptr ProcTabC -> IO a) -> IO (Either ProcError a)
withProcTabPtr TableConfig
cfg HasCallStack => Ptr ProcTabC -> IO ProcTabInfo
fromProcTabC