blob: f39298302b9ada35b382073603882ba9fb17d468 [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
Jayashankar Padatha0135602019-04-22 16:22:58 +05305#include <phosphor-logging/log.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
Jayashankar Padatha0135602019-04-22 16:22:58 +053010using namespace phosphor::logging;
11
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 {
29 log<level::ERR>("Error in mapper response for getting service name",
30 entry("PATH=%s", path.c_str()),
31 entry("INTERFACE=%s", interface.c_str()));
32 return std::string{};
33 }
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050034 }
Jayashankar Padatha0135602019-04-22 16:22:58 +053035 catch (const sdbusplus::exception::SdBusError& e)
36 {
37 log<level::ERR>("Error in mapper method call",
38 entry("ERROR=%s", e.what()),
39 entry("PATH=%s", path.c_str()),
40 entry("INTERFACE=%s", interface.c_str()));
41 return std::string{};
42 }
43 return response[0].first;
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050044}
45
George Liu0a06e972020-12-17 09:17:04 +080046void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile)
47{
48 std::ofstream outFile(dstFile, std::ios::out);
49 for (auto& file : srcFiles)
50 {
51 std::ifstream inFile;
52 inFile.open(file, std::ios_base::in);
53 if (!inFile)
54 {
55 continue;
56 }
57
58 inFile.peek();
59 if (inFile.eof())
60 {
61 inFile.close();
62 continue;
63 }
64
65 outFile << inFile.rdbuf();
66 inFile.close();
67 }
68 outFile.close();
69}
70
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -060071namespace internal
72{
73
74/* @brief Helper function to build a string from command arguments */
75static std::string buildCommandStr(const char* name, char** args)
76{
77 std::string command = name;
78 for (int i = 0; args[i]; i++)
79 {
80 command += " ";
81 command += args[i];
82 }
83 return command;
84}
85
86int executeCmd(const char* path, char** args)
87{
88 pid_t pid = fork();
89 if (pid == 0)
90 {
91 execv(path, args);
92
93 // execv only retruns on error
94 auto error = errno;
95 auto command = buildCommandStr(path, args);
96 log<level::ERR>("Failed to execute command", entry("ERRNO=%d", error),
97 entry("COMMAND=%s", command.c_str()));
98 return -1;
99 }
100 else if (pid > 0)
101 {
102 int status;
103 if (waitpid(pid, &status, 0) < 0)
104 {
105 auto error = errno;
106 log<level::ERR>("waitpid error", entry("ERRNO=%d", error));
107 return -1;
108 }
109 else if (WEXITSTATUS(status) != 0)
110 {
111 auto command = buildCommandStr(path, args);
112 log<level::ERR>("Error occurred when executing command",
113 entry("STATUS=%d", status),
114 entry("COMMAND=%s", command.c_str()));
115 return -1;
116 }
117 }
118 else
119 {
120 auto error = errno;
121 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
122 return -1;
123 }
124
125 return 0;
126}
127
128} // namespace internal
129
Jayashankar Padatha0135602019-04-22 16:22:58 +0530130} // namespace utils