]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/process-detach.rs
auto merge of #13600 : brandonw/rust/master, r=brson
[rust.git] / src / test / run-pass / process-detach.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-win32
12 // ignore-android
13
14 // This test ensures that the 'detach' field on processes does the right thing.
15 // By detaching the child process, they should be put into a separate process
16 // group. We test this by spawning a detached process, then killing our own
17 // group with a signal.
18 //
19 // Note that the first thing we do is put ourselves in our own process group so
20 // we don't interfere with other running tests.
21
22 extern crate green;
23 extern crate rustuv;
24 extern crate libc;
25
26 use std::io::process;
27 use std::io::signal::{Listener, Interrupt};
28
29 #[start]
30 fn start(argc: int, argv: **u8) -> int {
31     green::start(argc, argv, rustuv::event_loop, main)
32 }
33
34 fn main() {
35     unsafe { libc::setsid(); }
36
37     let config = process::ProcessConfig {
38         program : "/bin/sh",
39         args: &[~"-c", ~"read a"],
40         detach: true,
41         .. process::ProcessConfig::new()
42     };
43
44     // we shouldn't die because of an interrupt
45     let mut l = Listener::new();
46     l.register(Interrupt).unwrap();
47
48     // spawn the child
49     let mut p = process::Process::configure(config).unwrap();
50
51     // send an interrupt to everyone in our process group
52     unsafe { libc::funcs::posix88::signal::kill(0, libc::SIGINT); }
53
54     // Wait for the child process to die (terminate it's stdin and the read
55     // should fail).
56     drop(p.stdin.take());
57     match p.wait() {
58         process::ExitStatus(..) => {}
59         process::ExitSignal(..) => fail!()
60     }
61 }
62