1 | /********************************************************************\
|
---|
2 |
|
---|
3 | General code to start a server of the Evidence Control System
|
---|
4 |
|
---|
5 | - The server is started with the given name.
|
---|
6 | - DIM exit and error handlers are implemented.
|
---|
7 | - The Status service is published (special format, see below).
|
---|
8 | It can be updated with the State() method. The text will also be logged.
|
---|
9 | - If the severity of a State() call is FATAL, exit() will be called (with
|
---|
10 | this severity, the call to State() is guranteed not to return).
|
---|
11 | - Configuration data can be requested by GetConfig().
|
---|
12 | - Signal handlers to ignore common signals are installed.
|
---|
13 | These signals will then cause pause() to return which can be used
|
---|
14 | by the application to terminate gracefully.
|
---|
15 | - The static method ToString() converts the contents of a
|
---|
16 | DIMInfo service into text
|
---|
17 |
|
---|
18 | All memory allocated by the non-static methods will be freed by the
|
---|
19 | class destructor.
|
---|
20 |
|
---|
21 | Oliver Grimm, December 2009
|
---|
22 |
|
---|
23 | \********************************************************************/
|
---|
24 |
|
---|
25 | #include "Evidence.h"
|
---|
26 |
|
---|
27 | bool EvidenceServer::ExitRequest = false;
|
---|
28 |
|
---|
29 | // Constructor starts server with given name
|
---|
30 | EvidenceServer::EvidenceServer(const char *Name) {
|
---|
31 |
|
---|
32 | // Initialize
|
---|
33 | Status = NULL;
|
---|
34 | ConfigList = NULL;
|
---|
35 | ConfigNum = 0;
|
---|
36 |
|
---|
37 | // Catch some signals
|
---|
38 | signal(SIGQUIT, &SignalHandler); // CTRL-Backspace
|
---|
39 | signal(SIGTERM, &SignalHandler); // Termination signal
|
---|
40 | signal(SIGINT, &SignalHandler); // CTRL-C
|
---|
41 | signal(SIGHUP, &SignalHandler); // Terminal closed
|
---|
42 |
|
---|
43 | // Create server name
|
---|
44 | if (MakeString(&StatusName, "%s/Status", Name) == -1) {
|
---|
45 | State(FATAL, "Could not generate service name, asprintf() failed");
|
---|
46 | }
|
---|
47 |
|
---|
48 | // Start server
|
---|
49 | Status = new DimService(StatusName, (char *) "Server started");
|
---|
50 |
|
---|
51 | start(Name);
|
---|
52 | addExitHandler(this);
|
---|
53 | }
|
---|
54 |
|
---|
55 | // Destructor: Free memory
|
---|
56 | EvidenceServer::~EvidenceServer() {
|
---|
57 |
|
---|
58 | free(StatusName);
|
---|
59 |
|
---|
60 | for (unsigned int i=0; i<ConfigNum; i++) {
|
---|
61 | delete[] ConfigList[i].Name;
|
---|
62 | delete[] ConfigList[i].Value;
|
---|
63 | }
|
---|
64 | free(ConfigList);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // DIM exit handler
|
---|
68 | void EvidenceServer::exitHandler(int Code) {
|
---|
69 | State(INFO, "Server stopped (DIM exit code %d)", Code);
|
---|
70 | exit(EXIT_SUCCESS);
|
---|
71 | }
|
---|
72 |
|
---|
73 | // DIM error handler
|
---|
74 | void EvidenceServer::errorHandler(int Severity, int Code, char *Message) {
|
---|
75 | State(ERROR, "%s (DIM error code %d, severity %d)\n", Message, Code, Severity);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Set status of server
|
---|
79 | //
|
---|
80 | // The message format is special: after the string-terminating '\0' the Severity
|
---|
81 | // is given, terminated by another '\0' The buffer for the DIM service must have
|
---|
82 | // the same lifetime as the DIM service. If Severity is FATAL, exit() will be invoked.
|
---|
83 | void EvidenceServer::State(StateType Severity, const char *Format, ...) {
|
---|
84 |
|
---|
85 | static const char* StateString[] = {"Info", "Warn", "Error", "Fatal"};
|
---|
86 | static char ErrorString[] = "vasprintf() failed in State()";
|
---|
87 | static char SBuf[STATUS_SIZE];
|
---|
88 | char TBuf[STATUS_SIZE];
|
---|
89 | char *Tmp;
|
---|
90 |
|
---|
91 | // Assemble message from application
|
---|
92 | va_list ArgumentPointer;
|
---|
93 | va_start(ArgumentPointer, Format);
|
---|
94 | if (vasprintf(&Tmp, Format, ArgumentPointer) == -1) Tmp = ErrorString;
|
---|
95 | va_end(ArgumentPointer);
|
---|
96 |
|
---|
97 | snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", StatusName, StateString[Severity], Tmp); // Normal string
|
---|
98 | snprintf(SBuf, sizeof(SBuf), "%s*%c", Tmp, (char) Severity);
|
---|
99 | *(strrchr(SBuf, '*')) = '\0'; // String with severity encoding
|
---|
100 | if (Tmp != ErrorString) free(Tmp);
|
---|
101 |
|
---|
102 | // Send message to console and log file
|
---|
103 | printf("%s\n", TBuf);
|
---|
104 | if (Severity != INFO) DimClient::sendCommand("DColl/Log", TBuf);
|
---|
105 |
|
---|
106 | // Update DIM status service (including severity encoding)
|
---|
107 | if (Status != NULL) Status->updateService(SBuf, strlen(SBuf)+2);
|
---|
108 |
|
---|
109 | // Terminate if message type is fatal
|
---|
110 | if (Severity == FATAL) exit(EXIT_FAILURE);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Get configuration data (program terminates if data is missing)
|
---|
114 | //
|
---|
115 | // The memory allocated by all calls to this function will be freed by
|
---|
116 | // the destructor.
|
---|
117 | char* EvidenceServer::GetConfig(const char *Item) {
|
---|
118 |
|
---|
119 | // Determine configuration file update time
|
---|
120 | DimCurrentInfo ModifyTime("Config/ModifyTime", 0);
|
---|
121 | int Time = ModifyTime.getInt(), ItemNo = -1;
|
---|
122 |
|
---|
123 | // Check if configuration request already in list
|
---|
124 | for (unsigned int i=0; i<ConfigNum; i++) {
|
---|
125 | if (strcmp(ConfigList[i].Name, Item) == 0) {
|
---|
126 | // Return original value if still up to date
|
---|
127 | if (ConfigList[i].Time >= Time) return ConfigList[i].Value;
|
---|
128 |
|
---|
129 | // Otherwise, free memory of old value
|
---|
130 | delete[] ConfigList[i].Name;
|
---|
131 | delete[] ConfigList[i].Value;
|
---|
132 | ItemNo = i;
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | // Make configuration request
|
---|
138 | DimRpcInfo Config((char *) "ConfigRequest", (char *) "");
|
---|
139 | Config.setData((char *) Item);
|
---|
140 |
|
---|
141 | // Terminate if not successful
|
---|
142 | if (strlen(Config.getString()) == 0) {
|
---|
143 | State(FATAL, "Missing configuration data '%s'", Item);
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Enlarge memory to hold new pointer if necessary
|
---|
147 | if (ItemNo == -1) {
|
---|
148 | void *N = realloc(ConfigList, sizeof(struct ConfigItem)*(++ConfigNum));
|
---|
149 | if (N == NULL) {
|
---|
150 | State(WARN, "Could not realloc() memory for configuration, will lose memory (%s)", strerror(errno));
|
---|
151 | ConfigNum--;
|
---|
152 | }
|
---|
153 | else ConfigList = (struct ConfigItem *) N;
|
---|
154 |
|
---|
155 | ItemNo = ConfigNum-1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // Allocate memory for strings, and copy data to this memory
|
---|
159 | ConfigList[ItemNo].Value = new char [strlen(Config.getString())+1];
|
---|
160 | ConfigList[ItemNo].Name = new char [strlen(Item)+1];
|
---|
161 | strcpy(ConfigList[ItemNo].Name, Item);
|
---|
162 | strcpy(ConfigList[ItemNo].Value, Config.getString());
|
---|
163 |
|
---|
164 | ConfigList[ItemNo].Time = Time;
|
---|
165 |
|
---|
166 | // Return address to configuration value
|
---|
167 | return ConfigList[ItemNo].Value;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | // ====== Static methods ======
|
---|
172 |
|
---|
173 | // Signal handler (causes pause() and other syscalls to return)
|
---|
174 | void EvidenceServer::SignalHandler(int) {
|
---|
175 |
|
---|
176 | EvidenceServer::ExitRequest = true;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Translates DIMInfo to string (memory has to be freed by caller)
|
---|
180 | // No DIM structures are supported (only a single number or string is converted)
|
---|
181 | char *EvidenceServer::ToString(DimInfo *Item) {
|
---|
182 |
|
---|
183 | char *Text;
|
---|
184 |
|
---|
185 | if (strlen(Item->getFormat()) != 1) return NULL;
|
---|
186 |
|
---|
187 | switch (*(Item->getFormat())) {
|
---|
188 | case 'I': MakeString(&Text, "%d", Item->getInt()); break;
|
---|
189 | case 'C': MakeString(&Text, "%s", Item->getString()); break;
|
---|
190 | case 'S': MakeString(&Text, "%hd", Item->getShort()); break;
|
---|
191 | case 'F': MakeString(&Text, "%.5f", Item->getFloat()); break;
|
---|
192 | case 'D': MakeString(&Text, "%.5f", Item->getDouble()); break;
|
---|
193 | case 'X': MakeString(&Text, "%lld", Item->getLonglong()); break;
|
---|
194 | default: return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | return Text;
|
---|
198 | }
|
---|
199 |
|
---|
200 | //
|
---|
201 | // Generate string with vasprintf()
|
---|
202 | //
|
---|
203 | // The pointer will be set to NULL in case of error, so can always safely passed to free().
|
---|
204 | // In case vasprintf() is not available on a particular system, the functionality can
|
---|
205 | // be manually implemented in this routine.
|
---|
206 | //
|
---|
207 | int EvidenceServer::MakeString(char **Pointer, const char *Format, ...) {
|
---|
208 |
|
---|
209 | int Ret;
|
---|
210 | va_list ArgumentPointer;
|
---|
211 |
|
---|
212 | va_start(ArgumentPointer, Format);
|
---|
213 | if ((Ret = vasprintf(Pointer, Format, ArgumentPointer)) == -1) Pointer = NULL;
|
---|
214 | va_end(ArgumentPointer);
|
---|
215 |
|
---|
216 | return Ret;
|
---|
217 | }
|
---|