{-# LINE 1 "Network/Socket/Unix.hsc" #-}
{-# LANGUAGE CPP #-}
#include "HsNetDef.h"
module Network.Socket.Unix (
isUnixDomainSocketAvailable
, socketPair
, sendFd
, recvFd
, getPeerCredential
, getPeerCred
, getPeerEid
) where
import Network.Socket.Imports
import Network.Socket.Types
{-# LINE 22 "Network/Socket/Unix.hsc" #-}
{-# LINE 23 "Network/Socket/Unix.hsc" #-}
import Foreign.Marshal.Utils (with)
{-# LINE 25 "Network/Socket/Unix.hsc" #-}
{-# LINE 28 "Network/Socket/Unix.hsc" #-}
{-# LINE 29 "Network/Socket/Unix.hsc" #-}
import Control.Monad (void)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Marshal.Array (peekArray)
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable(..))
import Network.Socket.Fcntl
import Network.Socket.Internal
{-# LINE 38 "Network/Socket/Unix.hsc" #-}
{-# LINE 39 "Network/Socket/Unix.hsc" #-}
import Network.Socket.Options (c_getsockopt)
{-# LINE 41 "Network/Socket/Unix.hsc" #-}
getPeerCredential :: Socket -> IO (Maybe CUInt, Maybe CUInt, Maybe CUInt)
{-# LINE 56 "Network/Socket/Unix.hsc" #-}
getPeerCredential sock = do
(pid, uid, gid) <- getPeerCred sock
if uid == maxBound then
return (Nothing, Nothing, Nothing)
else
return (Just pid, Just uid, Just gid)
{-# LINE 72 "Network/Socket/Unix.hsc" #-}
getPeerCred :: Socket -> IO (CUInt, CUInt, CUInt)
{-# LINE 79 "Network/Socket/Unix.hsc" #-}
getPeerCred s = do
let sz = (12)
{-# LINE 81 "Network/Socket/Unix.hsc" #-}
withFdSocket s $ \fd -> allocaBytes sz $ \ ptr_cr ->
with (fromIntegral sz) $ \ ptr_sz -> do
_ <- ($) throwSocketErrorIfMinus1Retry "Network.Socket.getPeerCred" $
c_getsockopt fd (1) (17) ptr_cr ptr_sz
{-# LINE 85 "Network/Socket/Unix.hsc" #-}
pid <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr_cr
{-# LINE 86 "Network/Socket/Unix.hsc" #-}
uid <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) ptr_cr
{-# LINE 87 "Network/Socket/Unix.hsc" #-}
gid <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr_cr
{-# LINE 88 "Network/Socket/Unix.hsc" #-}
return (pid, uid, gid)
{-# LINE 92 "Network/Socket/Unix.hsc" #-}
{-# Deprecated getPeerCred "Use getPeerCredential instead" #-}
getPeerEid :: Socket -> IO (CUInt, CUInt)
{-# LINE 113 "Network/Socket/Unix.hsc" #-}
getPeerEid _ = return (0, 0)
{-# LINE 115 "Network/Socket/Unix.hsc" #-}
{-# Deprecated getPeerEid "Use getPeerCredential instead" #-}
isUnixDomainSocketAvailable :: Bool
{-# LINE 123 "Network/Socket/Unix.hsc" #-}
isUnixDomainSocketAvailable = True
{-# LINE 127 "Network/Socket/Unix.hsc" #-}
sendFd :: Socket -> CInt -> IO ()
{-# LINE 133 "Network/Socket/Unix.hsc" #-}
sendFd s outfd = void $ do
withFdSocket s $ \fd ->
throwSocketErrorWaitWrite s "Network.Socket.sendFd" $ c_sendFd fd outfd
foreign import ccall SAFE_ON_WIN "sendFd" c_sendFd :: CInt -> CInt -> IO CInt
{-# LINE 140 "Network/Socket/Unix.hsc" #-}
recvFd :: Socket -> IO CInt
{-# LINE 148 "Network/Socket/Unix.hsc" #-}
recvFd s = do
withFdSocket s $ \fd ->
throwSocketErrorWaitRead s "Network.Socket.recvFd" $ c_recvFd fd
foreign import ccall SAFE_ON_WIN "recvFd" c_recvFd :: CInt -> IO CInt
{-# LINE 155 "Network/Socket/Unix.hsc" #-}
socketPair :: Family
-> SocketType
-> ProtocolNumber
-> IO (Socket, Socket)
{-# LINE 165 "Network/Socket/Unix.hsc" #-}
socketPair family stype protocol =
allocaBytes (2 * sizeOf (1 :: CInt)) $ \ fdArr -> do
c_stype <- packSocketTypeOrThrow "socketPair" stype
_rc <- throwSocketErrorIfMinus1Retry "Network.Socket.socketpair" $
c_socketpair (packFamily family) c_stype protocol fdArr
[fd1,fd2] <- peekArray 2 fdArr
setNonBlockIfNeeded fd1
setNonBlockIfNeeded fd2
s1 <- mkSocket fd1
s2 <- mkSocket fd2
return (s1, s2)
foreign import ccall unsafe "socketpair"
c_socketpair :: CInt -> CInt -> CInt -> Ptr CInt -> IO CInt
{-# LINE 182 "Network/Socket/Unix.hsc" #-}