@@ -339,6 +339,10 @@ impl Command {
339
339
}
340
340
}
341
341
342
+ fn cvt_nz ( error : libc:: c_int ) -> io:: Result < ( ) > {
343
+ if error == 0 { Ok ( ( ) ) } else { Err ( io:: Error :: from_raw_os_error ( error) ) }
344
+ }
345
+
342
346
unsafe {
343
347
let mut file_actions = PosixSpawnFileActions ( MaybeUninit :: uninit ( ) ) ;
344
348
let mut attrs = PosixSpawnattr ( MaybeUninit :: uninit ( ) ) ;
@@ -347,51 +351,51 @@ impl Command {
347
351
libc:: posix_spawn_file_actions_init ( file_actions. 0 . as_mut_ptr ( ) ) ;
348
352
349
353
if let Some ( fd) = stdio. stdin . fd ( ) {
350
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
354
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
351
355
file_actions. 0 . as_mut_ptr ( ) ,
352
356
fd,
353
357
libc:: STDIN_FILENO ,
354
358
) ) ?;
355
359
}
356
360
if let Some ( fd) = stdio. stdout . fd ( ) {
357
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
361
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
358
362
file_actions. 0 . as_mut_ptr ( ) ,
359
363
fd,
360
364
libc:: STDOUT_FILENO ,
361
365
) ) ?;
362
366
}
363
367
if let Some ( fd) = stdio. stderr . fd ( ) {
364
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
368
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
365
369
file_actions. 0 . as_mut_ptr ( ) ,
366
370
fd,
367
371
libc:: STDERR_FILENO ,
368
372
) ) ?;
369
373
}
370
374
if let Some ( ( f, cwd) ) = addchdir {
371
- cvt ( f ( file_actions. 0 . as_mut_ptr ( ) , cwd. as_ptr ( ) ) ) ?;
375
+ cvt_nz ( f ( file_actions. 0 . as_mut_ptr ( ) , cwd. as_ptr ( ) ) ) ?;
372
376
}
373
377
374
378
let mut set = MaybeUninit :: < libc:: sigset_t > :: uninit ( ) ;
375
379
cvt ( sigemptyset ( set. as_mut_ptr ( ) ) ) ?;
376
- cvt ( libc:: posix_spawnattr_setsigmask ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
380
+ cvt_nz ( libc:: posix_spawnattr_setsigmask ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
377
381
cvt ( sigaddset ( set. as_mut_ptr ( ) , libc:: SIGPIPE ) ) ?;
378
- cvt ( libc:: posix_spawnattr_setsigdefault ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
382
+ cvt_nz ( libc:: posix_spawnattr_setsigdefault ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
379
383
380
384
let flags = libc:: POSIX_SPAWN_SETSIGDEF | libc:: POSIX_SPAWN_SETSIGMASK ;
381
- cvt ( libc:: posix_spawnattr_setflags ( attrs. 0 . as_mut_ptr ( ) , flags as _ ) ) ?;
385
+ cvt_nz ( libc:: posix_spawnattr_setflags ( attrs. 0 . as_mut_ptr ( ) , flags as _ ) ) ?;
382
386
383
387
// Make sure we synchronize access to the global `environ` resource
384
388
let _env_lock = sys:: os:: env_lock ( ) ;
385
389
let envp = envp. map ( |c| c. as_ptr ( ) ) . unwrap_or_else ( || * sys:: os:: environ ( ) as * const _ ) ;
386
- let ret = libc:: posix_spawnp (
390
+ cvt_nz ( libc:: posix_spawnp (
387
391
& mut p. pid ,
388
392
self . get_program_cstr ( ) . as_ptr ( ) ,
389
393
file_actions. 0 . as_ptr ( ) ,
390
394
attrs. 0 . as_ptr ( ) ,
391
395
self . get_argv ( ) . as_ptr ( ) as * const _ ,
392
396
envp as * const _ ,
393
- ) ;
394
- if ret == 0 { Ok ( Some ( p) ) } else { Err ( io :: Error :: from_raw_os_error ( ret ) ) }
397
+ ) ) ? ;
398
+ Ok ( Some ( p) )
395
399
}
396
400
}
397
401
}
0 commit comments