1- use std:: io:: { stdin, stdout} ;
1+ use std:: env;
2+ use std:: fs:: File ;
3+ use std:: io:: BufWriter ;
24use std:: path:: { Path , PathBuf } ;
3- use std:: process:: { Command , Stdio } ;
5+ use std:: process:: Command ;
46use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
57use std:: sync:: Arc ;
68
@@ -10,32 +12,32 @@ use tabox::result::SandboxExecutionResult;
1012use tabox:: { Sandbox , SandboxImplementation } ;
1113use task_maker_exec:: find_tools:: find_tools_path;
1214use task_maker_exec:: { RawSandboxResult , SandboxRunner } ;
15+ use tempfile:: NamedTempFile ;
1316
14- /// Actually parse the input and return the result.
15- fn run_sandbox ( ) -> Result < SandboxExecutionResult , Error > {
16- let config =
17- serde_json:: from_reader ( stdin ( ) ) . context ( "Cannot read configuration from stdin" ) ?;
17+ fn run_sandbox ( config : & str ) -> Result < SandboxExecutionResult , Error > {
18+ let config = serde_json:: from_str ( config) . context ( "Cannot parse configuration" ) ?;
1819 let sandbox = SandboxImplementation :: run ( config) . context ( "Failed to create sandbox" ) ?;
1920 let res = sandbox. wait ( ) . context ( "Failed to wait sandbox" ) ?;
2021 Ok ( res)
2122}
2223
2324/// Run the sandbox for an execution.
24- ///
25- /// It takes a `SandboxConfiguration`, JSON serialized via standard input and prints to standard
26- /// output a `RawSandboxResult`, JSON serialized.
2725pub fn main_sandbox ( ) {
28- match run_sandbox ( ) {
29- Ok ( res ) => {
30- serde_json :: to_writer ( stdout ( ) , & RawSandboxResult :: Success ( res ) )
31- . expect ( "Failed to print result" ) ;
32- }
26+ let mut args = env :: args ( ) . skip ( 2 ) ;
27+ let configuration = args . next ( ) . unwrap ( ) ;
28+ let output_file = args . next ( ) . unwrap ( ) ;
29+ let result = match run_sandbox ( & configuration ) {
30+ Ok ( res ) => RawSandboxResult :: Success ( res ) ,
3331 Err ( e) => {
3432 let err = format ! ( "Error: {e:?}" ) ;
35- serde_json:: to_writer ( stdout ( ) , & RawSandboxResult :: Error ( err) )
36- . expect ( "Failed to print result" ) ;
33+ RawSandboxResult :: Error ( err)
3734 }
38- }
35+ } ;
36+ let f = File :: options ( )
37+ . write ( true )
38+ . open ( & output_file)
39+ . expect ( "Failed to create output file" ) ;
40+ serde_json:: to_writer ( BufWriter :: new ( f) , & result) . expect ( "Failed to print result" ) ;
3941}
4042
4143/// Run the sandbox integrated in the task-maker-tools binary.
@@ -68,27 +70,19 @@ fn tools_sandbox_internal(
6870 config : SandboxConfiguration ,
6971 pid : Arc < AtomicU32 > ,
7072) -> Result < RawSandboxResult , Error > {
73+ let config = serde_json:: to_string ( & config) . context ( "Failed to serialize config" ) ?;
74+ // TODO(veluca): it would be nice to write the result in the sandbox.
75+ let outfile = NamedTempFile :: new ( ) . context ( "Failed creating output tempfile" ) ?;
7176 let mut cmd = Command :: new ( tools_path)
7277 . arg ( "internal-sandbox" )
73- . stdin ( Stdio :: piped ( ) )
74- . stdout ( Stdio :: piped ( ) )
75- . stderr ( Stdio :: piped ( ) )
78+ . arg ( config)
79+ . arg ( outfile. path ( ) . as_os_str ( ) )
7680 . spawn ( )
7781 . context ( "Cannot spawn the sandbox" ) ?;
7882 pid. store ( cmd. id ( ) , Ordering :: SeqCst ) ;
79- {
80- let stdin = cmd. stdin . as_mut ( ) . context ( "Failed to open stdin" ) ?;
81- serde_json:: to_writer ( stdin, & config. build ( ) ) . context ( "Failed to write config to stdin" ) ?;
82- }
83- let output = cmd
84- . wait_with_output ( )
85- . context ( "Failed to wait for the process" ) ?;
86- if !output. status . success ( ) {
87- bail ! (
88- "Sandbox process failed: {}\n {}" ,
89- output. status. to_string( ) ,
90- String :: from_utf8_lossy( & output. stderr)
91- ) ;
83+ let status = cmd. wait ( ) . context ( "Failed to wait for the process" ) ?;
84+ if !status. success ( ) {
85+ bail ! ( "Sandbox process failed: {}" , status. to_string( ) ) ;
9286 }
93- serde_json:: from_slice ( & output . stdout ) . context ( "Invalid output from sandbox" )
87+ serde_json:: from_reader ( outfile ) . context ( "Invalid output from sandbox" )
9488}
0 commit comments