@@ -319,79 +319,85 @@ impl Command {
319319
320320 let mut p = Process { pid : 0 , status : None } ;
321321
322- struct PosixSpawnFileActions ( MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
322+ struct PosixSpawnFileActions < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
323323
324- impl Drop for PosixSpawnFileActions {
324+ impl Drop for PosixSpawnFileActions < ' _ > {
325325 fn drop ( & mut self ) {
326326 unsafe {
327327 libc:: posix_spawn_file_actions_destroy ( self . 0 . as_mut_ptr ( ) ) ;
328328 }
329329 }
330330 }
331331
332- struct PosixSpawnattr ( MaybeUninit < libc:: posix_spawnattr_t > ) ;
332+ struct PosixSpawnattr < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawnattr_t > ) ;
333333
334- impl Drop for PosixSpawnattr {
334+ impl Drop for PosixSpawnattr < ' _ > {
335335 fn drop ( & mut self ) {
336336 unsafe {
337337 libc:: posix_spawnattr_destroy ( self . 0 . as_mut_ptr ( ) ) ;
338338 }
339339 }
340340 }
341341
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+
342346 unsafe {
343- let mut file_actions = PosixSpawnFileActions ( MaybeUninit :: uninit ( ) ) ;
344- let mut attrs = PosixSpawnattr ( MaybeUninit :: uninit ( ) ) ;
347+ let mut attrs = MaybeUninit :: uninit ( ) ;
348+ cvt_nz ( libc:: posix_spawnattr_init ( attrs. as_mut_ptr ( ) ) ) ?;
349+ let attrs = PosixSpawnattr ( & mut attrs) ;
345350
346- libc:: posix_spawnattr_init ( attrs. 0 . as_mut_ptr ( ) ) ;
347- libc:: posix_spawn_file_actions_init ( file_actions. 0 . as_mut_ptr ( ) ) ;
351+ let mut file_actions = MaybeUninit :: uninit ( ) ;
352+ cvt_nz ( libc:: posix_spawn_file_actions_init ( file_actions. as_mut_ptr ( ) ) ) ?;
353+ let file_actions = PosixSpawnFileActions ( & mut file_actions) ;
348354
349355 if let Some ( fd) = stdio. stdin . fd ( ) {
350- cvt ( libc:: posix_spawn_file_actions_adddup2 (
356+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
351357 file_actions. 0 . as_mut_ptr ( ) ,
352358 fd,
353359 libc:: STDIN_FILENO ,
354360 ) ) ?;
355361 }
356362 if let Some ( fd) = stdio. stdout . fd ( ) {
357- cvt ( libc:: posix_spawn_file_actions_adddup2 (
363+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
358364 file_actions. 0 . as_mut_ptr ( ) ,
359365 fd,
360366 libc:: STDOUT_FILENO ,
361367 ) ) ?;
362368 }
363369 if let Some ( fd) = stdio. stderr . fd ( ) {
364- cvt ( libc:: posix_spawn_file_actions_adddup2 (
370+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
365371 file_actions. 0 . as_mut_ptr ( ) ,
366372 fd,
367373 libc:: STDERR_FILENO ,
368374 ) ) ?;
369375 }
370376 if let Some ( ( f, cwd) ) = addchdir {
371- cvt ( f ( file_actions. 0 . as_mut_ptr ( ) , cwd. as_ptr ( ) ) ) ?;
377+ cvt_nz ( f ( file_actions. 0 . as_mut_ptr ( ) , cwd. as_ptr ( ) ) ) ?;
372378 }
373379
374380 let mut set = MaybeUninit :: < libc:: sigset_t > :: uninit ( ) ;
375381 cvt ( sigemptyset ( set. as_mut_ptr ( ) ) ) ?;
376- cvt ( libc:: posix_spawnattr_setsigmask ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
382+ cvt_nz ( libc:: posix_spawnattr_setsigmask ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
377383 cvt ( sigaddset ( set. as_mut_ptr ( ) , libc:: SIGPIPE ) ) ?;
378- cvt ( libc:: posix_spawnattr_setsigdefault ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
384+ cvt_nz ( libc:: posix_spawnattr_setsigdefault ( attrs. 0 . as_mut_ptr ( ) , set. as_ptr ( ) ) ) ?;
379385
380386 let flags = libc:: POSIX_SPAWN_SETSIGDEF | libc:: POSIX_SPAWN_SETSIGMASK ;
381- cvt ( libc:: posix_spawnattr_setflags ( attrs. 0 . as_mut_ptr ( ) , flags as _ ) ) ?;
387+ cvt_nz ( libc:: posix_spawnattr_setflags ( attrs. 0 . as_mut_ptr ( ) , flags as _ ) ) ?;
382388
383389 // Make sure we synchronize access to the global `environ` resource
384390 let _env_lock = sys:: os:: env_lock ( ) ;
385391 let envp = envp. map ( |c| c. as_ptr ( ) ) . unwrap_or_else ( || * sys:: os:: environ ( ) as * const _ ) ;
386- let ret = libc:: posix_spawnp (
392+ cvt_nz ( libc:: posix_spawnp (
387393 & mut p. pid ,
388394 self . get_program_cstr ( ) . as_ptr ( ) ,
389395 file_actions. 0 . as_ptr ( ) ,
390396 attrs. 0 . as_ptr ( ) ,
391397 self . get_argv ( ) . as_ptr ( ) as * const _ ,
392398 envp as * const _ ,
393- ) ;
394- if ret == 0 { Ok ( Some ( p) ) } else { Err ( io :: Error :: from_raw_os_error ( ret ) ) }
399+ ) ) ? ;
400+ Ok ( Some ( p) )
395401 }
396402 }
397403}
0 commit comments