2022-01-26 12:11:52 +01:00
|
|
|
#include "CommandExecutor.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "fsfw/container/DynamicFIFO.h"
|
|
|
|
#include "fsfw/container/SimpleRingBuffer.h"
|
|
|
|
#include "fsfw/serviceinterface.h"
|
|
|
|
|
|
|
|
CommandExecutor::CommandExecutor(const size_t maxSize) : readVec(maxSize) {
|
|
|
|
waiter.events = POLLIN;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandExecutor::load(std::string command, bool blocking, bool printOutput) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (state == States::PENDING) {
|
|
|
|
return COMMAND_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentCmd = command;
|
|
|
|
this->blocking = blocking;
|
|
|
|
this->printOutput = printOutput;
|
|
|
|
if (state == States::IDLE) {
|
|
|
|
state = States::COMMAND_LOADED;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandExecutor::execute() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (state == States::IDLE) {
|
|
|
|
return NO_COMMAND_LOADED_OR_PENDING;
|
|
|
|
} else if (state == States::PENDING) {
|
|
|
|
return COMMAND_PENDING;
|
|
|
|
}
|
2022-05-11 16:12:24 +02:00
|
|
|
// Reset data in read vector
|
|
|
|
std::memset(readVec.data(), 0, readVec.size());
|
2022-02-02 10:29:30 +01:00
|
|
|
currentCmdFile = popen(currentCmd.c_str(), "r");
|
|
|
|
if (currentCmdFile == nullptr) {
|
|
|
|
lastError = errno;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
if (blocking) {
|
|
|
|
ReturnValue_t result = executeBlocking();
|
|
|
|
state = States::IDLE;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
currentFd = fileno(currentCmdFile);
|
|
|
|
waiter.fd = currentFd;
|
|
|
|
}
|
|
|
|
state = States::PENDING;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandExecutor::close() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (state == States::PENDING) {
|
|
|
|
// Attempt to close process, irrespective of if it is running or not
|
|
|
|
if (currentCmdFile != nullptr) {
|
|
|
|
pclose(currentCmdFile);
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandExecutor::printLastError(std::string funcName) const {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (lastError != 0) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << funcName << " pclose failed with code " << lastError << ": "
|
|
|
|
<< strerror(lastError) << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printError("%s pclose failed with code %d: %s\n", funcName, lastError,
|
|
|
|
strerror(lastError));
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void CommandExecutor::setRingBuffer(SimpleRingBuffer* ringBuffer,
|
|
|
|
DynamicFIFO<uint16_t>* sizesFifo) {
|
|
|
|
this->ringBuffer = ringBuffer;
|
|
|
|
this->sizesFifo = sizesFifo;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandExecutor::check(bool& replyReceived) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (blocking) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
switch (state) {
|
|
|
|
case (States::IDLE):
|
|
|
|
case (States::COMMAND_LOADED): {
|
|
|
|
return NO_COMMAND_LOADED_OR_PENDING;
|
|
|
|
}
|
|
|
|
case (States::PENDING): {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = poll(&waiter, 1, 0);
|
|
|
|
switch (result) {
|
|
|
|
case (0): {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (1): {
|
|
|
|
if (waiter.revents & POLLIN) {
|
|
|
|
ssize_t readBytes = read(currentFd, readVec.data(), readVec.size());
|
|
|
|
if (readBytes == 0) {
|
|
|
|
// Should not happen
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "CommandExecutor::check: No bytes read "
|
|
|
|
"after poll event.."
|
|
|
|
<< std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning("CommandExecutor::check: No bytes read after poll event..\n");
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
break;
|
|
|
|
} else if (readBytes > 0) {
|
|
|
|
replyReceived = true;
|
|
|
|
if (printOutput) {
|
|
|
|
// It is assumed the command output is line terminated
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << currentCmd << " | " << readVec.data();
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("%s | %s", currentCmd, readVec.data());
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
if (ringBuffer != nullptr) {
|
|
|
|
ringBuffer->writeData(reinterpret_cast<const uint8_t*>(readVec.data()), readBytes);
|
|
|
|
}
|
|
|
|
if (sizesFifo != nullptr) {
|
|
|
|
if (not sizesFifo->full()) {
|
|
|
|
sizesFifo->insert(readBytes);
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Should also not happen
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "CommandExecutor::check: Error " << errno << ": " << strerror(errno)
|
|
|
|
<< std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning("CommandExecutor::check: Error %d: %s\n", errno, strerror(errno));
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
if (waiter.revents & POLLERR) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "CommandExecuter::check: Poll error" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning("CommandExecuter::check: Poll error\n");
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return COMMAND_ERROR;
|
|
|
|
}
|
|
|
|
if (waiter.revents & POLLHUP) {
|
|
|
|
result = pclose(currentCmdFile);
|
|
|
|
ReturnValue_t retval = EXECUTION_FINISHED;
|
|
|
|
if (result != 0) {
|
|
|
|
lastError = result;
|
|
|
|
retval = HasReturnvaluesIF::RETURN_FAILED;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
state = States::IDLE;
|
|
|
|
currentCmdFile = nullptr;
|
|
|
|
currentFd = 0;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandExecutor::reset() {
|
2022-02-02 10:29:30 +01:00
|
|
|
CommandExecutor::close();
|
|
|
|
currentCmdFile = nullptr;
|
|
|
|
currentFd = 0;
|
|
|
|
state = States::IDLE;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CommandExecutor::getLastError() const {
|
2022-02-02 10:29:30 +01:00
|
|
|
// See:
|
|
|
|
// https://stackoverflow.com/questions/808541/any-benefit-in-using-wexitstatus-macro-in-c-over-division-by-256-on-exit-statu
|
|
|
|
return WEXITSTATUS(this->lastError);
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
CommandExecutor::States CommandExecutor::getCurrentState() const { return state; }
|
2022-01-26 12:11:52 +01:00
|
|
|
|
|
|
|
ReturnValue_t CommandExecutor::executeBlocking() {
|
2022-02-02 10:29:30 +01:00
|
|
|
while (fgets(readVec.data(), readVec.size(), currentCmdFile) != nullptr) {
|
|
|
|
std::string output(readVec.data());
|
|
|
|
if (printOutput) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << currentCmd << " | " << output;
|
2022-01-26 12:11:52 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("%s | %s", currentCmd, output);
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
if (ringBuffer != nullptr) {
|
|
|
|
ringBuffer->writeData(reinterpret_cast<const uint8_t*>(output.data()), output.size());
|
|
|
|
}
|
|
|
|
if (sizesFifo != nullptr) {
|
|
|
|
if (not sizesFifo->full()) {
|
|
|
|
sizesFifo->insert(output.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int result = pclose(currentCmdFile);
|
|
|
|
if (result != 0) {
|
|
|
|
lastError = result;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2022-01-26 12:11:52 +01:00
|
|
|
}
|
2022-05-11 16:12:24 +02:00
|
|
|
|
|
|
|
const std::vector<char>& CommandExecutor::getReadVector() const {
|
|
|
|
return readVec;
|
|
|
|
}
|