From 5a3e4e1599b76cc41d0520d67fca7b66831cfbbe Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 22 Feb 2018 09:55:59 +0100 Subject: [PATCH] [Windows Agent] Avoid execute module at first time if is out cron --- pandora_agents/win32/misc/cron.cc | 16 ++++++++++++++++ pandora_agents/win32/misc/cron.h | 5 +++-- pandora_agents/win32/modules/pandora_module.cc | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pandora_agents/win32/misc/cron.cc b/pandora_agents/win32/misc/cron.cc index 4db0e40094..7a7e1da2f3 100644 --- a/pandora_agents/win32/misc/cron.cc +++ b/pandora_agents/win32/misc/cron.cc @@ -232,6 +232,22 @@ bool Cron::shouldExecuteAt (time_t date) { return this->utimestamp < date; } +/** + * @brief Check if a module should be executed when utimestamp is not calculated yet + * + * @param date Current date + * @return true It is not first time and current date fit in cron + * @return false Don't execute first time + */ +bool Cron::shouldExecuteAtFirst (time_t date) { + + // Return true if it is not first + if (this->utimestamp != 0) return true; + + // Check current date in cron + return isInCron(date); +} + /** * @brief Update the cron utimestamp * diff --git a/pandora_agents/win32/misc/cron.h b/pandora_agents/win32/misc/cron.h index e27d81062b..f1647b64cd 100644 --- a/pandora_agents/win32/misc/cron.h +++ b/pandora_agents/win32/misc/cron.h @@ -1,7 +1,7 @@ /* Pandora cron manager for Win32. - Copyright (C) 2016 Artica ST. - Written by Ramon Novoa. + Copyright (C) 2018 Artica ST. + Written by Fermin Hernandez. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -72,6 +72,7 @@ class Cron { // Other methods void update(time_t date, int interval); bool shouldExecuteAt(time_t date); + bool shouldExecuteAtFirst(time_t date); }; #endif diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index 57a2c26b15..d7d5fb63c0 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -1578,9 +1578,12 @@ Pandora_Module::checkCron (int interval) { time_t now = time(NULL); if (!this->cron->shouldExecuteAt(now)) return false; + // Check if should execute this module at first before update cron params + bool execute = this->cron->shouldExecuteAtFirst(now); + this->cron->update(now, interval); - return true; + return execute; } /**