@@ -432,3 +432,57 @@ fn translate_status(status: c_int) -> ExitStatus {
432
432
ExitStatus :: Signal ( imp:: WTERMSIG ( status) )
433
433
}
434
434
}
435
+
436
+ #[ cfg( test) ]
437
+ mod tests {
438
+ use super :: * ;
439
+ use prelude:: v1:: * ;
440
+
441
+ use ffi:: OsStr ;
442
+ use mem;
443
+ use ptr;
444
+ use libc;
445
+ use sys:: { self , c, cvt, pipe} ;
446
+
447
+ extern {
448
+ fn sigaddset ( set : * mut c:: sigset_t , signum : libc:: c_int ) -> libc:: c_int ;
449
+ }
450
+
451
+ #[ test]
452
+ fn test_process_mask ( ) {
453
+ unsafe {
454
+ // Test to make sure that a signal mask does not get inherited.
455
+ let cmd = Command :: new ( OsStr :: new ( "cat" ) ) ;
456
+ let ( stdin_read, stdin_write) = sys:: pipe:: anon_pipe ( ) . unwrap ( ) ;
457
+ let ( stdout_read, stdout_write) = sys:: pipe:: anon_pipe ( ) . unwrap ( ) ;
458
+
459
+ let mut set: c:: sigset_t = mem:: uninitialized ( ) ;
460
+ let mut old_set: c:: sigset_t = mem:: uninitialized ( ) ;
461
+ cvt ( c:: sigemptyset ( & mut set) ) . unwrap ( ) ;
462
+ cvt ( sigaddset ( & mut set, libc:: SIGINT ) ) . unwrap ( ) ;
463
+ cvt ( c:: pthread_sigmask ( c:: SIG_SETMASK , & set, & mut old_set) ) . unwrap ( ) ;
464
+
465
+ let cat = Process :: spawn ( & cmd, Stdio :: Piped ( stdin_read) ,
466
+ Stdio :: Piped ( stdout_write) ,
467
+ Stdio :: None ) . unwrap ( ) ;
468
+
469
+ cvt ( c:: pthread_sigmask ( c:: SIG_SETMASK , & old_set, ptr:: null_mut ( ) ) ) . unwrap ( ) ;
470
+
471
+ cvt ( libc:: funcs:: posix88:: signal:: kill ( cat. id ( ) as libc:: pid_t , libc:: SIGINT ) ) . unwrap ( ) ;
472
+ // We need to wait until SIGINT is definitely delivered. The
473
+ // easiest way is to write something to cat, and try to read it
474
+ // back: if SIGINT is unmasked, it'll get delivered when cat is
475
+ // next scheduled.
476
+ let _ = stdin_write. write ( b"Hello" ) ;
477
+ drop ( stdin_write) ;
478
+
479
+ // Either EOF or failure (EPIPE) is okay.
480
+ let mut buf = [ 0 ; 5 ] ;
481
+ if let Ok ( ret) = stdout_read. read ( & mut buf) {
482
+ assert ! ( ret == 0 ) ;
483
+ }
484
+
485
+ cat. wait ( ) . unwrap ( ) ;
486
+ }
487
+ }
488
+ }
0 commit comments