From 30ada00ab85a3b9ee770dcc4ae878f3151b81487 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 12 Feb 2021 21:24:28 +0100 Subject: Matrix: prevent new programs from being launched after a task is killed --- src/Program.cpp | 60 +++++++++++++++++++++++++++++++++++++++---------- src/QuickMedia.cpp | 9 ++++++++ src/plugins/Matrix.cpp | 5 ++++- src/plugins/Youtube.cpp | 2 ++ 4 files changed, 63 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/Program.cpp b/src/Program.cpp index 736532e..dc92f16 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -14,37 +14,73 @@ #define READ_END 0 #define WRITE_END 1 +struct ThreadProgram { + ReadProgram read_program; + bool killed; +}; + +static std::unordered_map thread_current_program; +static std::mutex thread_current_program_mutex; + class CurrentThreadProgram { public: + CurrentThreadProgram() { + std::lock_guard lock(thread_current_program_mutex); + ThreadProgram thread_program; + thread_program.read_program.pid = -1; + thread_program.read_program.read_fd = -1; + thread_program.killed = false; + thread_current_program[std::this_thread::get_id()] = std::move(thread_program); + } + + ~CurrentThreadProgram() { + thread_current_program.erase(std::this_thread::get_id()); + } + void set(ReadProgram read_program) { - std::lock_guard lock(mutex); - thread_current_program[std::this_thread::get_id()] = read_program; + std::lock_guard lock(thread_current_program_mutex); + auto it = thread_current_program.find(std::this_thread::get_id()); + if(it != thread_current_program.end()) + it->second.read_program = std::move(read_program); } void clear() { - std::lock_guard lock(mutex); - thread_current_program.erase(std::this_thread::get_id()); + std::lock_guard lock(thread_current_program_mutex); + auto it = thread_current_program.find(std::this_thread::get_id()); + if(it != thread_current_program.end()) { + it->second.read_program.pid = -1; + it->second.read_program.read_fd = -1; + } } void kill_in_thread(const std::thread::id &thread_id) { - std::lock_guard lock(mutex); + std::lock_guard lock(thread_current_program_mutex); auto it = thread_current_program.find(thread_id); - if(it != thread_current_program.end()) { - close(it->second.read_fd); - kill(it->second.pid, SIGTERM); + if(it != thread_current_program.end() && it->second.read_program.pid != -1 && it->second.read_program.read_fd != -1) { + close(it->second.read_program.read_fd); + kill(it->second.read_program.pid, SIGTERM); + it->second.killed = true; } } -private: - std::unordered_map thread_current_program; - std::mutex mutex; + + bool is_killed() { + std::lock_guard lock(thread_current_program_mutex); + auto it = thread_current_program.find(std::this_thread::get_id()); + if(it != thread_current_program.end()) + return it->second.killed; + return false; + } }; -static CurrentThreadProgram current_thread_program; +thread_local CurrentThreadProgram current_thread_program; int exec_program_pipe(const char **args, ReadProgram *read_program) { /* 1 arguments */ if(args[0] == NULL) return -1; + + if(current_thread_program.is_killed()) + return -1; int fd[2]; if(pipe(fd) == -1) { diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 5376535..5ef47a7 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3203,6 +3203,9 @@ namespace QuickMedia { // TODO: Optimize with hash map? auto find_body_item_by_event_id = [](std::shared_ptr *body_items, size_t num_body_items, const std::string &event_id, size_t *index_result = nullptr) -> std::shared_ptr { + if(event_id.empty()) + return nullptr; + for(size_t i = 0; i < num_body_items; ++i) { auto &body_item = body_items[i]; if(body_item->userdata && static_cast(body_item->userdata)->event_id == event_id) { @@ -3211,6 +3214,7 @@ namespace QuickMedia { return body_item; } } + return nullptr; }; @@ -3465,6 +3469,10 @@ namespace QuickMedia { auto body_item = find_body_item_by_event_id(tabs[MESSAGES_TAB_INDEX].body->items.data(), tabs[MESSAGES_TAB_INDEX].body->items.size(), message->related_event_id); if(body_item) { Message *orig_message = static_cast(body_item->userdata); + if(!orig_message) { + show_notification("QuickMedia", "Unexpected error, failed to set replaced by message", Urgency::CRITICAL); + return; + } orig_message->replaced_by = message; } } else if(message->related_event_type == RelatedEventType::REPLY) { @@ -4790,6 +4798,7 @@ namespace QuickMedia { room_desc.erase(room_desc.begin() + last_line_start - 1, room_desc.end()); } current_room->body_item->set_description(std::move(room_desc)); + current_room->body_item->set_description_color(sf::Color(179, 179, 179)); // TODO: Show a line like nheko instead for unread messages, or something else current_room->body_item->set_title_color(sf::Color::White); current_room->last_message_read = true; diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 2b72f03..4693997 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -528,8 +528,10 @@ namespace QuickMedia { if(last_unread_message && !sync_is_cache) { std::string room_desc = "Unread: " + matrix->message_get_author_displayname(last_unread_message) + ": " + message_to_room_description_text(last_unread_message); int unread_notification_count = room->unread_notification_count; - if(unread_notification_count > 0) + if(unread_notification_count > 0) { room_desc += "\n** " + std::to_string(unread_notification_count) + " unread mention(s) **"; // TODO: Better notification? + room->body_item->set_description_color(sf::Color(255, 100, 100)); + } room->body_item->set_description(std::move(room_desc)); room->body_item->set_title_color(sf::Color(255, 100, 100)); room->last_message_read = false; @@ -538,6 +540,7 @@ namespace QuickMedia { room_tags_page->move_room_to_top(room); } else if(is_initial_sync) { room->body_item->set_description(matrix->message_get_author_displayname(last_new_message.get()) + ": " + message_to_room_description_text(last_new_message.get())); + room->body_item->set_description_color(sf::Color(179, 179, 179)); } } diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 96d767f..cc20f2f 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -158,6 +158,7 @@ namespace QuickMedia { desc += description_snippet.value(); } body_item->set_description(std::move(desc)); + body_item->set_description_color(sf::Color(179, 179, 179)); if(scheduled_text.empty()) body_item->url = "https://www.youtube.com/watch?v=" + video_id_str; body_item->thumbnail_url = "https://img.youtube.com/vi/" + video_id_str + "/hqdefault.jpg"; @@ -216,6 +217,7 @@ namespace QuickMedia { desc += description.value(); } body_item->set_description(std::move(desc)); + body_item->set_description_color(sf::Color(179, 179, 179)); body_item->url = "https://www.youtube.com/channel/" + channel_id_json.asString(); if(thumbnail) { body_item->thumbnail_url = std::string("https:") + thumbnail->url; -- cgit v1.2.3