module System.Linux.KMod
(
Context
, new
, loadResources
, unloadResources
, Resources(..)
, validateResources
, Index(..)
, dumpIndex
, setLogPriority
, getLogPriority
, Key
, Value
, configGetBlacklists
, configGetInstallCommands
, configGetRemoveCommands
, configGetAliases
, configGetOptions
, configGetSoftdeps
, Module
, moduleNewFromLookup
, Filter(..)
, moduleNewFromLookupWithFilter
, moduleNewFromName
, moduleNewFromPath
, Options
, InsertFlags(..)
, moduleInsertModule
, ProbeFlags(..)
, Blacklist(..)
, RunInstall
, PrintAction
, BlacklistError(..)
, moduleProbeInsertModule
, RemoveFlags(..)
, moduleRemoveModule
, moduleGetInstallCommands
, moduleGetRemoveCommands
, moduleGetOptions
, moduleGetPath
, moduleGetDependencies
, moduleGetSoftdeps
, Symbol
, Bind(..)
, CRC
, moduleGetDependencySymbols
, Name
, Address
, moduleGetSections
, moduleGetSymbols
, moduleGetVersions
, moduleGetInfo
, moduleNewFromLoaded
, Initstate(..)
, moduleGetInitstate
, moduleGetSize
, moduleGetRefcnt
, moduleGetHolders
) where
import Control.Exception (Exception, bracket, throw)
import Data.Typeable
import Foreign hiding (new)
import Foreign.C
import System.Posix.Directory (closeDirStream, openDirStream, readDirStream)
import System.Posix.Types (Fd)
type Key = String
type Value = String
newtype Context = Context (ForeignPtr Context)
withContext :: Context -> (Ptr Context -> IO a) -> IO a
withContext (Context fptr) = withForeignPtr fptr
foreign import ccall "&kmod_unref"
p_unref :: FunPtr (Ptr Context -> IO (Ptr Context))
toContext :: Ptr Context -> IO Context
toContext ptr = newForeignPtr (castFunPtr p_unref) ptr >>= return . Context
foreign import ccall "kmod_new" kmod_new :: CString
-> Ptr CString
-> IO (Ptr Context)
new :: Maybe FilePath
-> Maybe [FilePath]
-> IO Context
new mdir mpaths = (throwIfNull "kmod_new returned NULL" $
maybeWith withCString mdir $ \d ->
maybeWith withCStringArray mpaths $ \p ->
kmod_new d p
) >>= toContext
withCStringArray :: [String] -> (Ptr CString -> IO a) -> IO a
withCStringArray xs f = bracket
(mapM newCString xs)
(mapM_ free)
(\ps -> withArray0 nullPtr ps f)
foreign import ccall "kmod_load_resources" load_resources :: Ptr Context
-> IO CInt
loadResources :: Context -> IO ()
loadResources ctx =
throwIf_ (/= 0) (\e -> "kmod_load_resources returned " ++ show e) $
withContext ctx load_resources
foreign import ccall "kmod_unload_resources" unload_resources :: Ptr Context
-> IO ()
unloadResources :: Context -> IO ()
unloadResources ctx = withContext ctx unload_resources
data Resources = Ok
| MustReload
| MustRecreate
deriving (Eq, Show)
toResources :: (Eq a, Num a, Show a) => a -> Resources
toResources 0 = Ok
toResources 1 = MustReload
toResources 2 = MustRecreate
toResources n = error ("System.KMod.toResources: bad argument: " ++ show n)
foreign import ccall "kmod_validate_resources"
validate_resources :: Ptr Context -> IO CInt
validateResources :: Context -> IO Resources
validateResources ctx = withContext ctx validate_resources >>=
return . toResources
data Index = ModulesDep
| ModulesAlias
| ModulesSymbol
| ModulesBuiltin
deriving (Eq, Show)
fromIndex :: Num a => Index -> a
fromIndex ModulesDep = 0
fromIndex ModulesAlias = 1
fromIndex ModulesSymbol = 2
fromIndex ModulesBuiltin = 3
foreign import ccall "kmod_dump_index"
dump_index :: Ptr Context -> CInt -> CInt -> IO CInt
dumpIndex :: Context -> Index -> Fd -> IO ()
dumpIndex ctx i fd =
withContext ctx $ \p ->
throwIf_ (/= 0) (\e -> "kmod_dump_index returned " ++ show e) $
dump_index p (fromIndex i) (fromIntegral fd)
foreign import ccall "kmod_set_log_priority"
set_log_priority :: Ptr Context -> CInt -> IO ()
setLogPriority :: Context -> Int -> IO ()
setLogPriority ctx i =
withContext ctx (\p -> set_log_priority p (fromIntegral i))
foreign import ccall "kmod_get_log_priority"
get_log_priority :: Ptr Context -> IO CInt
getLogPriority :: Context -> IO Int
getLogPriority ctx =
withContext ctx (\p -> get_log_priority p >>= return . fromIntegral)
data ConfigIter
foreign import ccall "kmod_config_iter_get_key"
config_iter_get_key :: Ptr ConfigIter -> IO CString
foreign import ccall "kmod_config_iter_get_value"
config_iter_get_value :: Ptr ConfigIter -> IO CString
foreign import ccall unsafe "kmod_config_iter_next"
config_iter_next :: Ptr ConfigIter -> IO Bool
foreign import ccall unsafe "kmod_config_iter_free_iter"
config_iter_free_iter :: Ptr ConfigIter -> IO ()
configIter2List :: (Ptr ConfigIter -> IO a)
-> Ptr ConfigIter
-> IO [a]
configIter2List fget p = do
if p == nullPtr
then throwErrno "configIter2List"
else do b <- config_iter_next p
if b
then do x <- fget p
xs <- configIter2List fget p
return (x:xs)
else do config_iter_free_iter p
return []
configIter2KeyValueList :: Ptr ConfigIter -> IO [(Key,Value)]
configIter2KeyValueList =
configIter2List $ \p -> do key <- config_iter_get_key p >>= peekCString
val <- config_iter_get_value p >>= peekCString
return (key,val)
configIter2StringList :: Ptr ConfigIter -> IO [String]
configIter2StringList = configIter2List
(\p -> do str <- config_iter_get_key p >>= peekCString
return str)
withContextAndWith :: (Ptr Context -> IO a) -> (a -> IO b) -> Context -> IO b
withContextAndWith f g m = withContext m f >>= g
foreign import ccall "kmod_config_get_blacklists"
config_get_blacklists :: Ptr Context -> IO (Ptr ConfigIter)
configGetBlacklists :: Context -> IO [String]
configGetBlacklists =
withContextAndWith config_get_blacklists configIter2StringList
foreign import ccall "kmod_config_get_install_commands"
config_get_install_commands :: Ptr Context -> IO (Ptr ConfigIter)
configGetInstallCommands :: Context -> IO [(Key,Value)]
configGetInstallCommands =
withContextAndWith config_get_install_commands configIter2KeyValueList
foreign import ccall "kmod_config_get_remove_commands"
config_get_remove_commands :: Ptr Context -> IO (Ptr ConfigIter)
configGetRemoveCommands :: Context -> IO [(Key,Value)]
configGetRemoveCommands =
withContextAndWith config_get_remove_commands configIter2KeyValueList
foreign import ccall "kmod_config_get_aliases"
config_get_aliases :: Ptr Context -> IO (Ptr ConfigIter)
configGetAliases :: Context -> IO [(Key,Value)]
configGetAliases = withContextAndWith config_get_aliases configIter2KeyValueList
foreign import ccall "kmod_config_get_options"
config_get_options :: Ptr Context -> IO (Ptr ConfigIter)
configGetOptions :: Context -> IO [(Key,Value)]
configGetOptions = withContextAndWith config_get_options configIter2KeyValueList
foreign import ccall "kmod_config_get_softdeps"
config_get_softdeps :: Ptr Context -> IO (Ptr ConfigIter)
configGetSoftdeps :: Context -> IO [(Key,Value)]
configGetSoftdeps =
withContextAndWith config_get_softdeps configIter2KeyValueList
data Module = Module (ForeignPtr Module) String
withModule :: Module -> (Ptr Module -> IO a) -> IO a
withModule (Module fptr _) = withForeignPtr fptr
foreign import ccall "&kmod_module_unref"
p_module_unref :: FunPtr (Ptr Module -> IO (Ptr Module))
foreign import ccall "kmod_module_get_name"
module_get_name :: Ptr Module -> IO CString
toModule :: Ptr Module -> IO Module
toModule ptr = do fptr <- newForeignPtr (castFunPtr p_module_unref) ptr
name <- module_get_name ptr >>= peekCString
return $ Module fptr name
instance Show Module where
show (Module _ name) = name
foreign import ccall "kmod_module_new_from_lookup"
module_new_from_lookup :: Ptr Context -> CString -> Ptr (Ptr List) -> IO CInt
fromLookup :: Context -> String -> IO (Ptr List)
fromLookup ctx str =
withContext ctx $ \p ->
withCString str $ \q ->
alloca $ \r ->
do poke r nullPtr
e <- module_new_from_lookup p q r
if e < 0
then fail ("kmod_module_new_from_lookup returned " ++ show e)
else peek r
moduleNewFromLookup :: Context -> String -> IO [Module]
moduleNewFromLookup ctx str = fromLookup ctx str >>= toModuleList
moduleNewFromLookupWithFilter :: Context -> String -> Filter -> IO [Module]
moduleNewFromLookupWithFilter ctx str fil = fromLookup ctx str >>=
toModuleListWithFilter ctx fil
foreign import ccall "kmod_module_new_from_name"
module_new_from_name :: Ptr Context -> CString -> Ptr (Ptr Module) -> IO CInt
foreign import ccall "kmod_module_new_from_path"
module_new_from_path :: Ptr Context -> CString -> Ptr (Ptr Module) -> IO CInt
moduleNewFrom :: (Ptr Context -> CString -> Ptr (Ptr Module) -> IO CInt)
-> String
-> Context -> String -> IO Module
moduleNewFrom fun err ctx str =
withContext ctx $ \p ->
withCString str $ \q ->
alloca $ \r ->
do e <- fun p q r
if e == 0
then peek r >>= toModule
else fail (err ++ " returned " ++ show e)
moduleNewFromName :: Context -> String -> IO Module
moduleNewFromName =
moduleNewFrom module_new_from_name "kmod_module_new_from_name"
moduleNewFromPath :: Context -> String -> IO Module
moduleNewFromPath =
moduleNewFrom module_new_from_path "kmod_module_new_from_path"
fromBitField :: (Bits b, Num b) => [(a -> Bool, b)] -> a -> b
fromBitField [] _ = 0
fromBitField (x:xs) a = (if fst x a then snd x else 0) .|. fromBitField xs a
data InsertFlags = InsertFlags { insertForceVerMagic :: Bool
, insertForceModVersion :: Bool
} deriving (Eq, Show)
fromInsertFlags :: (Bits a, Num a) => InsertFlags -> a
fromInsertFlags =
fromBitField
[ (insertForceVerMagic, 1)
, (insertForceModVersion, 2)
]
foreign import ccall "kmod_module_insert_module"
module_insert_module :: Ptr Module -> CUInt -> CString -> IO CInt
type Options = String
moduleInsertModule :: Module -> InsertFlags -> Options -> IO ()
moduleInsertModule m i o =
withModule m $ \p ->
withCString o $ \q ->
throwErrnoIf_ (/=0) "kmod_module_insert_module" $
module_insert_module p (fromInsertFlags i) q
data ProbeFlags = ProbeFlags { probeForceVerMagic :: Bool
, probeForceModVersion :: Bool
, probeIgnoreCommand :: Bool
, probeIgnoreLoaded :: Bool
, probeDryRun :: Bool
, probeFailOnLoaded :: Bool
} deriving (Eq, Show)
fromProbeFlags :: (Bits a, Num a) => ProbeFlags -> a
fromProbeFlags x =
fromBitField [ (probeForceVerMagic, 1)
, (probeForceModVersion, 2)
, (probeIgnoreCommand, 4)
, (probeIgnoreLoaded, 8)
, (probeDryRun, 16)
, (probeFailOnLoaded, 32)
] x
data Blacklist = BlacklistAll
| Blacklist
| BlacklistAliasOnly
| NoBlacklist
deriving (Eq, Show)
fromBlacklist :: Num a => Blacklist -> a
fromBlacklist BlacklistAll = 65536
fromBlacklist Blacklist = 131072
fromBlacklist BlacklistAliasOnly = 262144
fromBlacklist NoBlacklist = 0
foreign import ccall "kmod_module_probe_insert_module"
module_probe_insert_module :: Ptr Module
-> CUInt
-> CString
-> FunPtr CRunInstall
-> Ptr ()
-> FunPtr (CPrintAction)
-> IO CInt
type RunInstall = Module
-> String
-> IO ()
type CRunInstall = Ptr Module -> CString -> Ptr () -> IO CInt
foreign import ccall "wrapper" mkCRunInstallPtr :: CRunInstall
-> IO (FunPtr CRunInstall)
withRunInstall :: Maybe RunInstall -> (FunPtr CRunInstall -> IO a) -> IO a
withRunInstall Nothing f = f nullFunPtr
withRunInstall (Just ri) f = bracket
(mkCRunInstallPtr toCRunInstall)
freeHaskellFunPtr
f
where toCRunInstall p cs _ =
do m <- module_ref p >>= toModule
s <- peekCString cs
ri m s
return 0
data BlacklistError = BlacklistError
deriving (Eq, Show, Typeable)
instance Exception BlacklistError
moduleProbeInsertModule :: Module
-> ProbeFlags
-> Blacklist
-> Maybe Options
-> Maybe RunInstall
-> Maybe PrintAction
-> IO ()
moduleProbeInsertModule m flags bl opts ri pa =
withModule m $ \p ->
maybeWith withCString opts $ \cs ->
withRunInstall ri $ \fptr1 ->
withPrintAction pa $ \fptr2 ->
do e <- module_probe_insert_module p
(fromProbeFlags flags .|. fromBlacklist bl)
cs fptr1 nullPtr fptr2
if e == 0
then return ()
else if e < 0
then throwErrno "kmod_module_probe_insert_module"
else throw BlacklistError
type PrintAction = Module
-> Bool
-> Options
-> IO ()
type CBool = Word8
type CPrintAction = Ptr Module -> CBool -> CString -> IO ()
foreign import ccall "wrapper" mkCPrintActionPtr :: CPrintAction
-> IO (FunPtr CPrintAction)
withPrintAction :: Maybe PrintAction -> (FunPtr CPrintAction -> IO a) -> IO a
withPrintAction Nothing f = f nullFunPtr
withPrintAction (Just pa) f = bracket (mkCPrintActionPtr toCPrintAction)
freeHaskellFunPtr
f
where toCPrintAction p b cs =
do m <- module_ref p >>= toModule
s <- peekCString cs
pa m (toBool b) s
foreign import ccall "kmod_module_ref" module_ref :: Ptr Module
-> IO (Ptr Module)
data RemoveFlags = RemoveFlags { removeForce :: Bool
, removeNowait :: Bool
} deriving (Eq, Show)
fromRemoveFlags :: (Bits a, Num a) => RemoveFlags -> a
fromRemoveFlags = fromBitField [ (removeForce, 512)
, (removeNowait, 2048)
]
foreign import ccall "kmod_module_remove_module"
module_remove_module :: Ptr Module -> CUInt -> IO CInt
moduleRemoveModule :: Module -> RemoveFlags -> IO ()
moduleRemoveModule m r =
withModule m $ \p ->
throwErrnoIf_ (/=0) "kmod_module_remove_module" $
module_remove_module p (fromRemoveFlags r)
withModuleAndWith :: (Ptr Module -> IO a) -> (a -> IO b) -> Module -> IO b
withModuleAndWith f g m = withModule m f >>= g
foreign import ccall "kmod_module_get_install_commands"
module_get_install_commands :: Ptr Module -> IO CString
moduleGetInstallCommands :: Module -> IO (Maybe String)
moduleGetInstallCommands =
withModuleAndWith module_get_install_commands (maybePeek peekCString)
foreign import ccall "kmod_module_get_remove_commands"
module_get_remove_commands :: Ptr Module -> IO CString
moduleGetRemoveCommands :: Module -> IO (Maybe String)
moduleGetRemoveCommands =
withModuleAndWith module_get_remove_commands (maybePeek peekCString)
foreign import ccall "kmod_module_get_options"
module_get_options :: Ptr Module -> IO CString
moduleGetOptions :: Module -> IO (Maybe String)
moduleGetOptions = withModuleAndWith module_get_options (maybePeek peekCString)
foreign import ccall "kmod_module_get_path"
module_get_path :: Ptr Module -> IO CString
moduleGetPath :: Module -> IO (Maybe String)
moduleGetPath = withModuleAndWith module_get_path (maybePeek peekCString)
data List
foreign import ccall unsafe "kmod_list_last" list_last :: Ptr List
-> IO (Ptr List)
foreign import ccall unsafe "kmod_list_next" list_next :: Ptr List
-> Ptr List
-> IO (Ptr List)
toList :: (Ptr List -> IO a)
-> (Ptr List -> IO ())
-> Ptr List
-> IO [a]
toList get fr p = do l <- list_last p
if l == nullPtr
then do fr p
return []
else genList p
where genList q = do x <- get q
r <- list_next p q
if r == nullPtr
then do fr p
return [x]
else do xs <- genList r
return (x:xs)
foreign import ccall unsafe "kmod_module_get_module"
module_get_module :: Ptr List
-> IO (Ptr Module)
foreign import ccall unsafe "kmod_module_unref_list"
module_unref_list :: Ptr List -> IO CInt
toModuleList :: Ptr List -> IO [Module]
toModuleList = toList
(\p -> module_get_module p >>= toModule)
(\p -> throwErrnoIf_ (/= 0)
"kmod_module_unref_list returned non-zero exit status"
(module_unref_list p)
)
foreign import ccall "kmod_module_apply_filter"
module_apply_filter :: Ptr Context
-> CInt
-> Ptr List
-> Ptr (Ptr List)
-> IO CInt
data Filter = Filter { filterBlacklist :: Bool
, filterBuiltin :: Bool
} deriving (Eq, Show)
fromFilter :: (Bits a, Num a) => Filter -> a
fromFilter = fromBitField [ (filterBlacklist, 1)
, (filterBuiltin, 2)
]
toModuleListWithFilter :: Context -> Filter -> Ptr List -> IO [Module]
toModuleListWithFilter c f l =
withContext c $ \p ->
alloca $ \q ->
do e <- module_apply_filter p (fromFilter f) l q
if e >= 0
then peek q >>= toModuleList
else fail ("kmod_module_apply_filter returned " ++ show e)
foreign import ccall "kmod_module_get_dependencies"
module_get_dependencies :: Ptr Module -> IO (Ptr List)
moduleGetDependencies :: Module -> IO [Module]
moduleGetDependencies m =
withModule m (\p -> module_get_dependencies p >>= toModuleList)
foreign import ccall "kmod_module_get_softdeps"
module_get_softdeps :: Ptr Module
-> Ptr (Ptr List)
-> Ptr (Ptr List)
-> IO CInt
moduleGetSoftdeps :: Module -> IO ([Module], [Module])
moduleGetSoftdeps m =
withModule m $ \p ->
alloca $ \pre ->
do poke pre nullPtr
alloca $ \post ->
do poke post nullPtr
e <- module_get_softdeps p pre post
if e >= 0
then do l1 <- peek pre >>= toModuleList
l2 <- peek post >>= toModuleList
return (l1,l2)
else fail ("kmod_module_get_softdeps returned " ++
show e)
data Bind = None
| Local
| Global
| Weak
| Undefined
deriving (Eq, Show)
toBind :: (Eq a, Num a, Show a) => a -> Bind
toBind 0 = None
toBind 76 = Local
toBind 71 = Global
toBind 87 = Weak
toBind 85 = Undefined
toBind n = error ("System.KMod.toBind: bad argument: " ++ show n)
type CRC = Word64
type Symbol = String
module_dependency_symbol_get :: Ptr List -> IO (Symbol,Bind,CRC)
module_dependency_symbol_get p =
do s <- module_dependency_symbol_get_symbol p >>= peekCString
b <- module_dependency_symbol_get_bind p
c <- module_dependency_symbol_get_crc p
return (s,toBind b,c)
foreign import ccall "kmod_module_dependency_symbol_get_bind"
module_dependency_symbol_get_bind :: Ptr List -> IO CInt
foreign import ccall "kmod_module_dependency_symbol_get_crc"
module_dependency_symbol_get_crc :: Ptr List -> IO Word64
foreign import ccall "kmod_module_dependency_symbol_get_symbol"
module_dependency_symbol_get_symbol :: Ptr List -> IO CString
foreign import ccall "kmod_module_dependency_symbols_free_list"
module_dependency_symbols_free_list :: Ptr List -> IO ()
toDependencySymbolList :: Ptr List -> IO [(Symbol,Bind,CRC)]
toDependencySymbolList = toList
module_dependency_symbol_get
module_dependency_symbols_free_list
foreign import ccall "kmod_module_get_dependency_symbols"
module_get_dependency_symbols :: Ptr Module
-> Ptr (Ptr List)
-> IO CInt
moduleGet :: (Ptr Module -> Ptr (Ptr List) -> IO CInt)
-> (Ptr List -> IO a)
-> String
-> Module
-> IO a
moduleGet fget flist err m =
withModule m $ \p ->
alloca $ \q ->
do poke q nullPtr
e <- fget p q
if e < 0
then fail (err ++ " returned " ++ show e)
else peek q >>= flist
moduleGetDependencySymbols :: Module -> IO [(Symbol,Bind,CRC)]
moduleGetDependencySymbols =
moduleGet
module_get_dependency_symbols
toDependencySymbolList
"kmod_module_get_dependency_symbols"
type Name = String
type Address = Word64
moduleGetSections :: Module -> IO [(Name,Address)]
moduleGetSections m = do fs <- readDir secDir
mapM get (drop 2 fs)
where secDir = "/sys/module/" ++ show m ++ "/sections"
get f = do addr <- readFile (secDir ++ '/':f)
return (f, read addr)
readDir :: FilePath -> IO [FilePath]
readDir path = bracket (openDirStream path) closeDirStream getDir
where getDir d = do f <- readDirStream d
case f of
[] -> return []
_ -> do fs <- getDir d
return (f:fs)
foreign import ccall "kmod_module_symbol_get_crc"
module_symbol_get_crc :: Ptr List -> IO CRC
foreign import ccall "kmod_module_symbol_get_symbol"
module_symbol_get_symbol :: Ptr List -> IO CString
foreign import ccall "kmod_module_symbols_free_list"
module_symbols_free_list :: Ptr List -> IO ()
foreign import ccall "kmod_module_get_symbols"
module_get_symbols :: Ptr Module -> Ptr (Ptr List) -> IO CInt
moduleGetSymbols :: Module -> IO [(Symbol,CRC)]
moduleGetSymbols =
moduleGet module_get_symbols toSymbolList "kmod_module_get_symbols"
module_symbol_get :: Ptr List -> IO (Symbol,CRC)
module_symbol_get p = do s <- module_symbol_get_symbol p >>= peekCString
c <- module_symbol_get_crc p
return (s,c)
toSymbolList :: Ptr List -> IO [(Symbol,CRC)]
toSymbolList = toList
module_symbol_get
module_symbols_free_list
foreign import ccall "kmod_module_version_get_crc"
module_version_get_crc :: Ptr List -> IO CRC
foreign import ccall "kmod_module_version_get_symbol"
module_version_get_symbol :: Ptr List -> IO CString
foreign import ccall "kmod_module_versions_free_list"
module_versions_free_list :: Ptr List -> IO ()
foreign import ccall "kmod_module_get_versions"
module_get_versions :: Ptr Module -> Ptr (Ptr List) -> IO CInt
moduleGetVersions :: Module -> IO [(Symbol,CRC)]
moduleGetVersions =
moduleGet module_get_versions toVersionList "kmod_module_get_versions"
module_version_get :: Ptr List -> IO (Symbol,CRC)
module_version_get p = do s <- module_version_get_symbol p >>= peekCString
c <- module_version_get_crc p
return (s,c)
toVersionList :: Ptr List -> IO [(Symbol,CRC)]
toVersionList = toList
module_version_get
module_versions_free_list
foreign import ccall "kmod_module_info_get_key"
module_info_get_key :: Ptr List
-> IO CString
foreign import ccall "kmod_module_info_get_value"
module_info_get_value :: Ptr List
-> IO CString
foreign import ccall "kmod_module_get_info"
module_get_info :: Ptr Module
-> Ptr (Ptr List)
-> IO CInt
moduleGetInfo :: Module -> IO [(Key,Value)]
moduleGetInfo =
moduleGet module_get_info toInfoList "kmod_module_get_info"
module_info_get :: Ptr List -> IO (String, String)
module_info_get p = do k <- module_info_get_key p >>= peekCString
v <- module_info_get_value p >>= peekCString
return (k,v)
foreign import ccall "kmod_module_info_free_list"
module_info_free_list :: Ptr List
-> IO ()
toInfoList :: Ptr List -> IO [(String, String)]
toInfoList = toList
module_info_get
module_info_free_list
data Initstate = Builtin
| Live
| Coming
| Going
deriving (Eq, Show)
toInitstate :: (Eq a, Num a, Show a) => a -> Initstate
toInitstate 0 = Builtin
toInitstate 1 = Live
toInitstate 2 = Coming
toInitstate 3 = Going
toInitstate n = error ("System.KMod.toInitstate: bad argument: " ++ show n)
foreign import ccall "kmod_module_new_from_loaded"
module_new_from_loaded :: Ptr Context -> Ptr (Ptr List) -> IO CInt
moduleNewFromLoaded :: Context -> IO [Module]
moduleNewFromLoaded c =
withContext c $ \p ->
alloca $ \q ->
do throwIfNeg_ (\e -> "kmod_module_new_from_loaded retuned " ++
show e ) $ module_new_from_loaded p q
peek q >>= toModuleList
foreign import ccall "kmod_module_get_initstate"
module_get_initstate :: Ptr Module -> IO CInt
moduleGetInitstate :: Module -> IO Initstate
moduleGetInitstate m =
withModule m $ \p ->
(throwIfNeg (\e -> "kmod_module_get_initstate returned " ++ show e) $
module_get_initstate p)
>>= return . toInitstate
foreign import ccall "kmod_module_get_size"
module_get_size :: Ptr Module -> IO CInt
moduleGetSize :: Module -> IO Int
moduleGetSize m = withModule m module_get_size >>= return . fromIntegral
foreign import ccall "kmod_module_get_refcnt"
module_get_refcnt :: Ptr Module -> IO CInt
moduleGetRefcnt :: Module -> IO Int
moduleGetRefcnt m = withModule m module_get_refcnt >>= return . fromIntegral
foreign import ccall "kmod_module_get_holders"
module_get_holders :: Ptr Module -> IO (Ptr List)
moduleGetHolders :: Module -> IO [Module]
moduleGetHolders m = withModule m (\p -> module_get_holders p >>= toModuleList)