GCC Code Coverage Report


Directory: ./
File: pdserv/src/Main.h
Date: 2024-11-17 04:08:36
Exec Total Coverage
Lines: 1 1 100.0%
Branches: 0 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 #ifndef MAIN_H
25 #define MAIN_H
26
27 #include <map>
28 #include <set>
29 #include <list>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 #include <stdint.h>
34 #include <log4cplus/logger.h>
35
36 #include "PThread.h"
37 #include "Config.h"
38 #include "Event.h"
39 #include "config.h"
40
41 #ifdef GNUTLS_FOUND
42 #include <gnutls/gnutls.h>
43 #include "TLS.h"
44 #endif
45
46 struct timespec;
47 class TlsSessionDB;
48
49 namespace MsrProto {
50 class Server;
51 }
52
53 struct Blacklist;
54 class TlsSessionDB;
55
56 namespace PdServ {
57
58 class Signal;
59 class Event;
60 class Parameter;
61 class ProcessParameter;
62 class Variable;
63 class Task;
64 class Session;
65 struct SessionStatistics;
66
67 class Main {
68 public:
69 struct RtProcessExited {};
70
71 Main(const std::string& name, const std::string& version);
72 virtual ~Main();
73
74 const std::string name; /**< Application name */
75 const std::string version; /**< Application version */
76
77 void startServers();
78 void stopServers();
79
80 bool loginMandatory() const;
81
82 #ifdef GNUTLS_FOUND
83 bool tlsReady() const;
84 void initTlsSessionData(gnutls_session_t) const;
85 27 const Blacklist& getBlacklist() const { return blacklist; }
86 #endif
87
88 // Get the current system time.
89 // Reimplement this method in the class specialization
90 virtual int gettime(struct timespec *) const;
91
92 virtual std::list<const Task*> getTasks() const = 0;
93 virtual std::list<const Event*> getEvents() const = 0;
94 virtual std::list<const Parameter*> getParameters() const = 0;
95 virtual void prepare(Session *session) const;
96 virtual void cleanup(const Session *session) const;
97 virtual Config config(const char* section) const = 0;
98
99 void getActiveEvents(std::list<EventData>*) const;
100 EventData getEvent(uint32_t id) const;
101 uint32_t getCurrentEventId() const;
102
103 // Setting a parameter has various steps:
104 // 1) client calls parameter->setValue(session, ...)
105 // This virtual method is implemented by ProcessParameter
106 // 2) ProcessParameter calls
107 // main->setValue(processParameter, session, ...)
108 // so that main can check whether session is allowed to set it
109 // 3) main calls Main::setValue(...) virtual method to do the setting
110 // 4) on success, main can do various functions, e.g.
111 // - inform clients of a value change
112 // - save persistent parameter
113 // - etc
114 int setValue(const ProcessParameter* p, const Session *session,
115 const char* buf, size_t offset, size_t count);
116
117 protected:
118 void setupLogging(const std::string& configFile);
119
120 static int localtime(struct timespec *);
121
122 void savePersistent();
123 unsigned int setupPersistent();
124
125 void newEvent(const Event* event,
126 size_t element, int state, const struct timespec* time);
127
128 // virtual functions to be implemented in derived classes
129 virtual void initializeParameter(Parameter* p,
130 const char* data, const struct timespec* mtime,
131 const Signal* s) = 0;
132 virtual bool getPersistentSignalValue(const Signal *s,
133 char* buf, struct timespec* time) = 0;
134 virtual Parameter* findParameter(const std::string& path) const = 0;
135 virtual int setValue(const ProcessParameter* p,
136 const char* buf, size_t offset, size_t count,
137 const char** value, const struct timespec**) = 0;
138
139 private:
140 struct EventInstance;
141
142 EventInstance* eventPtr;
143 std::vector<EventInstance> eventInstance;
144 std::set<EventInstance*> danglingInstances;
145
146 typedef std::vector<EventInstance*> EventInstanceVector;
147 typedef std::map<const Event*, EventInstanceVector*> EventInstanceMap;
148 EventInstanceMap eventMap;
149
150 mutable pthread::RWLock eventMutex;
151 uint32_t eventSeqNo;
152
153 const log4cplus::Logger parameterLog;
154 const log4cplus::Logger eventLog;
155
156 bool persistentLogTraceOn;
157 log4cplus::Logger persistentLog;
158 log4cplus::Logger persistentLogTrace;
159 PdServ::Config persistentConfig;
160 typedef std::map<const Parameter*, const Signal*> PersistentMap;
161 PersistentMap persistentMap;
162
163 MsrProto::Server *msrproto;
164 // EtlProto::Server etlproto(this);
165
166 void consoleLogging();
167 void syslogLogging();
168 const EventInstance* m_getEventInstance(uint32_t seqNo) const;
169
170 void setupAccessControl(const Config&);
171 bool m_loginMandatory;
172 bool m_write;
173
174 #ifdef GNUTLS_FOUND
175 pthread::Mutex tls_mutex;
176 bool verifyClient;
177 std::unique_ptr<gnutls_certificate_credentials_st, TlsDeleter> tls;
178 std::unique_ptr<gnutls_priority_st, TlsDeleter> priority_cache;
179 std::unique_ptr<gnutls_dh_params_int, TlsDeleter> dh_params;
180
181 mutable TlsSessionDB tlsSessionDB;
182 Blacklist blacklist;
183
184 int setupTLS(Config, log4cplus::Logger&);
185 void destroyTLS();
186 void loadTrustFile(log4cplus::Logger&, const char* cafile);
187 void loadCrlFile(log4cplus::Logger&, const char* cafile);
188 void parseCertConfigDir(log4cplus::Logger&, const char* path,
189 void (Main::*func)(log4cplus::Logger&, const char*));
190 void parseCertConfigItem(log4cplus::Logger&, Config config,
191 void (Main::*func)(log4cplus::Logger&, const char*));
192 #endif
193 };
194
195 }
196 #endif // MAIN_H
197