diff --git a/daemon/daemonize.h b/daemon/daemonize.h deleted file mode 100644 index 5e7b142d..00000000 --- a/daemon/daemonize.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#ifndef MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H -#define MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H - -namespace cpputils { - void daemonize(); -} - -#endif diff --git a/daemon/daemonize.cpp b/process/daemonize.cpp similarity index 93% rename from daemon/daemonize.cpp rename to process/daemonize.cpp index 4e72c480..5207f803 100644 --- a/daemon/daemonize.cpp +++ b/process/daemonize.cpp @@ -44,7 +44,7 @@ namespace cpputils { exit(EXIT_FAILURE); } - // Close out the standard file descriptors. The daemon can't use them anyhow. + // Close out the standard file descriptors. The process can't use them anyhow. close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); diff --git a/process/daemonize.h b/process/daemonize.h new file mode 100644 index 00000000..b3cfc704 --- /dev/null +++ b/process/daemonize.h @@ -0,0 +1,9 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_PROCESS_DAEMONIZE_H +#define MESSMER_CPPUTILS_PROCESS_DAEMONIZE_H + +namespace cpputils { + void daemonize(); +} + +#endif diff --git a/process/subprocess.cpp b/process/subprocess.cpp new file mode 100644 index 00000000..4564b040 --- /dev/null +++ b/process/subprocess.cpp @@ -0,0 +1,29 @@ +#include "subprocess.h" +#include +#include + +using std::string; + +namespace cpputils { + string Subprocess::call(const string &command) { + //TODO Exception safety + FILE *subprocessOutput = popen(command.c_str(), "r"); + if (!subprocessOutput) + { + throw std::runtime_error("Error starting subprocess "+command); + } + + string result; + char buffer[1024]; + while(fgets(buffer, sizeof(buffer), subprocessOutput) != NULL) { + result += buffer; + } + + auto returncode = pclose(subprocessOutput); + if(WEXITSTATUS(returncode) != 0) { + throw std::runtime_error("Subprocess \""+command+"\" exited with code "+std::to_string(WEXITSTATUS(returncode))); + } + + return result; + } +} \ No newline at end of file diff --git a/process/subprocess.h b/process/subprocess.h new file mode 100644 index 00000000..2cffc288 --- /dev/null +++ b/process/subprocess.h @@ -0,0 +1,15 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_PROCESS_SUBPROCESS_H +#define MESSMER_CPPUTILS_PROCESS_SUBPROCESS_H + +#include + +namespace cpputils { + //TODO Test + class Subprocess { + public: + static std::string call(const std::string &command); + }; +} + +#endif diff --git a/test/daemon/daemonize_include_test.cpp b/test/process/daemonize_include_test.cpp similarity index 67% rename from test/daemon/daemonize_include_test.cpp rename to test/process/daemonize_include_test.cpp index 69fa6b6a..fb41a738 100644 --- a/test/daemon/daemonize_include_test.cpp +++ b/test/process/daemonize_include_test.cpp @@ -1,4 +1,4 @@ -#include "../../daemon/daemonize.h" +#include "../../process/daemonize.h" // Test the header can be included without needing additional dependencies diff --git a/test/process/subprocess_include_test.cpp b/test/process/subprocess_include_test.cpp new file mode 100644 index 00000000..df13b4af --- /dev/null +++ b/test/process/subprocess_include_test.cpp @@ -0,0 +1,4 @@ +#include "../../process/subprocess.h" + +// Test the header can be included without needing additional dependencies +