Skip to content

Commit

Permalink
Make destroy() send SIGTERM and add a new method called
Browse files Browse the repository at this point in the history
force_destroy() that sends SIGKILL - as suggested by 
@thestinger.
  • Loading branch information
gareth authored and gareth committed Apr 11, 2013
1 parent 483e95a commit 995d444
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/libcore/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ pub mod consts {
pub static F_TLOCK : int = 2;
pub static F_ULOCK : int = 0;
pub static SIGKILL : int = 9;
pub static SIGTERM : int = 15;
}
pub mod posix01 {
}
Expand Down Expand Up @@ -932,6 +933,7 @@ pub mod consts {
pub static F_TLOCK : int = 2;
pub static F_ULOCK : int = 0;
pub static SIGKILL : int = 9;
pub static SIGTERM : int = 15;
}
pub mod posix01 {
}
Expand Down Expand Up @@ -1001,6 +1003,7 @@ pub mod consts {
pub static F_TLOCK : int = 2;
pub static F_ULOCK : int = 0;
pub static SIGKILL : int = 9;
pub static SIGTERM : int = 15;
}
pub mod posix01 {
}
Expand Down
53 changes: 41 additions & 12 deletions src/libcore/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,22 @@ pub trait Program {
fn finish(&mut self) -> int;

/**
* Forcibly terminate the program. On Posix OSs SIGKILL will be sent
* to the process. On Win32 TerminateProcess(..) will be called.
* Terminate the program, giving it a chance to clean itself up if
* this is supported by the operating system.
*
* On Posix OSs SIGTERM will be sent to the process. On Win32
* TerminateProcess(..) will be called.
*/
fn destroy(&mut self);

/**
* Terminate the program as soon as possible without giving it a
* chance to clean itself up.
*
* On Posix OSs SIGKILL will be sent to the process. On Win32
* TerminateProcess(..) will be called.
*/
fn force_destroy(&mut self);
}


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

fn destroy_repr(r: &mut ProgRepr) {
killpid(r.pid);
fn destroy_repr(r: &mut ProgRepr, force: bool) {
killpid(r.pid, force);
finish_repr(&mut *r);
close_repr_outputs(&mut *r);

#[cfg(windows)]
fn killpid(pid: pid_t) {
fn killpid(pid: pid_t, _force: bool) {
unsafe {
libc::funcs::extra::kernel32::TerminateProcess(
cast::transmute(pid), 1);
}
}

#[cfg(unix)]
fn killpid(pid: pid_t) {
fn killpid(pid: pid_t, force: bool) {

let signal = if force {
libc::consts::os::posix88::SIGKILL
} else {
libc::consts::os::posix88::SIGTERM
};

unsafe {
libc::funcs::posix88::signal::kill(
pid, libc::consts::os::posix88::SIGKILL as c_int);
libc::funcs::posix88::signal::kill(pid, signal as c_int);
}
}
}
Expand Down Expand Up @@ -321,7 +339,8 @@ pub fn start_program(prog: &str, args: &[~str]) -> @Program {
}
fn close_input(&mut self) { close_repr_input(&mut self.r); }
fn finish(&mut self) -> int { finish_repr(&mut self.r) }
fn destroy(&mut self) { destroy_repr(&mut self.r); }
fn destroy(&mut self) { destroy_repr(&mut self.r, false); }
fn force_destroy(&mut self) { destroy_repr(&mut self.r, true); }
}

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

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

os::remove_file(&path);

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

#[test]
#[cfg(unix)]
pub fn test_unforced_destroy_actually_kills() {
test_destroy_actually_kills(false);
}

#[test]
#[cfg(unix)]
pub fn test_forced_destroy_actually_kills() {
test_destroy_actually_kills(true);
}
}

// Local Variables:
Expand Down

5 comments on commit 995d444

@bors
Copy link
Contributor

@bors bors commented on 995d444 Apr 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at Dretch@995d444

@bors
Copy link
Contributor

@bors bors commented on 995d444 Apr 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Dretch/rust/murder-death-kill = 995d444 into auto

@bors
Copy link
Contributor

@bors bors commented on 995d444 Apr 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dretch/rust/murder-death-kill = 995d444 merged ok, testing candidate = e2d5ceb

@bors
Copy link
Contributor

@bors bors commented on 995d444 Apr 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 995d444 Apr 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = e2d5ceb

Please sign in to comment.