~ubuntu-branches/ubuntu/vivid/haskell-mueval/vivid

« back to all changes in this revision

Viewing changes to watchdog.hs

  • Committer: Package Import Robot
  • Author(s): Joachim Breitner
  • Date: 2013-06-24 16:36:00 UTC
  • Revision ID: package-import@ubuntu.com-20130624163600-dl1f4j7mch20xfgk
Tags: upstream-0.9
ImportĀ upstreamĀ versionĀ 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- | This implements a watchdog process. It calls mueval with all the
 
2
--   user-specified arguments, sleeps, and then if mueval is still running
 
3
--   kills it.
 
4
--   Even an out-of-control mueval will have trouble avoiding 'terminateProcess'.
 
5
--   Note that it's too difficult to parse the user arguments to get the timeout,
 
6
--   so we specify it as a constant which is a little more generous than the default.
 
7
module Main where
 
8
 
 
9
import Control.Concurrent (forkIO, threadDelay)
 
10
import System.Environment (getArgs)
 
11
import System.Exit (exitWith, ExitCode(ExitFailure))
 
12
import System.Posix.Signals (signalProcess)
 
13
import System.Process (getProcessExitCode, runProcess, terminateProcess, waitForProcess)
 
14
import System.Process.Internals (withProcessHandle, ProcessHandle__(OpenHandle))
 
15
 
 
16
main :: IO ()
 
17
main = do args <- getArgs
 
18
          hdl <- runProcess "mueval-core" args Nothing Nothing Nothing Nothing Nothing
 
19
          _ <- forkIO $ do
 
20
                     threadDelay (7 * 700000)
 
21
                     status <- getProcessExitCode hdl
 
22
                     case status of 
 
23
                         Nothing -> do terminateProcess hdl
 
24
                                       _ <- withProcessHandle hdl (\x -> case x of 
 
25
                                                                      OpenHandle pid -> signalProcess 9 pid >> return (undefined, undefined)
 
26
                                                                      _ -> return (undefined,undefined))
 
27
                                       exitWith (ExitFailure 1)
 
28
                         Just a -> exitWith a
 
29
          stat <- waitForProcess hdl
 
30
          exitWith stat