packages feed

nova-nix-0.11.0.0 / src / Nix / Daemon / Dial.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE ScopedTypeVariables #-}-- | The client's end of a daemon's address: a Unix socket on a POSIX-- system and a named pipe on Windows ("Nix.Daemon.Pipe"), which-- 'connectDaemon' connects to and 'connectionTransport' reads and writes-- as the t'Transport' a client session runs over ("Nix.Daemon.Client").-- On Windows the connection is the pipe client's, which refuses a pipe no-- trusted account made before it sends a byte.  'addressPresence' asks-- whether anything is at an address without connecting to it.module Nix.Daemon.Dial  ( DaemonConnection,    connectDaemon,    connectionTransport,    closeDaemonConnection,    connectFailureText,    sunPathSize,    AddressPresence (..),    addressPresence,  )whereimport Data.ByteString (ByteString)import Data.Text (Text)import Nix.Daemon.Connection (Transport (..))#if !defined(mingw32_HOST_OS)import Control.Exception (bracketOnError, try)import qualified Data.ByteString as BSimport qualified Data.ByteString.Builder as BBimport qualified Data.ByteString.Char8 as BS8import qualified Data.Text as Timport qualified Data.Text.Encoding as TEimport Foreign.C.Error (Errno (..), eNOENT, eNOTDIR)import GHC.IO.Exception (IOException (ioe_description, ioe_errno))import Network.Socket (Family (AF_UNIX), SockAddr (SockAddrUnix), Socket, SocketType (Stream), close, connect, defaultProtocol, socket)import Network.Socket.ByteString (recv)import qualified Network.Socket.ByteString.Lazy as SocketLazyimport System.Posix.Files.ByteString (getSymbolicLinkStatus)#endif#if defined(mingw32_HOST_OS)import qualified Nix.Daemon.Pipe as Pipe#endif-- | A connection to a daemon, which 'closeDaemonConnection' closes.#if defined(mingw32_HOST_OS)newtype DaemonConnection = DaemonConnection Pipe.PipeConnection#elsenewtype DaemonConnection = DaemonConnection Socket#endif-- | Connect to the daemon listening at an address, or say why it cannot-- be reached.  On a POSIX system the address is a socket's path and the-- reason is the operating system's, as @No such file or directory@ or-- @Connection refused@; on Windows it is a named pipe's path-- (@\\\\.\\pipe\\NAME@) and the reason is the pipe client's-- ("Nix.Daemon.Pipe").connectDaemon :: ByteString -> IO (Either Text DaemonConnection)#if defined(mingw32_HOST_OS)connectDaemon address = case Pipe.pipePath address of  Left reason -> pure (Left reason)  Right path -> fmap DaemonConnection <$> Pipe.connectPipe path#elseconnectDaemon path  | BS.length path >= sunPathSize = pure (Left "path is too long")  | otherwise = bracketOnError (socket AF_UNIX Stream defaultProtocol) close $ \sock ->      try (connect sock (SockAddrUnix (BS8.unpack path))) >>= \case        Left (err :: IOException) -> Left (T.pack (ioe_description err)) <$ close sock        Right () -> pure (Right (DaemonConnection sock))#endif-- | The size of a Unix socket address's path on this system, its-- terminating NUL included: 104 bytes on macOS, 108 elsewhere.  A path of-- this length or longer does not fit.  Only the POSIX connection and the-- daemon's listener use it.sunPathSize :: Int#if defined(darwin_HOST_OS)sunPathSize = 104#elsesunPathSize = 108#endif#if !defined(mingw32_HOST_OS)-- | How much a read from the daemon asks for.receiveChunkSize :: IntreceiveChunkSize = 65536#endif-- | A connection's transport.  A read of nothing is the end of the-- daemon's output, which every read after it gives too.connectionTransport :: DaemonConnection -> Transport IO#if defined(mingw32_HOST_OS)connectionTransport (DaemonConnection conn) = Pipe.connectionTransport conn#elseconnectionTransport (DaemonConnection conn) =  Transport    { transportReceive = (\chunk -> if BS.null chunk then Nothing else Just chunk) <$> recv conn receiveChunkSize,      transportSend = SocketLazy.sendAll conn . BB.toLazyByteString    }#endif-- | Close the connection.closeDaemonConnection :: DaemonConnection -> IO ()#if defined(mingw32_HOST_OS)closeDaemonConnection (DaemonConnection conn) = Pipe.closeConnection conn#elsecloseDaemonConnection (DaemonConnection sock) = close sock#endif-- | Whether anything is at an address, which a client asks to decide-- whether to go through a daemon.data AddressPresence  = -- | Something is there: a socket's path names an entry, a pipe's    -- name is in the machine's pipes.  Whether a daemon answers is not    -- known.    AddressPresent  | -- | Nothing is.    AddressAbsent  deriving (Eq, Show)-- | Whether anything is at the address, without connecting to it, as-- upstream's @auto@ store looks for the daemon's socket: the address-- itself, a link not followed, on a POSIX system, and on Windows the-- list of the machine's pipes ("Nix.Daemon.Pipe.pipePresent"), since-- opening a pipe takes one of its instances.  An address that names a-- missing entry, or one below something that is not a directory, is-- absent; so is a pipe's path that is not one.  Any other failure to-- look, such as a directory the process may not search, is thrown as-- upstream words it: @getting status of "PATH": REASON@, the path as it is-- given, shown as UTF-8 where it is, and the operating system's reason.addressPresence :: ByteString -> IO AddressPresence#if defined(mingw32_HOST_OS)addressPresence address = case Pipe.pipePath address of  Left _ -> pure AddressAbsent  Right path ->    Pipe.pipePresent path >>= \case      True -> pure AddressPresent      False -> pure AddressAbsent#elseaddressPresence path =  try (getSymbolicLinkStatus path) >>= \case    Right _ -> pure AddressPresent    Left failure      | fmap Errno (ioe_errno failure) `elem` [Just eNOENT, Just eNOTDIR] -> pure AddressAbsent      | otherwise -> ioError (userError ("getting status of \"" <> T.unpack (TE.decodeUtf8Lenient path) <> "\": " <> ioe_description failure))#endif-- | A connection that cannot be made, given the address as it is shown-- and the reason, as upstream's client words it for a socket: the socket-- named in double quotes, then the reason.  On Windows the address is a-- named pipe's and the message says so.connectFailureText :: Text -> Text -> TextconnectFailureText shownAddress reason = "cannot connect to " <> endpoint <> " at \"" <> shownAddress <> "\": " <> reason  where#if defined(mingw32_HOST_OS)    endpoint = "named pipe"#else    endpoint = "socket"#endif