umdns: convert init.d script to ucode
authorJohn Crispin <john@phrozen.org>
Mon, 14 Oct 2024 10:20:42 +0000 (12:20 +0200)
committerJohn Crispin <john@phrozen.org>
Wed, 23 Oct 2024 12:14:38 +0000 (14:14 +0200)
Signed-off-by: John Crispin <john@phrozen.org>
package/network/services/umdns/files/umdns.init

index e9a2c05e093e3098363e43dc23b6ea1d34259d95..108ce9fcabae3b089800f1362cf11e7a002a48bc 100644 (file)
@@ -1,55 +1,52 @@
-#!/bin/sh /etc/rc.common
-# Copyright (c) 2014 OpenWrt.org
-
-START=80
-
-USE_PROCD=1
-PROG=/usr/sbin/umdns
-IFACES=""
-
-load_ifaces() {
-       local network="$(uci -q get umdns.@umdns[-1].network)"
-       for n in $network; do
-               local device
-               json_load "$(ifstatus $n)"
-               json_get_var device l3_device
-               echo -n "$device "
-       done
+#!/usr/bin/ucode /usr/share/ucode/procd.uc
+
+function instances() {
+       const instance = {
+               command: [ '/usr/sbin/umdns' ],
+               seccomp: "/etc/seccomp/umdns.json",
+               jail_permissions: [ 'ubus', 'log' ],
+               jail_mounts: [ '/etc/umdns' ],
+               respawn: procd_default_respawn(),
+               update_trigger: procd_update_trigger('ubus call umdns update', 5000),
+       };
+
+       let jail = +uci.get('umdns', '@umdns[-1]', 'jail');
+       let triggers = map(
+               uci.get('umdns', '@umdns[-1]', 'network'),
+               (network) => procd_interface_trigger(network)
+       );
+
+       return {
+               ...instance,
+               jail,
+               triggers
+       };
 }
 
-reload_service() {
-       json_init
-       json_add_array interfaces
-       for i in $(load_ifaces); do
-               json_add_string "" "$i"
-       done
-       json_close_array
+function service_reload() {
+       let networks = uci.get('umdns', '@umdns[-1]', 'network');
+       let interfaces = procd_get_network_devices(networks);
 
-       ubus call umdns set_config "$(json_dump)"
+       ubus.call('umdns', 'set_config', { interfaces });
+       ubus.call('umdns', 'update');
 }
 
-start_service() {
-       local network="$(uci -q get umdns.@umdns[-1].network)"
-
-       procd_open_instance
-       procd_set_param command "$PROG"
-       [ -f /etc/seccomp/umdns.json ] && procd_set_param seccomp /etc/seccomp/umdns.json
-       procd_set_param respawn
-       procd_open_trigger
-       procd_add_config_trigger "config.change" "umdns" /etc/init.d/umdns reload
-       for n in $network; do
-               procd_add_interface_trigger "interface.*" $n /etc/init.d/umdns reload
-       done
-       procd_add_raw_trigger "instance.update" 5000 "/bin/ubus" "call" "umdns" "reload"
-       procd_close_trigger
-       [ "$(uci get umdns.@umdns[-1].jail)" = 1 ] && {
-               procd_add_jail umdns ubus log
-               [ -d /etc/umdns ] && procd_add_jail_mount "/etc/umdns"
-       }
-       procd_close_instance
+function service_started() {
+       procd_ubus_wait_for("umdns");
+
+       service_reload();
 }
 
-service_started() {
-       ubus -t 10 wait_for umdns
-       [ $? = 0 ] && reload_service
+function service_triggers() {
+       return [
+               procd_config_reload_trigger("umdns"),
+       ];
 }
+
+procd_service('umdns', {
+       start: 80,
+       instances,
+       service_triggers,
+       service_started,
+       service_reload,
+});