87b7dc9ca9737cb5e2731722c110f6b0c8a76b41
[lsp-test.git] / src / Language / Haskell / LSP / Test / Compat.hs
1 {-# LANGUAGE CPP #-}
2 -- For some reason ghc warns about not using
3 -- Control.Monad.IO.Class but it's needed for
4 -- MonadIO
5 {-# OPTIONS_GHC -Wunused-imports #-}
6 module Language.Haskell.LSP.Test.Compat where
7
8 import Data.Maybe
9 import System.IO
10
11 #if MIN_VERSION_process(1,6,3)
12 -- We have to hide cleanupProcess for process-1.6.3.0
13 -- cause it is in the public api for 1.6.3.0 versions
14 -- shipped with ghc >= 8.6 and < 8.6.4
15 import System.Process hiding (getPid, cleanupProcess)
16 # if MIN_VERSION_process(1,6,4)
17 import qualified System.Process (getPid, cleanupProcess)
18 # else
19 import Foreign.C.Error
20 import GHC.IO.Exception ( IOErrorType(..), IOException(..) )
21
22 import qualified System.Process (getPid)
23 import qualified Control.Exception as C
24 # endif
25 #else
26 import Control.Concurrent.MVar
27 import Foreign.C.Error
28 import GHC.IO.Exception ( IOErrorType(..), IOException(..) )
29 import System.Process
30 import System.Process.Internals
31
32 import qualified Control.Exception as C
33 #endif
34
35 #ifdef mingw32_HOST_OS
36 import qualified System.Win32.Process
37 #else
38 import qualified System.Posix.Process
39 #endif
40
41
42 getCurrentProcessID :: IO Int
43 #ifdef mingw32_HOST_OS
44 getCurrentProcessID = fromIntegral <$> System.Win32.Process.getCurrentProcessId
45 #else
46 getCurrentProcessID = fromIntegral <$> System.Posix.Process.getProcessID
47 #endif
48
49 getProcessID :: ProcessHandle -> IO Int
50 getProcessID p = fromIntegral . fromJust <$> getProcessID' p
51   where
52 #if MIN_VERSION_process(1,6,3)
53   getProcessID' = System.Process.getPid
54 #else
55 #if MIN_VERSION_process(1,6,0)
56   getProcessID' (ProcessHandle mh _ _) = do
57 #else
58   getProcessID' (ProcessHandle mh _) = do
59 #endif
60     p_ <- readMVar mh
61     case p_ of
62 #ifdef mingw32_HOST_OS
63       OpenHandle h -> do
64         pid <- System.Win32.Process.getProcessId h
65         return $ Just pid
66 #else
67       OpenHandle pid -> return $ Just pid
68 #endif
69       _ -> return Nothing
70 #endif
71
72 cleanupRunningProcess :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
73 cleanupRunningProcess p@(_, _, _, ph) =
74   getProcessExitCode ph >>= maybe (cleanupProcess p) (const $ return ())
75
76 cleanupProcess :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
77 #if MIN_VERSION_process(1,6,4)
78 cleanupProcess = System.Process.cleanupProcess
79 #else
80 cleanupProcess (mb_stdin, mb_stdout, mb_stderr, ph) = do
81
82     terminateProcess ph
83     -- Note, it's important that other threads that might be reading/writing
84     -- these handles also get killed off, since otherwise they might be holding
85     -- the handle lock and prevent us from closing, leading to deadlock.
86     maybe (return ()) (ignoreSigPipe . hClose) mb_stdin
87     maybe (return ()) hClose mb_stdout
88     maybe (return ()) hClose mb_stderr
89
90     return ()
91
92 ignoreSigPipe :: IO () -> IO ()
93 ignoreSigPipe = C.handle $ \e -> case e of
94                                    IOError { ioe_type  = ResourceVanished
95                                            , ioe_errno = Just ioe }
96                                      | Errno ioe == ePIPE -> return ()
97                                    _ -> C.throwIO e
98 #endif