{"id":9611,"url":"https://patchwork.libcamera.org/api/1.1/patches/9611/?format=json","web_url":"https://patchwork.libcamera.org/patch/9611/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200915142038.28757-2-paul.elder@ideasonboard.com>","date":"2020-09-15T14:20:16","name":"[libcamera-devel,01/23] libcamera: ProcessManager: make ProcessManager lifetime explicitly managed","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"8872b4b27f38b25fe9a073175840071dde31d1ae","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9611/mbox/","series":[{"id":1291,"url":"https://patchwork.libcamera.org/api/1.1/series/1291/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1291","date":"2020-09-15T14:20:16","name":"IPA isolation implementation","version":1,"mbox":"https://patchwork.libcamera.org/series/1291/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9611/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9611/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 2797BBF01C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 15 Sep 2020 14:21:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1275F62E1B;\n\tTue, 15 Sep 2020 16:21:03 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A1F76603EE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 15 Sep 2020 16:21:01 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2400:4051:61:600:2c71:1b79:d06d:5032])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A12A7AEA;\n\tTue, 15 Sep 2020 16:20:52 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"nln4C0e7\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1600179654;\n\tbh=hZ5z9Pn83/b9ODQDApDcjez+uT4fueazDjez05BOfEM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=nln4C0e7U2NTC+07ZbVVju6fHevo+MWJB5vVkfBREKzxnGyqDM5rgbylVJLp2Ve54\n\tDi+JKaQsVTyiLkK0myJjr/tL9KgcPM75T67ayJLUv7q9BuGOEUCN/+6SZAuB0VvVTu\n\tl+31JE2fd4ufF5f2I2U/BlkczwBNP+r5TXb8zFoM=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 15 Sep 2020 23:20:16 +0900","Message-Id":"<20200915142038.28757-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200915142038.28757-1-paul.elder@ideasonboard.com>","References":"<20200915142038.28757-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH 01/23] libcamera: ProcessManager: make\n\tProcessManager lifetime explicitly managed","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"If any Process instances are destroyed after the ProcessManager is\ndestroyed, then a segfault will occur.\n\nFix this by making the lifetime of the ProcessManager explicit, and make\nthe CameraManager construct and deconstruct (automatically, via a unique\npointer) the ProcessManager.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\n include/libcamera/internal/process.h | 27 ++++++++++++++++\n src/libcamera/camera_manager.cpp     |  2 ++\n src/libcamera/process.cpp            | 46 ++++++++++++----------------\n 3 files changed, 48 insertions(+), 27 deletions(-)","diff":"diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h\nindex 36595106..df697c7f 100644\n--- a/include/libcamera/internal/process.h\n+++ b/include/libcamera/internal/process.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_INTERNAL_PROCESS_H__\n #define __LIBCAMERA_INTERNAL_PROCESS_H__\n \n+#include <signal.h>\n #include <string>\n #include <vector>\n \n@@ -50,6 +51,32 @@ private:\n \tfriend class ProcessManager;\n };\n \n+class ProcessManager\n+{\n+public:\n+\tProcessManager();\n+\t~ProcessManager();\n+\n+\tvoid registerProcess(Process *proc);\n+\n+\tstatic ProcessManager *instance();\n+\n+\tint writePipe() const;\n+\n+\tconst struct sigaction &oldsa() const;\n+\n+private:\n+\tstatic ProcessManager *self_;\n+\n+\tvoid sighandler(EventNotifier *notifier);\n+\n+\tstd::list<Process *> processes_;\n+\n+\tstruct sigaction oldsa_;\n+\tEventNotifier *sigEvent_;\n+\tint pipe_[2];\n+};\n+\n } /* namespace libcamera */\n \n #endif /* __LIBCAMERA_INTERNAL_PROCESS_H__ */\ndiff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\nindex 47d56256..b8d3ccc8 100644\n--- a/src/libcamera/camera_manager.cpp\n+++ b/src/libcamera/camera_manager.cpp\n@@ -18,6 +18,7 @@\n #include \"libcamera/internal/ipa_manager.h\"\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/pipeline_handler.h\"\n+#include \"libcamera/internal/process.h\"\n #include \"libcamera/internal/thread.h\"\n #include \"libcamera/internal/utils.h\"\n \n@@ -67,6 +68,7 @@ private:\n \tstd::unique_ptr<DeviceEnumerator> enumerator_;\n \n \tIPAManager ipaManager_;\n+\tProcessManager processManager_;\n };\n \n CameraManager::Private::Private(CameraManager *cm)\ndiff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\nindex 994190dc..72b5afe2 100644\n--- a/src/libcamera/process.cpp\n+++ b/src/libcamera/process.cpp\n@@ -41,28 +41,6 @@ LOG_DEFINE_CATEGORY(Process)\n  * The ProcessManager singleton keeps track of all created Process instances,\n  * and manages the signal handling involved in terminating processes.\n  */\n-class ProcessManager\n-{\n-public:\n-\tvoid registerProcess(Process *proc);\n-\n-\tstatic ProcessManager *instance();\n-\n-\tint writePipe() const;\n-\n-\tconst struct sigaction &oldsa() const;\n-\n-private:\n-\tvoid sighandler(EventNotifier *notifier);\n-\tProcessManager();\n-\t~ProcessManager();\n-\n-\tstd::list<Process *> processes_;\n-\n-\tstruct sigaction oldsa_;\n-\tEventNotifier *sigEvent_;\n-\tint pipe_[2];\n-};\n \n namespace {\n \n@@ -127,8 +105,20 @@ void ProcessManager::registerProcess(Process *proc)\n \tprocesses_.push_back(proc);\n }\n \n+ProcessManager *ProcessManager::self_ = nullptr;\n+\n+/**\n+ * \\brief Construct a ProcessManager instance\n+ *\n+ * The ProcessManager class is meant to only be instantiated once, by the\n+ * CameraManager.\n+ */\n ProcessManager::ProcessManager()\n {\n+\tif (self_)\n+\t\tLOG(Process, Fatal)\n+\t\t\t<< \"Multiple ProcessManager objects are not allowed\";\n+\n \tsigaction(SIGCHLD, NULL, &oldsa_);\n \n \tstruct sigaction sa;\n@@ -145,6 +135,8 @@ ProcessManager::ProcessManager()\n \t\t\t<< \"Failed to initialize pipe for signal handling\";\n \tsigEvent_ = new EventNotifier(pipe_[0], EventNotifier::Read);\n \tsigEvent_->activated.connect(this, &ProcessManager::sighandler);\n+\n+\tself_ = this;\n }\n \n ProcessManager::~ProcessManager()\n@@ -153,21 +145,21 @@ ProcessManager::~ProcessManager()\n \tdelete sigEvent_;\n \tclose(pipe_[0]);\n \tclose(pipe_[1]);\n+\n+\tself_ = nullptr;\n }\n \n /**\n  * \\brief Retrieve the Process manager instance\n  *\n- * The ProcessManager is a singleton and can't be constructed manually. This\n- * method shall instead be used to retrieve the single global instance of the\n- * manager.\n+ * The ProcessManager is constructed by the CameraManager. This function shall\n+ * be used to retrieve the single instance of the manager.\n  *\n  * \\return The Process manager instance\n  */\n ProcessManager *ProcessManager::instance()\n {\n-\tstatic ProcessManager processManager;\n-\treturn &processManager;\n+\treturn self_;\n }\n \n /**\n","prefixes":["libcamera-devel","01/23"]}