-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectorconfigserver.cpp
More file actions
315 lines (278 loc) · 10.4 KB
/
directorconfigserver.cpp
File metadata and controls
315 lines (278 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "directorconfigserver.h"
// POSIX
#include <dirent.h>
// STL
#include <algorithm>
// PUT
#include <put/cxxutils/syslogstream.h>
#ifndef DIRECTOR_CONFIG_PATH
#define DIRECTOR_CONFIG_PATH "/etc/director"
#endif
#ifndef DIRECTOR_USERNAME
#define DIRECTOR_USERNAME "director"
#endif
/*
static std::string extract_provider_name(const std::string& filename)
{
posix::size_t start = filename.rfind('/');
posix::size_t end = filename.rfind('.');
if(start == std::string::npos ||
end == std::string::npos)
return std::string();
return filename.substr(start, end - start);
}
*/
static const char* extract_provider_name(const char* filename)
{
char provider[NAME_MAX];
const char* start = posix::strrchr(filename, '/');
const char* end = posix::strrchr(filename, '.');
if(start == NULL || // if '/' NOT found OR
end == NULL || // '.' found AND
end < start || // occur in the incorrect order OR
posix::strcmp(end, ".conf")) // doesn't end with ".conf"
return nullptr;
return posix::strncpy(provider, start + 1, posix::size_t(end - start + 1)); // extract provider name
}
static const char* director_configfilename(const char* filename)
{
// construct config filename
static char fullpath[PATH_MAX];
posix::memset(fullpath, 0, PATH_MAX);
if(posix::snprintf(fullpath, PATH_MAX, "%s/%s", DIRECTOR_CONFIG_PATH, filename) == posix::error_response) // I don't how this could fail
return nullptr; // unable to build config filename
return fullpath;
}
static bool readconfig(const char* name, std::string& buffer)
{
posix::FILE* file = posix::fopen(name, "a+b");
if(file == NULL)
{
posix::syslog << posix::priority::warning
<< "Unable to open file: %1 : %2"
<< name
<< posix::strerror(errno)
<< posix::eom;
return false;
}
buffer.clear();
buffer.resize(posix::size_t(posix::ftell(file)), '\n');
if(buffer.size())
{
posix::rewind(file);
posix::fread(const_cast<char*>(buffer.data()), sizeof(std::string::value_type), buffer.size(), file);
}
posix::fclose(file);
return true;
}
DirectorConfigServer::DirectorConfigServer(void) noexcept
{
std::string buffer;
DIR* dir = ::opendir(DIRECTOR_CONFIG_PATH);
dirent* entry = NULL;
const char* provider = nullptr;
const char* filename = nullptr;
if(dir != NULL)
{
while((entry = ::readdir(dir)) != NULL)
{
if(entry->d_name[0] == '.') // if dot files/dirs
continue; // skip file
if((provider = extract_provider_name (entry->d_name)) == nullptr || // if provider name extraction failed OR
(filename = director_configfilename(entry->d_name)) == nullptr) // failed to build filename
continue; // skip file
if(readconfig(filename, buffer)) // able to read config file
{
auto& conffile = m_configfiles[provider];
conffile.fevent = std::make_unique<FileEvent>(filename, FileEvent::WriteEvent);
conffile.config.clear(); // erase any existing data
conffile.config.importText(buffer);
Object::connect(conffile.fevent->activated, this, &DirectorConfigServer::fileUpdated);
}
}
::closedir(dir);
}
/*
m_dir = EventBackend::watch(DIRECTOR_CONFIG_PATH, EventFlags::DirEvent);
if(m_dir > 0)
Object::connect(m_dir, this, &DirectorConfigServer::dirUpdated);
*/
Object::connect(newPeerRequest , this, &DirectorConfigServer::request);
Object::connect(newPeerMessage , this, &DirectorConfigServer::receive);
Object::connect(disconnectedPeer, this, &DirectorConfigServer::removePeer);
}
DirectorConfigServer::~DirectorConfigServer(void) noexcept
{
}
void DirectorConfigServer::fileUpdated(std::string filename, FileEvent::Flags_t flags) noexcept
{
const char* provider = nullptr;
if(flags.WriteEvent &&
(provider = extract_provider_name(filename.c_str())) != nullptr) // extracted provider name
for(auto& confpair : m_configfiles)
if(confpair.second.fevent->file() == provider)
{
std::string tmp_buffer;
std::unordered_map<std::string, std::string> old_config, new_config;
confpair.second.config.exportKeyPairs(old_config); // export data
confpair.second.config.clear(); // wipe config
if(readconfig(filename.c_str(), tmp_buffer) &&
confpair.second.config.importText(tmp_buffer))
{
confpair.second.config.exportKeyPairs(new_config);
for(auto& old_pair : old_config) // find removed and updated values
{
auto iter = new_config.find(old_pair.first);
if(iter == new_config.end())
for(auto& endpoint : m_endpoints)
valueUnset(endpoint.second, confpair.first, old_pair.first); // invoke value deletion
else if(iter->second != old_pair.second)
for(auto& endpoint : m_endpoints)
valueSet(endpoint.second, confpair.first, iter->first, iter->second); // invoke value update
}
for(auto& new_pair : new_config) // find completely new values
if(old_config.find(new_pair.first) == old_config.end()) // if old config doesn't have a new config key
for(auto& endpoint : m_endpoints)
valueSet(endpoint.second, confpair.first, new_pair.first, new_pair.second); // invoke value update
}
else
posix::syslog << posix::priority::warning
<< "Failed to read/parse config file: %1"
<< filename
<< posix::eom;
}
}
void DirectorConfigServer::dirUpdated(std::string dirname, FileEvent::Flags_t flags) noexcept
{
posix::printf("dir updated: %s - 0x%02x\n", dirname.c_str(), uint8_t(flags));
}
void DirectorConfigServer::listConfigsCall(posix::fd_t socket) noexcept
{
std::vector<std::string> names;
for(const auto& confpair : m_configfiles)
names.push_back(confpair.first);
listConfigsReturn(socket, names);
}
void DirectorConfigServer::syncCall(posix::fd_t socket) noexcept
{
bool ok = true;
for(const auto& confpair : m_configfiles) // for each parsed config file
{
std::unordered_map<std::string, std::string> data;
confpair.second.config.exportKeyPairs(data); // export config data
for(auto& pair : data) // for each key pair
ok &= valueSet(socket, confpair.first, pair.first, pair.second); // send value
}
syncReturn(socket, ok ? posix::success_response : errno); // send call response
}
void DirectorConfigServer::setCall(posix::fd_t socket, const std::string& config, const std::string& key, const std::string& value) noexcept
{
posix::error_t errcode = posix::success_response;
auto configfile = m_configfiles.find(config);
if(configfile == m_configfiles.end())
errcode = posix::error_t(posix::errc::invalid_argument); // not a valid config file name
else
configfile->second.config.getNode(key)->value = value;
setReturn(socket, errcode, config, key);
}
void DirectorConfigServer::getCall(posix::fd_t socket, const std::string& config, const std::string& key) noexcept
{
std::vector<std::string> children;
std::string value;
posix::error_t errcode = posix::success_response;
auto configfile = m_configfiles.find(config); // look up config by name
if(configfile == m_configfiles.end()) // if not found
errcode = posix::error_t(posix::errc::invalid_argument); // not a valid config file name
else
{
auto node = configfile->second.config.findNode(key); // find node in config file
if(node == nullptr)
errcode = posix::error_t(posix::errc::invalid_argument); // doesn't exist
else
{
switch(node->type)
{
case node_t::type_e::array:
case node_t::type_e::multisection:
case node_t::type_e::section:
for(const auto& child : node->children)
children.push_back(child.first);
case node_t::type_e::invalid:
case node_t::type_e::value:
case node_t::type_e::string:
value = node->value;
}
}
}
getReturn(socket, errcode, config, key, value, children);
}
void DirectorConfigServer::unsetCall(posix::fd_t socket, const std::string& config, const std::string& key) noexcept
{
posix::error_t errcode = posix::success_response;
auto configfile = m_configfiles.find(config); // look up config by name
if(configfile == m_configfiles.end())
errcode = posix::error_t(posix::errc::io_error); // no such config file!
else if(!configfile->second.config.deleteNode(key))
errcode = posix::error_t(posix::errc::invalid_argument); // doesn't exist
unsetReturn(socket, errcode, config, key);
}
bool DirectorConfigServer::peerChooser(posix::fd_t socket, const proccred_t& cred) noexcept
{
if(posix::strcmp(DIRECTOR_USERNAME, posix::getusername(cred.uid))) // username must be "director"
return false; // didn't match, reject connection
auto endpoint = m_endpoints.find(cred.pid);
if(endpoint == m_endpoints.end() || // if no connection exists OR
!peerData(endpoint->second)) // if old connection is mysteriously gone (can this happen?)
{
m_endpoints[cred.pid] = socket; // insert or assign new value
return true;
}
return false; // reject multiple connections from one endpoint
}
void DirectorConfigServer::removePeer(posix::fd_t socket) noexcept
{
for(auto endpoint : m_endpoints)
if(socket == endpoint.second)
{ m_endpoints.erase(endpoint.first); break; }
}
void DirectorConfigServer::request(posix::fd_t socket, posix::sockaddr_t addr, proccred_t cred) noexcept
{
(void)addr;
if(peerChooser(socket, cred))
acceptPeerRequest(socket);
else
rejectPeerRequest(socket);
}
void DirectorConfigServer::receive(posix::fd_t socket, vfifo buffer, posix::fd_t fd) noexcept
{
(void)fd;
std::string config, key, value;
if(!(buffer >> value).hadError() && value == "RPC" &&
!(buffer >> value).hadError())
{
switch(hash(value))
{
case "listConfigsCall"_hash:
listConfigsCall(socket);
break;
case "syncCall"_hash:
syncCall(socket);
break;
case "setCall"_hash:
buffer >> config >> key >> value;
if(!buffer.hadError())
setCall(socket, config, key, value);
break;
case "getCall"_hash:
buffer >> config >> key;
if(!buffer.hadError())
getCall(socket, config, key);
break;
case "unsetCall"_hash:
buffer >> config >> key;
if(!buffer.hadError())
unsetCall(socket, config, key);
break;
}
}
}