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 #pragma once
17 #include <boost/asio.hpp>
18 #include <phosphor-logging/elog-errors.hpp>
19 #include <phosphor-logging/lg2.hpp>
20 #include <sdbusplus/asio/object_server.hpp>
21 #include <xyz/openbmc_project/Common/error.hpp>
22 
23 #include <chrono>
24 #include <ctime>
25 #include <filesystem>
26 #include <string>
27 
28 static constexpr const char* sysdStartUnit = "StartUnit";
29 static constexpr const char* sysdStopUnit = "StopUnit";
30 static constexpr const char* sysdRestartUnit = "RestartUnit";
31 static constexpr const char* sysdReloadMethod = "Reload";
32 static constexpr const char* sysdGetJobMethod = "GetJob";
33 static constexpr const char* sysdReplaceMode = "replace";
34 static constexpr const char* dBusGetAllMethod = "GetAll";
35 static constexpr const char* dBusGetMethod = "Get";
36 static constexpr const char* sysdService = "org.freedesktop.systemd1";
37 static constexpr const char* sysdObjPath = "/org/freedesktop/systemd1";
38 static constexpr const char* sysdMgrIntf = "org.freedesktop.systemd1.Manager";
39 static constexpr const char* sysdUnitIntf = "org.freedesktop.systemd1.Unit";
40 static constexpr const char* sysdSocketIntf = "org.freedesktop.systemd1.Socket";
41 static constexpr const char* dBusPropIntf = "org.freedesktop.DBus.Properties";
42 static constexpr const char* stateMasked = "masked";
43 static constexpr const char* stateEnabled = "enabled";
44 static constexpr const char* stateDisabled = "disabled";
45 static constexpr const char* subStateRunning = "running";
46 static constexpr const char* subStateListening = "listening";
47 static constexpr const char* loadStateNotFound = "not-found";
48 
49 using ListUnitsType =
50     std::tuple<std::string, std::string, std::string, std::string, std::string,
51                std::string, sdbusplus::message::object_path, uint32_t,
52                std::string, sdbusplus::message::object_path>;
53 
54 enum class ListUnitElements
55 {
56     name,
57     descriptionString,
58     loadState,
59     activeState,
60     subState,
61     followedUnit,
62     objectPath,
63     queuedJobType,
64     jobType,
65     jobObject
66 };
67 
addInstanceName(const std::string & instanceName,const std::string & suffix)68 static inline std::string addInstanceName(const std::string& instanceName,
69                                           const std::string& suffix)
70 {
71     return (instanceName.empty() ? "" : suffix + instanceName);
72 }
73 
74 void checkAndThrowInternalFailure(boost::system::error_code& ec,
75                                   const std::string& msg);
76 
77 void systemdDaemonReload(
78     const std::shared_ptr<sdbusplus::asio::connection>& conn,
79     boost::asio::yield_context yield);
80 
81 void systemdUnitAction(const std::shared_ptr<sdbusplus::asio::connection>& conn,
82                        boost::asio::yield_context yield,
83                        const std::string& unitName,
84                        const std::string& actionMethod);
85 
86 void systemdUnitFilesStateChange(
87     const std::shared_ptr<sdbusplus::asio::connection>& conn,
88     boost::asio::yield_context yield, const std::vector<std::string>& unitFiles,
89     const std::string& unitState, bool maskedState, bool enabledState);
90