Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 6 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 7 | namespace utils |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 8 | { |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 9 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 10 | using namespace phosphor::logging; |
| 11 | |
| 12 | std::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 Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 24 | { |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 25 | 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 Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 34 | } |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 35 | 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 Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 44 | } |
| 45 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 46 | void 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 Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 71 | namespace internal |
| 72 | { |
| 73 | |
| 74 | /* @brief Helper function to build a string from command arguments */ |
| 75 | static 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 | |
| 86 | int 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 Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 130 | } // namespace utils |