From 9eabf026ac27c68da180cc9d8fd44bb08d6e0ec4 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Mon, 14 Oct 2024 16:44:01 +0200 Subject: [PATCH] busybox: convert sysntpd init.d script to ucode Signed-off-by: John Crispin --- package/utils/busybox/files/sysntpd | 179 ++++++++++------------------ 1 file changed, 65 insertions(+), 114 deletions(-) diff --git a/package/utils/busybox/files/sysntpd b/package/utils/busybox/files/sysntpd index 074f14b8f8a9..1cff9b29f4e2 100755 --- a/package/utils/busybox/files/sysntpd +++ b/package/utils/busybox/files/sysntpd @@ -1,130 +1,81 @@ -#!/bin/sh /etc/rc.common -# Copyright (C) 2011 OpenWrt.org +#!/usr/bin/ucode /usr/share/ucode/procd.uc -START=98 +function config_load() { + let config = uci.get_all('system', 'ntp'); -USE_PROCD=1 -PROG=/usr/sbin/ntpd -HOTPLUG_SCRIPT=/usr/sbin/ntpd-hotplug + config.enabled ??= 1; + config.use_dhcp ??= true; -get_dhcp_ntp_servers() { - local interfaces="$1" - local filter="*" - local interface ntpservers ntpserver - - for interface in $interfaces; do - [ "$filter" = "*" ] && filter="@.interface='$interface'" || filter="$filter,@.interface='$interface'" - done - - ntpservers=$(ubus call network.interface dump | jsonfilter -e "@.interface[$filter]['data']['ntpserver']") - - for ntpserver in $ntpservers; do - local duplicate=0 - local entry - for entry in $server; do - [ "$ntpserver" = "$entry" ] && duplicate=1 - done - [ "$duplicate" = 0 ] && server="$server $ntpserver" - done + return config; } -validate_ntp_section() { - uci_load_validate system timeserver "$1" "$2" \ - 'dhcp_interface:list(string)' \ - 'enable_server:bool:0' \ - 'enabled:bool:1' \ - 'interface:string' \ - 'server:list(host)' \ - 'use_dhcp:bool:1' -} - -start_ntpd_instance() { - local peer - - [ "$2" = 0 ] || { - echo "validation failed" - return 1 +function instances() { + let config = config_load(); + if (!config.enabled) + exit(0); + + if (!length(config?.server)) + exit(0); + + const instance = { + command: [ '/usr/sbin/ntpd', '-n', '-N', '-S', '/usr/sbin/ntpd-hotplug' ], + jail: 1, + jail_permissions: [ 'ubus' ], + jail_mounts: [ '/usr/sbin/ntpd-hotplug', '/usr/share/libubox/jshn.sh', '/usr/bin/env', '/usr/bin/jshn', '/bin/ubus' ], + capabilities: '/etc/capabilities/ntpd.json', + user: 'ntp', + group: 'ntp', + respawn: procd_default_respawn(), + }; + + if (config.enable_server) { + push(instance.command, '-l'); + if (config.interface) { + push(instance.command, '-I'); + push(instance.command, procd_get_network_devices(config.interface) ?? config.interface); + instance.netdev = config.interface; + } } - [ $enabled = 0 ] && return - - [ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface" - - [ -z "$server" -a "$enable_server" = "0" ] && return - - procd_open_instance - procd_set_param command "$PROG" -n -N - if [ "$enable_server" = "1" ]; then - procd_append_param command -l - [ -n "$interface" ] && { - local ifname + if (config.use_dhcp) { + let interfaces = ubus.call('network.interface', 'dump'); + for (let interface in interfaces.interface) + if (!config.interface || interface.interface == config.interface) + if (interface.data?.ntpserver) + push(config.server, interface.data?.ntpserver); + } - network_get_device ifname "$interface" || \ - ifname="$interface" - procd_append_param command -I "$ifname" - procd_append_param netdev "$ifname" - } - fi - [ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT" - for peer in $server; do - procd_append_param command -p $peer - done - procd_set_param respawn - [ -x /sbin/ujail -a -e /etc/capabilities/ntpd.json ] && { - procd_add_jail ntpd ubus - procd_add_jail_mount "$HOTPLUG_SCRIPT" - procd_add_jail_mount "/usr/share/libubox/jshn.sh" - procd_add_jail_mount "/usr/bin/env" - procd_add_jail_mount "/usr/bin/jshn" - procd_add_jail_mount "/bin/ubus" - procd_set_param capabilities /etc/capabilities/ntpd.json - procd_set_param user ntp - procd_set_param group ntp - procd_set_param no_new_privs 1 + for (let server in config.server) { + push(instance.command, '-p'); + push(instance.command, server); } - procd_close_instance -} -start_service() { - . /lib/functions/network.sh - validate_ntp_section ntp start_ntpd_instance + return instance; } -service_triggers() { - local script name use_dhcp enable_server interface - - script=$(readlink -f "$initscript") - name=$(basename ${script:-$initscript}) - - procd_add_config_trigger "config.change" "system" /etc/init.d/$name reload - - config_load system - config_get use_dhcp ntp use_dhcp 1 - - [ $use_dhcp = 1 ] && { - local dhcp_interface - config_get dhcp_interface ntp dhcp_interface - - if [ -n "$dhcp_interface" ]; then - for n in $dhcp_interface; do - procd_add_interface_trigger "interface.*" $n /etc/init.d/$name reload - done - else - procd_add_raw_trigger "interface.*" 1000 /etc/init.d/$name reload - fi +function service_triggers() { + let config = config_load(); + let triggers = [ + procd_config_reload_trigger('system'), + ]; + + if (config.use_dhcp) { + if (config.dhcp_interface) { + for (let dhcp_interface in config.dhcp_interface) + push(triggers, procd_interface_trigger(dhcp_interface)); + } else { + push(triggers, procd_raw_trigger("interface.*", [ '/etc/init.d/' + script_name, 'reload' ], 1000)); + } } - config_get_bool enable_server ntp enable_server 0 - config_get interface ntp interface - - [ $enable_server -eq 1 ] && [ -n "$interface" ] && { - local ifname + if (config.interface && config.enable_server) + procd_interface_trigger(procd_get_network_devices(config.interface)); - network_get_device ifname "$interface" || \ - ifname="$interface" - procd_add_interface_trigger "interface.*" "$ifname" \ - /etc/init.d/"$name" reload - } - - procd_add_validation validate_ntp_section + return triggers; } + +procd_service('ntpd', { + start: 98, + instances, + service_triggers, +}); -- 2.30.2