Lock and unlock mutex for process creation 78/298778/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Sep 2023 10:09:42 +0000 (19:09 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Sep 2023 10:39:30 +0000 (19:39 +0900)
To prevent memory corruption issue, this patch adds locking and
unlocking a mutex. If the memory allocation occurs when creating a
children process using fork(), the children process has a memory problem.

Change-Id: I6abc980533686421b30a7cbdac52f370cc352748
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/dbus.cc
src/launchpad-process-pool/executor.cc
src/launchpad-process-pool/rec_mutex.hh [new file with mode: 0644]

index 92a2a42..87d19c2 100644 (file)
@@ -27,6 +27,7 @@
 #include <shared-queue.hpp>
 
 #include "launchpad-process-pool/log_private.hh"
+#include "launchpad-process-pool/rec_mutex.hh"
 
 namespace launchpad {
 namespace {
@@ -133,6 +134,7 @@ class DBusManager {
     if (conn_)
       return conn_;
 
+    std::lock_guard<std::recursive_mutex> lock(RecMutex::GetInst().GetMutex());
     GError* error = nullptr;
     conn_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
     if (conn_ == nullptr) {
index 4aea7ca..31496f7 100644 (file)
 #include <sched_priority.hh>
 
 #include "launchpad-process-pool/log_private.hh"
+#include "launchpad-process-pool/rec_mutex.hh"
 
 namespace launchpad {
 
 Executor::Executor(Executor::Delegator* delegator) : delegator_(delegator) {}
 
 pid_t Executor::Execute(int priority) {
+  std::lock_guard<std::recursive_mutex> lock(RecMutex::GetInst().GetMutex());
   pid_t pid = fork();
   if (pid == -1) {
     _E("Failed to create child process. errno(%d)", errno);
diff --git a/src/launchpad-process-pool/rec_mutex.hh b/src/launchpad-process-pool/rec_mutex.hh
new file mode 100644 (file)
index 0000000..ee0cfda
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LAUNCHPAD_PROCESS_POOL_REC_MUTEX_HH_
+#define LAUNCHPAD_PROCESS_POOL_REC_MUTEX_HH_
+
+#include <mutex>
+
+namespace launchpad {
+
+class RecMutex {
+ public:
+  static RecMutex& GetInst() {
+    static RecMutex inst;
+    return inst;
+  }
+
+  std::recursive_mutex& GetMutex() const {
+    return mutex_;
+  }
+
+ private:
+  RecMutex() = default;
+  ~RecMutex() = default;
+
+ private:
+  mutable std::recursive_mutex mutex_;
+};
+
+}  // namespace launchpad
+
+#endif  // LAUNCHPAD_PROCESS_POOL_REC_MUTEX_HH_