From 006abf1773051ad355b52d70095f63f44a496b13 Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Sun, 25 Jul 2021 13:32:37 +0200
Subject: [PATCH 02/70] procd: add uxc support

Signed-off-by: John Crispin <john@phrozen.org>
---
 package/system/procd/Makefile       | 15 +++---
 package/system/procd/files/procd.sh | 79 +++++++++++++++++++++++++++++
 package/system/procd/files/uxc.init |  4 ++
 3 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/package/system/procd/Makefile b/package/system/procd/Makefile
index 30d5adf427..b831f86639 100644
--- a/package/system/procd/Makefile
+++ b/package/system/procd/Makefile
@@ -12,9 +12,9 @@ PKG_RELEASE:=$(AUTORELEASE)
 
 PKG_SOURCE_PROTO:=git
 PKG_SOURCE_URL=$(PROJECT_GIT)/project/procd.git
-PKG_SOURCE_DATE:=2021-02-23
-PKG_SOURCE_VERSION:=37eed131e9967a35f47bacb3437a9d3c8a57b3f4
-PKG_MIRROR_HASH:=2b0131ff9055ccf987cbeb5f36c2c2585dc780999df6be312fbbbcd61ce676d4
+PKG_MIRROR_HASH:=0d51642d82d7bb4150355a6986e54504dce171c6fcb7eeff312d20a5d106bad8
+PKG_SOURCE_DATE:=2021-11-04
+PKG_SOURCE_VERSION:=0ee8e734a7f67220cf4a3412b60ff674b5fb20dd
 CMAKE_INSTALL:=1
 
 PKG_LICENSE:=GPL-2.0
@@ -32,7 +32,7 @@ include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
 
 ifeq ($(DUMP),)
-  STAMP_CONFIGURED:=$(strip $(STAMP_CONFIGURED))_$(shell echo $(CONFIG_TARGET_INIT_PATH) | mkhash md5)
+  STAMP_CONFIGURED:=$(strip $(STAMP_CONFIGURED))_$(shell echo $(CONFIG_TARGET_INIT_PATH) | $(MKHASH) md5)
 endif
 
 CMAKE_OPTIONS += -DEARLY_PATH="$(TARGET_INIT_PATH)"
@@ -68,7 +68,7 @@ define Package/procd-ujail
   SECTION:=base
   CATEGORY:=Base system
   DEPENDS:=@KERNEL_NAMESPACES +@KERNEL_UTS_NS +@KERNEL_IPC_NS +@KERNEL_PID_NS \
-	  +libubox +libubus +libblobmsg-json
+	  +libubox +libubus +libuci +libblobmsg-json
   TITLE:=OpenWrt process jail helper
 endef
 
@@ -82,15 +82,14 @@ endef
 define Package/procd-seccomp
   SECTION:=base
   CATEGORY:=Base system
-  DEPENDS:=@(arm||armeb||mips||mipsel||i386||powerpc||x86_64) @!TARGET_uml \
-	  @KERNEL_SECCOMP +libubox +libblobmsg-json
+  DEPENDS:=@SECCOMP +libubox +libblobmsg-json
   TITLE:=OpenWrt process seccomp helper + utrace
 endef
 
 define Package/uxc
   SECTION:=base
   CATEGORY:=Base system
-  DEPENDS:=+procd-ujail +libubus +libubox +libblobmsg-json
+  DEPENDS:=+procd-ujail +libubus +libubox +libblobmsg-json +blockd +PACKAGE_uxc:rpcd
   TITLE:=OpenWrt container management
   MAINTAINER:=Daniel Golle <daniel@makrotopia.org>
 endef
diff --git a/package/system/procd/files/procd.sh b/package/system/procd/files/procd.sh
index d86b7219da..3549a5a914 100644
--- a/package/system/procd/files/procd.sh
+++ b/package/system/procd/files/procd.sh
@@ -329,6 +329,82 @@ _procd_add_config_trigger() {
 	json_close_array
 }
 
+_procd_add_mount_trigger() {
+	json_add_array
+	_procd_add_array_data "$1"
+	local action="$2"
+	local multi=0
+	shift ; shift
+
+	json_add_array
+	_procd_add_array_data "if"
+
+	if [ "$2" ]; then
+		json_add_array
+		_procd_add_array_data "or"
+		multi=1
+	fi
+
+	while [ "$1" ]; do
+		json_add_array
+		_procd_add_array_data "eq" "target" "$1"
+		shift
+		json_close_array
+	done
+
+	[ $multi = 1 ] && json_close_array
+
+	json_add_array
+	_procd_add_array_data "run_script" /etc/init.d/$name $action
+	json_close_array
+
+	json_close_array
+	_procd_add_timeout
+	json_close_array
+}
+
+_procd_add_action_mount_trigger() {
+	local script=$(readlink "$initscript")
+	local name=$(basename ${script:-$initscript})
+	local action="$1"
+	local mpath
+	shift
+
+	_procd_open_trigger
+	_procd_add_mount_trigger mount.add $action "$@"
+	_procd_close_trigger
+}
+
+procd_get_mountpoints() {
+	(
+		__procd_check_mount() {
+			local cfg="$1"
+			local path="${2%%/}/"
+			local target
+			config_get target "$cfg" target
+			target="${target%%/}/"
+			[ "$path" != "${path##$target}" ] && echo "${target%%/}"
+		}
+
+		config_load fstab
+		for mpath in "$@"; do
+			config_foreach __procd_check_mount mount "$mpath"
+		done
+	) | sort -u
+}
+
+_procd_add_restart_mount_trigger() {
+	local mountpoints="$(procd_get_mountpoints "$@")"
+	[ "${mountpoints//[[:space:]]}" ] &&
+		_procd_add_action_mount_trigger restart $mountpoints
+}
+
+_procd_add_reload_mount_trigger() {
+	local mountpoints="$(procd_get_mountpoints "$@")"
+	[ "${mountpoints//[[:space:]]}" ] &&
+		_procd_add_action_mount_trigger reload $mountpoints
+}
+
 _procd_add_raw_trigger() {
 	json_add_array
 	_procd_add_array_data "$1"
@@ -560,8 +636,11 @@ _procd_wrapper \
 	procd_add_raw_trigger \
 	procd_add_config_trigger \
 	procd_add_interface_trigger \
+	procd_add_mount_trigger \
 	procd_add_reload_trigger \
 	procd_add_reload_interface_trigger \
+	procd_add_reload_mount_trigger \
+	procd_add_restart_mount_trigger \
 	procd_open_trigger \
 	procd_close_trigger \
 	procd_open_instance \
diff --git a/package/system/procd/files/uxc.init b/package/system/procd/files/uxc.init
index 035c8b0b9e..1e75b796f8 100644
--- a/package/system/procd/files/uxc.init
+++ b/package/system/procd/files/uxc.init
@@ -16,3 +16,7 @@ boot() {
 	__BOOT_UXC=1
 	start
 }
+
+service_triggers() {
+	procd_add_raw_trigger "mount.add" 3000 /etc/init.d/uxc boot
+}
-- 
2.25.1

