blob: a6db7f9a56e880ccbb60131016b7e9f04ebed61e [file] [log] [blame]
Jayashankar Padatha0135602019-04-22 16:22:58 +05301#include "config.h"
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05002
Jayashankar Padatha0135602019-04-22 16:22:58 +05303#include <sdbusplus/server.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -05004
George Liu0a06e972020-12-17 09:17:04 +08005#include <fstream>
Adriana Kobylak58aa7502020-06-08 11:12:11 -05006#include <map>
Jayashankar Padatha0135602019-04-22 16:22:58 +05307#include <string>
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05008
Jayashankar Padatha0135602019-04-22 16:22:58 +05309namespace utils
10{
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050011
Jayashankar Padatha0135602019-04-22 16:22:58 +053012/**
13 * @brief Get the bus service
14 *
15 * @return the bus service as a string
16 **/
17std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
18 const std::string& interface);
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050019
George Liu0a06e972020-12-17 09:17:04 +080020/**
21 * @brief Merge more files
22 *
23 * @param[in] srcFiles - source files
24 * @param[out] dstFile - destination file
25 * @return
26 **/
27void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -060028
29namespace 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 */
40template <typename... Arg>
41constexpr 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 */
54int 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 */
64template <typename... Arg>
65int 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 Padatha0135602019-04-22 16:22:58 +053072} // namespace utils