{-# LINE 1 "libraries/unix/System/Posix/DynamicLinker/Common.hsc" #-}
{-# LANGUAGE Safe #-}
module System.Posix.DynamicLinker.Common (
module System.Posix.DynamicLinker.Prim,
dlsym,
dlerror,
dlclose,
undl,
throwDLErrorIf,
Module(..)
)
where
import System.Posix.DynamicLinker.Prim
import Foreign
import Foreign.C
dlclose :: DL -> IO ()
dlclose :: DL -> IO ()
dlclose (DLHandle Ptr ()
h) = String -> (CInt -> Bool) -> IO CInt -> IO ()
forall a. String -> (a -> Bool) -> IO a -> IO ()
throwDLErrorIf_ String
"dlclose" (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr () -> IO CInt
c_dlclose Ptr ()
h
dlclose DL
h = String -> IO ()
forall a. HasCallStack => String -> a
error (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"dlclose: invalid argument" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (DL -> String
forall a. Show a => a -> String
show DL
h)
dlerror :: IO String
dlerror :: IO String
dlerror = IO CString
c_dlerror IO CString -> (CString -> IO String) -> IO String
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
dlsym :: DL -> String -> IO (FunPtr a)
dlsym :: forall a. DL -> String -> IO (FunPtr a)
dlsym DL
source String
symbol = do
String -> (CString -> IO (FunPtr a)) -> IO (FunPtr a)
forall a. String -> (CString -> IO a) -> IO a
withCAString String
symbol ((CString -> IO (FunPtr a)) -> IO (FunPtr a))
-> (CString -> IO (FunPtr a)) -> IO (FunPtr a)
forall a b. (a -> b) -> a -> b
$ \ CString
s -> do
String -> (FunPtr a -> Bool) -> IO (FunPtr a) -> IO (FunPtr a)
forall a. String -> (a -> Bool) -> IO a -> IO a
throwDLErrorIf String
"dlsym" (FunPtr a -> FunPtr a -> Bool
forall a. Eq a => a -> a -> Bool
== FunPtr a
forall a. FunPtr a
nullFunPtr) (IO (FunPtr a) -> IO (FunPtr a)) -> IO (FunPtr a) -> IO (FunPtr a)
forall a b. (a -> b) -> a -> b
$ Ptr () -> CString -> IO (FunPtr a)
forall a. Ptr () -> CString -> IO (FunPtr a)
c_dlsym (DL -> Ptr ()
packDL DL
source) CString
s
undl :: DL -> Ptr ()
undl :: DL -> Ptr ()
undl = DL -> Ptr ()
packDL
throwDLErrorIf :: String -> (a -> Bool) -> IO a -> IO a
throwDLErrorIf :: forall a. String -> (a -> Bool) -> IO a -> IO a
throwDLErrorIf String
s a -> Bool
p IO a
f = do
r <- IO a
f
if (p r)
then dlerror >>= \ String
err -> IOError -> IO a
forall a. IOError -> IO a
ioError (String -> IOError
userError ( String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err))
else return r
throwDLErrorIf_ :: String -> (a -> Bool) -> IO a -> IO ()
throwDLErrorIf_ :: forall a. String -> (a -> Bool) -> IO a -> IO ()
throwDLErrorIf_ String
s a -> Bool
p IO a
f = String -> (a -> Bool) -> IO a -> IO a
forall a. String -> (a -> Bool) -> IO a -> IO a
throwDLErrorIf String
s a -> Bool
p IO a
f IO a -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
newtype Module = Module (Ptr ())