AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 1 | /*
|
| 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 |
|
| 18 | void 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 Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 31 | return;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 32 | },
|
| 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 |
|
| 47 | void 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 Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 55 | const sdbusplus::message::object_path &objPath) {
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 56 | if (ec)
|
| 57 | {
|
| 58 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 59 | "async error: Failed to do systemd action");
|
| 60 | return;
|
| 61 | }
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 62 | 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 Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 66 | },
|
| 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 Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 83 | void systemdUnitFilesStateChange(
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 84 | const std::shared_ptr<sdbusplus::asio::connection> &conn,
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 85 | const std::vector<std::string> &unitFiles, const std::string &unitState)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 86 | {
|
| 87 | try
|
| 88 | {
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 89 | 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 Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 96 | "async error: Failed to perform UnmaskUnitFiles.");
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 97 | return;
|
| 98 | }
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 99 | return;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 100 | },
|
| 101 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 102 | "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles",
|
| 103 | unitFiles, false);
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 104 | }
|
| 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 Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 112 | "async error: Failed to perform MaskUnitFiles.");
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 113 | return;
|
| 114 | }
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 115 | return;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 116 | },
|
| 117 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame^] | 118 | "org.freedesktop.systemd1.Manager", "MaskUnitFiles", unitFiles,
|
| 119 | false, false);
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 120 | }
|
| 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 |
|
| 142 | bool 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 | }
|