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 | }
|
| 31 | },
|
| 32 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
| 33 | "org.freedesktop.systemd1.Manager", "Reload");
|
| 34 | }
|
| 35 | catch (const sdbusplus::exception::SdBusError &e)
|
| 36 | {
|
| 37 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 38 | "daemon-reload operation failed.");
|
| 39 | phosphor::logging::elog<
|
| 40 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
|
| 41 | }
|
| 42 |
|
| 43 | return;
|
| 44 | }
|
| 45 |
|
| 46 | void systemdUnitAction(const std::shared_ptr<sdbusplus::asio::connection> &conn,
|
| 47 | const std::string &unitName,
|
| 48 | const std::string &actionMethod)
|
| 49 | {
|
| 50 | try
|
| 51 | {
|
| 52 | conn->async_method_call(
|
| 53 | [](boost::system::error_code ec,
|
| 54 | const sdbusplus::message::object_path &res) {
|
| 55 | if (ec)
|
| 56 | {
|
| 57 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 58 | "async error: Failed to do systemd action");
|
| 59 | return;
|
| 60 | }
|
| 61 | },
|
| 62 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
| 63 | "org.freedesktop.systemd1.Manager", actionMethod, unitName,
|
| 64 | "replace");
|
| 65 | }
|
| 66 | catch (const sdbusplus::exception::SdBusError &e)
|
| 67 | {
|
| 68 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 69 | "Systemd operation failed.",
|
| 70 | phosphor::logging::entry("ACTION=%s", actionMethod.c_str()));
|
| 71 | phosphor::logging::elog<
|
| 72 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
|
| 73 | }
|
| 74 |
|
| 75 | return;
|
| 76 | }
|
| 77 |
|
| 78 | void systemdUnitFileStateChange(
|
| 79 | const std::shared_ptr<sdbusplus::asio::connection> &conn,
|
| 80 | const std::string &unitName, const std::string &unitState)
|
| 81 | {
|
| 82 | try
|
| 83 | {
|
| 84 | // For now, we support two states(enabled & disabled). we can extend
|
| 85 | // to other states (enable-runtime, mask, link etc..) if needed.
|
| 86 | std::vector<std::string> unitFiles = {unitName};
|
| 87 | if (unitState == "enabled")
|
| 88 | {
|
| 89 | conn->async_method_call(
|
| 90 | [](boost::system::error_code ec) {
|
| 91 | if (ec)
|
| 92 | {
|
| 93 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 94 | "async error: Failed to do systemd reload.");
|
| 95 | return;
|
| 96 | }
|
| 97 | },
|
| 98 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
| 99 | "org.freedesktop.systemd1.Manager", "EnableUnitFiles",
|
| 100 | unitFiles, false, false);
|
| 101 | }
|
| 102 | else if (unitState == "disabled")
|
| 103 | {
|
| 104 | conn->async_method_call(
|
| 105 | [](boost::system::error_code ec) {
|
| 106 | if (ec)
|
| 107 | {
|
| 108 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 109 | "async error: Failed to do systemd reload.");
|
| 110 | return;
|
| 111 | }
|
| 112 | },
|
| 113 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
|
| 114 | "org.freedesktop.systemd1.Manager", "DisableUnitFiles",
|
| 115 | unitFiles, false);
|
| 116 | }
|
| 117 | else
|
| 118 | {
|
| 119 | // Not supported unit State
|
| 120 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 121 | "invalid Unit state",
|
| 122 | phosphor::logging::entry("STATE=%s", unitState.c_str()));
|
| 123 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
|
| 124 | Error::InternalFailure>();
|
| 125 | }
|
| 126 | }
|
| 127 | catch (const sdbusplus::exception::SdBusError &e)
|
| 128 | {
|
| 129 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 130 | "Systemd state change operation failed.");
|
| 131 | phosphor::logging::elog<
|
| 132 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
|
| 133 | }
|
| 134 |
|
| 135 | return;
|
| 136 | }
|
| 137 |
|
| 138 | bool checkSystemdUnitExist(const std::string &unitName)
|
| 139 | {
|
| 140 | std::experimental::filesystem::path unitFilePath(
|
| 141 | std::string("/lib/systemd/system/") + unitName);
|
| 142 | return std::experimental::filesystem::exists(unitFilePath);
|
| 143 | }
|