@@ -20,6 +20,7 @@ use serde::{Deserialize, Serialize};
20
20
pub use crate :: linux:: { LinuxBackend , LinuxChildProcess , LinuxSandbox } ;
21
21
22
22
use std:: {
23
+ error:: Error as StdError ,
23
24
fmt:: Debug ,
24
25
io:: { Read , Write } ,
25
26
time:: Duration ,
@@ -42,10 +43,14 @@ pub fn check() -> Option<String> {
42
43
43
44
/// Represents way of isolation
44
45
pub trait Backend : Debug + Send + Sync {
45
- type Sandbox : Sandbox ;
46
- type ChildProcess : ChildProcess ;
47
- fn new_sandbox ( & self , options : SandboxOptions ) -> Result < Self :: Sandbox > ;
48
- fn spawn ( & self , options : ChildProcessOptions < Self :: Sandbox > ) -> Result < Self :: ChildProcess > ;
46
+ type Error : StdError + Send + Sync + ' static ;
47
+ type Sandbox : Sandbox < Error = Self :: Error > ;
48
+ type ChildProcess : ChildProcess < Error = Self :: Error > ;
49
+ fn new_sandbox ( & self , options : SandboxOptions ) -> Result < Self :: Sandbox , Self :: Error > ;
50
+ fn spawn (
51
+ & self ,
52
+ options : ChildProcessOptions < Self :: Sandbox > ,
53
+ ) -> Result < Self :: ChildProcess , Self :: Error > ;
49
54
}
50
55
51
56
pub use command:: Command ;
@@ -116,20 +121,21 @@ impl SandboxOptions {
116
121
117
122
/// Represents highly-isolated sandbox
118
123
pub trait Sandbox : Clone + Debug + ' static {
124
+ type Error : StdError + Send + Sync + ' static ;
119
125
fn id ( & self ) -> String ;
120
126
121
127
/// Returns true if sandbox exceeded CPU time limit
122
- fn check_cpu_tle ( & self ) -> Result < bool > ;
128
+ fn check_cpu_tle ( & self ) -> Result < bool , Self :: Error > ;
123
129
124
130
/// Returns true if sandbox exceeded wall-clock time limit
125
- fn check_real_tle ( & self ) -> Result < bool > ;
131
+ fn check_real_tle ( & self ) -> Result < bool , Self :: Error > ;
126
132
127
133
/// Kills all processes in sandbox.
128
134
/// Probably, subsequent `spawn` requests will fail.
129
- fn kill ( & self ) -> Result < ( ) > ;
135
+ fn kill ( & self ) -> Result < ( ) , Self :: Error > ;
130
136
131
137
/// Returns information about resource usage by total sandbox
132
- fn resource_usage ( & self ) -> Result < ResourceUsageData > ;
138
+ fn resource_usage ( & self ) -> Result < ResourceUsageData , Self :: Error > ;
133
139
}
134
140
135
141
/// Configures stdin for child
@@ -240,72 +246,11 @@ pub struct ChildProcessOptions<Sandbox> {
240
246
pub pwd : PathBuf ,
241
247
}
242
248
243
- mod errors {
244
- #[ derive( Eq , PartialEq ) ]
245
- pub enum ErrorKind {
246
- /// This error typically means that isolated process tried to break its sandbox
247
- Sandbox ,
248
- /// Bug in code, using minion, or in minion itself
249
- System ,
250
- }
251
-
252
- #[ derive( Debug , thiserror:: Error ) ]
253
- #[ non_exhaustive]
254
- pub enum Error {
255
- #[ error( "requested operation is not supported by backend" ) ]
256
- NotSupported ,
257
- #[ error( "system call failed in undesired fashion (error code {})" , code) ]
258
- Syscall { code : i32 } ,
259
- #[ error( "io error" ) ]
260
- Io {
261
- #[ from]
262
- source : std:: io:: Error ,
263
- } ,
264
- #[ error( "sandbox interaction failed" ) ]
265
- Sandbox ,
266
- #[ error( "unknown error" ) ]
267
- Unknown ,
268
- }
269
-
270
- impl Error {
271
- pub fn kind ( & self ) -> ErrorKind {
272
- match self {
273
- Error :: NotSupported => ErrorKind :: System ,
274
- Error :: Syscall { .. } => ErrorKind :: System ,
275
- Error :: Io { .. } => ErrorKind :: System ,
276
- Error :: Sandbox => ErrorKind :: Sandbox ,
277
- Error :: Unknown => ErrorKind :: System ,
278
- }
279
- }
280
-
281
- pub fn is_system ( & self ) -> bool {
282
- self . kind ( ) == ErrorKind :: System
283
- }
284
-
285
- pub fn is_sandbox ( & self ) -> bool {
286
- self . kind ( ) == ErrorKind :: Sandbox
287
- }
288
- }
289
-
290
- impl From < nix:: Error > for Error {
291
- fn from ( err : nix:: Error ) -> Self {
292
- if let Some ( errno) = err. as_errno ( ) {
293
- Error :: Syscall { code : errno as i32 }
294
- } else {
295
- Error :: Unknown
296
- }
297
- }
298
- }
299
- }
300
-
301
- pub use errors:: Error ;
302
249
use std:: {
303
250
ffi:: OsString ,
304
251
path:: { Path , PathBuf } ,
305
252
} ;
306
253
307
- pub type Result < T > = std:: result:: Result < T , Error > ;
308
-
309
254
/// May be returned when process was killed
310
255
pub const EXIT_CODE_KILLED : i64 = 0x7eaddeadbeeff00d ;
311
256
@@ -324,12 +269,13 @@ pub enum WaitOutcome {
324
269
325
270
/// Represents child process.
326
271
pub trait ChildProcess : Debug + ' static {
272
+ type Error : StdError + Send + Sync + ' static ;
327
273
/// Represents pipe from current process to isolated
328
274
type PipeIn : Write + Send + Sync + ' static ;
329
275
/// Represents pipe from isolated process to current
330
276
type PipeOut : Read + Send + Sync + ' static ;
331
277
/// Returns exit code, if process had exited by the moment of call, or None otherwise.
332
- fn get_exit_code ( & self ) -> Result < Option < i64 > > ;
278
+ fn get_exit_code ( & self ) -> Result < Option < i64 > , Self :: Error > ;
333
279
334
280
/// Returns writeable stream, connected to child stdin
335
281
///
@@ -358,12 +304,12 @@ pub trait ChildProcess: Debug + 'static {
358
304
359
305
/// Waits for child process exit with timeout.
360
306
/// If timeout is None, `wait_for_exit` will block until child has exited
361
- fn wait_for_exit ( & self , timeout : Option < Duration > ) -> Result < WaitOutcome > ;
307
+ fn wait_for_exit ( & self , timeout : Option < Duration > ) -> Result < WaitOutcome , Self :: Error > ;
362
308
363
309
/// Refreshes information about process
364
- fn poll ( & self ) -> Result < ( ) > ;
310
+ fn poll ( & self ) -> Result < ( ) , Self :: Error > ;
365
311
366
312
/// Returns whether child process has exited by the moment of call
367
313
/// This function doesn't blocks on waiting (see `wait_for_exit`).
368
- fn is_finished ( & self ) -> Result < bool > ;
314
+ fn is_finished ( & self ) -> Result < bool , Self :: Error > ;
369
315
}
0 commit comments