From d05d4a29d4c609b1e81bf8dcdcb2bd0b3a10ad7a Mon Sep 17 00:00:00 2001
From: Thierry Escande <thierry.escande@huawei.com>
Date: Tue, 9 Aug 2022 15:26:16 +0200
Subject: [PATCH] param_service: Use libuv signal handlers

This patch reworks the files for the param_service standalone
executable. It now uses the libuv signal handling APIs so it doesn't
need a libuv idle callback anymore to monitor the app exit state.

Signed-off-by: Thierry Escande <thierry.escande@huawei.com>
---
 ...service.c-Fix-stopping-param_service.patch | 74 -----------------
 .../param_service_main.c                      | 66 ---------------
 ...m.patch => param_service_standalone.patch} | 83 ++++++++++++++++++-
 .../openharmony/openharmony-standard_3.0.bb   |  4 +-
 4 files changed, 80 insertions(+), 147 deletions(-)
 delete mode 100644 recipes-openharmony/openharmony/openharmony-standard-3.0/param_service.c-Fix-stopping-param_service.patch
 delete mode 100644 recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_main.c
 rename recipes-openharmony/openharmony/openharmony-standard-3.0/{param_service-Add-to-build-system.patch => param_service_standalone.patch} (56%)

diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service.c-Fix-stopping-param_service.patch b/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service.c-Fix-stopping-param_service.patch
deleted file mode 100644
index 77a7cb1a..00000000
--- a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service.c-Fix-stopping-param_service.patch
+++ /dev/null
@@ -1,74 +0,0 @@
-# SPDX-FileCopyrightText: Huawei Inc.
-#
-# SPDX-License-Identifier: Apache-2.0
-
-Patch for //base/startup/init_lite
-
-param_service.c: Fix stopping param_service
-
-StopParamService() have to be called from the uv_loop context to be able to
-successfully stop the uv_loop. To achieve that we need an uv_idle callback.
-
-Signed-off-by: Robert Drab <robert.drab@huawei.com>
-Upstream-Status: Pending
-
-diff --git a/services/param/service/param_service.c b/services/param/service/param_service.c
-index cd5d93be..6634fa3d 100644
---- a/services/param/service/param_service.c
-+++ b/services/param/service/param_service.c
-@@ -241,12 +241,36 @@ void StopParamService()
-     PARAM_LOGI("StopParamService.");
- }
- 
-+/*
-+ * uv_stop() must be called from the uv_loop context and therefore
-+ * StopParamService() cannot do it's job when called from the signal handler.
-+ * We need a uv_idle callback that will call StopParamService() which calls
-+ * uv_stop() from the uv_loop context to actually stop the param service loop.
-+ */
-+int g_paramService_asyncStopRequest = 0;
-+
-+int ParamServiceAsyncStopRequest() {
-+    g_paramService_asyncStopRequest = 0;
-+    return 0;
-+}
-+
-+void ParamServiceLoopIdleCallback(uv_idle_t *handle) {
-+    if (g_paramService_asyncStopRequest) {
-+        PARAM_LOGI("%s(): %d Stopping param service...", __FUNCTION__, __LINE__);
-+        StopParamService();
-+    }
-+}
-+
- int StartParamService()
- {
-     PARAM_LOGI("StartParamService.");
-     uv_fs_t req;
-     uv_fs_unlink(uv_default_loop(), &req, PIPE_NAME, NULL);
- 
-+    uv_idle_t idler;
-+    uv_idle_init(uv_default_loop(), &idler);
-+    uv_idle_start(&idler, ParamServiceLoopIdleCallback);
-+
-     uv_pipe_t pipeServer;
-     int ret = uv_pipe_init(uv_default_loop(), &pipeServer, 0);
-     PARAM_CHECK(ret == 0, return ret, "Failed to uv_pipe_init %d", ret);
-diff --git a/services/include/param/init_param.h b/services/include/param/init_param.h
-index 73a06795..4574cbaa 100644
---- a/services/include/param/init_param.h
-+++ b/services/include/param/init_param.h
-@@ -51,6 +51,14 @@ int StartParamService();
-  */
- void StopParamService();
- 
-+/**
-+ * ParamServiceAsyncStopRequest interface
-+ * Allows stopping ParamService from outside event loop context,
-+ * e.g. from signal handler
-+ *
-+ */
-+int ParamServiceAsyncStopRequest(void);
-+
- /**
-  * Init 接口
-  * 加载默认的参数值
diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_main.c b/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_main.c
deleted file mode 100644
index 5cd2a6c2..00000000
--- a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_main.c
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * SPDX-FileCopyrightText: Huawei Inc.
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#include <init_log.h>
-#include <init_param.h>
-#include <errno.h>
-#include <unistd.h>
-
-#include <signal.h>
-
-void sig_handler(int signum) {
-    INIT_LOGI("Requesting param service to stop...");
-    ParamServiceAsyncStopRequest();
-}
-
-char *parse_args(int argc, char **argv) {
-    char *fpath = NULL;
-
-    if (argc == 2 && argv[1]) {
-        if (access(argv[1], R_OK) == 0) {
-            fpath = argv[1];
-        }
-    }
-
-    return fpath;
-}
-
-int main(int argc, char **argv)
-{
-    char *params_fpath = "/system/etc/ohos.para";
-
-    if (argc > 1)
-        params_fpath = parse_args(argc, argv);
-
-    if (!params_fpath) {
-        INIT_LOGI("Invalid params file path!");
-        return -EINVAL;
-    }
-
-    InitParamService();
-    int ret = LoadDefaultParams(params_fpath);
-    if (ret) {
-        INIT_LOGE("Failed to load default params! Error code: %d", ret);
-    }
-    ret = LoadPersistParams();
-    if (ret) {
-        INIT_LOGE("Failed to load persist params! Error code: %d", ret);
-    }
-
-    struct sigaction action = { 0 };
-    action.sa_handler = sig_handler;
-
-    sigaction(SIGTERM, &action, NULL);
-    sigaction(SIGINT, &action, NULL);
-    sigaction(SIGHUP, &action, NULL);
-
-    // Following call spins up libuv event loop that will keep service running
-    INIT_LOGI("Starting param service...");
-    StartParamService();
-    INIT_LOGI("Param service stopped.");
-
-    return 0;
-}
diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service-Add-to-build-system.patch b/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_standalone.patch
similarity index 56%
rename from recipes-openharmony/openharmony/openharmony-standard-3.0/param_service-Add-to-build-system.patch
rename to recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_standalone.patch
index a0cd282f..0b5a13a6 100644
--- a/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service-Add-to-build-system.patch
+++ b/recipes-openharmony/openharmony/openharmony-standard-3.0/param_service_standalone.patch
@@ -4,17 +4,18 @@
 
 Patch for //base/startup/init_lite
 
-param_service: Add to build system
+param_service: Add standalone param_service executable
 
 In OpenHarmony 3.0 Param Service is a part of the whole Init system. This
 service is required by many other services. Therefore we have to split out
 the service as an independent from Init when Init is not used.
 
 Signed-off-by: Robert Drab <robert.drab@huawei.com>
+Signed-off-by: Thierry Escande <thierry.escande@huawei.com>
 Upstream-Status: Inappropriate
 
 diff --git a/services/BUILD.gn b/services/BUILD.gn
-index 1994c2ef..e094f7ce 100644
+index 1994c2ef..bdf34165 100644
 --- a/services/BUILD.gn
 +++ b/services/BUILD.gn
 @@ -85,6 +85,25 @@ if (defined(ohos_lite)) {
@@ -81,7 +82,7 @@ index 07e1d278..ba420b66 100644
 +  part_name = "startup_l2"
  }
 diff --git a/services/param/service/param_service.c b/services/param/service/param_service.c
-index cd5d93be..03a10ae0 100644
+index cd5d93be..2f0beffe 100644
 --- a/services/param/service/param_service.c
 +++ b/services/param/service/param_service.c
 @@ -131,7 +131,11 @@ static int ProcessParamSet(const RequestMsg *msg)
@@ -96,7 +97,23 @@ index cd5d93be..03a10ae0 100644
      return 0;
  }
  
-@@ -267,7 +271,11 @@ int SystemWriteParam(const char *name, const char *value)
+@@ -243,6 +247,15 @@ int StartParamService()
+     uv_fs_t req;
+     uv_fs_unlink(uv_default_loop(), &req, PIPE_NAME, NULL);
+ 
++    // Stop the param service properly on a few signals
++    uv_signal_t sigterm, sigint, sighup;
++    uv_signal_init(uv_default_loop(), &sigterm);
++    uv_signal_start(&sigterm, StopParamService, SIGTERM);
++    uv_signal_init(uv_default_loop(), &sigint);
++    uv_signal_start(&sigint, StopParamService, SIGINT);
++    uv_signal_init(uv_default_loop(), &sighup);
++    uv_signal_start(&sighup, StopParamService, SIGHUP);
++
+     uv_pipe_t pipeServer;
+     int ret = uv_pipe_init(uv_default_loop(), &pipeServer, 0);
+     PARAM_CHECK(ret == 0, return ret, "Failed to uv_pipe_init %d", ret);
+@@ -267,7 +280,11 @@ int SystemWriteParam(const char *name, const char *value)
      PARAM_CHECK(ret == 0, return ret, "Failed to set persist param %s", name);
  
      // notify event to process trigger
@@ -108,3 +125,61 @@ index cd5d93be..03a10ae0 100644
      return ret;
  }
  
+diff --git a/services/param/src/param_service_main.c b/services/param/src/param_service_main.c
+new file mode 100644
+index 00000000..0be35eb9
+--- /dev/null
++++ b/services/param/src/param_service_main.c
+@@ -0,0 +1,52 @@
++/*
++ * SPDX-FileCopyrightText: Huawei Inc.
++ *
++ * SPDX-License-Identifier: Apache-2.0
++ */
++
++#include <init_log.h>
++#include <init_param.h>
++#include <errno.h>
++#include <unistd.h>
++
++char *parse_args(int argc, char **argv) {
++    char *fpath = NULL;
++
++    if (argc == 2 && argv[1]) {
++        if (access(argv[1], R_OK) == 0) {
++            fpath = argv[1];
++        }
++    }
++
++    return fpath;
++}
++
++int main(int argc, char **argv)
++{
++    char *params_fpath = "/system/etc/ohos.para";
++
++    if (argc > 1)
++        params_fpath = parse_args(argc, argv);
++
++    if (!params_fpath) {
++        INIT_LOGI("Invalid params file path!");
++        return -EINVAL;
++    }
++
++    InitParamService();
++    int ret = LoadDefaultParams(params_fpath);
++    if (ret) {
++        INIT_LOGE("Failed to load default params! Error code: %d", ret);
++    }
++    ret = LoadPersistParams();
++    if (ret) {
++        INIT_LOGE("Failed to load persist params! Error code: %d", ret);
++    }
++
++    // Following call spins up libuv event loop that will keep service running
++    INIT_LOGI("Starting param service...");
++    StartParamService();
++    INIT_LOGI("Param service stopped.");
++
++    return 0;
++}
diff --git a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
index 5a280a70..827eced7 100644
--- a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
+++ b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
@@ -61,9 +61,7 @@ SRC_URI += "file://appspawn-procps.patch;patchdir=${S}/base/startup/appspawn_sta
 SRC_URI += "file://base_startup_appspawn_standard-disable-longProcName-resetting.patch;patchdir=${S}/base/startup/appspawn_standard"
 SRC_URI += "file://test_xts_acts-Align-tests-list-with-mandatory-set.patch;patchdir=${S}/test/xts/acts"
 
-SRC_URI += "file://param_service_main.c;subdir=${S}/base/startup/init_lite/services/param/src"
-SRC_URI += "file://param_service-Add-to-build-system.patch;patchdir=${S}/base/startup/init_lite"
-SRC_URI += "file://param_service.c-Fix-stopping-param_service.patch;patchdir=${S}/base/startup/init_lite"
+SRC_URI += "file://param_service_standalone.patch;patchdir=${S}/base/startup/init_lite"
 SRC_URI += "file://param_service-Add-to-startup-l2-part.patch;patchdir=${S}/base/startup/appspawn_standard"
 
 inherit python3native gn_base ptest
-- 
GitLab