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