From 9f7e62b95bf36b7a867ffcee576c929e9564c303 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 6 Sep 2017 18:30:45 -0400 Subject: [PATCH] std: add ChildProcess.kill --- std/os/child_process.zig | 18 ++++++++++++++++++ std/special/build_file_template.zig | 1 + 2 files changed, 19 insertions(+) diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 88eaca9d52..149c3aca12 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -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 { diff --git a/std/special/build_file_template.zig b/std/special/build_file_template.zig index db560c1c05..a3dfbc0c2c 100644 --- a/std/special/build_file_template.zig +++ b/std/special/build_file_template.zig @@ -6,4 +6,5 @@ pub fn build(b: &Builder) { exe.setBuildMode(mode); b.default_step.dependOn(&exe.step); + b.installArtifact(exe); }