blob: 777b78fcee1c2629dc62749549a6b89c6425d143 [file] [log] [blame]
Andrew Jeffery47af65a2021-12-01 14:16:31 +10301/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16/// \file PerformScan.cpp
17#include "EntityManager.hpp"
18
19#include <boost/algorithm/string/predicate.hpp>
20#include <boost/asio/steady_timer.hpp>
21#include <boost/container/flat_map.hpp>
22#include <boost/container/flat_set.hpp>
23
24#include <charconv>
25
26/* Hacks from splitting EntityManager.cpp */
27extern std::shared_ptr<sdbusplus::asio::connection> systemBus;
28extern nlohmann::json lastJson;
29extern void
30 propertiesChangedCallback(nlohmann::json& systemConfiguration,
31 sdbusplus::asio::object_server& objServer);
32
Andrew Jeffery47af65a2021-12-01 14:16:31 +103033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
36
37constexpr const int32_t maxMapperDepth = 0;
38
39constexpr const bool debug = false;
40
Andrew Jefferyd9b67842022-04-05 16:44:20 +093041struct DBusInterfaceInstance
42{
43 std::string busName;
44 std::string path;
45 std::string interface;
46};
47
Andrew Jeffery47af65a2021-12-01 14:16:31 +103048void getInterfaces(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093049 const DBusInterfaceInstance& instance,
Andrew Jeffery47af65a2021-12-01 14:16:31 +103050 const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
51 const std::shared_ptr<PerformScan>& scan, size_t retries = 5)
52{
53 if (!retries)
54 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093055 std::cerr << "retries exhausted on " << instance.busName << " "
56 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103057 return;
58 }
59
60 systemBus->async_method_call(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093061 [instance, scan, probeVector, retries](boost::system::error_code& errc,
62 const DBusInterface& resp) {
Andrew Jeffery47af65a2021-12-01 14:16:31 +103063 if (errc)
64 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093065 std::cerr << "error calling getall on " << instance.busName
66 << " " << instance.path << " "
67 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103068
Andrew Jeffery91c5eaa2022-04-05 16:45:52 +093069 auto timer = std::make_shared<boost::asio::steady_timer>(io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103070 timer->expires_after(std::chrono::seconds(2));
71
Andrew Jefferyd9b67842022-04-05 16:44:20 +093072 timer->async_wait([timer, instance, scan, probeVector,
Andrew Jeffery47af65a2021-12-01 14:16:31 +103073 retries](const boost::system::error_code&) {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093074 getInterfaces(instance, probeVector, scan, retries - 1);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103075 });
76 return;
77 }
78
Andrew Jefferyd9b67842022-04-05 16:44:20 +093079 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
Andrew Jeffery47af65a2021-12-01 14:16:31 +103080 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093081 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
82 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103083
84 if constexpr (debug)
85 {
86 std::cerr << __func__ << " " << __LINE__ << "\n";
87 }
88}
89
Andrew Jeffery4dcc55d2022-04-05 16:49:42 +093090static void registerCallback(nlohmann::json& systemConfiguration,
91 sdbusplus::asio::object_server& objServer,
92 const std::string& path)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103093{
94 static boost::container::flat_map<std::string, sdbusplus::bus::match::match>
95 dbusMatches;
96
97 auto find = dbusMatches.find(path);
98 if (find != dbusMatches.end())
99 {
100 return;
101 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030102
Andrew Jeffery26db5752022-04-05 16:53:36 +0930103 std::function<void(sdbusplus::message::message & message)> eventHandler =
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030104 [&](sdbusplus::message::message&) {
105 propertiesChangedCallback(systemConfiguration, objServer);
106 };
107
108 sdbusplus::bus::match::match match(
109 static_cast<sdbusplus::bus::bus&>(*systemBus),
110 "type='signal',member='PropertiesChanged',path='" + path + "'",
111 eventHandler);
112 dbusMatches.emplace(path, std::move(match));
113}
114
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930115static void
116 processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector,
117 const std::shared_ptr<PerformScan>& scan,
118 const GetSubTreeType& interfaceSubtree)
119{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930120 for (const auto& [path, object] : interfaceSubtree)
121 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930122 // Get a PropertiesChanged callback for all interfaces on this path.
123 registerCallback(scan->_systemConfiguration, scan->objServer, path);
124
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930125 for (const auto& [busname, ifaces] : object)
126 {
127 for (const std::string& iface : ifaces)
128 {
129 // The 3 default org.freedeskstop interfaces (Peer,
130 // Introspectable, and Properties) are returned by
131 // the mapper but don't have properties, so don't bother
132 // with the GetAll call to save some cycles.
133 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
134 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930135 getInterfaces({busname, path, iface}, probeVector, scan);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930136 }
137 }
138 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930139 }
140}
141
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030142// Populates scan->dbusProbeObjects with all interfaces and properties
143// for the paths that own the interfaces passed in.
144void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
145 boost::container::flat_set<std::string>&& interfaces,
146 const std::shared_ptr<PerformScan>& scan,
147 size_t retries = 5)
148{
149 // Filter out interfaces already obtained.
150 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
151 {
152 for (const auto& [interface, _] : probeInterfaces)
153 {
154 interfaces.erase(interface);
155 }
156 }
157 if (interfaces.empty())
158 {
159 return;
160 }
161
162 // find all connections in the mapper that expose a specific type
163 systemBus->async_method_call(
164 [interfaces, probeVector{std::move(probeVector)}, scan,
165 retries](boost::system::error_code& ec,
166 const GetSubTreeType& interfaceSubtree) mutable {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030167 if (ec)
168 {
169 if (ec.value() == ENOENT)
170 {
171 return; // wasn't found by mapper
172 }
173 std::cerr << "Error communicating to mapper.\n";
174
175 if (!retries)
176 {
177 // if we can't communicate to the mapper something is very
178 // wrong
179 std::exit(EXIT_FAILURE);
180 }
Andrew Jeffery91c5eaa2022-04-05 16:45:52 +0930181
182 auto timer = std::make_shared<boost::asio::steady_timer>(io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030183 timer->expires_after(std::chrono::seconds(10));
184
185 timer->async_wait(
186 [timer, interfaces{std::move(interfaces)}, scan,
187 probeVector{std::move(probeVector)},
188 retries](const boost::system::error_code&) mutable {
189 findDbusObjects(std::move(probeVector),
190 std::move(interfaces), scan,
191 retries - 1);
192 });
193 return;
194 }
195
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930196 processDbusObjects(probeVector, scan, interfaceSubtree);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030197 },
198 "xyz.openbmc_project.ObjectMapper",
199 "/xyz/openbmc_project/object_mapper",
200 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
201 interfaces);
202
203 if constexpr (debug)
204 {
205 std::cerr << __func__ << " " << __LINE__ << "\n";
206 }
207}
208
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930209static std::string getRecordName(const DBusInterface& probe,
210 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030211{
212 if (probe.empty())
213 {
214 return probeName;
215 }
216
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930217 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030218 auto device = nlohmann::json::array();
219 for (auto& devPair : probe)
220 {
221 device.push_back(devPair.first);
222 std::visit([&device](auto&& v) { device.push_back(v); },
223 devPair.second);
224 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930225
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930226 // hashes are hard to distinguish, use the non-hashed version if we want
227 // debug
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030228 if constexpr (debug)
229 {
230 return probeName + device.dump();
231 }
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930232
Andrew Jeffery86501872022-04-05 16:58:22 +0930233 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030234}
235
236PerformScan::PerformScan(nlohmann::json& systemConfiguration,
237 nlohmann::json& missingConfigurations,
238 std::list<nlohmann::json>& configurations,
239 sdbusplus::asio::object_server& objServerIn,
240 std::function<void()>&& callback) :
241 _systemConfiguration(systemConfiguration),
242 _missingConfigurations(missingConfigurations),
243 _configurations(configurations), objServer(objServerIn),
244 _callback(std::move(callback))
245{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930246
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930247static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930248{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930249 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930250 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930251 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930252 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930253 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930254
255 auto copy = nlohmann::json::array();
256 for (auto& expose : *findExposes)
257 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930258 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930259 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930260 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930261 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930262 }
263 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930264}
265
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030266void PerformScan::run()
267{
268 boost::container::flat_set<std::string> dbusProbeInterfaces;
269 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
270
271 for (auto it = _configurations.begin(); it != _configurations.end();)
272 {
273 auto findProbe = it->find("Probe");
274 auto findName = it->find("Name");
275
276 nlohmann::json probeCommand;
277 // check for poorly formatted fields, probe must be an array
278 if (findProbe == it->end())
279 {
280 std::cerr << "configuration file missing probe:\n " << *it << "\n";
281 it = _configurations.erase(it);
282 continue;
283 }
284 if ((*findProbe).type() != nlohmann::json::value_t::array)
285 {
286 probeCommand = nlohmann::json::array();
287 probeCommand.push_back(*findProbe);
288 }
289 else
290 {
291 probeCommand = *findProbe;
292 }
293
294 if (findName == it->end())
295 {
296 std::cerr << "configuration file missing name:\n " << *it << "\n";
297 it = _configurations.erase(it);
298 continue;
299 }
300 std::string probeName = *findName;
301
302 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
303 passedProbes.end())
304 {
305 it = _configurations.erase(it);
306 continue;
307 }
308 nlohmann::json* recordPtr = &(*it);
309
310 // store reference to this to children to makes sure we don't get
311 // destroyed too early
312 auto thisRef = shared_from_this();
313 auto probePointer = std::make_shared<PerformProbe>(
314 probeCommand, thisRef,
Andrew Jefferyac20bd92022-04-05 11:11:40 +0930315 [&, recordPtr,
Andrew Jefferyf5772d22022-04-12 22:05:30 +0930316 probeName](FoundDevices& foundDevices,
Andrew Jefferyef5c8432022-04-12 22:34:37 +0930317 const MapperGetSubTreeResponse& dbusSubtree) {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030318 _passed = true;
319 std::set<nlohmann::json> usedNames;
320 passedProbes.push_back(probeName);
321 std::list<size_t> indexes(foundDevices.size());
322 std::iota(indexes.begin(), indexes.end(), 1);
323
324 size_t indexIdx = probeName.find('$');
325 bool hasTemplateName = (indexIdx != std::string::npos);
326
327 // copy over persisted configurations and make sure we remove
328 // indexes that are already used
329 for (auto itr = foundDevices.begin();
330 itr != foundDevices.end();)
331 {
332 std::string recordName =
Andrew Jefferyf5772d22022-04-12 22:05:30 +0930333 getRecordName(itr->interface, probeName);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030334
335 auto fromLastJson = lastJson.find(recordName);
Andrew Jeffery1d221752022-04-13 17:34:10 +0930336 if (fromLastJson == lastJson.end())
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030337 {
Andrew Jeffery1d221752022-04-13 17:34:10 +0930338 itr++;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030339 continue;
340 }
Andrew Jeffery1d221752022-04-13 17:34:10 +0930341
342 pruneRecordExposes(*fromLastJson);
343
344 // keep user changes
345 _systemConfiguration[recordName] = *fromLastJson;
346 _missingConfigurations.erase(recordName);
347
348 // We've processed the device, remove it and advance the
349 // iterator
350 itr = foundDevices.erase(itr);
351
352 if (hasTemplateName)
353 {
354 auto nameIt = fromLastJson->find("Name");
355 if (nameIt == fromLastJson->end())
356 {
357 std::cerr << "Last JSON Illegal\n";
358 continue;
359 }
360 int index = 0;
361 auto str = nameIt->get<std::string>().substr(indexIdx);
362 auto [p, ec] = std::from_chars(
363 str.data(), str.data() + str.size(), index);
364 if (ec != std::errc())
365 {
366 continue; // non-numeric replacement
367 }
368 usedNames.insert(nameIt.value());
369 auto usedIt =
370 std::find(indexes.begin(), indexes.end(), index);
371
372 if (usedIt == indexes.end())
373 {
374 continue; // less items now
375 }
376 indexes.erase(usedIt);
377 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030378 }
379
380 std::optional<std::string> replaceStr;
381
Andrew Jeffery0a8de742022-04-12 22:32:01 +0930382 DBusObject emptyObject;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +0930383 DBusInterface emptyInterface;
Andrew Jeffery0a8de742022-04-12 22:32:01 +0930384 emptyObject.emplace(std::string{}, emptyInterface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030385
Andrew Jeffery920a2812022-04-12 22:17:32 +0930386 for (const auto& [foundDevice, path] : foundDevices)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030387 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030388 // Need all interfaces on this path so that template
389 // substitutions can be done with any of the contained
390 // properties. If the probe that passed didn't use an
391 // interface, such as if it was just TRUE, then
392 // templateCharReplace will just get passed in an empty
393 // map.
Andrew Jefferyd60b1472022-04-12 23:21:25 +0930394 auto objectIt = dbusSubtree.find(path);
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930395 const DBusObject& dbusObject =
Andrew Jefferyd60b1472022-04-12 23:21:25 +0930396 (objectIt == dbusSubtree.end()) ? emptyObject
397 : objectIt->second;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030398
399 nlohmann::json record = *recordPtr;
400 std::string recordName =
401 getRecordName(foundDevice, probeName);
402 size_t foundDeviceIdx = indexes.front();
403 indexes.pop_front();
404
405 // check name first so we have no duplicate names
406 auto getName = record.find("Name");
407 if (getName == record.end())
408 {
409 std::cerr << "Record Missing Name! " << record.dump();
410 continue; // this should be impossible at this level
411 }
412
413 nlohmann::json copyForName = {{"Name", getName.value()}};
414 nlohmann::json::iterator copyIt = copyForName.begin();
Andrew Jefferyc6558f62022-04-05 15:39:31 +0930415 std::optional<std::string> replaceVal = templateCharReplace(
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930416 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030417
418 if (!replaceStr && replaceVal)
419 {
420 if (usedNames.find(copyIt.value()) != usedNames.end())
421 {
422 replaceStr = replaceVal;
423 copyForName = {{"Name", getName.value()}};
424 copyIt = copyForName.begin();
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930425 templateCharReplace(copyIt, dbusObject,
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030426 foundDeviceIdx, replaceStr);
427 }
428 }
429
430 if (replaceStr)
431 {
432 std::cerr << "Duplicates found, replacing "
433 << *replaceStr
434 << " with found device index.\n Consider "
435 "fixing template to not have duplicates\n";
436 }
437
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930438 getName.value() = copyIt.value();
439 usedNames.insert(copyIt.value());
440
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030441 for (auto keyPair = record.begin(); keyPair != record.end();
442 keyPair++)
443 {
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930444 if (keyPair.key() != "Name")
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030445 {
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930446 templateCharReplace(keyPair, dbusObject,
447 foundDeviceIdx, replaceStr);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030448 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030449 }
450
451 // insert into configuration temporarily to be able to
452 // reference ourselves
453
454 _systemConfiguration[recordName] = record;
455
456 auto findExpose = record.find("Exposes");
457 if (findExpose == record.end())
458 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030459 continue;
460 }
461
462 for (auto& expose : *findExpose)
463 {
464 for (auto keyPair = expose.begin();
465 keyPair != expose.end(); keyPair++)
466 {
467
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930468 templateCharReplace(keyPair, dbusObject,
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030469 foundDeviceIdx, replaceStr);
470
471 bool isBind =
472 boost::starts_with(keyPair.key(), "Bind");
473 bool isDisable = keyPair.key() == "DisableNode";
474
475 // special cases
476 if (!(isBind || isDisable))
477 {
478 continue;
479 }
480
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030481 std::vector<std::string> matches;
482 if (keyPair.value().type() ==
483 nlohmann::json::value_t::string)
484 {
485 matches.emplace_back(keyPair.value());
486 }
Andrew Jeffery3c5f12b2022-04-07 09:49:15 +0930487 else if (keyPair.value().type() ==
488 nlohmann::json::value_t::array)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030489 {
490 for (const auto& value : keyPair.value())
491 {
492 if (value.type() !=
493 nlohmann::json::value_t::string)
494 {
495 std::cerr << "Value is invalid type "
496 << value << "\n";
497 break;
498 }
499 matches.emplace_back(value);
500 }
501 }
Andrew Jeffery3c5f12b2022-04-07 09:49:15 +0930502 else
503 {
504 std::cerr << "Value is invalid type "
505 << keyPair.key() << "\n";
506 continue;
507 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030508
509 std::set<std::string> foundMatches;
Andrew Jefferyf5184712022-03-25 13:38:07 +1030510 for (auto& [configId, config] :
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030511 _systemConfiguration.items())
512 {
513 if (isDisable)
514 {
515 // don't disable ourselves
Andrew Jefferyf5184712022-03-25 13:38:07 +1030516 if (configId == recordName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030517 {
518 continue;
519 }
520 }
Andrew Jefferyf5184712022-03-25 13:38:07 +1030521 auto configListFind = config.find("Exposes");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030522
Andrew Jefferyf5184712022-03-25 13:38:07 +1030523 if (configListFind == config.end() ||
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030524 configListFind->type() !=
525 nlohmann::json::value_t::array)
526 {
527 continue;
528 }
529 for (auto& exposedObject : *configListFind)
530 {
531 auto matchIt = std::find_if(
532 matches.begin(), matches.end(),
533 [name = (exposedObject)["Name"]
534 .get<std::string>()](
535 const std::string& s) {
536 return s == name;
537 });
538 if (matchIt == matches.end())
539 {
540 continue;
541 }
542 foundMatches.insert(*matchIt);
543
544 if (isBind)
545 {
546 std::string bind = keyPair.key().substr(
547 sizeof("Bind") - 1);
548
549 exposedObject["Status"] = "okay";
550 expose[bind] = exposedObject;
551 }
552 else if (isDisable)
553 {
554 exposedObject["Status"] = "disabled";
555 }
556 }
557 }
558 if (foundMatches.size() != matches.size())
559 {
560 std::cerr << "configuration file "
561 "dependency error, "
562 "could not find "
563 << keyPair.key() << " "
564 << keyPair.value() << "\n";
565 }
566 }
567 }
568 // overwrite ourselves with cleaned up version
569 _systemConfiguration[recordName] = record;
570 _missingConfigurations.erase(recordName);
571 }
572 });
573
574 // parse out dbus probes by discarding other probe types, store in a
575 // map
576 for (const nlohmann::json& probeJson : probeCommand)
577 {
578 const std::string* probe = probeJson.get_ptr<const std::string*>();
579 if (probe == nullptr)
580 {
581 std::cerr << "Probe statement wasn't a string, can't parse";
582 continue;
583 }
Andrew Jeffery666583b2021-12-01 15:50:12 +1030584 if (findProbeType(probe->c_str()))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030585 {
586 continue;
587 }
588 // syntax requires probe before first open brace
589 auto findStart = probe->find('(');
590 std::string interface = probe->substr(0, findStart);
591 dbusProbeInterfaces.emplace(interface);
592 dbusProbePointers.emplace_back(probePointer);
593 }
594 it++;
595 }
596
597 // probe vector stores a shared_ptr to each PerformProbe that cares
598 // about a dbus interface
599 findDbusObjects(std::move(dbusProbePointers),
600 std::move(dbusProbeInterfaces), shared_from_this());
601 if constexpr (debug)
602 {
603 std::cerr << __func__ << " " << __LINE__ << "\n";
604 }
605}
606
607PerformScan::~PerformScan()
608{
609 if (_passed)
610 {
611 auto nextScan = std::make_shared<PerformScan>(
612 _systemConfiguration, _missingConfigurations, _configurations,
613 objServer, std::move(_callback));
614 nextScan->passedProbes = std::move(passedProbes);
615 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
616 nextScan->run();
617
618 if constexpr (debug)
619 {
620 std::cerr << __func__ << " " << __LINE__ << "\n";
621 }
622 }
623 else
624 {
625 _callback();
626
627 if constexpr (debug)
628 {
629 std::cerr << __func__ << " " << __LINE__ << "\n";
630 }
631 }
632}