Directory: | ./ |
---|---|
File: | pdserv/src/Main.cpp |
Date: | 2025-06-08 04:08:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 319 | 474 | 67.3% |
Branches: | 428 | 1477 | 29.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************** | ||
2 | * | ||
3 | * $Id$ | ||
4 | * | ||
5 | * Copyright 2010 Richard Hacker (lerichi at gmx dot net) | ||
6 | * | ||
7 | * This file is part of the pdserv library. | ||
8 | * | ||
9 | * The pdserv library is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU Lesser General Public License as published | ||
11 | * by the Free Software Foundation, either version 3 of the License, or (at | ||
12 | * your option) any later version. | ||
13 | * | ||
14 | * The pdserv library is distributed in the hope that it will be useful, but | ||
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
17 | * License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public License | ||
20 | * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | *****************************************************************************/ | ||
23 | |||
24 | #include "config.h" | ||
25 | #include "Debug.h" | ||
26 | |||
27 | #include <cerrno> | ||
28 | #include <cstdio> // fread(), fileno() | ||
29 | #include <unistd.h> // fork(), getpid(), chdir, sysconf, close, fstat() | ||
30 | // stat() | ||
31 | #include <signal.h> // signal() | ||
32 | #include <limits.h> // _POSIX_OPEN_MAX | ||
33 | #include <sys/types.h> // umask(), fstat(), stat(), opendir() | ||
34 | #include <sys/stat.h> // umask(), fstat(), stat() | ||
35 | #include <sys/time.h> // gettimeofday() | ||
36 | #include <dirent.h> // opendir(), readdir() | ||
37 | #include <iomanip> // std::setw(), std::setfill() | ||
38 | #include <algorithm> // std::copy() | ||
39 | #include <iterator> // std::back_inserter() | ||
40 | #include <memory> | ||
41 | #include <set> | ||
42 | |||
43 | #include <pdserv.h> // pdserv_full_version | ||
44 | |||
45 | #undef USE_GETTEXT | ||
46 | #ifdef USE_GETTEXT | ||
47 | #include <libintl.h> // gettext() | ||
48 | #include <locale.h> // setlocale() | ||
49 | #else | ||
50 | #define gettext(s) (s) | ||
51 | #endif | ||
52 | |||
53 | #include <log4cplus/syslogappender.h> | ||
54 | #include <log4cplus/consoleappender.h> | ||
55 | #include <log4cplus/streams.h> | ||
56 | #include <log4cplus/configurator.h> | ||
57 | #include <log4cplus/loggingmacros.h> | ||
58 | #include <log4cplus/version.h> | ||
59 | |||
60 | #include "Exceptions.h" | ||
61 | #include "Main.h" | ||
62 | #include "Task.h" | ||
63 | #include "Signal.h" | ||
64 | #include "ProcessParameter.h" | ||
65 | #include "Database.h" | ||
66 | #include "Config.h" | ||
67 | #include "Event.h" | ||
68 | #include "Session.h" | ||
69 | #include "msrproto/Server.h" | ||
70 | |||
71 | #ifndef _GNU_SOURCE | ||
72 | # define _GNU_SOURCE | ||
73 | #endif | ||
74 | #include <string.h> // basename() | ||
75 | |||
76 | #ifdef GNUTLS_FOUND | ||
77 | #include "TLS.h" | ||
78 | 3 | log4cplus::Logger tlsLogger; | |
79 | #endif | ||
80 | |||
81 | #if __cplusplus < 201402L | ||
82 | namespace std { | ||
83 | template <typename T, typename... Args> | ||
84 | std::unique_ptr<T> make_unique(Args&&... args) { | ||
85 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); | ||
86 | } | ||
87 | } | ||
88 | #endif | ||
89 | |||
90 | using namespace PdServ; | ||
91 | |||
92 | ///////////////////////////////////////////////////////////////////////////// | ||
93 | 18 | inline bool operator== (const struct timespec& t1, const struct timespec& t2) | |
94 | { | ||
95 |
3/4✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 2 times.
|
18 | return t1.tv_sec == t2.tv_sec and t1.tv_nsec == t2.tv_nsec; |
96 | } | ||
97 | |||
98 | 19 | inline bool operator< (const struct timespec& t1, const struct timespec& t2) | |
99 | { | ||
100 | 19 | return (t1.tv_sec < t2.tv_sec | |
101 |
4/6✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 18 times.
|
19 | or (t1.tv_sec == t2.tv_sec and t1.tv_nsec < t2.tv_nsec)); |
102 | } | ||
103 | |||
104 | ///////////////////////////////////////////////////////////////////////////// | ||
105 | inline bool operator! (const struct timespec& t) | ||
106 | { | ||
107 | return !(t.tv_sec or t.tv_nsec); | ||
108 | } | ||
109 | |||
110 | ///////////////////////////////////////////////////////////////////////////// | ||
111 | struct Main::EventInstance { | ||
112 | const PdServ::Event* event; | ||
113 | size_t index; | ||
114 | Event::Priority priority; | ||
115 | uint32_t seqNo; | ||
116 | struct timespec time; | ||
117 | bool state; | ||
118 | |||
119 | 19 | bool operator()(const EventInstance* a, const EventInstance* b) const { | |
120 | 19 | return a->time < b->time | |
121 |
5/6✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
|
19 | or (a->time == b->time and int(a->seqNo - b->seqNo) < 0); |
122 | } | ||
123 | |||
124 | 48 | operator EventData() const { | |
125 | EventData d = { | ||
126 | 48 | this->event, | |
127 | 48 | this->seqNo, | |
128 | 48 | this->index, | |
129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | this->priority, |
130 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | this->state, |
131 | this->time | ||
132 | 240 | }; | |
133 | 48 | return d; | |
134 | } | ||
135 | |||
136 | friend std::ostream& operator<<(std::ostream& os, const EventInstance& i) { | ||
137 | os | ||
138 | << i.event | ||
139 | << ' ' << i.index | ||
140 | << ' ' << i.priority | ||
141 | << ' ' << i.seqNo | ||
142 | << ' ' << i.state; | ||
143 | return os; | ||
144 | } | ||
145 | }; | ||
146 | |||
147 | namespace std { | ||
148 | ✗ | std::ostream& operator<< (std::ostream& os, const timespec& ts) | |
149 | { | ||
150 | ✗ | char fill = os.fill('0'); | |
151 | ✗ | std::streamsize width = os.width(9); | |
152 | ✗ | os << ts.tv_sec << '.' << ts.tv_nsec; | |
153 | ✗ | os.width(width); | |
154 | ✗ | os.fill(fill); | |
155 | ✗ | return os; | |
156 | } | ||
157 | } | ||
158 | |||
159 | ///////////////////////////////////////////////////////////////////////////// | ||
160 | 157 | Main::Main(const std::string& name, const std::string& version): | |
161 | name(name), version(version), | ||
162 | parameterLog( | ||
163 | log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("parameter"))), | ||
164 | eventLog(log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("event"))) | ||
165 | #ifdef GNUTLS_FOUND | ||
166 | , | ||
167 |
11/22✓ Branch 7 taken 157 times.
✗ Branch 8 not taken.
✓ Branch 17 taken 157 times.
✗ Branch 18 not taken.
✓ Branch 23 taken 157 times.
✗ Branch 24 not taken.
✓ Branch 28 taken 157 times.
✗ Branch 29 not taken.
✓ Branch 38 taken 157 times.
✗ Branch 39 not taken.
✓ Branch 43 taken 157 times.
✗ Branch 44 not taken.
✓ Branch 51 taken 157 times.
✗ Branch 52 not taken.
✓ Branch 55 taken 157 times.
✗ Branch 56 not taken.
✓ Branch 59 taken 157 times.
✗ Branch 60 not taken.
✓ Branch 65 taken 157 times.
✗ Branch 66 not taken.
✓ Branch 75 taken 157 times.
✗ Branch 76 not taken.
|
157 | tlsSessionDB(&tls_mutex, TLS_DB_SIZE) |
168 | #endif | ||
169 | { | ||
170 | 157 | msrproto = 0; | |
171 | |||
172 | #ifdef USE_GETTEXT | ||
173 | // for gettext() | ||
174 | setlocale(LC_ALL, ""); | ||
175 | textdomain(name.c_str()); | ||
176 | #endif | ||
177 | 157 | } | |
178 | |||
179 | ///////////////////////////////////////////////////////////////////////////// | ||
180 | 314 | Main::~Main() | |
181 | { | ||
182 | #ifdef GNUTLS_FOUND | ||
183 | 157 | destroyTLS(); | |
184 | #endif | ||
185 | 157 | for (std::set<EventInstance*>::iterator it = danglingInstances.begin(); | |
186 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 157 times.
|
157 | it != danglingInstances.end(); ++it) |
187 | ✗ | delete *it; | |
188 | |||
189 | 157 | for (EventInstanceMap::iterator it = eventMap.begin(); | |
190 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 157 times.
|
157 | it != eventMap.end(); ++it) |
191 | ✗ | delete it->second; | |
192 | 157 | } | |
193 | |||
194 | ///////////////////////////////////////////////////////////////////////////// | ||
195 | 155 | void Main::setupLogging(const std::string& configFile) | |
196 | { | ||
197 | #ifdef PDS_DEBUG | ||
198 | consoleLogging(); | ||
199 | #endif | ||
200 | |||
201 |
1/2✓ Branch 6 taken 155 times.
✗ Branch 7 not taken.
|
155 | unsigned int history = config("eventhistory").toUInt(100); |
202 | 155 | eventInstance.resize(history); | |
203 | 155 | eventPtr = &eventInstance[0]; | |
204 | |||
205 |
2/4✓ Branch 6 taken 155 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 155 times.
✗ Branch 11 not taken.
|
155 | if (config("logging")) { |
206 | typedef std::basic_istringstream<log4cplus::tchar> tistringstream; | ||
207 | 155 | tistringstream is( | |
208 |
3/6✓ Branch 5 taken 155 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 155 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 155 times.
✗ Branch 16 not taken.
|
310 | LOG4CPLUS_STRING_TO_TSTRING(config("logging").toString())); |
209 |
3/6✓ Branch 2 taken 155 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 155 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 155 times.
✗ Branch 12 not taken.
|
155 | log4cplus::PropertyConfigurator(is).configure(); |
210 | } | ||
211 | else { | ||
212 | ✗ | syslogLogging(); | |
213 | } | ||
214 | |||
215 |
11/22✓ Branch 3 taken 155 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 155 times.
✗ Branch 8 not taken.
✓ Branch 16 taken 155 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 155 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 155 times.
✗ Branch 22 not taken.
✓ Branch 25 taken 155 times.
✗ Branch 26 not taken.
✓ Branch 29 taken 155 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 155 times.
✗ Branch 33 not taken.
✓ Branch 36 taken 155 times.
✗ Branch 37 not taken.
✓ Branch 40 taken 155 times.
✗ Branch 41 not taken.
✓ Branch 44 taken 155 times.
✗ Branch 45 not taken.
|
155 | LOG4CPLUS_INFO(log4cplus::Logger::getRoot(), |
216 | LOG4CPLUS_TEXT("Started application ") | ||
217 | << LOG4CPLUS_STRING_TO_TSTRING(name) | ||
218 | << LOG4CPLUS_TEXT(", Version ") | ||
219 | << LOG4CPLUS_STRING_TO_TSTRING(version)); | ||
220 | |||
221 |
9/18✓ Branch 3 taken 155 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 155 times.
✗ Branch 8 not taken.
✓ Branch 16 taken 155 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 155 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 155 times.
✗ Branch 22 not taken.
✓ Branch 25 taken 155 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 155 times.
✗ Branch 29 not taken.
✓ Branch 32 taken 155 times.
✗ Branch 33 not taken.
✓ Branch 36 taken 155 times.
✗ Branch 37 not taken.
|
155 | LOG4CPLUS_INFO(log4cplus::Logger::getRoot(), |
222 | LOG4CPLUS_TEXT("Using PdServ version ") | ||
223 | << LOG4CPLUS_STRING_TO_TSTRING(pdserv_full_version)); | ||
224 | |||
225 |
1/2✓ Branch 2 taken 155 times.
✗ Branch 3 not taken.
|
155 | if (!configFile.empty()) |
226 |
9/18✓ Branch 3 taken 155 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 155 times.
✗ Branch 8 not taken.
✓ Branch 16 taken 155 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 155 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 155 times.
✗ Branch 22 not taken.
✓ Branch 25 taken 155 times.
✗ Branch 26 not taken.
✓ Branch 29 taken 155 times.
✗ Branch 30 not taken.
✓ Branch 33 taken 155 times.
✗ Branch 34 not taken.
✓ Branch 37 taken 155 times.
✗ Branch 38 not taken.
|
155 | LOG4CPLUS_INFO(log4cplus::Logger::getRoot(), |
227 | LOG4CPLUS_TEXT("Using configuration: ") | ||
228 | << LOG4CPLUS_STRING_TO_TSTRING(configFile)); | ||
229 | 155 | } | |
230 | |||
231 | ///////////////////////////////////////////////////////////////////////////// | ||
232 | ✗ | void Main::consoleLogging() | |
233 | { | ||
234 | ✗ | log4cplus::BasicConfigurator::doConfigure(); | |
235 | // log4cplus::SharedAppenderPtr cerr(new log4cplus::ConsoleAppender(true)); | ||
236 | // cerr->setLayout( | ||
237 | // std::auto_ptr<log4cplus::Layout>(new log4cplus::PatternLayout( | ||
238 | // LOG4CPLUS_TEXT("%D %p %c %x: %m")))); | ||
239 | // | ||
240 | // log4cplus::Logger::getRoot().addAppender(cerr); | ||
241 | } | ||
242 | |||
243 | ///////////////////////////////////////////////////////////////////////////// | ||
244 | ✗ | void Main::syslogLogging() | |
245 | { | ||
246 | ✗ | log4cplus::helpers::Properties p; | |
247 | ✗ | p.setProperty(LOG4CPLUS_TEXT("ident"), | |
248 | LOG4CPLUS_STRING_TO_TSTRING(name)); | ||
249 | ✗ | p.setProperty(LOG4CPLUS_TEXT("facility"),LOG4CPLUS_TEXT("local0")); | |
250 | |||
251 | ✗ | log4cplus::SharedAppenderPtr appender( new log4cplus::SysLogAppender(p)); | |
252 | #if LOG4CPLUS_VERSION > LOG4CPLUS_MAKE_VERSION(1,2,0) | ||
253 | appender->setLayout(std::make_unique<log4cplus::PatternLayout>( | ||
254 | LOG4CPLUS_TEXT("%-5p %c <%x>: %m"))); | ||
255 | #else | ||
256 | ✗ | appender->setLayout( | |
257 | ✗ | std::auto_ptr<log4cplus::Layout>(new log4cplus::PatternLayout( | |
258 | ✗ | LOG4CPLUS_TEXT("%-5p %c <%x>: %m")))); | |
259 | #endif | ||
260 | |||
261 | ✗ | log4cplus::Logger root = log4cplus::Logger::getRoot(); | |
262 | ✗ | root.addAppender(appender); | |
263 | ✗ | root.setLogLevel(log4cplus::INFO_LOG_LEVEL); | |
264 | } | ||
265 | |||
266 | ///////////////////////////////////////////////////////////////////////////// | ||
267 | ✗ | int Main::gettime(struct timespec* t) const | |
268 | { | ||
269 | ✗ | return localtime(t); | |
270 | } | ||
271 | |||
272 | ///////////////////////////////////////////////////////////////////////////// | ||
273 | ✗ | int Main::localtime(struct timespec* t) | |
274 | { | ||
275 | ✗ | struct timeval tv; | |
276 | |||
277 | ✗ | if (::gettimeofday(&tv, 0)) | |
278 | ✗ | return errno; | |
279 | ✗ | t->tv_sec = tv.tv_sec; | |
280 | ✗ | t->tv_nsec = tv.tv_usec * 1000; | |
281 | |||
282 | ✗ | return 0; | |
283 | } | ||
284 | |||
285 | ///////////////////////////////////////////////////////////////////////////// | ||
286 | 154 | void Main::startServers() | |
287 | { | ||
288 |
1/2✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
|
308 | log4cplus::Logger logger = log4cplus::Logger::getRoot(); |
289 | |||
290 | #ifdef GNUTLS_FOUND | ||
291 |
3/6✓ Branch 7 taken 154 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 154 times.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 154 times.
|
154 | if (const int ret = setupTLS(config("tls"), logger)) { |
292 | ✗ | LOG4CPLUS_FATAL_STR(logger, | |
293 | LOG4CPLUS_TEXT("Fatal TLS error.")); | ||
294 | ✗ | throw PdServ::Errno(ret); | |
295 | } | ||
296 | #endif | ||
297 | |||
298 |
2/4✓ Branch 7 taken 154 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 154 times.
✗ Branch 12 not taken.
|
154 | setupAccessControl(config("access")); |
299 | |||
300 |
6/12✓ Branch 3 taken 154 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 154 times.
✗ Branch 8 not taken.
✓ Branch 16 taken 154 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 154 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 154 times.
✗ Branch 25 not taken.
✓ Branch 28 taken 154 times.
✗ Branch 29 not taken.
|
154 | LOG4CPLUS_INFO_STR(log4cplus::Logger::getRoot(), |
301 | LOG4CPLUS_TEXT("Starting servers")); | ||
302 | |||
303 |
4/6✓ Branch 4 taken 154 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 154 times.
✗ Branch 9 not taken.
✓ Branch 12 taken 149 times.
✓ Branch 13 taken 5 times.
|
159 | msrproto = new MsrProto::Server(this, config("msr")); |
304 | 149 | } | |
305 | |||
306 | ///////////////////////////////////////////////////////////////////////////// | ||
307 | 154 | void Main::stopServers() | |
308 | { | ||
309 |
2/2✓ Branch 3 taken 149 times.
✓ Branch 4 taken 5 times.
|
154 | delete msrproto; |
310 | |||
311 | 154 | savePersistent(); | |
312 | |||
313 |
6/12✓ Branch 3 taken 154 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 154 times.
✗ Branch 8 not taken.
✓ Branch 16 taken 154 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 154 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 154 times.
✗ Branch 25 not taken.
✓ Branch 28 taken 154 times.
✗ Branch 29 not taken.
|
154 | LOG4CPLUS_INFO_STR(log4cplus::Logger::getRoot(), |
314 | LOG4CPLUS_TEXT("Shut down servers")); | ||
315 | 154 | } | |
316 | |||
317 | ///////////////////////////////////////////////////////////////////////////// | ||
318 | 43 | int Main::setValue(const ProcessParameter* p, const Session* session, | |
319 | const char* buf, size_t offset, size_t count) | ||
320 | { | ||
321 |
6/10✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 41 times.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 43 times.
|
43 | if (!(m_write or session->loggedIn())) |
322 | ✗ | return -EACCES; | |
323 | |||
324 | // Ask the implementation to change value. | ||
325 | 43 | const char* data; | |
326 | 43 | const struct timespec* time; | |
327 |
1/2✓ Branch 6 taken 43 times.
✗ Branch 7 not taken.
|
43 | int rv = setValue(p, buf, offset, count, &data, &time); |
328 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (rv) |
329 | ✗ | return rv; | |
330 | |||
331 |
1/2✓ Branch 7 taken 43 times.
✗ Branch 8 not taken.
|
43 | msrproto->parameterChanged(p, offset, count, data, time); |
332 | |||
333 |
1/2✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
|
86 | PersistentMap::iterator it = persistentMap.find(p); |
334 | 43 | bool persistent = it != persistentMap.end(); | |
335 |
1/2✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
|
43 | bool log = parameterLog.isEnabledFor(log4cplus::INFO_LOG_LEVEL); |
336 | 86 | std::string logString; | |
337 | |||
338 | // Setup logString when parameter has to be logged | ||
339 |
5/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 43 times.
✗ Branch 10 not taken.
|
43 | if ((persistent and persistentLogTraceOn) or log) { |
340 |
1/2✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
|
86 | std::ostringstream os; |
341 |
2/4✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 43 times.
✗ Branch 6 not taken.
|
43 | os.imbue(std::locale::classic()); |
342 |
1/2✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
|
43 | os << p->path; |
343 | |||
344 |
1/2✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
|
43 | os << " = "; |
345 | |||
346 |
1/2✓ Branch 7 taken 43 times.
✗ Branch 8 not taken.
|
43 | static_cast<const ProcessParameter*>(p)->print(os, 0, p->memSize); |
347 | |||
348 |
1/2✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
|
43 | logString = os.str(); |
349 | } | ||
350 | |||
351 | // Save persistent parameter | ||
352 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
|
43 | if (persistent) { |
353 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
3 | PdServ::Database(persistentLog, |
354 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
2 | persistentConfig["database"].toString()) |
355 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
2 | .save(p, data, time); |
356 | |||
357 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
|
1 | if (persistentLogTraceOn) |
358 | ✗ | LOG4CPLUS_INFO_STR(persistentLogTrace, | |
359 | LOG4CPLUS_STRING_TO_TSTRING(logString)); | ||
360 | |||
361 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (it->second) { |
362 | ✗ | LOG4CPLUS_WARN(persistentLog, | |
363 | LOG4CPLUS_TEXT("Persistent parameter ") | ||
364 | << LOG4CPLUS_STRING_TO_TSTRING(p->path) | ||
365 | << LOG4CPLUS_TEXT(" is coupled to signal ") | ||
366 | << LOG4CPLUS_STRING_TO_TSTRING(it->second->path) | ||
367 | << LOG4CPLUS_TEXT( | ||
368 | ". Manually setting a parameter-signal pair " | ||
369 | "saves the parameter only and removes coupling. " | ||
370 | "Restart process soon.")); | ||
371 | ✗ | it->second = 0; | |
372 | } | ||
373 | } | ||
374 | |||
375 | // Log parameter change | ||
376 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | if (log) { |
377 |
1/2✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
|
43 | parameterLog.forcedLog(log4cplus::INFO_LOG_LEVEL, |
378 | LOG4CPLUS_STRING_TO_TSTRING(logString)); | ||
379 | } | ||
380 | |||
381 | 43 | return 0; | |
382 | } | ||
383 | |||
384 | ///////////////////////////////////////////////////////////////////////////// | ||
385 | 155 | unsigned int Main::setupPersistent() | |
386 | { | ||
387 | 310 | std::set<std::string> keys; | |
388 | |||
389 |
2/4✓ Branch 4 taken 155 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 155 times.
✗ Branch 10 not taken.
|
155 | persistentConfig = config("persistent"); |
390 |
3/4✓ Branch 2 taken 155 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 149 times.
✓ Branch 5 taken 6 times.
|
155 | if (!persistentConfig) |
391 | 149 | return 0; | |
392 | |||
393 | // Get variable list | ||
394 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
12 | PdServ::Config varList(persistentConfig["variables"]); |
395 |
3/6✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 6 times.
|
6 | if (!varList[size_t(0)]) { |
396 | ✗ | LOG4CPLUS_INFO_STR(persistentLog, | |
397 | LOG4CPLUS_TEXT("Persistent variable list is empty")); | ||
398 | ✗ | return 0; | |
399 | } | ||
400 | |||
401 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
|
6 | persistentLog = log4cplus::Logger::getRoot(); |
402 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 | persistentLogTraceOn = persistentConfig["trace"].toUInt(); |
403 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
|
6 | if (persistentLogTraceOn) |
404 | ✗ | persistentLogTrace = | |
405 | ✗ | log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("persistent")); | |
406 | |||
407 |
2/4✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
|
12 | const std::string databasePath(persistentConfig["database"].toString()); |
408 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (databasePath.empty()) { |
409 | ✗ | LOG4CPLUS_WARN_STR(persistentLog, | |
410 | LOG4CPLUS_TEXT("No persistent database path specified")); | ||
411 | ✗ | return 0; | |
412 | } | ||
413 | |||
414 |
1/2✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
12 | Database dataBase(persistentLog, databasePath); |
415 | |||
416 | // Copy signals from tasks | ||
417 | typedef std::map<std::string, const Signal*> SignalMap; | ||
418 | 12 | SignalMap signalMap; | |
419 | |||
420 | typedef std::list<const Task*> TaskList; | ||
421 |
1/2✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
12 | TaskList task = getTasks(); |
422 | 18 | for (TaskList::iterator it = task.begin(); | |
423 |
2/2✓ Branch 6 taken 12 times.
✓ Branch 7 taken 6 times.
|
18 | it != task.end(); ++it) { |
424 | typedef std::list<const PdServ::Signal*> SignalList; | ||
425 | |||
426 | 12 | SignalList signals = | |
427 |
1/2✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
|
24 | static_cast<const PdServ::Task*>(*it)->getSignals(); |
428 | |||
429 | 48 | for (SignalList::iterator it = signals.begin(); | |
430 |
2/2✓ Branch 6 taken 36 times.
✓ Branch 7 taken 12 times.
|
48 | it != signals.end(); ++it) |
431 |
1/2✓ Branch 5 taken 36 times.
✗ Branch 6 not taken.
|
36 | signalMap[(*it)->path] = *it; |
432 | } | ||
433 | |||
434 | |||
435 | // Go through the variables section of the configuration to | ||
436 | // find parameters to watch. There are 2 ways of specifying a variable | ||
437 | // 1) string only pointing to a parameter to watch | ||
438 | // 2) map with "parameter:" string pointing to a parameter to watch | ||
439 | // 2) map with "parameter" and "signal" of a pair to watch | ||
440 | // e.g. | ||
441 | // variables: | ||
442 | // - "/path/to/parameter" | ||
443 | // - parameter: "/path/to/parameter" | ||
444 | // - parameter: /path/to/integrator/offset | ||
445 | // signal: "/path/to/integrator" | ||
446 |
4/6✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 6 times.
✓ Branch 11 taken 6 times.
|
12 | for (size_t i = 0; varList[i]; ++i) { |
447 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
|
12 | const PdServ::Config& item = varList[i]; |
448 | Parameter* param; | ||
449 | 6 | const Signal* signal = 0; | |
450 | |||
451 | 6 | std::string path = | |
452 |
7/12✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 17 taken 6 times.
✗ Branch 18 not taken.
✓ Branch 24 taken 6 times.
✗ Branch 25 not taken.
|
12 | (item.isMapping() ? item["parameter"] : item).toString(); |
453 | |||
454 |
1/2✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 | param = findParameter(path); |
455 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!param) { |
456 | // Parameter does not exist | ||
457 | ✗ | LOG4CPLUS_WARN(persistentLog, | |
458 | LOG4CPLUS_TEXT("Persistent parameter ") | ||
459 | << LOG4CPLUS_STRING_TO_TSTRING(path) | ||
460 | << LOG4CPLUS_TEXT(" does not exist.")); | ||
461 | ✗ | continue; | |
462 | } | ||
463 | |||
464 |
2/4✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 6 times.
|
6 | if (persistentMap.find(param) != persistentMap.end()) { |
465 | ✗ | LOG4CPLUS_INFO(persistentLog, | |
466 | LOG4CPLUS_TEXT("Persistent parameter ") | ||
467 | << LOG4CPLUS_STRING_TO_TSTRING(path) | ||
468 | << LOG4CPLUS_TEXT(" traced already.")); | ||
469 | ✗ | continue; | |
470 | } | ||
471 | |||
472 |
12/20✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 4 times.
✓ Branch 17 taken 2 times.
✓ Branch 19 taken 4 times.
✓ Branch 20 taken 2 times.
✓ Branch 22 taken 4 times.
✓ Branch 23 taken 2 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
|
6 | if (item.isMapping() and item["signal"]) { |
473 |
2/4✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
|
4 | path = item["signal"].toString(); |
474 | |||
475 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | if (!path.empty()) { |
476 | // Parameter <-> Signal pair | ||
477 |
2/4✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
|
8 | SignalMap::const_iterator srcIt(signalMap.find(path)); |
478 | |||
479 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
|
4 | if (srcIt == signalMap.end()) { |
480 | // Signal does not exist | ||
481 | ✗ | LOG4CPLUS_WARN(persistentLog, | |
482 | LOG4CPLUS_TEXT("Signal ") | ||
483 | << LOG4CPLUS_STRING_TO_TSTRING(path) | ||
484 | << LOG4CPLUS_TEXT( | ||
485 | " of persistent pair does not exist.")); | ||
486 | ✗ | continue; | |
487 | } | ||
488 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | signal = srcIt->second; |
489 | } | ||
490 | } | ||
491 | |||
492 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if (signal) { |
493 | // Check whether signal is compatable to parameter | ||
494 |
2/4✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
|
4 | if (signal->dtype != param->dtype) { |
495 | // Data types do not match | ||
496 | ✗ | LOG4CPLUS_WARN(persistentLog, | |
497 | LOG4CPLUS_TEXT("Data type of signal ") | ||
498 | << LOG4CPLUS_STRING_TO_TSTRING(signal->path) | ||
499 | << LOG4CPLUS_TEXT(" and parameter ") | ||
500 | << LOG4CPLUS_STRING_TO_TSTRING(param->path) | ||
501 | << LOG4CPLUS_TEXT(" does not match")); | ||
502 | ✗ | continue; | |
503 | } | ||
504 |
2/4✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
|
4 | else if (signal->dim != param->dim) { |
505 | // Data dimensions do not match | ||
506 | ✗ | LOG4CPLUS_WARN(persistentLog, | |
507 | LOG4CPLUS_TEXT("Data dimension of signal ") | ||
508 | << LOG4CPLUS_STRING_TO_TSTRING(signal->path) | ||
509 | << LOG4CPLUS_TEXT(" and parameter ") | ||
510 | << LOG4CPLUS_STRING_TO_TSTRING(param->path) | ||
511 | << LOG4CPLUS_TEXT(" does not match")); | ||
512 | ✗ | continue; | |
513 | } | ||
514 | } | ||
515 | |||
516 |
1/2✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | persistentMap[param] = signal; |
517 |
1/2✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
6 | keys.insert(param->path); |
518 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if (signal) |
519 |
9/18✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 4 times.
✗ Branch 23 not taken.
✓ Branch 26 taken 4 times.
✗ Branch 27 not taken.
✓ Branch 30 taken 4 times.
✗ Branch 31 not taken.
✓ Branch 34 taken 4 times.
✗ Branch 35 not taken.
|
4 | LOG4CPLUS_DEBUG(persistentLog, |
520 | LOG4CPLUS_TEXT("Added persistent parameter-signal pair: ") | ||
521 | << LOG4CPLUS_STRING_TO_TSTRING(signal->path) | ||
522 | << LOG4CPLUS_TEXT(" -> ") | ||
523 | << LOG4CPLUS_STRING_TO_TSTRING(param->path)); | ||
524 | else | ||
525 |
7/14✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 2 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 2 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 2 times.
✗ Branch 24 not taken.
✓ Branch 27 taken 2 times.
✗ Branch 28 not taken.
|
2 | LOG4CPLUS_DEBUG(persistentLog, |
526 | LOG4CPLUS_TEXT("Added persistent parameter: ") | ||
527 | << LOG4CPLUS_STRING_TO_TSTRING(param->path)); | ||
528 | |||
529 | // Last but not least, read peristent value from database and | ||
530 | // set parameter | ||
531 | 6 | const struct timespec* mtime; | |
532 | 6 | const char* value; | |
533 |
3/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2 times.
|
6 | if (dataBase.read(param, &value, &mtime)) { |
534 |
1/2✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
|
4 | initializeParameter(param, value, mtime, signal); |
535 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
|
4 | if (persistentLogTraceOn) { |
536 | ✗ | std::ostringstream os; | |
537 | ✗ | os.imbue(std::locale::classic()); | |
538 | ✗ | os << param->path; | |
539 | |||
540 | ✗ | os << " = "; | |
541 | |||
542 | ✗ | param->dtype.print(os, value, value, value + param->memSize); | |
543 | |||
544 | ✗ | LOG4CPLUS_INFO_STR(persistentLogTrace, | |
545 | LOG4CPLUS_STRING_TO_TSTRING(os.str())); | ||
546 | } | ||
547 | } | ||
548 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | else if (signal) |
549 |
2/4✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
|
1 | initializeParameter(param, 0, 0, signal); |
550 | } | ||
551 | |||
552 | // Purge unneeded parameters from database, unless disabled by | ||
553 | // configuration | ||
554 |
3/6✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
|
6 | if (persistentConfig["cleanup"].toUInt(1)) |
555 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | dataBase.checkKeys(keys); |
556 | |||
557 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 | return persistentConfig["interval"].toUInt(); |
558 | } | ||
559 | |||
560 | ///////////////////////////////////////////////////////////////////////////// | ||
561 | 154 | void Main::savePersistent() | |
562 | { | ||
563 |
2/2✓ Branch 2 taken 149 times.
✓ Branch 3 taken 5 times.
|
154 | if (persistentMap.empty()) |
564 | 149 | return; | |
565 | |||
566 |
4/8✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 5 times.
✗ Branch 15 not taken.
✓ Branch 18 taken 5 times.
✗ Branch 19 not taken.
|
5 | LOG4CPLUS_INFO_STR(persistentLog, |
567 | LOG4CPLUS_TEXT("Saving persistent parameters")); | ||
568 | |||
569 | // Open database and return if there are problems | ||
570 | 5 | PdServ::Database db(persistentLog, | |
571 |
5/8✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 5 times.
✗ Branch 10 not taken.
✓ Branch 14 taken 5 times.
✗ Branch 15 not taken.
✓ Branch 23 taken 5 times.
✓ Branch 24 taken 149 times.
|
10 | persistentConfig["database"].toString()); |
572 |
2/4✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (!db) |
573 | ✗ | return; | |
574 | |||
575 | // Only save signal/parameter pairs. Persistent paremters are saved as | ||
576 | // they are changed | ||
577 |
1/2✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
|
10 | for (PersistentMap::iterator it = persistentMap.begin(); |
578 |
2/2✓ Branch 6 taken 5 times.
✓ Branch 7 taken 5 times.
|
10 | it != persistentMap.end(); ++it) { |
579 | 5 | const Signal* signal = it->second; | |
580 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (!signal) |
581 | 2 | continue; | |
582 | |||
583 | 3 | const Parameter* param = it->first; | |
584 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | char buf[param->memSize]; |
585 | 3 | struct timespec time; | |
586 | |||
587 | log_debug("Save %s", signal->path.c_str()); | ||
588 |
2/4✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
|
3 | if (getPersistentSignalValue(signal, buf, &time)) { |
589 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | db.save(param, buf, &time); |
590 | |||
591 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
|
3 | if (persistentLogTraceOn) { |
592 | ✗ | std::ostringstream os; | |
593 | ✗ | os.imbue(std::locale::classic()); | |
594 | ✗ | os << param->path; | |
595 | |||
596 | ✗ | os << " = "; | |
597 | |||
598 | ✗ | param->dtype.print(os, buf, buf, buf + param->memSize); | |
599 | |||
600 | ✗ | LOG4CPLUS_INFO_STR(persistentLogTrace, | |
601 | LOG4CPLUS_STRING_TO_TSTRING(os.str())); | ||
602 | } | ||
603 | |||
604 |
11/20✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 3 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 3 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 3 times.
✗ Branch 23 not taken.
✓ Branch 26 taken 3 times.
✗ Branch 27 not taken.
✓ Branch 30 taken 3 times.
✗ Branch 31 not taken.
✓ Branch 34 taken 3 times.
✗ Branch 35 not taken.
✓ Branch 39 taken 3 times.
✓ Branch 40 taken 2 times.
|
3 | LOG4CPLUS_DEBUG(persistentLog, |
605 | LOG4CPLUS_TEXT("Saved persistent parameter ") | ||
606 | << LOG4CPLUS_STRING_TO_TSTRING(param->path) | ||
607 | << LOG4CPLUS_TEXT(" from signal ") | ||
608 | << LOG4CPLUS_STRING_TO_TSTRING(signal->path)); | ||
609 | } | ||
610 | 5 | } | |
611 | } | ||
612 | |||
613 | ///////////////////////////////////////////////////////////////////////////// | ||
614 | 27 | void Main::newEvent(const Event* event, | |
615 | size_t index, int i_state, const struct timespec* time) | ||
616 | { | ||
617 |
1/2✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
|
54 | pthread::WriteLock lock(eventMutex); |
618 | |||
619 | 27 | bool state = i_state >= 0; | |
620 |
1/2✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
27 | EventInstanceVector*& vec = eventMap[event]; |
621 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 13 times.
|
27 | if (!vec) |
622 |
2/4✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
|
14 | vec = new EventInstanceVector(event->nelem); |
623 | |||
624 |
1/2✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
|
27 | EventInstance*& instance = vec->at(index); |
625 | |||
626 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
27 | if (!state and !instance) |
627 | ✗ | return; | |
628 | |||
629 | 27 | Event::Priority priority = state | |
630 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
38 | ? static_cast<Event::Priority>(i_state) |
631 | 11 | : instance->priority; | |
632 | |||
633 | // Check whether instance has been outcast from eventInstance and | ||
634 | // lives in separate memory | ||
635 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
54 | if (instance |
636 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
✓ Branch 5 taken 11 times.
✗ Branch 6 not taken.
|
27 | and (instance < &eventInstance.front() |
637 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | or instance > &eventInstance.back())) { |
638 | ✗ | danglingInstances.erase(instance); | |
639 | ✗ | delete instance; | |
640 | } | ||
641 | |||
642 | // Make sure that the event in history list has been reset. | ||
643 | // Otherwise separate it out and make it dangling | ||
644 |
1/6✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
27 | if (eventPtr->event and eventPtr->state) { |
645 | EventInstance*& instPtrRef = | ||
646 | ✗ | eventMap[eventPtr->event]->at(eventPtr->index); | |
647 | ✗ | if (instPtrRef == eventPtr) { | |
648 | ✗ | instPtrRef = new EventInstance; | |
649 | ✗ | *instPtrRef = *eventPtr; | |
650 | ✗ | danglingInstances.insert(instPtrRef); | |
651 | } | ||
652 | } | ||
653 | |||
654 | // Let instance point to new event | ||
655 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11 times.
|
27 | instance = state ? eventPtr : 0; |
656 | |||
657 | 27 | eventPtr->event = event; | |
658 | 27 | eventPtr->index = index; | |
659 | 27 | eventPtr->priority = priority; | |
660 | 27 | eventPtr->seqNo = eventSeqNo++; | |
661 | 27 | eventPtr->time = *time; | |
662 | 27 | eventPtr->state = state; | |
663 | |||
664 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 27 times.
|
27 | if (eventPtr++ == &eventInstance.back()) |
665 | ✗ | eventPtr = &eventInstance.front(); | |
666 | |||
667 | log4cplus::LogLevel log_prio; | ||
668 |
2/5✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
27 | switch (priority) { |
669 | 22 | case PdServ::Event::Emergency: | |
670 | case PdServ::Event::Alert: | ||
671 | case PdServ::Event::Critical: | ||
672 | 22 | log_prio = log4cplus::FATAL_LOG_LEVEL; | |
673 | 22 | break; | |
674 | ✗ | case PdServ::Event::Error: | |
675 | ✗ | log_prio = log4cplus::ERROR_LOG_LEVEL; | |
676 | ✗ | break; | |
677 | 5 | case PdServ::Event::Warning: | |
678 | 5 | log_prio = log4cplus::WARN_LOG_LEVEL; | |
679 | 5 | break; | |
680 | ✗ | case PdServ::Event::Notice: | |
681 | case PdServ::Event::Info: | ||
682 | ✗ | log_prio = log4cplus::INFO_LOG_LEVEL; | |
683 | ✗ | break; | |
684 | ✗ | case PdServ::Event::Debug: | |
685 | default: | ||
686 | ✗ | log_prio = log4cplus::DEBUG_LOG_LEVEL; | |
687 | ✗ | break; | |
688 | } | ||
689 | |||
690 |
2/4✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 27 times.
✗ Branch 8 not taken.
|
54 | log4cplus::tostringstream os; |
691 | |||
692 |
1/2✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
|
27 | os << time->tv_sec << '.' |
693 |
4/8✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 27 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 17 taken 27 times.
✗ Branch 18 not taken.
|
27 | << std::setw(6) << std::setfill('0') << time->tv_nsec/1000 |
694 | << std::setw(0) | ||
695 |
2/4✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 27 times.
✗ Branch 7 not taken.
|
27 | << LOG4CPLUS_TEXT(": "); |
696 | |||
697 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
|
27 | if (!state) { |
698 | 11 | log_prio = log4cplus::INFO_LOG_LEVEL; | |
699 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | os << LOG4CPLUS_TEXT("RESET "); |
700 | } | ||
701 | |||
702 |
2/4✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
|
27 | os << '/' << LOG4CPLUS_STRING_TO_TSTRING(event->path); |
703 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 10 times.
|
27 | if (event->nelem > 1) |
704 |
3/6✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 7 taken 17 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 17 times.
✗ Branch 12 not taken.
|
17 | os << '[' << index << ']'; |
705 |
2/4✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
|
27 | os << '/' << '/'; |
706 | |||
707 |
4/6✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
|
27 | if (state and event->message and event->message[index]) |
708 | os << ' ' | ||
709 |
3/6✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 9 taken 16 times.
✗ Branch 10 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
|
16 | << LOG4CPLUS_C_STR_TO_TSTRING(gettext(event->message[index])); |
710 | |||
711 |
2/4✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 27 times.
✗ Branch 10 not taken.
|
27 | eventLog.log(log_prio, os.str()); |
712 | } | ||
713 | |||
714 | ///////////////////////////////////////////////////////////////////////////// | ||
715 | 9680 | const Main::EventInstance* Main::m_getEventInstance(uint32_t seqNo) const | |
716 | { | ||
717 | 9680 | size_t ptr = size_t(seqNo) - eventInstance.front().seqNo; | |
718 | 9680 | size_t len = eventInstance.size(); | |
719 | |||
720 |
2/2✓ Branch 0 taken 9473 times.
✓ Branch 1 taken 207 times.
|
9680 | if (ptr >= len) |
721 | 9473 | ptr += len; | |
722 | |||
723 |
2/2✓ Branch 0 taken 207 times.
✓ Branch 1 taken 9473 times.
|
9680 | return ptr < len ? &eventInstance[ptr] : 0; |
724 | } | ||
725 | |||
726 | ///////////////////////////////////////////////////////////////////////////// | ||
727 | 21 | void Main::getActiveEvents(std::list<EventData>* list) const | |
728 | { | ||
729 | 42 | pthread::ReadLock lock(eventMutex); | |
730 | |||
731 | // A set of sorted instances | ||
732 | typedef std::set<const EventInstance*, EventInstance> EventDataSet; | ||
733 | 42 | EventDataSet set; | |
734 | |||
735 | 38 | for (EventInstanceMap::const_iterator it = eventMap.begin(); | |
736 |
2/2✓ Branch 6 taken 17 times.
✓ Branch 7 taken 21 times.
|
38 | it != eventMap.end(); ++it) { |
737 | 17 | const EventInstanceVector* vec = it->second; | |
738 | 52 | for (EventInstanceVector::const_iterator it2 = vec->begin(); | |
739 |
2/2✓ Branch 6 taken 35 times.
✓ Branch 7 taken 17 times.
|
52 | it2 != vec->end(); ++it2) { |
740 | 35 | const EventInstance* instance = *it2; | |
741 | |||
742 |
4/6✓ Branch 0 taken 9 times.
✓ Branch 1 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
|
35 | if (instance and instance->state) |
743 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | set.insert(instance); |
744 | } | ||
745 | } | ||
746 | |||
747 | // Add last event as well | ||
748 | 21 | const EventInstance* last = m_getEventInstance(eventSeqNo-1); | |
749 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4 times.
|
21 | if (last) |
750 |
1/2✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 | set.insert(last); |
751 | |||
752 |
2/2✓ Branch 8 taken 18 times.
✓ Branch 9 taken 21 times.
|
39 | for (EventDataSet::iterator it = set.begin(); it != set.end(); ++it) |
753 |
1/2✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
|
18 | list->push_back(**it); |
754 | 21 | } | |
755 | |||
756 | ///////////////////////////////////////////////////////////////////////////// | ||
757 | 9659 | EventData Main::getEvent(uint32_t seqNo) const | |
758 | { | ||
759 | 19318 | pthread::ReadLock lock(eventMutex); | |
760 | static EventData nullEvent; | ||
761 | |||
762 | 9659 | const EventInstance* e = m_getEventInstance(seqNo); | |
763 | |||
764 |
5/6✓ Branch 0 taken 190 times.
✓ Branch 1 taken 9469 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 160 times.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
|
19318 | return e and e->event and e->seqNo == seqNo ? *e : nullEvent; |
765 | } | ||
766 | |||
767 | ///////////////////////////////////////////////////////////////////////////// | ||
768 | 148 | uint32_t Main::getCurrentEventId() const | |
769 | { | ||
770 | 296 | pthread::ReadLock lock(eventMutex); | |
771 | 296 | return eventSeqNo; | |
772 | } | ||
773 | |||
774 | ///////////////////////////////////////////////////////////////////////////// | ||
775 | 148 | void Main::prepare(Session* /*session*/) const | |
776 | { | ||
777 | 148 | } | |
778 | |||
779 | ///////////////////////////////////////////////////////////////////////////// | ||
780 | 148 | void Main::cleanup(const Session* /*session*/) const | |
781 | { | ||
782 | 148 | } | |
783 | |||
784 | ///////////////////////////////////////////////////////////////////////////// | ||
785 | ///////////////////////////////////////////////////////////////////////////// | ||
786 | #ifdef GNUTLS_FOUND | ||
787 | ///////////////////////////////////////////////////////////////////////////// | ||
788 | 154 | int Main::setupTLS(Config tlsConf, log4cplus::Logger& logger) | |
789 | { | ||
790 | int result; | ||
791 | 154 | verifyClient = false; | |
792 | |||
793 |
3/4✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
✓ Branch 4 taken 71 times.
|
154 | if (!tlsConf) |
794 | 83 | return 0; | |
795 | |||
796 |
4/8✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 71 times.
✗ Branch 8 not taken.
✓ Branch 13 taken 71 times.
✗ Branch 14 not taken.
✓ Branch 17 taken 71 times.
✗ Branch 18 not taken.
|
71 | LOG4CPLUS_INFO_STR(logger, |
797 | LOG4CPLUS_TEXT("Starting TLS configuration")); | ||
798 | |||
799 | |||
800 | // GnuTLS logging facility | ||
801 |
2/4✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 71 times.
✗ Branch 7 not taken.
|
71 | int logLevel = tlsConf["loglevel"].toUInt(); |
802 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (logLevel > 0) { |
803 | ✗ | gnutls_global_set_log_level(logLevel); | |
804 | ✗ | gnutls_global_set_log_function([](int /*prio*/, const char *err) { | |
805 | ✗ | tlsLogger.forcedLog( | |
806 | ✗ | log4cplus::INFO_LOG_LEVEL, LOG4CPLUS_C_STR_TO_TSTRING(err)); | |
807 | ✗ | }); | |
808 | ✗ | ::tlsLogger = logger; | |
809 | } | ||
810 | |||
811 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | result = gnutls_global_init(); |
812 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
813 | ✗ | LOG4CPLUS_FATAL(logger, | |
814 | LOG4CPLUS_TEXT("gnutls_global_init() failed: ") | ||
815 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
816 | << LOG4CPLUS_TEXT(" (") | ||
817 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
818 | gnutls_strerror_name(result)) | ||
819 | << LOG4CPLUS_TEXT(")")); | ||
820 | ✗ | return result; | |
821 | } | ||
822 | |||
823 | 71 | gnutls_dh_params_t p = nullptr; | |
824 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | result = gnutls_dh_params_init(&p); // Allocate memory |
825 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
826 | ✗ | LOG4CPLUS_FATAL(logger, | |
827 | LOG4CPLUS_TEXT("gnutls_dh_params_init() failed: ") | ||
828 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
829 | << LOG4CPLUS_TEXT(" (") | ||
830 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
831 | gnutls_strerror_name(result)) | ||
832 | << LOG4CPLUS_TEXT(")")); | ||
833 | ✗ | return result; | |
834 | } | ||
835 | 71 | dh_params.reset(p); | |
836 | |||
837 |
1/2✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
142 | Config dh(tlsConf["dh"]); |
838 |
2/4✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 71 times.
|
71 | if (dh) { |
839 | ✗ | FILE* fd = fopen(dh.toString().c_str(), "r"); | |
840 | ✗ | struct stat sb; | |
841 | |||
842 | ✗ | if (!fd or fstat(fileno(fd), &sb)) { | |
843 | ✗ | const char* func = fd ? "fstat" : "fdopen"; | |
844 | ✗ | LOG4CPLUS_WARN(logger, | |
845 | LOG4CPLUS_C_STR_TO_TSTRING(func) | ||
846 | << LOG4CPLUS_TEXT("(") | ||
847 | << LOG4CPLUS_STRING_TO_TSTRING(dh.toString()) | ||
848 | << LOG4CPLUS_TEXT(") failed: ") | ||
849 | << LOG4CPLUS_C_STR_TO_TSTRING(strerror(errno)) | ||
850 | << LOG4CPLUS_TEXT(" (") | ||
851 | << errno | ||
852 | << LOG4CPLUS_TEXT(")")); | ||
853 | ✗ | sb.st_size = 0; | |
854 | } | ||
855 | else { | ||
856 | ✗ | unsigned char buf[sb.st_size]; | |
857 | ✗ | gnutls_datum_t params; | |
858 | ✗ | params.size = fread(buf, 1, sb.st_size, fd); | |
859 | ✗ | params.data = buf; | |
860 | |||
861 | ✗ | result = gnutls_dh_params_import_pkcs3( | |
862 | ✗ | dh_params.get(), ¶ms, GNUTLS_X509_FMT_PEM); | |
863 | |||
864 | ✗ | if (result) { | |
865 | ✗ | LOG4CPLUS_FATAL(logger, | |
866 | LOG4CPLUS_TEXT("gnutls_dh_params_import_pkcs3(") | ||
867 | << LOG4CPLUS_STRING_TO_TSTRING(dh.toString()) | ||
868 | << LOG4CPLUS_TEXT(") failed: ") | ||
869 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
870 | << LOG4CPLUS_TEXT(" (") | ||
871 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
872 | gnutls_strerror_name(result)) | ||
873 | << LOG4CPLUS_TEXT(")")); | ||
874 | ✗ | return result; | |
875 | } | ||
876 | else { | ||
877 | ✗ | LOG4CPLUS_INFO_STR(logger, | |
878 | LOG4CPLUS_TEXT( | ||
879 | "Successfully loaded DH parameters from file")); | ||
880 | } | ||
881 | } | ||
882 | |||
883 | ✗ | if (fd) | |
884 | ✗ | fclose(fd); | |
885 | } | ||
886 | else { | ||
887 |
2/4✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 71 times.
✗ Branch 7 not taken.
|
71 | unsigned int bits(tlsConf["dh-bits"].toUInt(1024)); |
888 |
1/2✓ Branch 3 taken 71 times.
✗ Branch 4 not taken.
|
71 | result = gnutls_dh_params_generate2(dh_params.get(), bits); |
889 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
890 | ✗ | LOG4CPLUS_FATAL(logger, | |
891 | LOG4CPLUS_TEXT("gnutls_dh_params_generate2(") | ||
892 | << bits | ||
893 | << LOG4CPLUS_TEXT(") failed: ") | ||
894 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
895 | << LOG4CPLUS_TEXT(" (") | ||
896 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
897 | gnutls_strerror_name(result)) | ||
898 | << LOG4CPLUS_TEXT(")")); | ||
899 | ✗ | return result; | |
900 | } | ||
901 | else { | ||
902 |
8/16✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 71 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 71 times.
✗ Branch 11 not taken.
✓ Branch 14 taken 71 times.
✗ Branch 15 not taken.
✓ Branch 20 taken 71 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 71 times.
✗ Branch 25 not taken.
✓ Branch 28 taken 71 times.
✗ Branch 29 not taken.
✓ Branch 32 taken 71 times.
✗ Branch 33 not taken.
|
71 | LOG4CPLUS_INFO(logger, |
903 | LOG4CPLUS_TEXT( | ||
904 | "Successfully generated DH parameters with ") | ||
905 | << bits | ||
906 | << LOG4CPLUS_TEXT(" bits")); | ||
907 | } | ||
908 | } | ||
909 | |||
910 | // Set priority string | ||
911 |
3/6✓ Branch 3 taken 71 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 71 times.
✗ Branch 10 not taken.
✓ Branch 13 taken 71 times.
✗ Branch 14 not taken.
|
142 | std::string priority(tlsConf["priority"].toString("NORMAL")); |
912 | 71 | const char* errpos = 0; | |
913 | 71 | gnutls_priority_t prio = nullptr; | |
914 |
1/2✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | result = gnutls_priority_init(&prio, priority.c_str(), &errpos); |
915 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
916 | ✗ | LOG4CPLUS_FATAL(logger, | |
917 | LOG4CPLUS_TEXT("gnutls_priority_init(") | ||
918 | << LOG4CPLUS_STRING_TO_TSTRING(priority) | ||
919 | << LOG4CPLUS_TEXT(") failed at position ") | ||
920 | << (errpos - priority.c_str()) | ||
921 | << LOG4CPLUS_TEXT(": ") | ||
922 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
923 | << LOG4CPLUS_TEXT(" (") | ||
924 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
925 | gnutls_strerror_name(result)) | ||
926 | << LOG4CPLUS_TEXT(")")); | ||
927 | ✗ | return result; | |
928 | } | ||
929 | else { | ||
930 |
4/8✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 71 times.
✗ Branch 8 not taken.
✓ Branch 13 taken 71 times.
✗ Branch 14 not taken.
✓ Branch 17 taken 71 times.
✗ Branch 18 not taken.
|
71 | LOG4CPLUS_INFO_STR(logger, |
931 | LOG4CPLUS_TEXT("Successfully initialized priority string")); | ||
932 | 71 | priority_cache.reset(prio); | |
933 | } | ||
934 | |||
935 | 71 | gnutls_certificate_credentials_t cc = nullptr; | |
936 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | result = gnutls_certificate_allocate_credentials(&cc); |
937 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
938 | ✗ | LOG4CPLUS_FATAL(logger, | |
939 | LOG4CPLUS_TEXT( | ||
940 | "gnutls_certificate_allocate_credentials() failed: ") | ||
941 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
942 | << LOG4CPLUS_TEXT(" (") | ||
943 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
944 | gnutls_strerror_name(result)) | ||
945 | << LOG4CPLUS_TEXT(")")); | ||
946 | ✗ | return result; | |
947 | } | ||
948 | 71 | tls.reset(cc); | |
949 | |||
950 |
1/2✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
|
71 | gnutls_certificate_set_dh_params(tls.get(), dh_params.get()); |
951 | |||
952 |
2/4✓ Branch 3 taken 71 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 71 times.
✗ Branch 10 not taken.
|
142 | std::string cert(tlsConf["cert"].toString()); |
953 |
2/4✓ Branch 3 taken 71 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 71 times.
✗ Branch 10 not taken.
|
142 | std::string key(tlsConf["key"].toString()); |
954 |
1/2✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
|
142 | result = gnutls_certificate_set_x509_key_file( |
955 | 71 | tls.get(), cert.c_str(), key.c_str(), GNUTLS_X509_FMT_PEM); | |
956 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (result) { |
957 | ✗ | LOG4CPLUS_FATAL(logger, | |
958 | LOG4CPLUS_TEXT("gnutls_certificate_set_x509_key_file(") | ||
959 | << LOG4CPLUS_TEXT("key=") | ||
960 | << LOG4CPLUS_STRING_TO_TSTRING(key) | ||
961 | << LOG4CPLUS_TEXT(", cert=") | ||
962 | << LOG4CPLUS_STRING_TO_TSTRING(cert) | ||
963 | << LOG4CPLUS_TEXT(") failed: ") | ||
964 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
965 | << LOG4CPLUS_TEXT(" (") | ||
966 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
967 | gnutls_strerror_name(result)) | ||
968 | << LOG4CPLUS_TEXT(")")); | ||
969 | ✗ | tls.reset(); | |
970 | ✗ | return result; | |
971 | } | ||
972 | else { | ||
973 |
4/8✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 71 times.
✗ Branch 8 not taken.
✓ Branch 13 taken 71 times.
✗ Branch 14 not taken.
✓ Branch 17 taken 71 times.
✗ Branch 18 not taken.
|
71 | LOG4CPLUS_INFO_STR(logger, |
974 | LOG4CPLUS_TEXT( | ||
975 | "Successfully loaded server key and certificate")); | ||
976 | } | ||
977 | |||
978 |
2/4✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 71 times.
✗ Branch 9 not taken.
|
71 | parseCertConfigItem(logger, tlsConf["ca"], &Main::loadTrustFile); |
979 |
2/4✓ Branch 5 taken 71 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 71 times.
✗ Branch 9 not taken.
|
71 | parseCertConfigItem(logger, tlsConf["crl"], &Main::loadCrlFile); |
980 | |||
981 | 71 | return 0; | |
982 | } | ||
983 | |||
984 | ///////////////////////////////////////////////////////////////////////////// | ||
985 | 157 | void Main::destroyTLS() | |
986 | { | ||
987 | 157 | gnutls_global_deinit(); | |
988 | 157 | } | |
989 | |||
990 | ///////////////////////////////////////////////////////////////////////////// | ||
991 | 28 | void Main::loadTrustFile(log4cplus::Logger& logger, const char* cafile) | |
992 | { | ||
993 | 28 | int result = gnutls_certificate_set_x509_trust_file( | |
994 | 56 | tls.get(), cafile, GNUTLS_X509_FMT_PEM); | |
995 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (result > 0) { |
996 |
4/8✓ Branch 5 taken 28 times.
✗ Branch 6 not taken.
✓ Branch 19 taken 28 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 28 times.
✗ Branch 24 not taken.
✓ Branch 33 taken 28 times.
✗ Branch 34 not taken.
|
28 | LOG4CPLUS_INFO(logger, |
997 | LOG4CPLUS_TEXT("Successfully loaded ") | ||
998 | << result | ||
999 | << LOG4CPLUS_TEXT(" certificates from ") | ||
1000 | << LOG4CPLUS_C_STR_TO_TSTRING(cafile)); | ||
1001 | } | ||
1002 | ✗ | else if (result == 0) { | |
1003 | ✗ | LOG4CPLUS_WARN(logger, | |
1004 | LOG4CPLUS_TEXT("gnutls_certificate_set_x509_trust_file(): ") | ||
1005 | << LOG4CPLUS_STRING_TO_TSTRING(cafile) | ||
1006 | << LOG4CPLUS_TEXT(" contains no certificate authorities.")); | ||
1007 | ✗ | throw PdServ::Errno(EINVAL); | |
1008 | } | ||
1009 | else { | ||
1010 | ✗ | LOG4CPLUS_WARN(logger, | |
1011 | LOG4CPLUS_TEXT("gnutls_certificate_set_x509_trust_file(") | ||
1012 | << LOG4CPLUS_STRING_TO_TSTRING(cafile) | ||
1013 | << LOG4CPLUS_TEXT(") failed: ") | ||
1014 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
1015 | << LOG4CPLUS_TEXT(" (") | ||
1016 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
1017 | gnutls_strerror_name(result)) | ||
1018 | << LOG4CPLUS_TEXT(")")); | ||
1019 | ✗ | throw PdServ::Errno(result); | |
1020 | } | ||
1021 | 28 | } | |
1022 | |||
1023 | ///////////////////////////////////////////////////////////////////////////// | ||
1024 | ✗ | void Main::loadCrlFile(log4cplus::Logger& logger, const char* crlfile) | |
1025 | { | ||
1026 | ✗ | struct stat sb; | |
1027 | |||
1028 | ✗ | LOG4CPLUS_DEBUG(logger, | |
1029 | LOG4CPLUS_TEXT("Considering ") | ||
1030 | << LOG4CPLUS_C_STR_TO_TSTRING(crlfile) | ||
1031 | << LOG4CPLUS_TEXT(" as CRL file")); | ||
1032 | |||
1033 | ✗ | if (stat(crlfile, &sb) or !S_ISREG(sb.st_mode) or sb.st_size == 0) { | |
1034 | ✗ | const char* name = ::basename(crlfile); | |
1035 | ✗ | if (name) { | |
1036 | ✗ | datum_string str(name); | |
1037 | |||
1038 | ✗ | blacklist.insert(str); | |
1039 | ✗ | LOG4CPLUS_INFO(logger, | |
1040 | LOG4CPLUS_TEXT("Added Key ID ") | ||
1041 | << LOG4CPLUS_STRING_TO_TSTRING(std::string(str)) | ||
1042 | << LOG4CPLUS_TEXT(" to blacklist")); | ||
1043 | } | ||
1044 | } | ||
1045 | else { | ||
1046 | ✗ | int result = gnutls_certificate_set_x509_crl_file( | |
1047 | ✗ | tls.get(), crlfile, GNUTLS_X509_FMT_PEM); | |
1048 | ✗ | if (result > 0) { | |
1049 | ✗ | LOG4CPLUS_INFO(logger, | |
1050 | LOG4CPLUS_TEXT("Successfully loaded ") | ||
1051 | << result | ||
1052 | << LOG4CPLUS_TEXT(" revoked certificates from ") | ||
1053 | << LOG4CPLUS_C_STR_TO_TSTRING(crlfile)); | ||
1054 | } | ||
1055 | ✗ | else if (result == 0) { | |
1056 | ✗ | LOG4CPLUS_WARN(logger, | |
1057 | LOG4CPLUS_TEXT("gnutls_certificate_set_x509_crl_file(): ") | ||
1058 | << LOG4CPLUS_STRING_TO_TSTRING(crlfile) | ||
1059 | << LOG4CPLUS_TEXT(" contains no CRL.")); | ||
1060 | ✗ | throw PdServ::Errno(EINVAL); | |
1061 | } | ||
1062 | else { | ||
1063 | ✗ | LOG4CPLUS_WARN(logger, | |
1064 | LOG4CPLUS_TEXT("gnutls_certificate_set_x509_crl_file(") | ||
1065 | << LOG4CPLUS_STRING_TO_TSTRING(crlfile) | ||
1066 | << LOG4CPLUS_TEXT(") failed: ") | ||
1067 | << LOG4CPLUS_C_STR_TO_TSTRING(gnutls_strerror(result)) | ||
1068 | << LOG4CPLUS_TEXT(" (") | ||
1069 | << LOG4CPLUS_C_STR_TO_TSTRING( | ||
1070 | gnutls_strerror_name(result)) | ||
1071 | << LOG4CPLUS_TEXT(")")); | ||
1072 | ✗ | throw PdServ::Errno(result); | |
1073 | } | ||
1074 | } | ||
1075 | } | ||
1076 | |||
1077 | ///////////////////////////////////////////////////////////////////////////// | ||
1078 | 28 | void Main::parseCertConfigDir(log4cplus::Logger& logger, const char* path, | |
1079 | void (Main::*loadFunc)(log4cplus::Logger&, const char*)) | ||
1080 | { | ||
1081 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | DIR* dir = opendir(path); |
1082 | struct dirent* entry; | ||
1083 | |||
1084 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (dir) |
1085 | ✗ | while ((entry = readdir(dir))) { | |
1086 | ✗ | if (entry->d_name[0] != '.') { | |
1087 | ✗ | std::string name(path); | |
1088 | ✗ | name.append(1,'/'); | |
1089 | ✗ | name.append(entry->d_name); | |
1090 | ✗ | parseCertConfigDir(logger, name.c_str(), loadFunc); | |
1091 | } | ||
1092 | } | ||
1093 | else | ||
1094 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
|
28 | (this->*loadFunc)(logger, path); |
1095 | 28 | } | |
1096 | |||
1097 | ///////////////////////////////////////////////////////////////////////////// | ||
1098 | 142 | void Main::parseCertConfigItem(log4cplus::Logger& logger, Config config, | |
1099 | void (Main::*loadFunc)(log4cplus::Logger&, const char*)) | ||
1100 | { | ||
1101 |
2/2✓ Branch 1 taken 114 times.
✓ Branch 2 taken 28 times.
|
142 | if (!config) |
1102 | 114 | return; | |
1103 | |||
1104 | 28 | verifyClient = true; | |
1105 | 28 | gnutls_certificate_set_verify_function( | |
1106 | 28 | tls.get(), Session::gnutls_verify_client); | |
1107 | |||
1108 | 28 | size_t i = 0; | |
1109 |
6/8✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 56 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 28 times.
✓ Branch 10 taken 28 times.
✓ Branch 11 taken 28 times.
|
84 | while (Config item = config[i++]) |
1110 |
4/6✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✓ Branch 13 taken 28 times.
✗ Branch 14 not taken.
✓ Branch 20 taken 28 times.
✓ Branch 21 taken 28 times.
|
56 | parseCertConfigDir(logger, item.toString().c_str(), loadFunc); |
1111 | } | ||
1112 | |||
1113 | ///////////////////////////////////////////////////////////////////////////// | ||
1114 | 151 | bool Main::tlsReady() const | |
1115 | { | ||
1116 | 151 | return static_cast<bool>(tls); | |
1117 | } | ||
1118 | |||
1119 | ///////////////////////////////////////////////////////////////////////////// | ||
1120 | 66 | void Main::initTlsSessionData(gnutls_session_t session) const | |
1121 | { | ||
1122 | 66 | gnutls_priority_set(session, priority_cache.get()); | |
1123 | 66 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, tls.get()); | |
1124 |
3/4✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✓ Branch 5 taken 28 times.
✓ Branch 6 taken 38 times.
|
66 | if (verifyClient) |
1125 | 28 | gnutls_certificate_server_set_request(session, GNUTLS_CERT_REQUIRE); | |
1126 | |||
1127 |
1/2✓ Branch 3 taken 66 times.
✗ Branch 4 not taken.
|
132 | gnutls_db_set_retrieve_function(session, [](void *ptr, gnutls_datum_t key) { |
1128 | ✗ | return reinterpret_cast<TlsSessionDB *>(ptr)->retrieve(key); | |
1129 | 66 | }); | |
1130 |
1/2✓ Branch 3 taken 66 times.
✗ Branch 4 not taken.
|
132 | gnutls_db_set_remove_function(session, [](void *ptr, gnutls_datum_t key) { |
1131 | ✗ | return reinterpret_cast<TlsSessionDB *>(ptr)->erase(key); | |
1132 | 66 | }); | |
1133 |
1/2✓ Branch 3 taken 66 times.
✗ Branch 4 not taken.
|
66 | gnutls_db_set_store_function( |
1134 | 66 | session, [](void *ptr, gnutls_datum_t key, gnutls_datum_t data) { | |
1135 | ✗ | return reinterpret_cast<TlsSessionDB *>(ptr)->store(key, data); | |
1136 | 66 | }); | |
1137 | 66 | gnutls_db_set_ptr(session, &tlsSessionDB); | |
1138 | 66 | } | |
1139 | |||
1140 | ///////////////////////////////////////////////////////////////////////////// | ||
1141 | |||
1142 | #endif | ||
1143 | |||
1144 | ///////////////////////////////////////////////////////////////////////////// | ||
1145 | 154 | void Main::setupAccessControl(const Config& config) | |
1146 | { | ||
1147 |
2/4✓ Branch 3 taken 154 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 154 times.
✗ Branch 10 not taken.
|
308 | const std::string guest = config["guest"].toString(); |
1148 | |||
1149 | 154 | m_loginMandatory = guest == "deny"; | |
1150 |
3/4✓ Branch 1 taken 27 times.
✓ Branch 2 taken 127 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
|
154 | m_write = guest.empty() or guest == "write"; |
1151 | 154 | } | |
1152 | |||
1153 | ///////////////////////////////////////////////////////////////////////////// | ||
1154 | 751 | bool Main::loginMandatory() const | |
1155 | { | ||
1156 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 751 times.
|
751 | return m_loginMandatory; |
1157 | 9 | } | |
1158 |