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 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.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 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 10 | PHOSPHOR_LOG2_USING; |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 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 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 29 | error( |
| 30 | "Empty response from mapper for getting service name: {PATH} {INTERFACE}", |
| 31 | "PATH", path, "INTERFACE", interface); |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 32 | return std::string{}; |
| 33 | } |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 34 | } |
Patrick Williams | 4ce901c | 2021-09-02 09:34:45 -0500 | [diff] [blame] | 35 | catch (const sdbusplus::exception::exception& e) |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 36 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 37 | error("Error in mapper method call for ({PATH}, {INTERFACE}: {ERROR}", |
| 38 | "ERROR", e, "PATH", path, "INTERFACE", interface); |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 39 | return std::string{}; |
| 40 | } |
| 41 | return response[0].first; |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 42 | } |
| 43 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 44 | void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile) |
| 45 | { |
| 46 | std::ofstream outFile(dstFile, std::ios::out); |
| 47 | for (auto& file : srcFiles) |
| 48 | { |
| 49 | std::ifstream inFile; |
| 50 | inFile.open(file, std::ios_base::in); |
| 51 | if (!inFile) |
| 52 | { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | inFile.peek(); |
| 57 | if (inFile.eof()) |
| 58 | { |
| 59 | inFile.close(); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | outFile << inFile.rdbuf(); |
| 64 | inFile.close(); |
| 65 | } |
| 66 | outFile.close(); |
| 67 | } |
| 68 | |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 69 | namespace internal |
| 70 | { |
| 71 | |
| 72 | /* @brief Helper function to build a string from command arguments */ |
| 73 | static std::string buildCommandStr(const char* name, char** args) |
| 74 | { |
| 75 | std::string command = name; |
| 76 | for (int i = 0; args[i]; i++) |
| 77 | { |
| 78 | command += " "; |
| 79 | command += args[i]; |
| 80 | } |
| 81 | return command; |
| 82 | } |
| 83 | |
| 84 | int executeCmd(const char* path, char** args) |
| 85 | { |
| 86 | pid_t pid = fork(); |
| 87 | if (pid == 0) |
| 88 | { |
| 89 | execv(path, args); |
| 90 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 91 | // execv only retruns on err |
| 92 | auto err = errno; |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 93 | auto command = buildCommandStr(path, args); |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 94 | error("Failed ({ERRNO}) to execute command: {COMMAND}", "ERRNO", err, |
| 95 | "COMMAND", command); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 96 | return -1; |
| 97 | } |
| 98 | else if (pid > 0) |
| 99 | { |
| 100 | int status; |
| 101 | if (waitpid(pid, &status, 0) < 0) |
| 102 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 103 | error("Error ({ERRNO}) during waitpid.", "ERRNO", errno); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | else if (WEXITSTATUS(status) != 0) |
| 107 | { |
| 108 | auto command = buildCommandStr(path, args); |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 109 | error("Error ({STATUS}) occurred when executing command: {COMMAND}", |
| 110 | "STATUS", status, "COMMAND", command); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 116 | error("Error ({ERRNO}) during fork.", "ERRNO", errno); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | } // namespace internal |
| 124 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 125 | } // namespace utils |