blob: 2d7a4b016cb1a3e7958ffc01a569e7d8d1f6de98 [file] [log] [blame]
Vernon Maueryba2c0832020-07-15 10:02:38 -07001/*
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#include "srvcfg_manager.hpp"
17
18#include <boost/algorithm/string/replace.hpp>
19#include <cereal/archives/json.hpp>
20#include <cereal/types/tuple.hpp>
21#include <cereal/types/unordered_map.hpp>
22#include <sdbusplus/bus/match.hpp>
23
24#include <filesystem>
25#include <fstream>
26
27std::unique_ptr<boost::asio::steady_timer> timer = nullptr;
28std::unique_ptr<boost::asio::steady_timer> initTimer = nullptr;
29std::map<std::string, std::shared_ptr<phosphor::service::ServiceConfig>>
30 srvMgrObjects;
31static bool unitQueryStarted = false;
32
33static constexpr const char* srvCfgMgrFile = "/etc/srvcfg-mgr.json";
George Liuf2744892022-01-05 17:54:45 +080034static constexpr const char* tmpFileBad = "/tmp/srvcfg-mgr.json.bad";
Vernon Maueryba2c0832020-07-15 10:02:38 -070035
36// Base service name list. All instance of these services and
37// units(service/socket) will be managed by this daemon.
George Liua19b5092021-05-24 15:54:02 +080038static std::array<std::string, 6> serviceNames = {
39 "phosphor-ipmi-net", "bmcweb", "phosphor-ipmi-kcs",
40 "start-ipkvm", "obmc-console", "dropbear"};
Vernon Maueryba2c0832020-07-15 10:02:38 -070041
42enum class UnitType
43{
44 service,
45 socket,
46 target,
47 device,
48 invalid
49};
50
51using MonitorListMap =
52 std::unordered_map<std::string, std::tuple<std::string, std::string,
53 std::string, std::string>>;
54MonitorListMap unitsToMonitor;
55
56enum class monitorElement
57{
58 unitName,
59 instanceName,
60 serviceObjPath,
61 socketObjPath
62};
63
64std::tuple<std::string, UnitType, std::string>
65 getUnitNameTypeAndInstance(const std::string& fullUnitName)
66{
67 UnitType type = UnitType::invalid;
68 std::string instanceName;
69 std::string unitName;
70 // get service type
71 auto typePos = fullUnitName.rfind(".");
72 if (typePos != std::string::npos)
73 {
74 const auto& typeStr = fullUnitName.substr(typePos + 1);
75 // Ignore types other than service and socket
76 if (typeStr == "service")
77 {
78 type = UnitType::service;
79 }
80 else if (typeStr == "socket")
81 {
82 type = UnitType::socket;
83 }
84 // get instance name if available
85 auto instancePos = fullUnitName.rfind("@");
86 if (instancePos != std::string::npos)
87 {
88 instanceName =
89 fullUnitName.substr(instancePos + 1, typePos - instancePos - 1);
90 unitName = fullUnitName.substr(0, instancePos);
91 }
92 else
93 {
94 unitName = fullUnitName.substr(0, typePos);
95 }
96 }
97 return std::make_tuple(unitName, type, instanceName);
98}
99
100static inline void
101 handleListUnitsResponse(sdbusplus::asio::object_server& server,
102 std::shared_ptr<sdbusplus::asio::connection>& conn,
103 boost::system::error_code /*ec*/,
104 const std::vector<ListUnitsType>& listUnits)
105{
106 // Loop through all units, and mark all units, which has to be
107 // managed, irrespective of instance name.
108 for (const auto& unit : listUnits)
109 {
110 const auto& fullUnitName =
111 std::get<static_cast<int>(ListUnitElements::name)>(unit);
112 auto [unitName, type, instanceName] =
113 getUnitNameTypeAndInstance(fullUnitName);
114 if (std::find(serviceNames.begin(), serviceNames.end(), unitName) !=
115 serviceNames.end())
116 {
117 std::string instantiatedUnitName =
118 unitName + addInstanceName(instanceName, "_40");
119 boost::replace_all(instantiatedUnitName, "-", "_2d");
120 const sdbusplus::message::object_path& objectPath =
121 std::get<static_cast<int>(ListUnitElements::objectPath)>(unit);
122 // Group the service & socket units togther.. Same services
123 // are managed together.
124 auto it = unitsToMonitor.find(instantiatedUnitName);
125 if (it != unitsToMonitor.end())
126 {
127 auto& value = it->second;
128 if (type == UnitType::service)
129 {
130 std::get<static_cast<int>(monitorElement::unitName)>(
131 value) = unitName;
132 std::get<static_cast<int>(monitorElement::instanceName)>(
133 value) = instanceName;
134 std::get<static_cast<int>(monitorElement::serviceObjPath)>(
135 value) = objectPath;
136 }
137 else if (type == UnitType::socket)
138 {
139 std::get<static_cast<int>(monitorElement::socketObjPath)>(
140 value) = objectPath;
141 }
142 }
143 if (type == UnitType::service)
144 {
145 unitsToMonitor.emplace(instantiatedUnitName,
146 std::make_tuple(unitName, instanceName,
147 objectPath.str, ""));
148 }
149 else if (type == UnitType::socket)
150 {
151 unitsToMonitor.emplace(
152 instantiatedUnitName,
153 std::make_tuple("", "", "", objectPath.str));
154 }
155 }
156 }
157
158 bool updateRequired = false;
159 bool jsonExist = std::filesystem::exists(srvCfgMgrFile);
160 if (jsonExist)
161 {
George Liuf2744892022-01-05 17:54:45 +0800162 try
Vernon Maueryba2c0832020-07-15 10:02:38 -0700163 {
George Liuf2744892022-01-05 17:54:45 +0800164 std::ifstream file(srvCfgMgrFile);
165 cereal::JSONInputArchive archive(file);
166 MonitorListMap savedMonitorList;
167 archive(savedMonitorList);
168
169 // compare the unit list read from systemd1 and the save list.
170 MonitorListMap diffMap;
171 std::set_difference(begin(unitsToMonitor), end(unitsToMonitor),
172 begin(savedMonitorList), end(savedMonitorList),
173 std::inserter(diffMap, begin(diffMap)));
174 for (auto& unitIt : diffMap)
Vernon Maueryba2c0832020-07-15 10:02:38 -0700175 {
George Liuf2744892022-01-05 17:54:45 +0800176 auto it = savedMonitorList.find(unitIt.first);
177 if (it == savedMonitorList.end())
178 {
179 savedMonitorList.insert(unitIt);
180 updateRequired = true;
181 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700182 }
George Liuf2744892022-01-05 17:54:45 +0800183 unitsToMonitor = savedMonitorList;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700184 }
George Liuf2744892022-01-05 17:54:45 +0800185 catch (const std::exception& e)
186 {
187 lg2::error(
188 "Failed to load {FILEPATH} file, need to rewrite: {ERROR}.",
189 "FILEPATH", srvCfgMgrFile, "ERROR", e);
190
191 // The "bad" files need to be moved to /tmp/ so that we can try to
192 // find out the cause of the file corruption. If we encounter this
193 // failure multiple times, we will only overwrite it to ensure that
194 // we don't accidentally fill up /tmp/.
195 std::error_code ec;
196 std::filesystem::copy_file(
197 srvCfgMgrFile, tmpFileBad,
198 std::filesystem::copy_options::overwrite_existing, ec);
199 if (ec)
200 {
201 lg2::error("Failed to copy {SRCFILE} file to {DSTFILE}.",
202 "SRCFILE", srvCfgMgrFile, "DSTFILE", tmpFileBad);
203 }
204
205 updateRequired = true;
206 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700207 }
208 if (!jsonExist || updateRequired)
209 {
210 std::ofstream file(srvCfgMgrFile);
211 cereal::JSONOutputArchive archive(file);
212 archive(CEREAL_NVP(unitsToMonitor));
213 }
214
215 // create objects for needed services
216 for (auto& it : unitsToMonitor)
217 {
218 std::string objPath(std::string(phosphor::service::srcCfgMgrBasePath) +
219 "/" + it.first);
220 std::string instanciatedUnitName =
221 std::get<static_cast<int>(monitorElement::unitName)>(it.second) +
222 addInstanceName(
223 std::get<static_cast<int>(monitorElement::instanceName)>(
224 it.second),
225 "@");
226 auto srvCfgObj = std::make_unique<phosphor::service::ServiceConfig>(
227 server, conn, objPath,
228 std::get<static_cast<int>(monitorElement::unitName)>(it.second),
229 std::get<static_cast<int>(monitorElement::instanceName)>(it.second),
230 std::get<static_cast<int>(monitorElement::serviceObjPath)>(
231 it.second),
232 std::get<static_cast<int>(monitorElement::socketObjPath)>(
233 it.second));
234 srvMgrObjects.emplace(
235 std::make_pair(std::move(objPath), std::move(srvCfgObj)));
236 }
237}
238
239void init(sdbusplus::asio::object_server& server,
240 std::shared_ptr<sdbusplus::asio::connection>& conn)
241{
242 // Go through all systemd units, and dynamically detect and manage
243 // the service daemons
244 conn->async_method_call(
245 [&server, &conn](boost::system::error_code ec,
246 const std::vector<ListUnitsType>& listUnits) {
247 if (ec)
248 {
George Liucb267c82022-01-05 17:53:28 +0800249 lg2::error("async_method_call error: ListUnits failed: {EC}",
250 "EC", ec.value());
Vernon Maueryba2c0832020-07-15 10:02:38 -0700251 return;
252 }
253 handleListUnitsResponse(server, conn, ec, listUnits);
254 },
255 sysdService, sysdObjPath, sysdMgrIntf, "ListUnits");
256}
257
258void checkAndInit(sdbusplus::asio::object_server& server,
259 std::shared_ptr<sdbusplus::asio::connection>& conn)
260{
261 // Check whether systemd completed all the loading before initializing
262 conn->async_method_call(
263 [&server, &conn](boost::system::error_code ec,
264 const std::variant<uint64_t>& value) {
265 if (ec)
266 {
George Liucb267c82022-01-05 17:53:28 +0800267 lg2::error("async_method_call error: ListUnits failed: {EC}",
268 "EC", ec.value());
Vernon Maueryba2c0832020-07-15 10:02:38 -0700269 return;
270 }
271 if (std::get<uint64_t>(value))
272 {
273 if (!unitQueryStarted)
274 {
275 unitQueryStarted = true;
276 init(server, conn);
277 }
278 }
279 else
280 {
281 // FIX-ME: Latest up-stream sync caused issue in receiving
282 // StartupFinished signal. Unable to get StartupFinished signal
283 // from systemd1 hence using poll method too, to trigger it
284 // properly.
285 constexpr size_t pollTimeout = 10; // seconds
286 initTimer->expires_after(std::chrono::seconds(pollTimeout));
287 initTimer->async_wait([&server, &conn](
288 const boost::system::error_code& ec) {
289 if (ec == boost::asio::error::operation_aborted)
290 {
291 // Timer reset.
292 return;
293 }
294 if (ec)
295 {
George Liucb267c82022-01-05 17:53:28 +0800296 lg2::error(
297 "service config mgr - init - async wait error: {EC}",
298 "EC", ec.value());
Vernon Maueryba2c0832020-07-15 10:02:38 -0700299 return;
300 }
301 checkAndInit(server, conn);
302 });
303 }
304 },
305 sysdService, sysdObjPath, dBusPropIntf, dBusGetMethod, sysdMgrIntf,
306 "FinishTimestamp");
307}
308
309int main()
310{
311 boost::asio::io_service io;
312 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
313 timer = std::make_unique<boost::asio::steady_timer>(io);
314 initTimer = std::make_unique<boost::asio::steady_timer>(io);
315 conn->request_name(phosphor::service::serviceConfigSrvName);
316 auto server = sdbusplus::asio::object_server(conn, true);
Vernon Maueryba2c0832020-07-15 10:02:38 -0700317 server.add_manager(phosphor::service::srcCfgMgrBasePath);
318 // Initialize the objects after systemd indicated startup finished.
319 auto userUpdatedSignal = std::make_unique<sdbusplus::bus::match::match>(
320 static_cast<sdbusplus::bus::bus&>(*conn),
321 "type='signal',"
322 "member='StartupFinished',path='/org/freedesktop/systemd1',"
323 "interface='org.freedesktop.systemd1.Manager'",
324 [&server, &conn](sdbusplus::message::message& /*msg*/) {
325 if (!unitQueryStarted)
326 {
327 unitQueryStarted = true;
328 init(server, conn);
329 }
330 });
331 // this will make sure to initialize the objects, when daemon is
332 // restarted.
333 checkAndInit(server, conn);
334
335 io.run();
336
337 return 0;
338}