Skip to content

Commit 995d444

Browse files
garethgareth
gareth
authored and
gareth
committed
Make destroy() send SIGTERM and add a new method called
force_destroy() that sends SIGKILL - as suggested by @thestinger.
1 parent 483e95a commit 995d444

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/libcore/libc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ pub mod consts {
864864
pub static F_TLOCK : int = 2;
865865
pub static F_ULOCK : int = 0;
866866
pub static SIGKILL : int = 9;
867+
pub static SIGTERM : int = 15;
867868
}
868869
pub mod posix01 {
869870
}
@@ -932,6 +933,7 @@ pub mod consts {
932933
pub static F_TLOCK : int = 2;
933934
pub static F_ULOCK : int = 0;
934935
pub static SIGKILL : int = 9;
936+
pub static SIGTERM : int = 15;
935937
}
936938
pub mod posix01 {
937939
}
@@ -1001,6 +1003,7 @@ pub mod consts {
10011003
pub static F_TLOCK : int = 2;
10021004
pub static F_ULOCK : int = 0;
10031005
pub static SIGKILL : int = 9;
1006+
pub static SIGTERM : int = 15;
10041007
}
10051008
pub mod posix01 {
10061009
}

src/libcore/run.rs

+41-12
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,22 @@ pub trait Program {
6363
fn finish(&mut self) -> int;
6464

6565
/**
66-
* Forcibly terminate the program. On Posix OSs SIGKILL will be sent
67-
* to the process. On Win32 TerminateProcess(..) will be called.
66+
* Terminate the program, giving it a chance to clean itself up if
67+
* this is supported by the operating system.
68+
*
69+
* On Posix OSs SIGTERM will be sent to the process. On Win32
70+
* TerminateProcess(..) will be called.
6871
*/
6972
fn destroy(&mut self);
73+
74+
/**
75+
* Terminate the program as soon as possible without giving it a
76+
* chance to clean itself up.
77+
*
78+
* On Posix OSs SIGKILL will be sent to the process. On Win32
79+
* TerminateProcess(..) will be called.
80+
*/
81+
fn force_destroy(&mut self);
7082
}
7183

7284

@@ -266,24 +278,30 @@ pub fn start_program(prog: &str, args: &[~str]) -> @Program {
266278
return waitpid(r.pid);
267279
}
268280

269-
fn destroy_repr(r: &mut ProgRepr) {
270-
killpid(r.pid);
281+
fn destroy_repr(r: &mut ProgRepr, force: bool) {
282+
killpid(r.pid, force);
271283
finish_repr(&mut *r);
272284
close_repr_outputs(&mut *r);
273285

274286
#[cfg(windows)]
275-
fn killpid(pid: pid_t) {
287+
fn killpid(pid: pid_t, _force: bool) {
276288
unsafe {
277289
libc::funcs::extra::kernel32::TerminateProcess(
278290
cast::transmute(pid), 1);
279291
}
280292
}
281293

282294
#[cfg(unix)]
283-
fn killpid(pid: pid_t) {
295+
fn killpid(pid: pid_t, force: bool) {
296+
297+
let signal = if force {
298+
libc::consts::os::posix88::SIGKILL
299+
} else {
300+
libc::consts::os::posix88::SIGTERM
301+
};
302+
284303
unsafe {
285-
libc::funcs::posix88::signal::kill(
286-
pid, libc::consts::os::posix88::SIGKILL as c_int);
304+
libc::funcs::posix88::signal::kill(pid, signal as c_int);
287305
}
288306
}
289307
}
@@ -321,7 +339,8 @@ pub fn start_program(prog: &str, args: &[~str]) -> @Program {
321339
}
322340
fn close_input(&mut self) { close_repr_input(&mut self.r); }
323341
fn finish(&mut self) -> int { finish_repr(&mut self.r) }
324-
fn destroy(&mut self) { destroy_repr(&mut self.r); }
342+
fn destroy(&mut self) { destroy_repr(&mut self.r, false); }
343+
fn force_destroy(&mut self) { destroy_repr(&mut self.r, true); }
325344
}
326345

327346
let mut repr = ProgRepr {
@@ -559,10 +578,9 @@ mod tests {
559578
p.destroy(); // ...and nor should this (and nor should the destructor)
560579
}
561580

562-
#[test]
563581
#[cfg(unix)] // there is no way to sleep on windows from inside libcore...
564-
pub fn test_destroy_actually_kills() {
565-
let path = Path("test/core-run-test-destroy-actually-kills.tmp");
582+
pub fn test_destroy_actually_kills(force: bool) {
583+
let path = Path(fmt!("test/core-run-test-destroy-actually-kills-%?.tmp", force));
566584

567585
os::remove_file(&path);
568586

@@ -580,6 +598,17 @@ mod tests {
580598
assert!(!path.exists());
581599
}
582600

601+
#[test]
602+
#[cfg(unix)]
603+
pub fn test_unforced_destroy_actually_kills() {
604+
test_destroy_actually_kills(false);
605+
}
606+
607+
#[test]
608+
#[cfg(unix)]
609+
pub fn test_forced_destroy_actually_kills() {
610+
test_destroy_actually_kills(true);
611+
}
583612
}
584613

585614
// Local Variables:

0 commit comments

Comments
 (0)