blob: 410ddc841d804127e0469818848c03cd7737cf0a [file] [log] [blame]
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05001#include "utils.hpp"
2
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -06003#include <unistd.h>
4
Patrick Williamsc9bb6422021-08-27 06:18:35 -05005#include <phosphor-logging/lg2.hpp>
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05006
Jayashankar Padatha0135602019-04-22 16:22:58 +05307namespace utils
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05008{
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05009
Patrick Williamsc9bb6422021-08-27 06:18:35 -050010PHOSPHOR_LOG2_USING;
Jayashankar Padatha0135602019-04-22 16:22:58 +053011
12std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
13 const std::string& interface)
14{
15 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
16 MAPPER_BUSNAME, "GetObject");
17
18 method.append(path);
19 method.append(std::vector<std::string>({interface}));
20
21 std::vector<std::pair<std::string, std::vector<std::string>>> response;
22
23 try
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050024 {
Jayashankar Padatha0135602019-04-22 16:22:58 +053025 auto reply = bus.call(method);
26 reply.read(response);
27 if (response.empty())
28 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050029 error(
30 "Empty response from mapper for getting service name: {PATH} {INTERFACE}",
31 "PATH", path, "INTERFACE", interface);
Jayashankar Padatha0135602019-04-22 16:22:58 +053032 return std::string{};
33 }
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050034 }
Patrick Williams4ce901c2021-09-02 09:34:45 -050035 catch (const sdbusplus::exception::exception& e)
Jayashankar Padatha0135602019-04-22 16:22:58 +053036 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050037 error("Error in mapper method call for ({PATH}, {INTERFACE}: {ERROR}",
38 "ERROR", e, "PATH", path, "INTERFACE", interface);
Jayashankar Padatha0135602019-04-22 16:22:58 +053039 return std::string{};
40 }
41 return response[0].first;
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050042}
43
George Liufc025e12021-11-09 19:29:12 +080044const PropertyValue getProperty(sdbusplus::bus::bus& bus,
45 const std::string& objectPath,
46 const std::string& interface,
47 const std::string& propertyName)
48{
49 PropertyValue value{};
50 auto service = getService(bus, objectPath, interface);
51 if (service.empty())
52 {
53 return value;
54 }
55
56 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
57 "org.freedesktop.DBus.Properties", "Get");
58 method.append(interface, propertyName);
59
60 auto reply = bus.call(method);
61 reply.read(value);
62
63 return value;
64}
65
66void setProperty(sdbusplus::bus::bus& bus, const std::string& objectPath,
67 const std::string& interface, const std::string& propertyName,
68 const PropertyValue& value)
69{
70 auto service = getService(bus, objectPath, interface);
71 if (service.empty())
72 {
73 return;
74 }
75
76 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
77 "org.freedesktop.DBus.Properties", "Set");
78 method.append(interface.c_str(), propertyName.c_str(), value);
79
80 bus.call_noreply(method);
81}
82
George Liu0a06e972020-12-17 09:17:04 +080083void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile)
84{
85 std::ofstream outFile(dstFile, std::ios::out);
86 for (auto& file : srcFiles)
87 {
88 std::ifstream inFile;
89 inFile.open(file, std::ios_base::in);
90 if (!inFile)
91 {
92 continue;
93 }
94
95 inFile.peek();
96 if (inFile.eof())
97 {
98 inFile.close();
99 continue;
100 }
101
102 outFile << inFile.rdbuf();
103 inFile.close();
104 }
105 outFile.close();
106}
107
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600108namespace internal
109{
110
111/* @brief Helper function to build a string from command arguments */
112static std::string buildCommandStr(const char* name, char** args)
113{
114 std::string command = name;
115 for (int i = 0; args[i]; i++)
116 {
117 command += " ";
118 command += args[i];
119 }
120 return command;
121}
122
123int executeCmd(const char* path, char** args)
124{
125 pid_t pid = fork();
126 if (pid == 0)
127 {
128 execv(path, args);
129
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500130 // execv only retruns on err
131 auto err = errno;
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600132 auto command = buildCommandStr(path, args);
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500133 error("Failed ({ERRNO}) to execute command: {COMMAND}", "ERRNO", err,
134 "COMMAND", command);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600135 return -1;
136 }
137 else if (pid > 0)
138 {
139 int status;
140 if (waitpid(pid, &status, 0) < 0)
141 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500142 error("Error ({ERRNO}) during waitpid.", "ERRNO", errno);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600143 return -1;
144 }
145 else if (WEXITSTATUS(status) != 0)
146 {
147 auto command = buildCommandStr(path, args);
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500148 error("Error ({STATUS}) occurred when executing command: {COMMAND}",
149 "STATUS", status, "COMMAND", command);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600150 return -1;
151 }
152 }
153 else
154 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -0500155 error("Error ({ERRNO}) during fork.", "ERRNO", errno);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600156 return -1;
157 }
158
159 return 0;
160}
161
162} // namespace internal
163
Jayashankar Padatha0135602019-04-22 16:22:58 +0530164} // namespace utils