Renamed daemon folder to process and added Subprocess class to it
This commit is contained in:
parent
4ced78b07b
commit
8b585c39fe
@ -1,9 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
|
|
||||||
#define MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
|
|
||||||
|
|
||||||
namespace cpputils {
|
|
||||||
void daemonize();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -44,7 +44,7 @@ namespace cpputils {
|
|||||||
exit(EXIT_FAILURE);
|
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(STDIN_FILENO);
|
||||||
close(STDOUT_FILENO);
|
close(STDOUT_FILENO);
|
||||||
close(STDERR_FILENO);
|
close(STDERR_FILENO);
|
9
process/daemonize.h
Normal file
9
process/daemonize.h
Normal 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
29
process/subprocess.cpp
Normal 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
15
process/subprocess.h
Normal 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
|
@ -1,4 +1,4 @@
|
|||||||
#include "../../daemon/daemonize.h"
|
#include "../../process/daemonize.h"
|
||||||
|
|
||||||
// Test the header can be included without needing additional dependencies
|
// Test the header can be included without needing additional dependencies
|
||||||
|
|
4
test/process/subprocess_include_test.cpp
Normal file
4
test/process/subprocess_include_test.cpp
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "../../process/subprocess.h"
|
||||||
|
|
||||||
|
// Test the header can be included without needing additional dependencies
|
||||||
|
|
Loading…
Reference in New Issue
Block a user