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 | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 44 | const 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 | |
| 66 | void 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 | |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 83 | void mergeFiles(const std::vector<std::string>& srcFiles, |
| 84 | const std::string& dstFile) |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 85 | { |
| 86 | std::ofstream outFile(dstFile, std::ios::out); |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 87 | for (const auto& file : srcFiles) |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 88 | { |
| 89 | std::ifstream inFile; |
| 90 | inFile.open(file, std::ios_base::in); |
| 91 | if (!inFile) |
| 92 | { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | inFile.peek(); |
| 97 | if (inFile.eof()) |
| 98 | { |
| 99 | inFile.close(); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | outFile << inFile.rdbuf(); |
| 104 | inFile.close(); |
| 105 | } |
| 106 | outFile.close(); |
| 107 | } |
| 108 | |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 109 | namespace internal |
| 110 | { |
| 111 | |
| 112 | /* @brief Helper function to build a string from command arguments */ |
| 113 | static std::string buildCommandStr(const char* name, char** args) |
| 114 | { |
| 115 | std::string command = name; |
| 116 | for (int i = 0; args[i]; i++) |
| 117 | { |
| 118 | command += " "; |
| 119 | command += args[i]; |
| 120 | } |
| 121 | return command; |
| 122 | } |
| 123 | |
| 124 | int executeCmd(const char* path, char** args) |
| 125 | { |
| 126 | pid_t pid = fork(); |
| 127 | if (pid == 0) |
| 128 | { |
| 129 | execv(path, args); |
| 130 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 131 | // execv only retruns on err |
| 132 | auto err = errno; |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 133 | auto command = buildCommandStr(path, args); |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 134 | error("Failed ({ERRNO}) to execute command: {COMMAND}", "ERRNO", err, |
| 135 | "COMMAND", command); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | else if (pid > 0) |
| 139 | { |
| 140 | int status; |
| 141 | if (waitpid(pid, &status, 0) < 0) |
| 142 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 143 | error("Error ({ERRNO}) during waitpid.", "ERRNO", errno); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | else if (WEXITSTATUS(status) != 0) |
| 147 | { |
| 148 | auto command = buildCommandStr(path, args); |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 149 | error("Error ({STATUS}) occurred when executing command: {COMMAND}", |
| 150 | "STATUS", status, "COMMAND", command); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | else |
| 155 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 156 | error("Error ({ERRNO}) during fork.", "ERRNO", errno); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | } // namespace internal |
| 164 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 165 | } // namespace utils |