10084047dSAppaRao Puli /*
20084047dSAppaRao Puli // Copyright (c) 2018 Intel Corporation
30084047dSAppaRao Puli //
40084047dSAppaRao Puli // Licensed under the Apache License, Version 2.0 (the "License");
50084047dSAppaRao Puli // you may not use this file except in compliance with the License.
60084047dSAppaRao Puli // You may obtain a copy of the License at
70084047dSAppaRao Puli //
80084047dSAppaRao Puli // http://www.apache.org/licenses/LICENSE-2.0
90084047dSAppaRao Puli //
100084047dSAppaRao Puli // Unless required by applicable law or agreed to in writing, software
110084047dSAppaRao Puli // distributed under the License is distributed on an "AS IS" BASIS,
120084047dSAppaRao Puli // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130084047dSAppaRao Puli // See the License for the specific language governing permissions and
140084047dSAppaRao Puli // limitations under the License.
150084047dSAppaRao Puli */
160084047dSAppaRao Puli #pragma once
170084047dSAppaRao Puli #include <boost/asio.hpp>
18ee853eb2SAppaRao Puli #include <phosphor-logging/elog-errors.hpp>
19cb267c8fSGeorge Liu #include <phosphor-logging/lg2.hpp>
20ee853eb2SAppaRao Puli #include <sdbusplus/asio/object_server.hpp>
21ee853eb2SAppaRao Puli #include <xyz/openbmc_project/Common/error.hpp>
22ee853eb2SAppaRao Puli
23ee853eb2SAppaRao Puli #include <chrono>
24ee853eb2SAppaRao Puli #include <ctime>
2582e9557eSTom Joseph #include <filesystem>
26ee853eb2SAppaRao Puli #include <string>
270084047dSAppaRao Puli
2890f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdStartUnit = "StartUnit";
2990f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdStopUnit = "StopUnit";
3090f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdRestartUnit = "RestartUnit";
3190f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdReloadMethod = "Reload";
3290f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdGetJobMethod = "GetJob";
3390f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdReplaceMode = "replace";
3490f2da3fSRichard Marian Thomaiyar static constexpr const char* dBusGetAllMethod = "GetAll";
3590f2da3fSRichard Marian Thomaiyar static constexpr const char* dBusGetMethod = "Get";
3690f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdService = "org.freedesktop.systemd1";
3790f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdObjPath = "/org/freedesktop/systemd1";
3890f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdMgrIntf = "org.freedesktop.systemd1.Manager";
3990f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdUnitIntf = "org.freedesktop.systemd1.Unit";
4090f2da3fSRichard Marian Thomaiyar static constexpr const char* sysdSocketIntf = "org.freedesktop.systemd1.Socket";
4190f2da3fSRichard Marian Thomaiyar static constexpr const char* dBusPropIntf = "org.freedesktop.DBus.Properties";
4290f2da3fSRichard Marian Thomaiyar static constexpr const char* stateMasked = "masked";
4390f2da3fSRichard Marian Thomaiyar static constexpr const char* stateEnabled = "enabled";
4490f2da3fSRichard Marian Thomaiyar static constexpr const char* stateDisabled = "disabled";
4590f2da3fSRichard Marian Thomaiyar static constexpr const char* subStateRunning = "running";
46a19b5093SGeorge Liu static constexpr const char* subStateListening = "listening";
47*d2a99a4dSJiaqing Zhao static constexpr const char* loadStateNotFound = "not-found";
48a19b5093SGeorge Liu
49a19b5093SGeorge Liu using ListUnitsType =
50a19b5093SGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string,
51a19b5093SGeorge Liu std::string, sdbusplus::message::object_path, uint32_t,
52a19b5093SGeorge Liu std::string, sdbusplus::message::object_path>;
53a19b5093SGeorge Liu
54a19b5093SGeorge Liu enum class ListUnitElements
55a19b5093SGeorge Liu {
56a19b5093SGeorge Liu name,
57a19b5093SGeorge Liu descriptionString,
58a19b5093SGeorge Liu loadState,
59a19b5093SGeorge Liu activeState,
60a19b5093SGeorge Liu subState,
61a19b5093SGeorge Liu followedUnit,
62a19b5093SGeorge Liu objectPath,
63a19b5093SGeorge Liu queuedJobType,
64a19b5093SGeorge Liu jobType,
65a19b5093SGeorge Liu jobObject
66a19b5093SGeorge Liu };
6790f2da3fSRichard Marian Thomaiyar
addInstanceName(const std::string & instanceName,const std::string & suffix)6890f2da3fSRichard Marian Thomaiyar static inline std::string addInstanceName(const std::string& instanceName,
6990f2da3fSRichard Marian Thomaiyar const std::string& suffix)
7090f2da3fSRichard Marian Thomaiyar {
7190f2da3fSRichard Marian Thomaiyar return (instanceName.empty() ? "" : suffix + instanceName);
7290f2da3fSRichard Marian Thomaiyar }
730084047dSAppaRao Puli
74a19b5093SGeorge Liu void checkAndThrowInternalFailure(boost::system::error_code& ec,
75a19b5093SGeorge Liu const std::string& msg);
76a19b5093SGeorge Liu
770084047dSAppaRao Puli void systemdDaemonReload(
7890f2da3fSRichard Marian Thomaiyar const std::shared_ptr<sdbusplus::asio::connection>& conn,
7990f2da3fSRichard Marian Thomaiyar boost::asio::yield_context yield);
800084047dSAppaRao Puli
810084047dSAppaRao Puli void systemdUnitAction(const std::shared_ptr<sdbusplus::asio::connection>& conn,
8290f2da3fSRichard Marian Thomaiyar boost::asio::yield_context yield,
830084047dSAppaRao Puli const std::string& unitName,
840084047dSAppaRao Puli const std::string& actionMethod);
850084047dSAppaRao Puli
86e55cfd6eSAppaRao Puli void systemdUnitFilesStateChange(
870084047dSAppaRao Puli const std::shared_ptr<sdbusplus::asio::connection>& conn,
8890f2da3fSRichard Marian Thomaiyar boost::asio::yield_context yield, const std::vector<std::string>& unitFiles,
8990f2da3fSRichard Marian Thomaiyar const std::string& unitState, bool maskedState, bool enabledState);
90