{-# LINE 1 "Network/Socket/Unix.hsc" #-}{-# LANGUAGE CPP #-}{-# LANGUAGE ScopedTypeVariables #-} #include "HsNetDef.h" moduleNetwork.Socket.Unix(isUnixDomainSocketAvailable,socketPair,sendFd,recvFd ,getPeerCredential,getPeerCred,getPeerEid)whereimportForeign.Marshal.Alloc(allocaBytes)importNetwork.Socket.BufferimportNetwork.Socket.FcntlimportNetwork.Socket.ImportsimportNetwork.Socket.TypesimportSystem.Posix.Types(Fd(..)){-# LINE 31 "Network/Socket/Unix.hsc" #-}importForeign.Marshal.Array(peekArray)importNetwork.Socket.InternalimportNetwork.Socket.Posix.Cmsg{-# LINE 35 "Network/Socket/Unix.hsc" #-}{-# LINE 39 "Network/Socket/Unix.hsc" #-}{-# LINE 42 "Network/Socket/Unix.hsc" #-}{-# LINE 44 "Network/Socket/Unix.hsc" #-}importNetwork.Socket.Options{-# LINE 46 "Network/Socket/Unix.hsc" #-}-- | Getting process ID, user ID and group ID for UNIX-domain sockets.---- This is implemented with SO_PEERCRED on Linux and getpeereid()-- on BSD variants. Unfortunately, on some BSD variants-- getpeereid() returns unexpected results, rather than an error,-- for AF_INET sockets. It is the user's responsibility to make sure-- that the socket is a UNIX-domain socket.-- Also, on some BSD variants, getpeereid() does not return credentials-- for sockets created via 'socketPair', only separately created and then-- explicitly connected UNIX-domain sockets work on such systems.---- Since 2.7.0.0.getPeerCredential::Socket->IO(MaybeCUInt,MaybeCUInt,MaybeCUInt){-# LINE 61 "Network/Socket/Unix.hsc" #-}getPeerCredentialsock=do(pid,uid,gid)<-getPeerCredsockifuid==maxBoundthenreturn(Nothing,Nothing,Nothing)elsereturn(Justpid,Justuid,Justgid){-# LINE 77 "Network/Socket/Unix.hsc" #-}-- | Returns the processID, userID and groupID of the peer of-- a UNIX-domain socket.---- Only available on platforms that support SO_PEERCRED.getPeerCred::Socket->IO(CUInt,CUInt,CUInt){-# LINE 84 "Network/Socket/Unix.hsc" #-}getPeerCreds=doletopt=SockOpt(1)(17){-# LINE 86 "Network/Socket/Unix.hsc" #-}PeerCredcred<-getSockOptsoptreturncrednewtypePeerCred=PeerCred(CUInt,CUInt,CUInt)instanceStorablePeerCredwheresizeOf~_=(12){-# LINE 92 "Network/Socket/Unix.hsc" #-}alignment~_=alignment(0::CInt)poke__=return()peekp=dopid<-((\hsc_ptr->peekByteOffhsc_ptr0))p{-# LINE 96 "Network/Socket/Unix.hsc" #-}uid<-((\hsc_ptr->peekByteOffhsc_ptr4))p{-# LINE 97 "Network/Socket/Unix.hsc" #-}gid<-((\hsc_ptr->peekByteOffhsc_ptr8))p{-# LINE 98 "Network/Socket/Unix.hsc" #-}return$PeerCred(pid,uid,gid){-# LINE 102 "Network/Socket/Unix.hsc" #-}{-# DeprecatedgetPeerCred"Use getPeerCredential instead"#-}-- | Returns the userID and groupID of the peer of-- a UNIX-domain socket.---- Only available on platforms that support getpeereid().getPeerEid::Socket->IO(CUInt,CUInt){-# LINE 123 "Network/Socket/Unix.hsc" #-}getPeerEid_=return(0,0){-# LINE 125 "Network/Socket/Unix.hsc" #-}{-# DeprecatedgetPeerEid"Use getPeerCredential instead"#-}-- | Whether or not UNIX-domain sockets are available.-- 'AF_UNIX' is supported on Windows since 3.1.3.0.-- So, this variable is 'True` on all platforms.---- Since 2.7.0.0.isUnixDomainSocketAvailable::BoolisUnixDomainSocketAvailable=True-- | Send a file descriptor over a UNIX-domain socket.-- This function does not work on Windows.sendFd::Socket->CInt->IO()sendFdsoutfd=void$allocaBytesdummyBufSize$\buf->doletcmsg=encodeCmsg[Fdoutfd]sendBufMsgsNullSockAddr[(buf,dummyBufSize)][cmsg]memptywheredummyBufSize=1-- | Receive a file descriptor over a UNIX-domain socket. Note that the resulting-- file descriptor may have to be put into non-blocking mode in order to be-- used safely. See 'setNonBlockIfNeeded'.-- This function does not work on Windows.recvFd::Socket->IOCIntrecvFds=allocaBytesdummyBufSize$\buf->do(NullSockAddr,_,cmsgs,_)<-recvBufMsgs[(buf,dummyBufSize)]32memptycase(lookupCmsgCmsgIdFdscmsgs>>=decodeCmsg)::Maybe[Fd]ofJust(Fdfd:_)->returnfd_->return(-1)wheredummyBufSize=16-- | Build a pair of connected socket objects.-- On Windows, this function emulates socketpair() using-- 'AF_UNIX' and a temporary file will remain.socketPair::Family-- Family Name (usually AF_UNIX)->SocketType-- Socket Type (usually Stream)->ProtocolNumber-- Protocol Number->IO(Socket,Socket)-- unnamed and connected.{-# LINE 180 "Network/Socket/Unix.hsc" #-}socketPairfamilystypeprotocol=allocaBytes(2*sizeOf(1::CInt))$\fdArr->doletc_stype=packSocketTypestype_rc<-throwSocketErrorIfMinus1Retry"Network.Socket.socketpair"$c_socketpair(packFamilyfamily)c_stypeprotocolfdArr[fd1,fd2]<-peekArray2fdArrsetNonBlockIfNeededfd1setNonBlockIfNeededfd2s1<-mkSocketfd1s2<-mkSocketfd2return(s1,s2)foreignimportccallunsafe"socketpair"c_socketpair::CInt->CInt->CInt->PtrCInt->IOCInt{-# LINE 195 "Network/Socket/Unix.hsc" #-}