std: add ChildProcess.kill

This commit is contained in:
Andrew Kelley 2017-09-06 18:30:45 -04:00
parent 7e59f4ff69
commit 9f7e62b95b
2 changed files with 19 additions and 0 deletions

View File

@ -9,6 +9,9 @@ const BufMap = @import("../buf_map.zig").BufMap;
const builtin = @import("builtin");
const Os = builtin.Os;
error PermissionDenied;
error ProcessNotFound;
pub const ChildProcess = struct {
pid: i32,
err_pipe: [2]i32,
@ -43,6 +46,21 @@ pub const ChildProcess = struct {
}
}
/// Forcibly terminates child process and then cleans up all resources.
pub fn kill(self: &ChildProcess) -> %Term {
const ret = posix.kill(self.pid, posix.SIGTERM);
const err = posix.getErrno(ret);
if (err > 0) {
return switch (err) {
posix.EINVAL => unreachable,
posix.EPERM => error.PermissionDenied,
posix.ESRCH => error.ProcessNotFound,
else => error.Unexpected,
};
}
return self.wait();
}
/// Blocks until child process terminates and then cleans up all resources.
pub fn wait(self: &ChildProcess) -> %Term {
defer {

View File

@ -6,4 +6,5 @@ pub fn build(b: &Builder) {
exe.setBuildMode(mode);
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
}