192 lines
6.1 KiB
C
192 lines
6.1 KiB
C
/*-
|
|
* Copyright (c) 2017-2019 Carsten Sonne Larsen <cs@innolan.net>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "state.h"
|
|
#include "message.h"
|
|
#include "log.h"
|
|
#include <stdarg.h>
|
|
|
|
static const char *settingChangedLong = "%s changed: %ld -> %ld";
|
|
static const char *settingTooLow = "%s < %ld (too low)";
|
|
static const char *settingTooHigh = "%s > %ld (too high)";
|
|
static const char *settingGreaterThan = "%s * 2 > %s";
|
|
|
|
static void LogValidationLine(const char *format, va_list ap)
|
|
{
|
|
char message[SETTINGMESSAGELEN];
|
|
VSNPrintf(message, SETTINGMESSAGELEN - 1, format, ap);
|
|
|
|
if (Globals->Broker->Task == NULL || FindTask(NULL) == Globals->Broker->Task)
|
|
{
|
|
LogInfo(message);
|
|
}
|
|
else
|
|
{
|
|
SendInfoMessage(message);
|
|
}
|
|
}
|
|
|
|
static void LogValidation(const char *format, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, format);
|
|
LogValidationLine(format, args);
|
|
va_end(args);
|
|
}
|
|
|
|
static void ValidateInterval(void)
|
|
{
|
|
if (Globals->Settings->Interval < INTERVAL_MIN)
|
|
{
|
|
LogValidation(settingTooLow,
|
|
SettingKeys->Interval,
|
|
INTERVAL_MIN);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Interval,
|
|
Globals->Settings->Interval,
|
|
INTERVAL_MIN);
|
|
Globals->Settings->Interval = INTERVAL_MIN;
|
|
}
|
|
}
|
|
|
|
static void ValidateTimeout(void)
|
|
{
|
|
if (Globals->Settings->Timeout < TIMEOUT_MIN)
|
|
{
|
|
LogValidation(settingTooLow,
|
|
SettingKeys->Timeout,
|
|
TIMEOUT_MIN);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Timeout,
|
|
Globals->Settings->Timeout,
|
|
TIMEOUT_MIN);
|
|
Globals->Settings->Timeout = TIMEOUT_MIN;
|
|
}
|
|
|
|
if (Globals->Settings->Timeout > Globals->Settings->Interval / 2)
|
|
{
|
|
LogValidation(settingGreaterThan,
|
|
SettingKeys->Timeout,
|
|
SettingKeys->Interval);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Timeout,
|
|
Globals->Settings->Timeout,
|
|
Globals->Settings->Interval / 2);
|
|
Globals->Settings->Timeout = Globals->Settings->Interval / 2;
|
|
}
|
|
|
|
if (Globals->Settings->Timeout > TIMEOUT_MAX)
|
|
{
|
|
LogValidation(settingTooHigh,
|
|
SettingKeys->Timeout,
|
|
TIMEOUT_MAX);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Timeout,
|
|
Globals->Settings->Timeout,
|
|
TIMEOUT_MAX);
|
|
Globals->Settings->Timeout = TIMEOUT_MAX;
|
|
}
|
|
}
|
|
|
|
static void ValidatePriority(void)
|
|
{
|
|
if (Globals->Settings->Priority < PRIORITY_MIN)
|
|
{
|
|
LogValidation(settingTooLow,
|
|
SettingKeys->Priority,
|
|
PRIORITY_MIN);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Priority,
|
|
Globals->Settings->Priority,
|
|
PRIORITY_MIN);
|
|
Globals->Settings->Priority = PRIORITY_MIN;
|
|
}
|
|
|
|
if (Globals->Settings->Priority > PRIORITY_MAX)
|
|
{
|
|
LogValidation(settingTooHigh,
|
|
SettingKeys->Priority,
|
|
PRIORITY_MAX);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Priority,
|
|
Globals->Settings->Priority,
|
|
PRIORITY_MAX);
|
|
Globals->Settings->Priority = PRIORITY_MAX;
|
|
}
|
|
}
|
|
|
|
static void ValidateThreshold(void)
|
|
{
|
|
if (Globals->Settings->Threshold < THRESHOLD_MIN)
|
|
{
|
|
LogValidation(settingTooLow,
|
|
SettingKeys->Threshold,
|
|
THRESHOLD_MIN);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Threshold,
|
|
Globals->Settings->Threshold,
|
|
THRESHOLD_MIN);
|
|
Globals->Settings->Threshold = THRESHOLD_MIN;
|
|
}
|
|
}
|
|
|
|
static void ValidateVerbose(void)
|
|
{
|
|
if (Globals->Settings->Verbose < VERBOSE_MIN)
|
|
{
|
|
LogValidation(settingTooLow,
|
|
SettingKeys->Verbose,
|
|
VERBOSE_MIN);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Verbose,
|
|
Globals->Settings->Verbose,
|
|
VERBOSE_MIN);
|
|
Globals->Settings->Verbose = VERBOSE_MIN;
|
|
}
|
|
|
|
if (Globals->Settings->Verbose > VERBOSE_MAX)
|
|
{
|
|
LogValidation(settingTooHigh,
|
|
SettingKeys->Verbose,
|
|
VERBOSE_MAX);
|
|
LogValidation(settingChangedLong,
|
|
SettingKeys->Verbose,
|
|
Globals->Settings->Verbose,
|
|
VERBOSE_MAX);
|
|
Globals->Settings->Verbose = VERBOSE_MAX;
|
|
}
|
|
}
|
|
|
|
void SanitizeSettings(void)
|
|
{
|
|
ValidateInterval();
|
|
ValidateTimeout();
|
|
ValidatePriority();
|
|
ValidateThreshold();
|
|
ValidateVerbose();
|
|
}
|