blob: 42a42465d6696bb8e6135cd5c25601edddeb9b2c [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>
Jiaqing Zhaof4766832022-02-28 14:28:18 +080026#include <unordered_map>
Vernon Maueryba2c0832020-07-15 10:02:38 -070027
28std::unique_ptr<boost::asio::steady_timer> timer = nullptr;
29std::unique_ptr<boost::asio::steady_timer> initTimer = nullptr;
30std::map<std::string, std::shared_ptr<phosphor::service::ServiceConfig>>
31 srvMgrObjects;
32static bool unitQueryStarted = false;
33
34static constexpr const char* srvCfgMgrFile = "/etc/srvcfg-mgr.json";
George Liuf2744892022-01-05 17:54:45 +080035static constexpr const char* tmpFileBad = "/tmp/srvcfg-mgr.json.bad";
Vernon Maueryba2c0832020-07-15 10:02:38 -070036
37// Base service name list. All instance of these services and
38// units(service/socket) will be managed by this daemon.
Jiaqing Zhaof4766832022-02-28 14:28:18 +080039static std::unordered_map<std::string /* unitName */,
40 bool /* isSocketActivated */>
41 managedServices = {{"phosphor-ipmi-net", false}, {"bmcweb", false},
42 {"phosphor-ipmi-kcs", false}, {"start-ipkvm", false},
Jiaqing Zhaoef5cece2021-12-10 00:44:11 +080043 {"obmc-console", false}, {"dropbear", true},
44 {"obmc-console-ssh", true}};
Vernon Maueryba2c0832020-07-15 10:02:38 -070045
46enum class UnitType
47{
48 service,
49 socket,
50 target,
51 device,
52 invalid
53};
54
55using MonitorListMap =
56 std::unordered_map<std::string, std::tuple<std::string, std::string,
57 std::string, std::string>>;
58MonitorListMap unitsToMonitor;
59
60enum class monitorElement
61{
62 unitName,
63 instanceName,
64 serviceObjPath,
65 socketObjPath
66};
67
68std::tuple<std::string, UnitType, std::string>
69 getUnitNameTypeAndInstance(const std::string& fullUnitName)
70{
71 UnitType type = UnitType::invalid;
72 std::string instanceName;
73 std::string unitName;
74 // get service type
75 auto typePos = fullUnitName.rfind(".");
76 if (typePos != std::string::npos)
77 {
78 const auto& typeStr = fullUnitName.substr(typePos + 1);
79 // Ignore types other than service and socket
80 if (typeStr == "service")
81 {
82 type = UnitType::service;
83 }
84 else if (typeStr == "socket")
85 {
86 type = UnitType::socket;
87 }
88 // get instance name if available
89 auto instancePos = fullUnitName.rfind("@");
90 if (instancePos != std::string::npos)
91 {
Patrick Williamsdfc72702023-05-10 07:51:18 -050092 instanceName = fullUnitName.substr(instancePos + 1,
93 typePos - instancePos - 1);
Vernon Maueryba2c0832020-07-15 10:02:38 -070094 unitName = fullUnitName.substr(0, instancePos);
95 }
96 else
97 {
98 unitName = fullUnitName.substr(0, typePos);
99 }
100 }
101 return std::make_tuple(unitName, type, instanceName);
102}
103
104static inline void
105 handleListUnitsResponse(sdbusplus::asio::object_server& server,
106 std::shared_ptr<sdbusplus::asio::connection>& conn,
107 boost::system::error_code /*ec*/,
108 const std::vector<ListUnitsType>& listUnits)
109{
110 // Loop through all units, and mark all units, which has to be
111 // managed, irrespective of instance name.
112 for (const auto& unit : listUnits)
113 {
Jiaqing Zhaod2a99a42022-02-25 17:26:30 +0800114 // Ignore non-existent units
115 if (std::get<static_cast<int>(ListUnitElements::loadState)>(unit) ==
116 loadStateNotFound)
117 {
118 continue;
119 }
120
Vernon Maueryba2c0832020-07-15 10:02:38 -0700121 const auto& fullUnitName =
122 std::get<static_cast<int>(ListUnitElements::name)>(unit);
Patrick Williamsdfc72702023-05-10 07:51:18 -0500123 auto [unitName, type,
124 instanceName] = getUnitNameTypeAndInstance(fullUnitName);
Jiaqing Zhaof4766832022-02-28 14:28:18 +0800125 if (managedServices.count(unitName))
Vernon Maueryba2c0832020-07-15 10:02:38 -0700126 {
Jiaqing Zhaof4766832022-02-28 14:28:18 +0800127 // For socket-activated units, ignore all its instances
128 if (managedServices.at(unitName) == true && !instanceName.empty())
129 {
130 continue;
131 }
132
Vernon Maueryba2c0832020-07-15 10:02:38 -0700133 std::string instantiatedUnitName =
Jiaqing Zhao4b8637d2022-04-01 21:25:12 +0800134 unitName + addInstanceName(instanceName, "@");
Vernon Maueryba2c0832020-07-15 10:02:38 -0700135 const sdbusplus::message::object_path& objectPath =
136 std::get<static_cast<int>(ListUnitElements::objectPath)>(unit);
137 // Group the service & socket units togther.. Same services
138 // are managed together.
139 auto it = unitsToMonitor.find(instantiatedUnitName);
140 if (it != unitsToMonitor.end())
141 {
142 auto& value = it->second;
143 if (type == UnitType::service)
144 {
Vernon Maueryba2c0832020-07-15 10:02:38 -0700145 std::get<static_cast<int>(monitorElement::serviceObjPath)>(
Jiaqing Zhaoaa41e672021-12-14 15:14:30 +0800146 value) = objectPath.str;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700147 }
148 else if (type == UnitType::socket)
149 {
150 std::get<static_cast<int>(monitorElement::socketObjPath)>(
Jiaqing Zhaoaa41e672021-12-14 15:14:30 +0800151 value) = objectPath.str;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700152 }
Jiaqing Zhaoaa41e672021-12-14 15:14:30 +0800153 continue;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700154 }
Jiaqing Zhaoaa41e672021-12-14 15:14:30 +0800155 // If not grouped with any existing entry, create a new one
Vernon Maueryba2c0832020-07-15 10:02:38 -0700156 if (type == UnitType::service)
157 {
158 unitsToMonitor.emplace(instantiatedUnitName,
159 std::make_tuple(unitName, instanceName,
160 objectPath.str, ""));
161 }
162 else if (type == UnitType::socket)
163 {
Jiaqing Zhaoaa41e672021-12-14 15:14:30 +0800164 unitsToMonitor.emplace(instantiatedUnitName,
165 std::make_tuple(unitName, instanceName,
166 "", objectPath.str));
Vernon Maueryba2c0832020-07-15 10:02:38 -0700167 }
168 }
169 }
170
171 bool updateRequired = false;
172 bool jsonExist = std::filesystem::exists(srvCfgMgrFile);
173 if (jsonExist)
174 {
George Liuf2744892022-01-05 17:54:45 +0800175 try
Vernon Maueryba2c0832020-07-15 10:02:38 -0700176 {
George Liuf2744892022-01-05 17:54:45 +0800177 std::ifstream file(srvCfgMgrFile);
178 cereal::JSONInputArchive archive(file);
179 MonitorListMap savedMonitorList;
180 archive(savedMonitorList);
181
182 // compare the unit list read from systemd1 and the save list.
183 MonitorListMap diffMap;
184 std::set_difference(begin(unitsToMonitor), end(unitsToMonitor),
185 begin(savedMonitorList), end(savedMonitorList),
186 std::inserter(diffMap, begin(diffMap)));
187 for (auto& unitIt : diffMap)
Vernon Maueryba2c0832020-07-15 10:02:38 -0700188 {
George Liuf2744892022-01-05 17:54:45 +0800189 auto it = savedMonitorList.find(unitIt.first);
190 if (it == savedMonitorList.end())
191 {
192 savedMonitorList.insert(unitIt);
193 updateRequired = true;
194 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700195 }
George Liuf2744892022-01-05 17:54:45 +0800196 unitsToMonitor = savedMonitorList;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700197 }
George Liuf2744892022-01-05 17:54:45 +0800198 catch (const std::exception& e)
199 {
200 lg2::error(
201 "Failed to load {FILEPATH} file, need to rewrite: {ERROR}.",
202 "FILEPATH", srvCfgMgrFile, "ERROR", e);
203
204 // The "bad" files need to be moved to /tmp/ so that we can try to
205 // find out the cause of the file corruption. If we encounter this
206 // failure multiple times, we will only overwrite it to ensure that
207 // we don't accidentally fill up /tmp/.
208 std::error_code ec;
209 std::filesystem::copy_file(
210 srvCfgMgrFile, tmpFileBad,
211 std::filesystem::copy_options::overwrite_existing, ec);
212 if (ec)
213 {
214 lg2::error("Failed to copy {SRCFILE} file to {DSTFILE}.",
215 "SRCFILE", srvCfgMgrFile, "DSTFILE", tmpFileBad);
216 }
217
218 updateRequired = true;
219 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700220 }
221 if (!jsonExist || updateRequired)
222 {
223 std::ofstream file(srvCfgMgrFile);
224 cereal::JSONOutputArchive archive(file);
225 archive(CEREAL_NVP(unitsToMonitor));
226 }
227
Chicago Duan25a0f632021-11-11 16:32:07 +0800228#ifdef USB_CODE_UPDATE
229 unitsToMonitor.emplace(
Jiaqing Zhao4b8637d2022-04-01 21:25:12 +0800230 "phosphor-usb-code-update",
Chicago Duan25a0f632021-11-11 16:32:07 +0800231 std::make_tuple(
Jiaqing Zhao430d7ea2022-04-01 23:23:25 +0800232 phosphor::service::usbCodeUpdateUnitName, "",
Chicago Duan25a0f632021-11-11 16:32:07 +0800233 "/org/freedesktop/systemd1/unit/usb_2dcode_2dupdate_2eservice",
234 ""));
235#endif
236
Vernon Maueryba2c0832020-07-15 10:02:38 -0700237 // create objects for needed services
238 for (auto& it : unitsToMonitor)
239 {
Jiaqing Zhao4b8637d2022-04-01 21:25:12 +0800240 sdbusplus::message::object_path basePath(
241 phosphor::service::srcCfgMgrBasePath);
242 std::string objPath(basePath / it.first);
Vernon Maueryba2c0832020-07-15 10:02:38 -0700243 auto srvCfgObj = std::make_unique<phosphor::service::ServiceConfig>(
244 server, conn, objPath,
245 std::get<static_cast<int>(monitorElement::unitName)>(it.second),
246 std::get<static_cast<int>(monitorElement::instanceName)>(it.second),
247 std::get<static_cast<int>(monitorElement::serviceObjPath)>(
248 it.second),
249 std::get<static_cast<int>(monitorElement::socketObjPath)>(
250 it.second));
251 srvMgrObjects.emplace(
252 std::make_pair(std::move(objPath), std::move(srvCfgObj)));
253 }
254}
255
256void init(sdbusplus::asio::object_server& server,
257 std::shared_ptr<sdbusplus::asio::connection>& conn)
258{
259 // Go through all systemd units, and dynamically detect and manage
260 // the service daemons
261 conn->async_method_call(
262 [&server, &conn](boost::system::error_code ec,
263 const std::vector<ListUnitsType>& listUnits) {
Patrick Williamsdfc72702023-05-10 07:51:18 -0500264 if (ec)
265 {
266 lg2::error("async_method_call error: ListUnits failed: {EC}", "EC",
267 ec.value());
268 return;
269 }
270 handleListUnitsResponse(server, conn, ec, listUnits);
Vernon Maueryba2c0832020-07-15 10:02:38 -0700271 },
272 sysdService, sysdObjPath, sysdMgrIntf, "ListUnits");
273}
274
275void checkAndInit(sdbusplus::asio::object_server& server,
276 std::shared_ptr<sdbusplus::asio::connection>& conn)
277{
278 // Check whether systemd completed all the loading before initializing
279 conn->async_method_call(
280 [&server, &conn](boost::system::error_code ec,
281 const std::variant<uint64_t>& value) {
Patrick Williamsdfc72702023-05-10 07:51:18 -0500282 if (ec)
283 {
284 lg2::error("async_method_call error: ListUnits failed: {EC}", "EC",
285 ec.value());
286 return;
287 }
288 if (std::get<uint64_t>(value))
289 {
290 if (!unitQueryStarted)
Vernon Maueryba2c0832020-07-15 10:02:38 -0700291 {
Patrick Williamsdfc72702023-05-10 07:51:18 -0500292 unitQueryStarted = true;
293 init(server, conn);
Vernon Maueryba2c0832020-07-15 10:02:38 -0700294 }
Patrick Williamsdfc72702023-05-10 07:51:18 -0500295 }
296 else
297 {
298 // FIX-ME: Latest up-stream sync caused issue in receiving
299 // StartupFinished signal. Unable to get StartupFinished signal
300 // from systemd1 hence using poll method too, to trigger it
301 // properly.
302 constexpr size_t pollTimeout = 10; // seconds
303 initTimer->expires_after(std::chrono::seconds(pollTimeout));
304 initTimer->async_wait(
305 [&server, &conn](const boost::system::error_code& ec) {
306 if (ec == boost::asio::error::operation_aborted)
Vernon Maueryba2c0832020-07-15 10:02:38 -0700307 {
Patrick Williamsdfc72702023-05-10 07:51:18 -0500308 // Timer reset.
309 return;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700310 }
Patrick Williamsdfc72702023-05-10 07:51:18 -0500311 if (ec)
312 {
313 lg2::error(
314 "service config mgr - init - async wait error: {EC}",
315 "EC", ec.value());
316 return;
317 }
318 checkAndInit(server, conn);
319 });
320 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700321 },
322 sysdService, sysdObjPath, dBusPropIntf, dBusGetMethod, sysdMgrIntf,
323 "FinishTimestamp");
324}
325
326int main()
327{
Ed Tanousba287d82023-03-01 10:42:28 -0800328 boost::asio::io_context io;
Vernon Maueryba2c0832020-07-15 10:02:38 -0700329 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
330 timer = std::make_unique<boost::asio::steady_timer>(io);
331 initTimer = std::make_unique<boost::asio::steady_timer>(io);
332 conn->request_name(phosphor::service::serviceConfigSrvName);
333 auto server = sdbusplus::asio::object_server(conn, true);
Vernon Maueryba2c0832020-07-15 10:02:38 -0700334 server.add_manager(phosphor::service::srcCfgMgrBasePath);
335 // Initialize the objects after systemd indicated startup finished.
Patrick Williams2ff37282022-07-22 19:26:57 -0500336 auto userUpdatedSignal = std::make_unique<sdbusplus::bus::match_t>(
337 static_cast<sdbusplus::bus_t&>(*conn),
Vernon Maueryba2c0832020-07-15 10:02:38 -0700338 "type='signal',"
339 "member='StartupFinished',path='/org/freedesktop/systemd1',"
340 "interface='org.freedesktop.systemd1.Manager'",
Patrick Williams2ff37282022-07-22 19:26:57 -0500341 [&server, &conn](sdbusplus::message_t& /*msg*/) {
Patrick Williamsdfc72702023-05-10 07:51:18 -0500342 if (!unitQueryStarted)
343 {
344 unitQueryStarted = true;
345 init(server, conn);
346 }
Vernon Maueryba2c0832020-07-15 10:02:38 -0700347 });
348 // this will make sure to initialize the objects, when daemon is
349 // restarted.
350 checkAndInit(server, conn);
351
352 io.run();
353
354 return 0;
355}