From 316a249017da11fec0c44e3d220b1c1a9930dd79 Mon Sep 17 00:00:00 2001
From: Esben Haabendal <esben@geanix.com>
Date: Thu, 9 Mar 2023 20:24:12 +0100
Subject: [PATCH] openharmony-standard: Run media_service as systemd service

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 .../openharmony/files/media.service              | 16 ++++++++++++----
 .../openharmony/files/media.service.license      |  3 +++
 .../openharmony/files/openharmony-preinit        | 11 -----------
 .../openharmony/openharmony-standard_3.0.bb      |  1 +
 4 files changed, 16 insertions(+), 15 deletions(-)
 create mode 100644 recipes-openharmony/openharmony/files/media.service.license

diff --git a/recipes-openharmony/openharmony/files/media.service b/recipes-openharmony/openharmony/files/media.service
index c592e01f..837f0cfb 100644
--- a/recipes-openharmony/openharmony/files/media.service
+++ b/recipes-openharmony/openharmony/files/media.service
@@ -1,10 +1,18 @@
 [Unit]
-Description=OpenHarmony media service
+Description=OpenHarmony media service (SA: 3002)
+Requires=samgr.service
+After=samgr.service
+# Logging to hilogd, but seems to work without it
+Wants=hilogd.service
+After=hilogd.service
 
 [Service]
-Type=oneshot
-ExecStart=/bin/echo "OpenHarmony Media Service"
-RemainAfterExit=yes
+Type=notify
+ExecStart=/usr/bin/sa_main /system/profile/media_service.xml
+User=system
+Group=media_rw
+SupplementaryGroups=system
+LimitNICE=40
 
 [Install]
 WantedBy=multi-user.target
diff --git a/recipes-openharmony/openharmony/files/media.service.license b/recipes-openharmony/openharmony/files/media.service.license
new file mode 100644
index 00000000..98a0b3f4
--- /dev/null
+++ b/recipes-openharmony/openharmony/files/media.service.license
@@ -0,0 +1,3 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
diff --git a/recipes-openharmony/openharmony/files/openharmony-preinit b/recipes-openharmony/openharmony/files/openharmony-preinit
index 191772c8..bd0b8bd7 100644
--- a/recipes-openharmony/openharmony/files/openharmony-preinit
+++ b/recipes-openharmony/openharmony/files/openharmony-preinit
@@ -136,17 +136,6 @@ fi
 #sa_main /system/profile/dps_service.xml & # SA: 1401 180 3502
 #sleep "$STARTUP_CMD_SLEEP"
 
-# trigger: boot
-# "name" : "media_service",
-# "path" : ["/system/bin/sa_main", "/system/profile/media_service.xml"],
-# "uid" : "system",
-# "gid" : ["media_rw", "system"]
-if systemctl -q is-enabled media.service; then
-	echo >/dev/console "Starting OpenHarmony media service"
-	su system -c '/system/bin/sa_main /system/profile/media_service.xml &'
-	sleep "$STARTUP_CMD_SLEEP"
-fi
-
 # trigger: boot
 # "name" : "inputmethod_service",
 # "path" : ["/system/bin/sa_main", "/system/profile/inputmethod_service.xml"],
diff --git a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
index f61d5dd2..2ec718c1 100644
--- a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
+++ b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
@@ -2035,6 +2035,7 @@ inherit useradd
 USERADD_PACKAGES = "${PN}"
 USERADD_PARAM:${PN} = "-u 1000 -U -s /bin/sh system"
 USERADD_PARAM:${PN}:append = ";-u 1007 -U -s /bin/false log"
+USERADD_PARAM:${PN}:append = ";-u 1023 -U -s /bin/false media_rw"
 USERADD_PARAM:${PN}:append = ";-u 1036 -U -s /bin/false logd"
 USERADD_PARAM:${PN}:append = ";-u 2000 -U -s /bin/false shell"
 USERADD_PARAM:${PN}:append = ";-u 3009 -U -s /bin/false readproc"
-- 
GitLab