Coverage Report

Created: 2025-09-05 06:36

/src/pdns/pdns/dnsdistdist/dnsdist-configuration.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
23
#include "dnsdist-configuration.hh"
24
#include "sholder.hh"
25
26
namespace dnsdist::configuration
27
{
28
static GlobalStateHolder<RuntimeConfiguration> s_currentRuntimeConfiguration;
29
static ImmutableConfiguration s_immutableConfiguration;
30
static std::atomic<bool> s_immutableConfigurationDone{false};
31
32
static const RuntimeConfiguration& getCurrentRuntimeConfigurationInternal(bool refresh)
33
0
{
34
0
  static thread_local auto t_threadLocalConfiguration = s_currentRuntimeConfiguration.getLocal();
35
0
  return t_threadLocalConfiguration.get(!refresh);
36
0
}
37
38
const RuntimeConfiguration& getCurrentRuntimeConfiguration()
39
0
{
40
0
  return getCurrentRuntimeConfigurationInternal(false);
41
0
}
42
43
const RuntimeConfiguration& refreshLocalRuntimeConfiguration()
44
0
{
45
0
  return getCurrentRuntimeConfigurationInternal(true);
46
0
}
47
48
void updateRuntimeConfiguration(const std::function<void(RuntimeConfiguration&)>& mutator)
49
0
{
50
0
  s_currentRuntimeConfiguration.modify(mutator);
51
  /* refresh the local "cache" right away */
52
0
  refreshLocalRuntimeConfiguration();
53
0
}
54
55
void updateImmutableConfiguration(const std::function<void(ImmutableConfiguration&)>& mutator)
56
0
{
57
0
  if (isImmutableConfigurationDone()) {
58
0
    throw std::runtime_error("Trying to update an immutable setting at runtime!");
59
0
  }
60
61
0
  mutator(s_immutableConfiguration);
62
0
}
63
64
const ImmutableConfiguration& getImmutableConfiguration()
65
0
{
66
0
  return s_immutableConfiguration;
67
0
}
68
69
bool isImmutableConfigurationDone()
70
0
{
71
0
  return s_immutableConfigurationDone.load();
72
0
}
73
74
void setImmutableConfigurationDone()
75
0
{
76
0
  if (s_immutableConfigurationDone.exchange(true)) {
77
0
    throw std::runtime_error("Trying to seal the runtime-immutable configuration a second time");
78
0
  }
79
0
}
80
}