]> git.localhorst.tv Git - blank.git/blobdiff - src/app/proc.cpp
first test for actual program binary
[blank.git] / src / app / proc.cpp
index 33d1a14c9c83a8126d4bd039897aaa6a278b58cb..3137b0433a990e839a691a3d59f5d023e1c22cf7 100644 (file)
@@ -1,14 +1,16 @@
 #include "Process.hpp"
 
 #ifdef _WIN32
-#  error "TODO: windows implementation of Process"
+#  include <tchar.h>
+#  include <windows.h>
 #else
-#  include <cstdio>
 #  include <fcntl.h>
+#  include <signal.h>
 #  include <unistd.h>
 #  include <sys/wait.h>
 #endif
 
+#include <cstdio>
 #include <stdexcept>
 
 using namespace std;
@@ -28,6 +30,7 @@ struct Process::Impl {
        size_t ReadOut(void *buffer, size_t max_len);
        size_t ReadErr(void *buffer, size_t max_len);
 
+       void Terminate();
        int Join();
 
 #ifdef _WIN32
@@ -72,6 +75,12 @@ size_t Process::ReadErr(void *buffer, size_t max_len) {
        return impl->ReadErr(buffer, max_len);
 }
 
+void Process::Terminate() {
+       if (!joined) {
+               impl->Terminate();
+       }
+}
+
 int Process::Join() {
        if (joined) {
                return status;
@@ -266,6 +275,16 @@ size_t Process::Impl::ReadErr(void *buffer, size_t max_len) {
 #endif
 }
 
+void Process::Impl::Terminate() {
+#ifdef _WIN32
+       if (!TerminateProcess(pi.hProcess, -1)) {
+#else
+       if (kill(pid, SIGTERM) == -1) {
+#endif
+               throw runtime_error("failed to terminate child process");
+       }
+}
+
 int Process::Impl::Join() {
 #ifdef _WIN32
        CloseHandle(fd_in[1]);