File: mightyctl.hs

package info (click to toggle)
mighttpd2 4.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: haskell: 1,382; makefile: 4; sh: 3
file content (66 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Main where

import Data.List
import System.Environment
import System.Exit
import System.Posix.Signals
import System.Posix.Types

import Program.Mighty (getMightyPid, sigInfo, sigReload, sigRetire, sigStop)

commandDB :: [(String, Signal)]
commandDB =
    [ ("stop", sigStop)
    , ("reload", sigReload)
    , ("retire", sigRetire)
    , ("info", sigInfo)
    ]

usage :: IO a
usage = do
    putStrLn "Usage:"
    putStrLn $ "    mightyctl " ++ cmds ++ " [pid]"
    exitFailure
  where
    cmds = intercalate "|" $ map fst commandDB

main :: IO ()
main = do
    (sig, mpid) <- getArgs >>= checkArgs
    pid <- maybe getProcessIdWithPS return mpid
    signalProcess sig pid

checkArgs :: [String] -> IO (Signal, Maybe ProcessID)
checkArgs [cmd] = do
    sig <- getSignal cmd
    return (sig, Nothing)
checkArgs [cmd, num] = do
    sig <- getSignal cmd
    pid <- getProcessId num
    return (sig, Just pid)
checkArgs _ = usage

getSignal :: String -> IO Signal
getSignal cmd = check $ lookup cmd commandDB
  where
    check (Just sig) = return sig
    check Nothing = do
        putStrLn $ "No such command: " ++ cmd
        usage

getProcessId :: String -> IO ProcessID
getProcessId num = check $ reads num
  where
    check [(pid, "")] = return . fromIntegral $ (pid :: Int)
    check _ = do
        putStrLn $ "No such process id: " ++ num
        usage

getProcessIdWithPS :: IO ProcessID
getProcessIdWithPS = getMightyPid >>= check
  where
    check [] = putStrLn "No Mighty found" >> usage
    check [pid] = return pid
    check pids = do
        putStrLn $ "Multiple Mighty found: " ++ intercalate ", " (map show pids)
        usage