/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 | | const RuntimeConfiguration& getCurrentRuntimeConfiguration() |
33 | 0 | { |
34 | 0 | static thread_local auto t_threadLocalConfiguration = s_currentRuntimeConfiguration.getLocal(); |
35 | 0 | return *t_threadLocalConfiguration; |
36 | 0 | } |
37 | | |
38 | | void updateRuntimeConfiguration(const std::function<void(RuntimeConfiguration&)>& mutator) |
39 | 0 | { |
40 | 0 | s_currentRuntimeConfiguration.modify(mutator); |
41 | 0 | } |
42 | | |
43 | | void updateImmutableConfiguration(const std::function<void(ImmutableConfiguration&)>& mutator) |
44 | 0 | { |
45 | 0 | if (isImmutableConfigurationDone()) { |
46 | 0 | throw std::runtime_error("Trying to update an immutable setting at runtime!"); |
47 | 0 | } |
48 | | |
49 | 0 | mutator(s_immutableConfiguration); |
50 | 0 | } |
51 | | |
52 | | const ImmutableConfiguration& getImmutableConfiguration() |
53 | 0 | { |
54 | 0 | return s_immutableConfiguration; |
55 | 0 | } |
56 | | |
57 | | bool isImmutableConfigurationDone() |
58 | 0 | { |
59 | 0 | return s_immutableConfigurationDone.load(); |
60 | 0 | } |
61 | | |
62 | | void setImmutableConfigurationDone() |
63 | 0 | { |
64 | 0 | if (s_immutableConfigurationDone.exchange(true)) { |
65 | 0 | throw std::runtime_error("Trying to seal the runtime-immutable configuration a second time"); |
66 | 0 | } |
67 | 0 | } |
68 | | } |