-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_serve.hs
47 lines (40 loc) · 1.23 KB
/
tcp_serve.hs
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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network (listenOn, PortID(..), sClose)
import Network.Socket (accept, Socket(..))
import System.Environment
import Data.List
import System.Posix.Process
import System.Posix.IO
import System.Posix.Types
import System.Posix.Signals
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types (CInt)
import Control.Applicative
import Control.Monad.State
import System.IO
import System.Exit
import ProcessUtil
import Common
mkServer port = listenOn (PortNumber port)
waitForClient ssocket = fst <$> accept ssocket
serve port cmd = do
ssocket <- mkServer (fromIntegral $ (read port :: Int))
installHandler sigCHLD (Catch collectAnyChild) Nothing
runStateT loop ssocket
where loop :: StateT Socket IO ()
loop = do
ssocket <- get
csocket <- liftIO $ waitForClient ssocket
runHandler (socketFd csocket) cmd
loop
runHandler :: Fd -> [String] -> StateT Socket IO ()
runHandler cfd cmd = do
executeInForkShell' False (B.pack $ intercalate " " cmd) Nothing (Just cfd) (Just cfd)
liftIO $ closeFd cfd
return ()
main = do
args <- getArgs
case args of
(port:rest) -> serve port rest