From 685a4613bcfc3231066d66263e198399d78cea44 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Jos=C3=A9=20Bollo?= <jose.bollo@iot.bzh>
Date: Tue, 14 Mar 2017 13:07:12 +0100
Subject: [PATCH] Move to AGL framework on top of systemd
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

This new version of the framework leverages systemd
features to launch its applications.

Some of the advantages are:
 - possible use of systemd features (namespace, cgroups, autostart,
   dependency resolution, socket activation, ...)
 - more feature are let open to integrator's design

Some of the drawbacks are:
 - not more possible to launch an other instance of an application
   already launched
 - pause/resume is no more available by the framework
 - the remote mode is to be redefined

This commit integrates the first version of the framework on
top of systemd. More work is to come.

None of the current drawback is definitive.

Bug-AGL: SPEC-138, SPEC-425, SPEC-426, SPEC-427

Change-Id: Idfb98761c0db23562bb783bed1b03aeb956fc587
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
---
 .../af-main/add-qt-wayland-shell-integration.patch | 21 +++++------
 .../af-main/af-main/init-afm-dirs.service          | 15 --------
 .../recipes-core/af-main/af-main/init-afm-dirs.sh  |  7 ----
 .../recipes-core/af-main/af-main_1.0.bb            | 42 ++++++++++------------
 .../recipes-core/af-main/af-main_1.0.inc           |  8 ++++-
 .../recipes-core/af-main/nativesdk-af-main_1.0.bb  |  4 ---
 6 files changed, 37 insertions(+), 60 deletions(-)
 delete mode 100644 meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.service
 delete mode 100644 meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.sh

diff --git a/meta-app-framework/recipes-core/af-main/af-main/add-qt-wayland-shell-integration.patch b/meta-app-framework/recipes-core/af-main/af-main/add-qt-wayland-shell-integration.patch
index 8e21678be..c92415b80 100644
--- a/meta-app-framework/recipes-core/af-main/af-main/add-qt-wayland-shell-integration.patch
+++ b/meta-app-framework/recipes-core/af-main/af-main/add-qt-wayland-shell-integration.patch
@@ -1,11 +1,12 @@
-diff --git a/conf/afm-user-daemon.service b/conf/afm-user-daemon.service
-index 6b5c1d8..e0f6799 100644
---- a/conf/afm-user-daemon.service
-+++ b/conf/afm-user-daemon.service
-@@ -7,6 +7,7 @@ BusName=org.AGL.afm.user
- ExecStart=/usr/bin/afm-user-daemon --user-dbus=unix:path=%t/bus
- Environment=AFM_APP_INSTALL_DIR=%%r
- Environment=LD_PRELOAD=/usr/lib/libEGL.so
+diff --git a/conf/afm-unit.conf b/conf/afm-unit.conf
+index 82113ef..2fbc9e2 100644
+--- a/conf/afm-unit.conf
++++ b/conf/afm-unit.conf
+@@ -127,6 +127,7 @@ SuccessExitStatus=0 SIGKILL
+ WorkingDirectory=-{{&#metadata.app-data-dir}}/{{id}}
+ ExecStartPre=/bin/mkdir -p {{&#metadata.app-data-dir}}/{{id}}
+ Environment=AFM_APP_INSTALL_DIR={{:#metadata.install-dir}}
 +Environment=QT_WAYLAND_SHELL_INTEGRATION=ivi-shell
- Restart=on-failure
- RestartSec=5
+ 
+ %systemd-unit user
+ {{#required-permission.urn:AGL:permission::public:hidden}}\
diff --git a/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.service b/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.service
deleted file mode 100644
index 7e3b9e4e8..000000000
--- a/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.service
+++ /dev/null
@@ -1,15 +0,0 @@
-#
-# Install security-manager DB to /var
-
-[Unit]
-Description=Deploy AFM directories to /var
-After=sysinit.target
-Before=afm-system-daemon.service
-Before=afm-user-daemon.service
-
-[Install]
-WantedBy=default.target
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/init-afm-dirs.sh
diff --git a/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.sh b/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.sh
deleted file mode 100644
index 97cf272f2..000000000
--- a/meta-app-framework/recipes-core/af-main/af-main/init-afm-dirs.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-
-if [ ! -e "/var/lib/afm" ]; then
-	mkdir -p /var/lib
-	cp -ra /usr/share/afm /var/lib
-fi
-
diff --git a/meta-app-framework/recipes-core/af-main/af-main_1.0.bb b/meta-app-framework/recipes-core/af-main/af-main_1.0.bb
index 86b5d34e2..3c1b692f3 100644
--- a/meta-app-framework/recipes-core/af-main/af-main_1.0.bb
+++ b/meta-app-framework/recipes-core/af-main/af-main_1.0.bb
@@ -11,15 +11,9 @@ BBCLASSEXTEND = "native"
 
 SECTION = "base"
 
-DEPENDS = "openssl libxml2 xmlsec1 systemd libzip json-c security-manager libcap-native af-binder"
+DEPENDS = "openssl libxml2 xmlsec1 systemd libzip json-c systemd security-manager libcap-native af-binder"
 DEPENDS_class-native = "openssl libxml2 xmlsec1 libzip json-c"
 
-afm_name    = "afm"
-afm_confdir = "${sysconfdir}/${afm_name}"
-afm_datadir = "/var/lib/${afm_name}"
-afm_init_datadir = "${datadir}/${afm_name}"
-afb_binding_dir = "${libdir}/afb"
-
 EXTRA_OECMAKE_class-native  = "\
 	-DUSE_LIBZIP=1 \
 	-DUSE_SIMULATION=1 \
@@ -36,6 +30,7 @@ EXTRA_OECMAKE = "\
 	-Dafm_name=${afm_name} \
 	-Dafm_confdir=${afm_confdir} \
 	-Dafm_datadir=${afm_datadir} \
+	-Dsystemd_units_root=${systemd_units_root} \
 	-DUNITDIR_USER=${systemd_user_unitdir} \
 	-DUNITDIR_SYSTEM=${systemd_system_unitdir} \
 "
@@ -47,37 +42,31 @@ GROUPADD_PARAM_${PN} = "-r ${afm_name}"
 SYSTEMD_SERVICE_${PN} = "afm-system-daemon.service"
 SYSTEMD_AUTO_ENABLE = "enable"
 
-SRC_URI_append = "file://init-afm-dirs.sh \
-		  ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'file://init-afm-dirs.service', '', d)}"
-
 FILES_${PN} += "\
-	${bindir}/init-afm-dirs.sh \
-	${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '${systemd_user_unitdir}/afm-user-daemon.service ${systemd_unitdir}/system/init-afm-dirs.service', '', d)} \
+	${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '${systemd_user_unitdir}/afm-user-daemon.service', '', d)} \
 "
-
 RDEPENDS_${PN}_append_smack = " smack-userspace"
 DEPENDS_append_smack = " smack-userspace-native"
 
 # short hacks here
 SRC_URI += "\
 	file://Hack-to-allow-the-debugging.patch \
-	file://add-qt-wayland-shell-integration.patch \
 "
 
 # tools used to install wgt at first boot
 SRC_URI += "\
 	file://afm-install \
+	file://add-qt-wayland-shell-integration.patch \
 "
 
 do_install_append() {
     install -d ${D}${bindir}
-    install -m 0755 ${WORKDIR}/init-afm-dirs.sh ${D}${bindir}
+    install -d -m 0775 ${D}${systemd_units_root}/{system,user}
+    install -d -m 0775 ${D}${systemd_units_root}/{system,user}/default.target.wants
+    install -d ${D}${afm_datadir}/{applications,icons}
     if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
-        mkdir -p ${D}${sysconfdir}/systemd/user/default.target.wants
-        mkdir -p ${D}${sysconfdir}/systemd/system/default.target.wants
+        mkdir -p ${D}${sysconfdir}/systemd/{system,user}/default.target.wants
         ln -sf ${systemd_user_unitdir}/afm-user-daemon.service ${D}${sysconfdir}/systemd/user/default.target.wants
-	install -m 644 -p -D ${WORKDIR}/init-afm-dirs.service ${D}${systemd_unitdir}/system/init-afm-dirs.service
-	ln -sf ${systemd_unitdir}/system/init-afm-dirs.service ${D}${sysconfdir}/systemd/system/default.target.wants
     fi
     install -m 0755 ${WORKDIR}/afm-install ${D}${bindir}
 }
@@ -87,18 +76,25 @@ do_install_append_qemux86-64() {
 }
 
 pkg_postinst_${PN}() {
-    mkdir -p $D${afm_init_datadir}/applications $D${afm_init_datadir}/icons
+    if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
+        chgrp ${afm_name} $D${systemd_units_root}/{system,user}/{default.target.wants,.}
+    fi
+    chown ${afm_name}:${afm_name} $D${afm_datadir}/{applications,icons,.}
     setcap cap_mac_override,cap_dac_override=ep $D${bindir}/afm-system-daemon
     setcap cap_mac_override,cap_mac_admin,cap_setgid=ep $D${bindir}/afm-user-daemon
 }
 
 pkg_postinst_${PN}_smack() {
-    mkdir -p $D${afm_init_datadir}/applications $D${afm_init_datadir}/icons
-    chown ${afm_name}:${afm_name} $D${afm_init_datadir} $D${afm_init_datadir}/applications $D${afm_init_datadir}/icons
-    chsmack -a 'System::Shared' -t $D${afm_init_datadir} $D${afm_init_datadir}/applications $D${afm_init_datadir}/icons
+    if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
+        chgrp ${afm_name} $D${systemd_units_root}/{system,user}/{default.target.wants,.}
+        chsmack -a 'System::Shared' -t $D${systemd_units_root}/{system,user}/{default.target.wants,.}
+    fi
+    chown ${afm_name}:${afm_name} $D${afm_datadir}/{applications,icons,.}
+    chsmack -a 'System::Shared' -t $D${afm_datadir}/{applications,icons,.}
     setcap cap_mac_override,cap_dac_override=ep $D${bindir}/afm-system-daemon
     setcap cap_mac_override,cap_mac_admin,cap_setgid=ep $D${bindir}/afm-user-daemon
 }
+FILES_${PN} += " ${systemd_units_root} "
 
 PACKAGES =+ "${PN}-binding ${PN}-binding-dbg"
 FILES_${PN}-binding = " ${afb_binding_dir}/afm-main-binding.so "
diff --git a/meta-app-framework/recipes-core/af-main/af-main_1.0.inc b/meta-app-framework/recipes-core/af-main/af-main_1.0.inc
index c38dad4d4..6ce87ed71 100644
--- a/meta-app-framework/recipes-core/af-main/af-main_1.0.inc
+++ b/meta-app-framework/recipes-core/af-main/af-main_1.0.inc
@@ -14,7 +14,13 @@ SRC_URI = "${SRC_URI_git} \
            ${SRC_URI_files} \
           "
 
-SRCREV = "863bf1c6b4e10176edf8b26a9703109ab8db2c43"
+SRCREV = "255c83029f56e8d90e7ce185b007c4ca65afec1e"
 
 S = "${WORKDIR}/git"
 
+afm_name    = "afm"
+afm_confdir = "${sysconfdir}/${afm_name}"
+afm_datadir = "/var/local/lib/${afm_name}"
+afb_binding_dir = "${libdir}/afb"
+systemd_units_root = "/usr/local/lib/systemd"
+
diff --git a/meta-app-framework/recipes-core/af-main/nativesdk-af-main_1.0.bb b/meta-app-framework/recipes-core/af-main/nativesdk-af-main_1.0.bb
index 021c9ac00..8d044345f 100644
--- a/meta-app-framework/recipes-core/af-main/nativesdk-af-main_1.0.bb
+++ b/meta-app-framework/recipes-core/af-main/nativesdk-af-main_1.0.bb
@@ -6,10 +6,6 @@ SECTION = "base"
 
 DEPENDS = "nativesdk-openssl nativesdk-libxml2 nativesdk-xmlsec1 nativesdk-libzip nativesdk-json-c"
 
-afm_name    = "afm"
-afm_confdir = "${sysconfdir}/${afm_name}"
-afm_datadir = "/var/lib/${afm_name}"
-
 EXTRA_OECMAKE = "\
 	-DUSE_LIBZIP=1 \
 	-DUSE_SIMULATION=1 \
-- 
2.16.6