From 1b13f9e38cde45a82bc8899921f4f55954a16a67 Mon Sep 17 00:00:00 2001 From: Alexis Date: Sat, 8 Dec 2018 12:58:47 +0100 Subject: [PATCH] fix(custom): close endless scripts --- include/modules/custom.hpp | 2 ++ src/modules/custom.cpp | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 3dd31bff..40ff5f29 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -12,6 +12,7 @@ namespace waybar::modules { class Custom : public ALabel { public: Custom(const std::string, const Json::Value&); + ~Custom(); auto update() -> void; private: void delayWorker(); @@ -27,6 +28,7 @@ class Custom : public ALabel { waybar::util::SleeperThread thread_; waybar::util::command::res output_; waybar::util::JsonParser parser_; + FILE* fp_; }; } diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 3029a68f..f10ea208 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -2,7 +2,7 @@ waybar::modules::Custom::Custom(const std::string name, const Json::Value& config) - : ALabel(config, "{}"), name_(name) + : ALabel(config, "{}"), name_(name), fp_(nullptr) { if (config_["exec"].isString()) { if (interval_.count() > 0) { @@ -15,6 +15,14 @@ waybar::modules::Custom::Custom(const std::string name, } } +waybar::modules::Custom::~Custom() +{ + if (fp_) { + pclose(fp_); + fp_ = nullptr; + } +} + void waybar::modules::Custom::delayWorker() { thread_ = [this] { @@ -38,15 +46,16 @@ void waybar::modules::Custom::delayWorker() void waybar::modules::Custom::continuousWorker() { auto cmd = config_["exec"].asString(); - FILE* fp(popen(cmd.c_str(), "r")); - if (!fp) { + fp_ = popen(cmd.c_str(), "r"); + if (!fp_) { throw std::runtime_error("Unable to open " + cmd); } - thread_ = [this, fp] { + thread_ = [this] { char* buff = nullptr; size_t len = 0; - if (getline(&buff, &len, fp) == -1) { - pclose(fp); + if (getline(&buff, &len, fp_) == -1) { + pclose(fp_); + fp_ = nullptr; thread_.stop(); output_ = { 1, "" }; std::cerr << name_ + " just stopped, is it endless?" << std::endl;