blob: 32ac69d4ff3caa49e85efc963534eebb9df11227 [file] [log] [blame]
AppaRao Puli00840472018-10-03 19:37:46 +05301/*
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 "utils.hpp"
17
18void systemdDaemonReload(
19 const std::shared_ptr<sdbusplus::asio::connection> &conn)
20{
21 try
22 {
23 conn->async_method_call(
24 [](boost::system::error_code ec) {
25 if (ec)
26 {
27 phosphor::logging::log<phosphor::logging::level::ERR>(
28 "async error: Failed to do systemd reload.");
29 return;
30 }
AppaRao Pulie55cfd62019-02-15 15:35:29 +053031 return;
AppaRao Puli00840472018-10-03 19:37:46 +053032 },
33 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
34 "org.freedesktop.systemd1.Manager", "Reload");
35 }
36 catch (const sdbusplus::exception::SdBusError &e)
37 {
38 phosphor::logging::log<phosphor::logging::level::ERR>(
39 "daemon-reload operation failed.");
40 phosphor::logging::elog<
41 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
42 }
43
44 return;
45}
46
47void systemdUnitAction(const std::shared_ptr<sdbusplus::asio::connection> &conn,
48 const std::string &unitName,
49 const std::string &actionMethod)
50{
51 try
52 {
53 conn->async_method_call(
54 [](boost::system::error_code ec,
AppaRao Pulie55cfd62019-02-15 15:35:29 +053055 const sdbusplus::message::object_path &objPath) {
AppaRao Puli00840472018-10-03 19:37:46 +053056 if (ec)
57 {
58 phosphor::logging::log<phosphor::logging::level::ERR>(
59 "async error: Failed to do systemd action");
60 return;
61 }
AppaRao Pulie55cfd62019-02-15 15:35:29 +053062 phosphor::logging::log<phosphor::logging::level::ERR>(
63 "Created unit action job.",
64 phosphor::logging::entry("JobID=%s", objPath.str.c_str()));
65 return;
AppaRao Puli00840472018-10-03 19:37:46 +053066 },
67 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
68 "org.freedesktop.systemd1.Manager", actionMethod, unitName,
69 "replace");
70 }
71 catch (const sdbusplus::exception::SdBusError &e)
72 {
73 phosphor::logging::log<phosphor::logging::level::ERR>(
74 "Systemd operation failed.",
75 phosphor::logging::entry("ACTION=%s", actionMethod.c_str()));
76 phosphor::logging::elog<
77 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
78 }
79
80 return;
81}
82
AppaRao Pulie55cfd62019-02-15 15:35:29 +053083void systemdUnitFilesStateChange(
AppaRao Puli00840472018-10-03 19:37:46 +053084 const std::shared_ptr<sdbusplus::asio::connection> &conn,
AppaRao Pulie55cfd62019-02-15 15:35:29 +053085 const std::vector<std::string> &unitFiles, const std::string &unitState)
AppaRao Puli00840472018-10-03 19:37:46 +053086{
87 try
88 {
AppaRao Puli00840472018-10-03 19:37:46 +053089 if (unitState == "enabled")
90 {
91 conn->async_method_call(
92 [](boost::system::error_code ec) {
93 if (ec)
94 {
95 phosphor::logging::log<phosphor::logging::level::ERR>(
AppaRao Pulie55cfd62019-02-15 15:35:29 +053096 "async error: Failed to perform UnmaskUnitFiles.");
AppaRao Puli00840472018-10-03 19:37:46 +053097 return;
98 }
AppaRao Pulie55cfd62019-02-15 15:35:29 +053099 return;
AppaRao Puli00840472018-10-03 19:37:46 +0530100 },
101 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
AppaRao Pulie55cfd62019-02-15 15:35:29 +0530102 "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles",
103 unitFiles, false);
AppaRao Puli00840472018-10-03 19:37:46 +0530104 }
105 else if (unitState == "disabled")
106 {
107 conn->async_method_call(
108 [](boost::system::error_code ec) {
109 if (ec)
110 {
111 phosphor::logging::log<phosphor::logging::level::ERR>(
AppaRao Pulie55cfd62019-02-15 15:35:29 +0530112 "async error: Failed to perform MaskUnitFiles.");
AppaRao Puli00840472018-10-03 19:37:46 +0530113 return;
114 }
AppaRao Pulie55cfd62019-02-15 15:35:29 +0530115 return;
AppaRao Puli00840472018-10-03 19:37:46 +0530116 },
117 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
AppaRao Pulie55cfd62019-02-15 15:35:29 +0530118 "org.freedesktop.systemd1.Manager", "MaskUnitFiles", unitFiles,
119 false, false);
AppaRao Puli00840472018-10-03 19:37:46 +0530120 }
121 else
122 {
123 // Not supported unit State
124 phosphor::logging::log<phosphor::logging::level::ERR>(
125 "invalid Unit state",
126 phosphor::logging::entry("STATE=%s", unitState.c_str()));
127 phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
128 Error::InternalFailure>();
129 }
130 }
131 catch (const sdbusplus::exception::SdBusError &e)
132 {
133 phosphor::logging::log<phosphor::logging::level::ERR>(
134 "Systemd state change operation failed.");
135 phosphor::logging::elog<
136 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
137 }
138
139 return;
140}
141
142bool checkSystemdUnitExist(const std::string &unitName)
143{
144 std::experimental::filesystem::path unitFilePath(
145 std::string("/lib/systemd/system/") + unitName);
146 return std::experimental::filesystem::exists(unitFilePath);
147}