security-manager: remove manual forks from ScopedAppLauncher 62/322762/2
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 8 Apr 2025 10:42:07 +0000 (12:42 +0200)
committerKrzysztof Malysa <k.malysa@samsung.com>
Tue, 29 Apr 2025 13:10:51 +0000 (15:10 +0200)
Change-Id: If6b88d39a2b6cea4aff073876f54b47bc21efa93

src/security-manager-tests/common/scoped_app_launcher.cpp
src/security-manager-tests/common/scoped_app_launcher.h

index 6a48b1617a6217002a92cbcd94d3878c8cf25b0c..1f53fc40ba386523563c0cf0204ca21e04941d3b 100644 (file)
 using namespace SecurityManagerTest;
 
 ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper& app,
-                                     const std::function<void(void)>& runInAppContext) :
-    m_uid(app.getUID()),
-    m_appId(app.getAppId())
+                                     const std::function<void(void)>& runInAppContext)
+    : m_uid(app.getUID()),
+      m_gid(app.getGID()),
+      m_appId(app.getAppId())
 {
-    m_pid = fork();
-    RUNNER_ASSERT_ERRNO_MSG(m_pid >= 0, "Fork failed");
-    if (m_pid != 0) {
-        m_syncPipe.claimParentEp();
-        m_syncPipe.wait();
-    } else {
+    launch(runInAppContext);
+}
+
+ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper &app, const TemporaryTestUser &user,
+                                     const std::function<void(void)> &runInAppContext)
+
+    : m_uid(user.getUid()),
+      m_gid(user.getGid()),
+      m_appId(app.getAppId())
+{
+
+    launch(runInAppContext);
+}
+
+void ScopedAppLauncher::launch(const std::function<void(void)> &runInAppContext)
+{
+    m_pid = runInChild([&] {
         m_syncPipe.claimChildEp();
 
-        try {
-            RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(app.getUID(), app.getGID()) == 0,
-                                    "launcher failed");
-            Api::prepareAppCandidate();
-            Api::prepareApp(app.getAppId());
-            runInAppContext();
-        } catch (...) {
-            m_syncPipe.post();
-            throw;
-        }
+        RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(m_uid, m_gid) == 0,
+                                "launcher failed");
+        Api::prepareAppCandidate();
+        Api::prepareApp(m_appId);
         m_syncPipe.post();
+        runInAppContext();
         m_syncPipe.wait();
-        exit(0);
-    }
+    });
+
+    m_syncPipe.claimParentEp();
+    m_syncPipe.wait();
 }
 
 ScopedAppLauncher::~ScopedAppLauncher()
index 133e7c864b7e41f6510c2f039450df32ad802951..970fedff457e465d7607e503a4a5bb1610a6ae99 100644 (file)
 
 #include <app_install_helper.h>
 #include <synchronization_pipe.h>
+#include <temp_test_user.h>
 
 class ScopedAppLauncher final {
 public:
     explicit ScopedAppLauncher(const AppInstallHelper& app,
                                const std::function<void(void)>& runInAppContext = []{});
+    ScopedAppLauncher(const AppInstallHelper& app, const TemporaryTestUser& user,
+                      const std::function<void(void)>& runInAppContext = []{});
     ~ScopedAppLauncher();
 
     ScopedAppLauncher(const ScopedAppLauncher&) = delete;
     ScopedAppLauncher& operator=(const ScopedAppLauncher&) = delete;
 
 private:
+    void launch(const std::function<void(void)>& runInAppContext);
     SynchronizationPipe m_syncPipe;
     pid_t m_pid;
     const uid_t m_uid;
+    const gid_t m_gid;
     const std::string m_appId;
 };