Commit 9f7e62b95b

Andrew Kelley <superjoe30@gmail.com>
2017-09-07 00:30:45
std: add ChildProcess.kill
1 parent 7e59f4f
Changed files (2)
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 {
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);
 }