@@ -309,18 +309,9 @@ impl Command {
309
309
310
310
let ( ours, theirs) = self . setup_io ( default, needs_stdin) ?;
311
311
312
- let ( maybe_process, err) = unsafe { self . do_exec ( & theirs) } ;
313
- // We don't want FileDesc::drop to be called on any stdio. It would close their handles.
314
- let ChildPipes { stdin : their_stdin, stdout : their_stdout, stderr : their_stderr } = theirs;
315
- their_stdin. fd ( ) ;
316
- their_stdout. fd ( ) ;
317
- their_stderr. fd ( ) ;
318
-
319
- if let Some ( ( launchpad, process_handle) ) = maybe_process {
320
- Ok ( ( Process { launchpad : launchpad, handle : process_handle, status : None } , ours) )
321
- } else {
322
- Err ( err)
323
- }
312
+ let ( launchpad, process_handle) = unsafe { self . do_exec ( theirs) ? } ;
313
+
314
+ Ok ( ( Process { launchpad : launchpad, handle : process_handle, status : None } , ours) )
324
315
}
325
316
326
317
#[ cfg( not( target_os = "fuchsia" ) ) ]
@@ -453,23 +444,16 @@ impl Command {
453
444
}
454
445
455
446
#[ cfg( target_os = "fuchsia" ) ]
456
- unsafe fn do_exec ( & mut self , stdio : & ChildPipes )
457
- -> ( Option < ( * mut launchpad_t , mx_handle_t ) > , io :: Error ) {
447
+ unsafe fn do_exec ( & mut self , stdio : ChildPipes )
448
+ -> io :: Result < ( * mut launchpad_t , mx_handle_t ) > {
458
449
use sys:: magenta:: * ;
459
450
460
- macro_rules! t {
461
- ( $e: expr) => ( match $e {
462
- Ok ( e) => e,
463
- Err ( e) => return ( None , e) ,
464
- } )
465
- }
466
-
467
451
macro_rules! tlp {
468
452
( $lp: expr, $e: expr) => ( match $e {
469
453
Ok ( e) => e,
470
454
Err ( e) => {
471
455
launchpad_destroy( $lp) ;
472
- return ( None , e) ;
456
+ return Err ( e) ;
473
457
} ,
474
458
} )
475
459
}
@@ -484,46 +468,23 @@ impl Command {
484
468
let mut job_copy: mx_handle_t = MX_HANDLE_INVALID ;
485
469
486
470
// Duplicate the job handle
487
- t ! ( mx_cvt( mx_handle_duplicate( job_handle, MX_RIGHT_SAME_RIGHTS ,
488
- & mut job_copy as * mut mx_handle_t) ) ) ;
471
+ mx_cvt ( mx_handle_duplicate ( job_handle, MX_RIGHT_SAME_RIGHTS ,
472
+ & mut job_copy as * mut mx_handle_t ) ) ? ;
489
473
// Create a launchpad
490
- t ! ( mx_cvt( launchpad_create( job_copy, self . argv[ 0 ] ,
491
- & mut launchpad as * mut * mut launchpad_t) ) ) ;
474
+ mx_cvt ( launchpad_create ( job_copy, self . argv [ 0 ] ,
475
+ & mut launchpad as * mut * mut launchpad_t ) ) ? ;
492
476
// Set the process argv
493
477
tlp ! ( launchpad, mx_cvt( launchpad_arguments( launchpad, self . argv. len( ) as i32 - 1 ,
494
- self . argv. as_ptr( ) ) ) ) ;
478
+ self . argv. as_ptr( ) ) ) ) ;
495
479
// Setup the environment vars
496
- let status = launchpad_environ ( launchpad, envp) ;
497
- if status != NO_ERROR {
498
- launchpad_destroy ( launchpad) ;
499
- return ( None , io:: Error :: last_os_error ( ) ) ;
500
- }
501
- let status = launchpad_add_vdso_vmo ( launchpad) ;
502
- if status != NO_ERROR {
503
- launchpad_destroy ( launchpad) ;
504
- return ( None , io:: Error :: last_os_error ( ) ) ;
505
- }
506
- let status = launchpad_clone_mxio_root ( launchpad) ;
507
- if status != NO_ERROR {
508
- launchpad_destroy ( launchpad) ;
509
- return ( None , io:: Error :: last_os_error ( ) ) ;
510
- }
480
+ tlp ! ( launchpad, mx_cvt( launchpad_environ( launchpad, envp) ) ) ;
481
+ tlp ! ( launchpad, mx_cvt( launchpad_add_vdso_vmo( launchpad) ) ) ;
482
+ tlp ! ( launchpad, mx_cvt( launchpad_clone_mxio_root( launchpad) ) ) ;
511
483
// Load the executable
512
- let status = launchpad_elf_load ( launchpad, launchpad_vmo_from_file ( self . argv [ 0 ] ) ) ;
513
- if status != NO_ERROR {
514
- launchpad_destroy ( launchpad) ;
515
- return ( None , io:: Error :: last_os_error ( ) ) ;
516
- }
517
- let status = launchpad_load_vdso ( launchpad, MX_HANDLE_INVALID ) ;
518
- if status != NO_ERROR {
519
- launchpad_destroy ( launchpad) ;
520
- return ( None , io:: Error :: last_os_error ( ) ) ;
521
- }
522
- let status = launchpad_clone_mxio_cwd ( launchpad) ;
523
- if status != NO_ERROR {
524
- launchpad_destroy ( launchpad) ;
525
- return ( None , io:: Error :: last_os_error ( ) ) ;
526
- }
484
+ tlp ! ( launchpad,
485
+ mx_cvt( launchpad_elf_load( launchpad, launchpad_vmo_from_file( self . argv[ 0 ] ) ) ) ) ;
486
+ tlp ! ( launchpad, mx_cvt( launchpad_load_vdso( launchpad, MX_HANDLE_INVALID ) ) ) ;
487
+ tlp ! ( launchpad, mx_cvt( launchpad_clone_mxio_cwd( launchpad) ) ) ;
527
488
528
489
// Clone stdin, stdout, and stderr
529
490
if let Some ( fd) = stdio. stdin . fd ( ) {
@@ -542,17 +503,20 @@ impl Command {
542
503
launchpad_clone_fd ( launchpad, 2 , 2 ) ;
543
504
}
544
505
506
+ // We don't want FileDesc::drop to be called on any stdio. It would close their fds. The
507
+ // fds will be closed once the child process finishes.
508
+ let ChildPipes { stdin : child_stdin, stdout : child_stdout, stderr : child_stderr } = stdio;
509
+ if let ChildStdio :: Owned ( fd) = child_stdin { fd. into_raw ( ) ; }
510
+ if let ChildStdio :: Owned ( fd) = child_stdout { fd. into_raw ( ) ; }
511
+ if let ChildStdio :: Owned ( fd) = child_stderr { fd. into_raw ( ) ; }
512
+
545
513
for callback in self . closures . iter_mut ( ) {
546
- t ! ( callback( ) ) ;
514
+ callback ( ) ? ;
547
515
}
548
516
549
- let process_handle = launchpad_start ( launchpad) ;
550
- if process_handle < 0 {
551
- launchpad_destroy ( launchpad) ;
552
- return ( None , io:: Error :: last_os_error ( ) ) ;
553
- }
517
+ let process_handle = tlp ! ( launchpad, mx_cvt( launchpad_start( launchpad) ) ) ;
554
518
555
- ( Some ( ( launchpad, process_handle) ) , io :: Error :: last_os_error ( ) )
519
+ Ok ( ( launchpad, process_handle) )
556
520
}
557
521
558
522
fn setup_io ( & self , default : Stdio , needs_stdin : bool )
0 commit comments