Skip to content

Commit f09592a

Browse files
committed
io: Implement process wait timeouts
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
1 parent 9f7caed commit f09592a

23 files changed

+876
-326
lines changed

src/compiletest/procsrv.rs

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,16 +502,17 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path)
502502
let args = &[lldb_batchmode_script, test_executable_str, debugger_script_str];
503503
let env = &[("PYTHONPATH".to_owned(), config.lldb_python_dir.clone().unwrap())];
504504

505-
let mut opt_process = Process::configure(ProcessConfig {
505+
let opt_process = Process::configure(ProcessConfig {
506506
program: "python",
507507
args: args,
508508
env: Some(env),
509509
.. ProcessConfig::new()
510510
});
511511

512512
let (status, out, err) = match opt_process {
513-
Ok(ref mut process) => {
514-
let ProcessOutput { status, output, error } = process.wait_with_output();
513+
Ok(process) => {
514+
let ProcessOutput { status, output, error } =
515+
process.wait_with_output().unwrap();
515516

516517
(status,
517518
str::from_utf8(output.as_slice()).unwrap().to_owned(),

src/libcore/str.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 17 deletions
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};
@@ -2473,8 +2473,6 @@ pub mod consts {
24732473

24742474
pub static CLOCK_REALTIME: c_int = 0;
24752475
pub static CLOCK_MONOTONIC: c_int = 1;
2476-
2477-
pub static WNOHANG: c_int = 1;
24782476
}
24792477
pub mod posix08 {
24802478
}
@@ -2924,8 +2922,6 @@ pub mod consts {
29242922

29252923
pub static CLOCK_REALTIME: c_int = 0;
29262924
pub static CLOCK_MONOTONIC: c_int = 4;
2927-
2928-
pub static WNOHANG: c_int = 1;
29292925
}
29302926
pub mod posix08 {
29312927
}
@@ -3313,8 +3309,6 @@ pub mod consts {
33133309
pub static PTHREAD_CREATE_JOINABLE: c_int = 1;
33143310
pub static PTHREAD_CREATE_DETACHED: c_int = 2;
33153311
pub static PTHREAD_STACK_MIN: size_t = 8192;
3316-
3317-
pub static WNOHANG: c_int = 1;
33183312
}
33193313
pub mod posix08 {
33203314
}
@@ -3980,16 +3974,6 @@ pub mod funcs {
39803974
}
39813975
}
39823976

3983-
pub mod wait {
3984-
use types::os::arch::c95::{c_int};
3985-
use types::os::arch::posix88::{pid_t};
3986-
3987-
extern {
3988-
pub fn waitpid(pid: pid_t, status: *mut c_int, options: c_int)
3989-
-> pid_t;
3990-
}
3991-
}
3992-
39933977
pub mod glob {
39943978
use types::os::arch::c95::{c_char, c_int};
39953979
use types::os::common::posix01::{glob_t};

src/libnative/io/c_unix.rs

Lines changed: 109 additions & 0 deletions
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)