Renamed daemon folder to process and added Subprocess class to it

This commit is contained in:
Sebastian Messmer 2015-11-03 12:20:15 -08:00
parent 4ced78b07b
commit 8b585c39fe
7 changed files with 59 additions and 11 deletions

View File

@ -1,9 +0,0 @@
#pragma once
#ifndef MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
#define MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
namespace cpputils {
void daemonize();
}
#endif

View File

@ -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);

9
process/daemonize.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#ifndef MESSMER_CPPUTILS_PROCESS_DAEMONIZE_H
#define MESSMER_CPPUTILS_PROCESS_DAEMONIZE_H
namespace cpputils {
void daemonize();
}
#endif

29
process/subprocess.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "subprocess.h"
#include <cstdio>
#include <stdexcept>
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;
}
}

15
process/subprocess.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#ifndef MESSMER_CPPUTILS_PROCESS_SUBPROCESS_H
#define MESSMER_CPPUTILS_PROCESS_SUBPROCESS_H
#include <string>
namespace cpputils {
//TODO Test
class Subprocess {
public:
static std::string call(const std::string &command);
};
}
#endif

View File

@ -1,4 +1,4 @@
#include "../../daemon/daemonize.h"
#include "../../process/daemonize.h"
// Test the header can be included without needing additional dependencies

View File

@ -0,0 +1,4 @@
#include "../../process/subprocess.h"
// Test the header can be included without needing additional dependencies