-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigserver.cpp
More file actions
262 lines (226 loc) · 8.09 KB
/
configserver.cpp
File metadata and controls
262 lines (226 loc) · 8.09 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
#include "configserver.h"
// STL
#include <algorithm>
// PUT
#include <put/cxxutils/syslogstream.h>
#include <put/specialized/procstat.h>
#ifndef CONFIG_CONFIG_PATH
#define CONFIG_CONFIG_PATH "/etc/config"
#endif
#ifndef CONFIG_GROUPNAME
#define CONFIG_GROUPNAME "config"
#endif
static const char* configfilename(const char* base)
{
// construct config filename
static char name[PATH_MAX];
posix::memset(name, 0, PATH_MAX);
if(posix::snprintf(name, PATH_MAX, "%s/%s.conf", CONFIG_CONFIG_PATH, base) == posix::error_response) // I don't how this could fail
return nullptr; // unable to build config filename
return name;
}
static bool readconfig(const std::string& name, std::string& buffer)
{
posix::FILE* file = posix::fopen(name.c_str(), "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;
}
ConfigServer::ConfigServer(void) noexcept
{
Object::connect(newPeerRequest , this, &ConfigServer::request);
Object::connect(newPeerMessage , this, &ConfigServer::receive);
Object::connect(disconnectedPeer, this, &ConfigServer::removePeer);
}
ConfigServer::~ConfigServer(void) noexcept
{
}
void ConfigServer::setCall(posix::fd_t socket, const std::string& key, const std::string& value) noexcept
{
posix::error_t errcode = posix::success_response;
auto configfile = m_configfiles.find(socket);
if(configfile == m_configfiles.end())
errcode = posix::error_t(posix::errc::io_error); // not a valid key!
else
configfile->second.config.getNode(key)->value = value;
setReturn(socket, errcode, key);
}
void ConfigServer::getCall(posix::fd_t socket, const std::string& key) noexcept
{
posix::error_t errcode = posix::success_response;
std::list<std::string> children;
std::string value;
auto configfile = m_configfiles.find(socket);
if(configfile != m_configfiles.end())
errcode = posix::error_t(posix::errc::io_error); // no config file for socket
else
{
auto node = configfile->second.config.findNode(key);
if(node == nullptr)
errcode = posix::error_t(posix::errc::invalid_argument); // node 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, key, value, children);
}
void ConfigServer::unsetCall(posix::fd_t socket, const std::string& key) noexcept
{
posix::error_t errcode = posix::success_response;
auto configfile = m_configfiles.find(socket);
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, key);
}
void ConfigServer::syncCall(posix::fd_t socket) noexcept
{
bool ok = true;
const auto& confpair = m_configfiles.find(socket); // find parsed config file
if(confpair != m_configfiles.end())
{
std::unordered_map<std::string, std::string> data;
confpair->second.config.exportKeyPairs(data); // export config data
for(const auto& pair : data) // for each key pair
ok &= valueSet(socket, pair.first, pair.second); // send value
}
syncReturn(socket, ok ? posix::success_response : errno); // send call response
}
bool ConfigServer::peerChooser(posix::fd_t socket, const proccred_t& cred) noexcept
{
if(!posix::useringroup(CONFIG_GROUPNAME, posix::getusername(cred.uid)))
return false;
process_state_t state;
if(!procstat(cred.pid, state)) // get state information about the connecting process
return false; // unable to get state
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?)
{
std::string buffer;
const char* filename = configfilename(state.name.c_str());
posix::chown(filename, ::getuid(), cred.gid); // reset ownership
posix::chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); // reset permissions
readconfig(filename, buffer);
auto& conffile = m_configfiles[socket];
conffile.fevent = std::make_unique<FileEvent>(filename, FileEvent::WriteEvent); // monitor file for write event
conffile.config.clear(); // erase any existing data
conffile.config.importText(buffer);
Object::connect(conffile.fevent->activated, this, &ConfigServer::fileUpdated);
m_endpoints[cred.pid] = socket; // insert or assign new value
return true;
}
return false; // reject multiple connections from one endpoint
}
void ConfigServer::fileUpdated(std::string filename, FileEvent::Flags_t flags) noexcept
{
posix::fd_t socket = posix::invalid_descriptor;
if(flags.WriteEvent)
for(auto& confpair : m_configfiles)
if(confpair.second.fevent->file() == filename)
{
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
socket = confpair.first;
if(readconfig(filename, 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())
valueUnset(socket, old_pair.first); // invoke value deletion
else if(iter->second != old_pair.second)
valueSet(socket, 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
valueSet(socket, 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 ConfigServer::removePeer(posix::fd_t socket) noexcept
{
auto configfile = m_configfiles.find(socket);
if(configfile != m_configfiles.end())
{
m_configfiles.erase(configfile);
for(auto endpoint : m_endpoints)
if(socket == endpoint.second)
{ m_endpoints.erase(endpoint.first); break; }
}
}
void ConfigServer::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 ConfigServer::receive(posix::fd_t socket, vfifo buffer, posix::fd_t fd) noexcept
{
(void)fd;
std::string key, value;
if(!(buffer >> value).hadError() && value == "RPC" &&
!(buffer >> value).hadError())
{
switch(hash(value))
{
case "syncCall"_hash:
syncCall(socket);
break;
case "setCall"_hash:
buffer >> key >> value;
if(!buffer.hadError())
setCall(socket, key, value);
break;
case "getCall"_hash:
buffer >> key;
if(!buffer.hadError())
getCall(socket, key);
break;
case "unsetCall"_hash:
buffer >> key;
if(!buffer.hadError())
unsetCall(socket, key);
break;
}
}
}