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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
module Script.Shell (
ShellScript(..),
ShellStatement(ShellStatement),
ShellPipeline(ShellPipeline),
ShellCommand(ShellCommand),
ShellArgument(..),
withShellProcess,
) where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Class
import Control.Monad.Reader
import Data.Maybe
import Data.Text (Text)
import Data.Text qualified as T
import Foreign.C.Types
import Foreign.Ptr
import Foreign.Marshal.Array
import Foreign.Storable
import System.Directory
import System.Exit
import System.FilePath
import System.IO
import System.Posix.IO qualified as P
import System.Posix.Process
import System.Posix.Types
import System.Process hiding (ShellCommand)
import Network
import Network.Ip
import Output
import Process
import Run.Monad
import Script.Expr.Class
import Script.Var
newtype ShellScript = ShellScript [ ShellStatement ]
data ShellState = ShellState
{ shellWorkingDirectory :: FilePath
, shellOldWorkingDirectory :: FilePath
}
data ShellStatement = ShellStatement
{ shellPipeline :: ShellPipeline
, shellSourceLine :: SourceLine
}
data ShellPipeline = ShellPipeline
{ pipeCommand :: ShellCommand
, pipeUpstream :: Maybe ShellPipeline
}
data ShellCommand = ShellCommand
{ cmdCommand :: Text
, cmdExtArguments :: [ ShellArgument ]
, cmdSourceLine :: SourceLine
}
data ShellArgument
= ShellArgument Text
| ShellRedirectStdin Text
| ShellRedirectStdout Bool Text
| ShellRedirectStderr Bool Text
cmdArguments :: ShellCommand -> [ Text ]
cmdArguments = catMaybes . map (\case ShellArgument x -> Just x; _ -> Nothing) . cmdExtArguments
instance ExprType ShellScript where
textExprType _ = T.pack "ShellScript"
textExprValue _ = "<shell-script>"
instance ExprType ShellStatement where
textExprType _ = T.pack "ShellStatement"
textExprValue _ = "<shell-statement>"
instance ExprType ShellPipeline where
textExprType _ = T.pack "ShellPipeline"
textExprValue _ = "<shell-pipeline>"
instance ExprType ShellCommand where
textExprType _ = T.pack "ShellCommand"
textExprValue _ = "<shell-command>"
instance ExprType ShellArgument where
textExprType _ = T.pack "ShellArgument"
textExprValue _ = "<shell-argument>"
data ShellExecInfo = ShellExecInfo
{ seiNode :: Node
, seiProcName :: ProcName
, seiStatusVar :: MVar ExitCode
}
data HandleHandling
= CloseHandle Handle
| KeepHandle Handle
closeIfRequested :: MonadIO m => HandleHandling -> m ()
closeIfRequested (CloseHandle h) = liftIO $ hClose h
closeIfRequested (KeepHandle _) = return ()
handledHandle :: HandleHandling -> Handle
handledHandle (CloseHandle h) = h
handledHandle (KeepHandle h) = h
executeCommand :: ShellExecInfo -> ShellState -> HandleHandling -> HandleHandling -> HandleHandling -> ShellCommand -> TestRun ShellState
executeCommand sei@ShellExecInfo {..} st pstdin pstdout pstderr scmd@ShellCommand {..} = do
let args = cmdArguments scmd
( pstdin', pstdout', pstderr' ) <- (\f -> foldM f ( pstdin, pstdout, pstderr ) cmdExtArguments) $ \cur@( cin, cout, cerr ) -> \case
ShellRedirectStdin path -> do
closeIfRequested cin
h <- liftIO $ openBinaryFile (nodeDir seiNode </> T.unpack path) ReadMode
return ( CloseHandle h, cout, cerr )
ShellRedirectStdout append path -> do
closeIfRequested cout
h <- liftIO $ openBinaryFile (nodeDir seiNode </> T.unpack path) $ if append then AppendMode else WriteMode
return ( cin, CloseHandle h, cerr )
ShellRedirectStderr append path -> do
closeIfRequested cerr
h <- liftIO $ openBinaryFile (nodeDir seiNode </> T.unpack path) $ if append then AppendMode else WriteMode
return ( cin, cout, CloseHandle h )
_ -> do
return cur
( getExitStatus, state' ) <- executeCommandProcess sei st (handledHandle pstdin') (handledHandle pstdout') (handledHandle pstderr') args cmdCommand
let failedWithStatus status = do
liftIO $ putMVar seiStatusVar status
() <- throwError Failed
return state'
mapM_ closeIfRequested [ pstdin', pstdout', pstderr' ]
getExitStatus >>= \case
Exited ExitSuccess -> do
return state'
Exited status -> do
outLine OutputChildFail (Just $ textProcName seiProcName) $ "failed at: " <> textSourceLine cmdSourceLine
failedWithStatus status
Terminated sig _ -> do
outLine OutputChildFail (Just $ textProcName seiProcName) $ "killed with " <> T.pack (show sig) <> " at: " <> textSourceLine cmdSourceLine
failedWithStatus (ExitFailure (- fromIntegral sig))
Stopped sig -> do
outLine OutputChildFail (Just $ textProcName seiProcName) $ "stopped with " <> T.pack (show sig) <> " at: " <> textSourceLine cmdSourceLine
failedWithStatus (ExitFailure (- fromIntegral sig))
executeCommandProcess :: ShellExecInfo -> ShellState -> Handle -> Handle -> Handle -> [ Text ] -> Text -> TestRun ( TestRun ProcessStatus, ShellState )
executeCommandProcess ShellExecInfo {..} st@ShellState {..} pstdin pstdout pstderr args = \case
"cd"
| [] <- args -> liftIO $ do
hPutStrLn pstdout (nodeDir seiNode)
return ( return (Exited ExitSuccess), st
{ shellWorkingDirectory = nodeDir seiNode
, shellOldWorkingDirectory = shellWorkingDirectory
} )
| [ "-" ] <- args -> liftIO $ do
hPutStrLn pstdout shellOldWorkingDirectory
return ( return (Exited ExitSuccess), st
{ shellWorkingDirectory = shellOldWorkingDirectory
, shellOldWorkingDirectory = shellWorkingDirectory
} )
| [ dir ] <- args -> liftIO $ do
cd <- canonicalizePath $ shellWorkingDirectory </> T.unpack dir
doesDirectoryExist cd >>= \case
True -> return ( return (Exited ExitSuccess), st
{ shellWorkingDirectory = cd
, shellOldWorkingDirectory = shellWorkingDirectory
} )
False -> do
hPutStrLn pstderr $ "cd: no such directory: " <> T.unpack dir
return ( return (Exited (ExitFailure (-1))), st )
| otherwise -> do
liftIO $ hPutStrLn pstderr $ "cd: too many arguments"
return ( return (Exited (ExitFailure (-1))), st )
"pwd"
| [] <- args -> do
liftIO $ hPutStrLn pstdout shellWorkingDirectory
return ( return (Exited ExitSuccess), st )
| otherwise -> do
liftIO $ hPutStrLn pstderr $ "pwd: too many arguments"
return ( return (Exited (ExitFailure (-1))), st )
cmd -> liftIO $ do
(_, _, _, phandle) <- createProcess_ "shell"
(proc (T.unpack cmd) (map T.unpack args))
{ std_in = UseHandle pstdin
, std_out = UseHandle pstdout
, std_err = UseHandle pstderr
, cwd = Just shellWorkingDirectory
, env = Just []
}
Just pid <- getPid phandle
let getProcessStatus' =
liftIO (getProcessStatus True False pid) >>= \case
Just status -> return status
Nothing -> do
outLine OutputChildFail (Just $ textProcName seiProcName) $ "no exit status"
return (Exited (ExitFailure (-1)))
return ( getProcessStatus', st )
executePipeline :: ShellExecInfo -> ShellState -> HandleHandling -> HandleHandling -> HandleHandling -> ShellPipeline -> TestRun ShellState
executePipeline sei st pstdin pstdout pstderr ShellPipeline {..} = do
case pipeUpstream of
Nothing -> do
executeCommand sei st pstdin pstdout pstderr pipeCommand
Just upstream -> do
( pipeRead, pipeWrite ) <- createPipeCloexec
void $ forkTestUsing forkOS $ do
void $ executePipeline sei st pstdin (CloseHandle pipeWrite) (KeepHandle $ handledHandle pstderr) upstream
state' <- executeCommand sei st (CloseHandle pipeRead) pstdout (KeepHandle $ handledHandle pstderr) pipeCommand
closeIfRequested pstderr
return state'
executeScript :: ShellExecInfo -> Handle -> Handle -> Handle -> ShellScript -> TestRun ()
executeScript sei@ShellExecInfo {..} pstdin pstdout pstderr (ShellScript statements) = do
setNetworkNamespace $ getNetns seiNode
let initialState = ShellState
{ shellWorkingDirectory = nodeDir seiNode
, shellOldWorkingDirectory = nodeDir seiNode
}
_ <- (\f -> foldM f initialState statements) $ \st ShellStatement {..} -> do
executePipeline sei st (KeepHandle pstdin) (KeepHandle pstdout) (KeepHandle pstderr) shellPipeline
liftIO $ putMVar seiStatusVar ExitSuccess
spawnShell :: Node -> ProcName -> ShellScript -> TestRun Process
spawnShell procNode procName script = do
idVar <- asks $ teNextProcId . fst
procId <- liftIO $ modifyMVar idVar (\x -> return ( x + 1, ProcessId x ))
procOutput <- liftIO $ newTVarIO []
procIgnore <- liftIO $ newTVarIO ( 0, [] )
seiStatusVar <- liftIO $ newEmptyMVar
( pstdin, procStdin ) <- createPipeCloexec
( hout, pstdout ) <- createPipeCloexec
( herr, pstderr ) <- createPipeCloexec
procHandle <- fmap (Right . (, seiStatusVar)) $ forkTestUsing forkOS $ do
let seiNode = procNode
seiProcName = procName
executeScript ShellExecInfo {..} pstdin pstdout pstderr script
liftIO $ do
hClose pstdin
hClose pstdout
hClose pstderr
let procKillWith = Nothing
let procPid = Nothing
let process = Process {..}
startProcessIOLoops process hout herr
return process
withShellProcess :: Node -> ProcName -> ShellScript -> (Process -> TestRun a) -> TestRun a
withShellProcess node pname script inner = do
procVar <- asks $ teProcesses . fst
process <- spawnShell node pname script
liftIO $ modifyMVar_ procVar $ return . (process:)
inner process `finally` do
ps <- liftIO $ takeMVar procVar
closeTestProcess process `finally` do
liftIO $ putMVar procVar $ filter (/=process) ps
foreign import ccall "shell_pipe_cloexec" c_pipe_cloexec :: Ptr Fd -> IO CInt
createPipeCloexec :: (MonadIO m, MonadFail m) => m ( Handle, Handle )
createPipeCloexec = liftIO $ do
allocaArray 2 $ \ptr -> do
c_pipe_cloexec ptr >>= \case
0 -> do
rh <- P.fdToHandle =<< peekElemOff ptr 0
wh <- P.fdToHandle =<< peekElemOff ptr 1
return ( rh, wh )
_ -> do
fail $ "failed to create pipe"
|