Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 1 | #include "config.h" |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 2 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 3 | #include <sdbusplus/server.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 4 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 5 | #include <fstream> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 6 | #include <map> |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 7 | #include <string> |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 8 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 9 | namespace utils |
| 10 | { |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 11 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 12 | /** |
| 13 | * @brief Get the bus service |
| 14 | * |
| 15 | * @return the bus service as a string |
| 16 | **/ |
| 17 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 18 | const std::string& interface); |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 19 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 20 | /** |
| 21 | * @brief Merge more files |
| 22 | * |
| 23 | * @param[in] srcFiles - source files |
| 24 | * @param[out] dstFile - destination file |
| 25 | * @return |
| 26 | **/ |
| 27 | void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 28 | |
| 29 | namespace internal |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * @brief Construct an argument vector to be used with an exec command, which |
| 34 | * requires the name of the executable to be the first argument, and a |
| 35 | * null terminator to be the last. |
| 36 | * @param[in] name - Name of the executable |
| 37 | * @param[in] args - Optional arguments |
| 38 | * @return char* vector |
| 39 | */ |
| 40 | template <typename... Arg> |
| 41 | constexpr auto constructArgv(const char* name, Arg&&... args) |
| 42 | { |
| 43 | std::vector<char*> argV{ |
| 44 | {const_cast<char*>(name), const_cast<char*>(args)..., nullptr}}; |
| 45 | return argV; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @brief Helper function to execute command in child process |
| 50 | * @param[in] path - Fully qualified name of the executable to run |
| 51 | * @param[in] args - Optional arguments |
| 52 | * @return 0 on success |
| 53 | */ |
| 54 | int executeCmd(const char* path, char** args); |
| 55 | |
| 56 | } // namespace internal |
| 57 | |
| 58 | /** |
| 59 | * @brief Execute command in child process |
| 60 | * @param[in] path - Fully qualified name of the executable to run |
| 61 | * @param[in] args - Optional arguments |
| 62 | * @return 0 on success |
| 63 | */ |
| 64 | template <typename... Arg> |
| 65 | int execute(const char* path, Arg&&... args) |
| 66 | { |
| 67 | auto argArray = internal::constructArgv(path, std::forward<Arg>(args)...); |
| 68 | |
| 69 | return internal::executeCmd(path, argArray.data()); |
| 70 | } |
| 71 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 72 | } // namespace utils |