From cdd80434898d19b9515573734979a4851da49c2c Mon Sep 17 00:00:00 2001 From: flightlessmango Date: Mon, 8 Apr 2024 09:44:54 +0200 Subject: [PATCH] param: shell: fix unused variable warning --- src/shell.cpp | 23 +++++++++++++++++++++-- src/shell.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/shell.cpp b/src/shell.cpp index 84277aa8..83d7c529 100644 --- a/src/shell.cpp +++ b/src/shell.cpp @@ -2,6 +2,7 @@ #include #include #include +#include std::string Shell::readOutput() { std::string output; @@ -37,8 +38,22 @@ std::string Shell::readOutput() { } Shell::Shell() { - pipe(to_shell); - pipe(from_shell); + static bool failed; + if (pipe(to_shell) == -1) { + SPDLOG_ERROR("Failed to create to_shell pipe: {}", strerror(errno)); + failed = true; + } + + if (pipe(from_shell) == -1) { + SPDLOG_ERROR("Failed to create from_shell pipe: {}", strerror(errno)); + failed = true; + } + + // if either pipe fails, there's no point in continuing. + if (failed){ + SPDLOG_ERROR("Shell has failed, will not be able to use exec"); + return; + } shell_pid = fork(); @@ -58,9 +73,13 @@ Shell::Shell() { // Set the read end of the from_shell pipe to non-blocking setNonBlocking(from_shell[0]); } + success = true; } std::string Shell::exec(std::string cmd) { + if (!success) + return ""; + writeCommand(cmd); return readOutput(); } diff --git a/src/shell.h b/src/shell.h index 7e625882..1154a3f3 100644 --- a/src/shell.h +++ b/src/shell.h @@ -13,6 +13,7 @@ private: int to_shell[2]; int from_shell[2]; pid_t shell_pid; + bool success; #ifdef __linux__ void setNonBlocking(int fd) {