Skip to content

Commit 120c654

Browse files
committed
auto merge of #13957 : alexcrichton/rust/process-wait, r=brson
This implements set_timeout() for std::io::Process which will affect wait() operations on the process. This follows the same pattern as the rest of the timeouts emerging in std::io::net. The implementation was super easy for everything except libnative on unix (backwards from usual!), which required a good bit of signal handling. There's a doc comment explaining the strategy in libnative. Internally, this also required refactoring the "helper thread" implementation used by libnative to allow for an extra helper thread (not just the timer). This is a breaking change in terms of the io::Process API. It is now possible for wait() to fail, and subsequently wait_with_output(). These two functions now return IoResult<T> due to the fact that they can time out. Additionally, the wait_with_output() function has moved from taking `&mut self` to taking `self`. If a timeout occurs while waiting with output, the semantics are undesirable in almost all cases if attempting to re-wait on the process. Equivalent functionality can still be achieved by dealing with the output handles manually. [breaking-change] cc #13523
2 parents 72fc4a5 + b0546f1 commit 120c654

22 files changed

+871
-324
lines changed

src/compiletest/procsrv.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ pub fn run(lib_path: &str,
6868
input: Option<~str>) -> Option<Result> {
6969

7070
let env = env.clone().append(target_env(lib_path, prog).as_slice());
71-
let mut opt_process = Process::configure(ProcessConfig {
71+
let opt_process = Process::configure(ProcessConfig {
7272
program: prog,
7373
args: args,
7474
env: Some(env.as_slice()),
7575
.. ProcessConfig::new()
7676
});
7777

7878
match opt_process {
79-
Ok(ref mut process) => {
79+
Ok(mut process) => {
8080
for input in input.iter() {
8181
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
8282
}
83-
let ProcessOutput { status, output, error } = process.wait_with_output();
83+
let ProcessOutput { status, output, error } =
84+
process.wait_with_output().unwrap();
8485

8586
Some(Result {
8687
status: status,

src/compiletest/runtest.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -482,16 +482,17 @@ fn run_debuginfo_lldb_test(config: &config, props: &TestProps, testfile: &Path)
482482
let args = &[lldb_batchmode_script, test_executable_str, debugger_script_str];
483483
let env = &[("PYTHONPATH".to_owned(), config.lldb_python_dir.clone().unwrap())];
484484

485-
let mut opt_process = Process::configure(ProcessConfig {
485+
let opt_process = Process::configure(ProcessConfig {
486486
program: "python",
487487
args: args,
488488
env: Some(env),
489489
.. ProcessConfig::new()
490490
});
491491

492492
let (status, out, err) = match opt_process {
493-
Ok(ref mut process) => {
494-
let ProcessOutput { status, output, error } = process.wait_with_output();
493+
Ok(process) => {
494+
let ProcessOutput { status, output, error } =
495+
process.wait_with_output().unwrap();
495496

496497
(status,
497498
str::from_utf8(output.as_slice()).unwrap().to_owned(),

src/libcore/str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,7 @@ impl<'a> StrSlice<'a> for &'a str {
17251725
#[inline]
17261726
fn is_char_boundary(&self, index: uint) -> bool {
17271727
if index == self.len() { return true; }
1728+
if index > self.len() { return false; }
17281729
let b = self[index];
17291730
return b < 128u8 || b >= 192u8;
17301731
}

src/liblibc/lib.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub use funcs::bsd43::{shutdown};
173173
#[cfg(unix)] pub use consts::os::posix88::{EADDRINUSE, ENOENT, EISDIR, EAGAIN, EWOULDBLOCK};
174174
#[cfg(unix)] pub use consts::os::posix88::{ECANCELED, SIGINT, EINPROGRESS};
175175
#[cfg(unix)] pub use consts::os::posix88::{SIGTERM, SIGKILL, SIGPIPE, PROT_NONE};
176-
#[cfg(unix)] pub use consts::os::posix01::{SIG_IGN, WNOHANG};
176+
#[cfg(unix)] pub use consts::os::posix01::{SIG_IGN};
177177
#[cfg(unix)] pub use consts::os::bsd44::{AF_UNIX};
178178

179179
#[cfg(unix)] pub use types::os::common::posix01::{pthread_t, timespec, timezone};
@@ -2461,8 +2461,6 @@ pub mod consts {
24612461

24622462
pub static CLOCK_REALTIME: c_int = 0;
24632463
pub static CLOCK_MONOTONIC: c_int = 1;
2464-
2465-
pub static WNOHANG: c_int = 1;
24662464
}
24672465
pub mod posix08 {
24682466
}
@@ -2912,8 +2910,6 @@ pub mod consts {
29122910

29132911
pub static CLOCK_REALTIME: c_int = 0;
29142912
pub static CLOCK_MONOTONIC: c_int = 4;
2915-
2916-
pub static WNOHANG: c_int = 1;
29172913
}
29182914
pub mod posix08 {
29192915
}
@@ -3301,8 +3297,6 @@ pub mod consts {
33013297
pub static PTHREAD_CREATE_JOINABLE: c_int = 1;
33023298
pub static PTHREAD_CREATE_DETACHED: c_int = 2;
33033299
pub static PTHREAD_STACK_MIN: size_t = 8192;
3304-
3305-
pub static WNOHANG: c_int = 1;
33063300
}
33073301
pub mod posix08 {
33083302
}
@@ -3968,16 +3962,6 @@ pub mod funcs {
39683962
}
39693963
}
39703964

3971-
pub mod wait {
3972-
use types::os::arch::c95::{c_int};
3973-
use types::os::arch::posix88::{pid_t};
3974-
3975-
extern {
3976-
pub fn waitpid(pid: pid_t, status: *mut c_int, options: c_int)
3977-
-> pid_t;
3978-
}
3979-
}
3980-
39813965
pub mod glob {
39823966
use types::os::arch::c95::{c_char, c_int};
39833967
use types::os::common::posix01::{glob_t};

src/libnative/io/c_unix.rs

+109
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
//! C definitions used by libnative that don't belong in liblibc
1212
13+
#![allow(dead_code)]
14+
1315
pub use self::select::fd_set;
16+
pub use self::signal::{sigaction, siginfo, sigset_t};
17+
pub use self::signal::{SA_ONSTACK, SA_RESTART, SA_RESETHAND, SA_NOCLDSTOP};
18+
pub use self::signal::{SA_NODEFER, SA_NOCLDWAIT, SA_SIGINFO, SIGCHLD};
1419

1520
use libc;
1621

@@ -34,6 +39,8 @@ pub static MSG_DONTWAIT: libc::c_int = 0x80;
3439
#[cfg(target_os = "android")]
3540
pub static MSG_DONTWAIT: libc::c_int = 0x40;
3641

42+
pub static WNOHANG: libc::c_int = 1;
43+
3744
extern {
3845
pub fn gettimeofday(timeval: *mut libc::timeval,
3946
tzp: *libc::c_void) -> libc::c_int;
@@ -49,6 +56,17 @@ extern {
4956
optlen: *mut libc::socklen_t) -> libc::c_int;
5057
pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int;
5158

59+
60+
pub fn waitpid(pid: libc::pid_t, status: *mut libc::c_int,
61+
options: libc::c_int) -> libc::pid_t;
62+
63+
pub fn sigaction(signum: libc::c_int,
64+
act: *sigaction,
65+
oldact: *mut sigaction) -> libc::c_int;
66+
67+
pub fn sigaddset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
68+
pub fn sigdelset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
69+
pub fn sigemptyset(set: *mut sigset_t) -> libc::c_int;
5270
}
5371

5472
#[cfg(target_os = "macos")]
@@ -81,3 +99,94 @@ mod select {
8199
set.fds_bits[fd / uint::BITS] |= 1 << (fd % uint::BITS);
82100
}
83101
}
102+
103+
#[cfg(target_os = "linux")]
104+
#[cfg(target_os = "android")]
105+
mod signal {
106+
use libc;
107+
108+
pub static SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
109+
pub static SA_NOCLDWAIT: libc::c_ulong = 0x00000002;
110+
pub static SA_NODEFER: libc::c_ulong = 0x40000000;
111+
pub static SA_ONSTACK: libc::c_ulong = 0x08000000;
112+
pub static SA_RESETHAND: libc::c_ulong = 0x80000000;
113+
pub static SA_RESTART: libc::c_ulong = 0x10000000;
114+
pub static SA_SIGINFO: libc::c_ulong = 0x00000004;
115+
pub static SIGCHLD: libc::c_int = 17;
116+
117+
// This definition is not as accurate as it could be, {pid, uid, status} is
118+
// actually a giant union. Currently we're only interested in these fields,
119+
// however.
120+
pub struct siginfo {
121+
si_signo: libc::c_int,
122+
si_errno: libc::c_int,
123+
si_code: libc::c_int,
124+
pub pid: libc::pid_t,
125+
pub uid: libc::uid_t,
126+
pub status: libc::c_int,
127+
}
128+
129+
pub struct sigaction {
130+
pub sa_handler: extern fn(libc::c_int),
131+
pub sa_mask: sigset_t,
132+
pub sa_flags: libc::c_ulong,
133+
sa_restorer: *mut libc::c_void,
134+
}
135+
136+
#[cfg(target_word_size = "32")]
137+
pub struct sigset_t {
138+
__val: [libc::c_ulong, ..32],
139+
}
140+
#[cfg(target_word_size = "64")]
141+
pub struct sigset_t {
142+
__val: [libc::c_ulong, ..16],
143+
}
144+
}
145+
146+
#[cfg(target_os = "macos")]
147+
#[cfg(target_os = "freebsd")]
148+
mod signal {
149+
use libc;
150+
151+
pub static SA_ONSTACK: libc::c_int = 0x0001;
152+
pub static SA_RESTART: libc::c_int = 0x0002;
153+
pub static SA_RESETHAND: libc::c_int = 0x0004;
154+
pub static SA_NOCLDSTOP: libc::c_int = 0x0008;
155+
pub static SA_NODEFER: libc::c_int = 0x0010;
156+
pub static SA_NOCLDWAIT: libc::c_int = 0x0020;
157+
pub static SA_SIGINFO: libc::c_int = 0x0040;
158+
pub static SIGCHLD: libc::c_int = 20;
159+
160+
#[cfg(target_os = "macos")]
161+
pub type sigset_t = u32;
162+
#[cfg(target_os = "freebsd")]
163+
pub struct sigset_t {
164+
bits: [u32, ..4],
165+
}
166+
167+
// This structure has more fields, but we're not all that interested in
168+
// them.
169+
pub struct siginfo {
170+
pub si_signo: libc::c_int,
171+
pub si_errno: libc::c_int,
172+
pub si_code: libc::c_int,
173+
pub pid: libc::pid_t,
174+
pub uid: libc::uid_t,
175+
pub status: libc::c_int,
176+
}
177+
178+
#[cfg(target_os = "macos")]
179+
pub struct sigaction {
180+
pub sa_handler: extern fn(libc::c_int),
181+
sa_tramp: *mut libc::c_void,
182+
pub sa_mask: sigset_t,
183+
pub sa_flags: libc::c_int,
184+
}
185+
186+
#[cfg(target_os = "freebsd")]
187+
pub struct sigaction {
188+
pub sa_handler: extern fn(libc::c_int),
189+
pub sa_flags: libc::c_int,
190+
pub sa_mask: sigset_t,
191+
}
192+
}

0 commit comments

Comments
 (0)