@@ -319,79 +319,85 @@ impl Command {
319
319
320
320
let mut p = Process { pid : 0 , status : None } ;
321
321
322
- struct PosixSpawnFileActions ( MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
322
+ struct PosixSpawnFileActions < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
323
323
324
- impl Drop for PosixSpawnFileActions {
324
+ impl Drop for PosixSpawnFileActions < ' _ > {
325
325
fn drop ( & mut self ) {
326
326
unsafe {
327
327
libc:: posix_spawn_file_actions_destroy ( self . 0 . as_mut_ptr ( ) ) ;
328
328
}
329
329
}
330
330
}
331
331
332
- struct PosixSpawnattr ( MaybeUninit < libc:: posix_spawnattr_t > ) ;
332
+ struct PosixSpawnattr < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawnattr_t > ) ;
333
333
334
- impl Drop for PosixSpawnattr {
334
+ impl Drop for PosixSpawnattr < ' _ > {
335
335
fn drop ( & mut self ) {
336
336
unsafe {
337
337
libc:: posix_spawnattr_destroy ( self . 0 . as_mut_ptr ( ) ) ;
338
338
}
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
- 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) ;
345
350
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) ;
348
354
349
355
if let Some ( fd) = stdio. stdin . fd ( ) {
350
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
356
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
351
357
file_actions. 0 . as_mut_ptr ( ) ,
352
358
fd,
353
359
libc:: STDIN_FILENO ,
354
360
) ) ?;
355
361
}
356
362
if let Some ( fd) = stdio. stdout . fd ( ) {
357
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
363
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
358
364
file_actions. 0 . as_mut_ptr ( ) ,
359
365
fd,
360
366
libc:: STDOUT_FILENO ,
361
367
) ) ?;
362
368
}
363
369
if let Some ( fd) = stdio. stderr . fd ( ) {
364
- cvt ( libc:: posix_spawn_file_actions_adddup2 (
370
+ cvt_nz ( libc:: posix_spawn_file_actions_adddup2 (
365
371
file_actions. 0 . as_mut_ptr ( ) ,
366
372
fd,
367
373
libc:: STDERR_FILENO ,
368
374
) ) ?;
369
375
}
370
376
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 ( ) ) ) ?;
372
378
}
373
379
374
380
let mut set = MaybeUninit :: < libc:: sigset_t > :: uninit ( ) ;
375
381
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 ( ) ) ) ?;
377
383
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 ( ) ) ) ?;
379
385
380
386
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 _ ) ) ?;
382
388
383
389
// Make sure we synchronize access to the global `environ` resource
384
390
let _env_lock = sys:: os:: env_lock ( ) ;
385
391
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 (
387
393
& mut p. pid ,
388
394
self . get_program_cstr ( ) . as_ptr ( ) ,
389
395
file_actions. 0 . as_ptr ( ) ,
390
396
attrs. 0 . as_ptr ( ) ,
391
397
self . get_argv ( ) . as_ptr ( ) as * const _ ,
392
398
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) )
395
401
}
396
402
}
397
403
}
0 commit comments