common: move asyncSystem() to common

asyncSystem() is shared across updaters, so moved to common.

Change-Id: I3545d34ecc1f1bf8a226d020267ae00bcf42c5f9
Signed-off-by: Kevin Tung <Kevin.Tung@quantatw.com>
diff --git a/eeprom-device/eeprom_device.cpp b/eeprom-device/eeprom_device.cpp
index ca27c28..fbe65f3 100644
--- a/eeprom-device/eeprom_device.cpp
+++ b/eeprom-device/eeprom_device.cpp
@@ -1,6 +1,7 @@
 #include "eeprom_device.hpp"
 
 #include "common/include/software.hpp"
+#include "common/include/utils.hpp"
 
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/async.hpp>
@@ -88,55 +89,6 @@
     return lineBulks;
 }
 
-sdbusplus::async::task<int> asyncSystem(sdbusplus::async::context& ctx,
-                                        const std::string& cmd)
-{
-    int pipefd[2];
-    if (pipe(pipefd) == -1)
-    {
-        perror("pipe");
-        co_return -1;
-    }
-
-    pid_t pid = fork();
-    if (pid == -1)
-    {
-        perror("fork");
-        close(pipefd[0]);
-        close(pipefd[1]);
-        co_return -1;
-    }
-    else if (pid == 0)
-    {
-        close(pipefd[0]);
-        int exitCode = std::system(cmd.c_str());
-
-        ssize_t status = write(pipefd[1], &exitCode, sizeof(exitCode));
-        close(pipefd[1]);
-        exit((status == sizeof(exitCode)) ? 0 : 1);
-    }
-    else
-    {
-        close(pipefd[1]);
-
-        auto fdio = std::make_unique<sdbusplus::async::fdio>(ctx, pipefd[0]);
-        if (!fdio)
-        {
-            perror("fdio creation failed");
-            close(pipefd[0]);
-            co_return -1;
-        }
-
-        co_await fdio->next();
-
-        int status;
-        waitpid(pid, &status, 0);
-        close(pipefd[0]);
-
-        co_return WEXITSTATUS(status);
-    }
-}
-
 static std::string getDriverPath(const std::string& chipModel)
 {
     // Currently, only EEPROM chips with the model AT24 are supported.
@@ -220,13 +172,7 @@
 
     setUpdateProgress(40);
 
-    const int rc = co_await writeEEPROM(image, image_size);
-    if (rc != 0)
-    {
-        error("Error writing to EEPROM, exit code {CODE}", "CODE", rc);
-    }
-
-    bool success = (rc == 0);
+    auto success = co_await writeEEPROM(image, image_size);
 
     if (success)
     {
@@ -378,8 +324,8 @@
     return std::filesystem::exists(driverPath + "/" + i2cDeviceId);
 }
 
-sdbusplus::async::task<int> EEPROMDevice::writeEEPROM(const uint8_t* image,
-                                                      size_t image_size) const
+sdbusplus::async::task<bool> EEPROMDevice::writeEEPROM(const uint8_t* image,
+                                                       size_t image_size) const
 {
     auto eepromPath = getEEPROMPath(bus, address);
     if (eepromPath.empty())
@@ -415,11 +361,11 @@
 
     debug("Running {CMD}", "CMD", cmd);
 
-    const int exitCode = co_await asyncSystem(ctx, cmd);
+    auto success = co_await asyncSystem(ctx, cmd);
 
     std::filesystem::remove(path);
 
-    co_return exitCode;
+    co_return success;
 }
 
 sdbusplus::async::task<> EEPROMDevice::processHostStateChange()
diff --git a/eeprom-device/eeprom_device.hpp b/eeprom-device/eeprom_device.hpp
index 6e6eee5..de1fd2d 100644
--- a/eeprom-device/eeprom_device.hpp
+++ b/eeprom-device/eeprom_device.hpp
@@ -65,8 +65,8 @@
      * @param image_size    - Size of the data to write in bytes.
      * @return `true` on success, `false` otherwise.
      */
-    sdbusplus::async::task<int> writeEEPROM(const uint8_t* image,
-                                            size_t image_size) const;
+    sdbusplus::async::task<bool> writeEEPROM(const uint8_t* image,
+                                             size_t image_size) const;
     /**
      *  @brief Handle async host state change signal and updates the version.
      */