miniupnpd: convert to procd
authorRosen Penev <rosenp@gmail.com>
Fri, 7 Feb 2020 05:47:58 +0000 (21:47 -0800)
committerRosen Penev <rosenp@gmail.com>
Fri, 17 Apr 2020 22:21:35 +0000 (15:21 -0700)
Massive cleanup of the shell script. Many issues were fixed.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
(cherry picked from commit c61614a849d1c7fc0cb6d0dafd79440a35bb5fc6)

net/miniupnpd/Makefile
net/miniupnpd/files/miniupnpd.hotplug
net/miniupnpd/files/miniupnpd.init
net/miniupnpd/files/upnpd.config
net/miniupnpd/patches/100-no-daemon.patch [new file with mode: 0644]

index c35c4318aeb7a71b6597e40fbae0b5ded032186d..d3cbf2981cff07744af7c1a70d32138a7ae9c1a1 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=miniupnpd
 PKG_VERSION:=2.1.20191006
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 
 PKG_SOURCE_URL:=https://miniupnp.tuxfamily.org/files
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
index 9820d42543e3d441fb561d56c50e65048b954da2..9e525f9b5f799fa609e0ad862e68b22adb62b7c0 100644 (file)
@@ -1,8 +1,6 @@
-#!/bin/sh
+#!/bin/sh /etc/rc.common
 
-/etc/init.d/miniupnpd enabled || exit 0
-
-. /lib/functions/service.sh
+enabled miniupnpd || exit 0
 
 # If miniupnpd is not running:
 # - check on _any_ event (even updates may contribute to network_find_wan*)
@@ -11,7 +9,7 @@
 # - check only on ifup (otherwise lease updates etc would cause
 #   miniupnpd state loss)
 
-[ "$ACTION" != "ifup" ] && service_check /usr/sbin/miniupnpd && exit 0
+[ "$ACTION" != "ifup" ] && service_running miniupnpd && exit 0
 
 tmpconf="/var/etc/miniupnpd.conf"
 extiface=$(uci get upnpd.config.external_iface)
@@ -19,15 +17,13 @@ extzone=$(uci get upnpd.config.external_zone)
 
 . /lib/functions/network.sh
 
-if [ -z "$extiface" ] ; then
+[ -z "$extiface" ] && {
   # manual external zone (if dynamically find interfaces
   # belonging to it) overrides network_find_wan*
-  if [ -n "$extzone" ] ; then
-    ifname=$(fw3 -q zone $extzone | head -1)
-  fi
-  [ -n "$extiface" ] || network_find_wan extiface
-  [ -n "$extiface" ] || network_find_wan6 extiface
-fi
-
-[ -n "$ifname" ] || network_get_device ifname "$extiface"
+  [ -n "$extzone" ] && ifname=$(fw3 -q zone "$extzone" | head -1)
+  [ -z "$extiface" ] && network_find_wan extiface
+  [ -z "$extiface" ] && network_find_wan6 extiface
+}
+
+[ -z "$ifname" ] && network_get_device ifname "$extiface"
 grep -q "ext_ifname=$ifname" "$tmpconf" || /etc/init.d/miniupnpd restart
index 9a85d0078bcf891b93e233e76a24284000527e3e..01434e9153304ae78b13a6c4c7808424da891808 100644 (file)
@@ -3,8 +3,8 @@
 
 START=94
 STOP=15
-
-SERVICE_USE_PID=1
+USE_PROCD=1
+PROG=/usr/sbin/miniupnpd
 
 upnpd_get_port_range() {
        local var="$1"; shift
@@ -26,9 +26,8 @@ upnpd_get_port_range() {
 
 conf_rule_add() {
        local cfg="$1"
-       local tmpconf="$2"
-       local action external_port_start external_port_end int_addr
-       local internal_port_start internal_port_end comment
+       local action int_addr
+       local ext_start ext_end int_start int_end comment
 
        config_get action "$cfg" action "deny"                # allow or deny
        upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
@@ -39,7 +38,7 @@ conf_rule_add() {
        # Make a single IP IP/32 so that miniupnpd.conf can use it.
        [ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
 
-       echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment" >>$tmpconf
+       echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment"
 }
 
 upnpd_write_bool() {
@@ -50,161 +49,145 @@ upnpd_write_bool() {
 
        config_get_bool val config "$opt" "$def"
        if [ "$val" -eq 0 ]; then
-               echo "$alt=no" >> $tmpconf
+               echo "$alt=no"
        else
-               echo "$alt=yes" >> $tmpconf
+               echo "$alt=yes"
        fi
 }
 
-boot() {
-       return
-}
-
-start() {
+upnpd() {
        config_load "upnpd"
-       local extiface intiface upload download logging secure enabled natpmp
-       local extip port usesysuptime conffile serial_number model_number
-       local uuid notify_interval presentation_url enable_upnp
+       local external_iface external_iface6 external_zone external_ip internal_iface
+       local upload download log_output port config_file serial_number model_number
+       local use_stun stun_host stun_port uuid notify_interval presentation_url
        local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
-       local ipv6_listening_ip enabled
 
+       local enabled
        config_get_bool enabled config enabled 1
-
        [ "$enabled" -eq 0 ] && return 1
 
-       config_get extiface config external_iface
-       config_get extzone config external_zone
-       config_get intiface config internal_iface
-       config_get extip config external_ip
+       config_get external_iface config external_iface
+       config_get external_zone config external_zone
+       config_get external_ip config external_ip
+       config_get internal_iface config internal_iface
        config_get port config port 5000
        config_get upload config upload
        config_get download config download
-       config_get_bool logging config log_output 0
-       config_get conffile config config_file
+       config_get_bool log_output config log_output 0
+       config_get config_file config config_file
        config_get serial_number config serial_number
        config_get model_number config model_number
        config_get uuid config uuid
+       config_get stun_host config stun_host
+       config_get stun_port config stun_port
        config_get notify_interval config notify_interval
        config_get presentation_url config presentation_url
        config_get upnp_lease_file config upnp_lease_file
        config_get clean_ruleset_threshold config clean_ruleset_threshold
        config_get clean_ruleset_interval config clean_ruleset_interval
-       config_get ipv6_listening_ip config ipv6_listening_ip
 
-       local args ifname
+       local conf ifname ifname6
 
        . /lib/functions/network.sh
 
-        # manual external interface overrides everything
-        if [ -z "$extiface" ] ; then
-            # manual external zone (if dynamically find interfaces
-            # belonging to it) overrides network_find_wan*
-            if [ -n "$extzone" ] ; then
-                ifname=$(fw3 -q zone $extzone | head -1)
-            fi
-            [ -n "$extiface" ] || network_find_wan extiface
-            [ -n "$extiface" ] || network_find_wan6 extiface
-        fi
-
-       [ -n "$ifname" ] || network_get_device ifname $extiface
-
-       if [ -n "$conffile" ]; then
-               args="-f $conffile"
+       # manual external interface overrides everything
+       [ -z "$external_iface" ] && {
+               # manual external zone (if dynamically find interfaces
+               # belonging to it) overrides network_find_wan*
+               [ -n "$external_zone" ] && ifname=$(fw3 -q zone "$external_zone" | head -1)
+               [ -z "$external_iface" ] && network_find_wan external_iface
+               [ -z "$external_iface6" ] && network_find_wan6 external_iface6
+       }
+
+       [ -z "$ifname" ] && network_get_device ifname "$external_iface"
+       [ -z "$ifname6" ] && network_get_device ifname6 "$external_iface6"
+
+       if [ -n "$config_file" ]; then
+               conf="$config_file"
        else
                local tmpconf="/var/etc/miniupnpd.conf"
-               args="-f $tmpconf"
+               conf="$tmpconf"
                mkdir -p /var/etc
 
-               echo "ext_ifname=$ifname" >$tmpconf
-
-               [ -n "$extip" ] && \
-                       echo "ext_ip=$extip" >>$tmpconf
+               {
+               echo "ext_ifname=$ifname"
+               echo "ext_ifname6=$ifname6"
+               [ -n "$external_ip" ] && echo "ext_ip=$external_ip"
 
                local iface
-               for iface in ${intiface:-lan}; do
+               for iface in ${internal_iface:-lan}; do
                        local device
-                       network_get_device device "$iface" && {
-                               echo "listening_ip=$device" >>$tmpconf
-                       }
+                       network_get_device device "$iface" && echo "listening_ip=$device"
+                       network_get_device device "$iface" && echo "ipv6_listening_ip=$device"
                done
 
-               [ "$port" != "auto" ] && \
-                       echo "port=$port" >>$tmpconf
-
                config_load "upnpd"
                upnpd_write_bool enable_natpmp 1
                upnpd_write_bool enable_upnp 1
                upnpd_write_bool secure_mode 1
-               upnpd_write_bool pcp_allow_thirdparty 0
                upnpd_write_bool system_uptime 1
                upnpd_write_bool igdv1 0 force_igd_desc_v1
+               upnpd_write_bool use_stun 0 ext_perform_stun
 
-               [ -n "$upnp_lease_file" ] && \
-                       echo "lease_file=$upnp_lease_file" >>$tmpconf
-
-               [ -n "$upload" -a -n "$download" ] && {
-                       echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
-                       echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
+               [ "$use_stun" -eq 0 ] || {
+                       [ -n "$stun_host" ] && echo "ext_stun_host=$stun_host"
+                       [ -n "$stun_port" ] && echo "ext_stun_port=$stun_port"
                }
 
-               [ -n "${presentation_url}" ] && \
-                       echo "presentation_url=${presentation_url}" >>$tmpconf
-
-               [ -n "${notify_interval}" ] && \
-                       echo "notify_interval=${notify_interval}" >>$tmpconf
-
-               [ -n "${clean_ruleset_threshold}" ] && \
-                       echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
-
-               [ -n "${clean_ruleset_interval}" ] && \
-                       echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
+               [ -n "$upload" ] && [ -n "$download" ] && {
+                       echo "bitrate_down=$((download * 1024 * 8))"
+                       echo "bitrate_up=$((upload * 1024 * 8))"
+               }
 
-               [ -n "${ipv6_listening_ip}" ] && \
-                       echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
+               [ -n "$upnp_lease_file" ] && touch "$upnp_lease_file" && echo "lease_file=$upnp_lease_file"
+               [ -n "$presentation_url" ] && echo "presentation_url=$presentation_url"
+               [ -n "$notify_interval" ] && echo "notify_interval=$notify_interval"
+               [ -n "$clean_ruleset_threshold" ] && echo "clean_ruleset_threshold=$clean_ruleset_threshold"
+               [ -n "$clean_ruleset_interval" ] && echo "clean_ruleset_interval=$clean_ruleset_interval"
+               [ -n "$serial_number" ] && echo "serial=$serial_number"
+               [ -n "$model_number" ] && echo "model_number=$model_number"
+               [ -n "$port" ] && echo "port=$port"
 
                [ -z "$uuid" ] && {
                        uuid="$(cat /proc/sys/kernel/random/uuid)"
-                       uci set upnpd.config.uuid=$uuid
+                       uci set upnpd.config.uuid="$uuid"
                        uci commit upnpd
                }
 
-               [ "$uuid" = "nocli" ] || \
-                       echo "uuid=$uuid" >>$tmpconf
-
-               [ -n "${serial_number}" ] && \
-                       echo "serial=${serial_number}" >>$tmpconf
+               [ "$uuid" = "nocli" ] || echo "uuid=$uuid"
 
-               [ -n "${model_number}" ] && \
-                       echo "model_number=${model_number}" >>$tmpconf
+               config_foreach conf_rule_add perm_rule
 
-               config_foreach conf_rule_add perm_rule "$tmpconf"
+               } > "$tmpconf"
        fi
 
-
        if [ -n "$ifname" ]; then
                # start firewall
                iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
-
-               if [ "$logging" = "1" ]; then
-                       SERVICE_DAEMONIZE=1 \
-                       service_start /usr/sbin/miniupnpd $args -d
-               else
-                       SERVICE_DAEMONIZE= \
-                       service_start /usr/sbin/miniupnpd $args
-               fi
        else
                logger -t "upnp daemon" "external interface not found, not starting"
        fi
-}
 
-stop() {
-       service_stop /usr/sbin/miniupnpd
+       procd_open_instance
+       procd_set_param command "$PROG"
+       procd_append_param command -f "$conf"
+       [ "$log_output" = "1" ] && procd_append_param command -d
+       procd_close_instance
+}
 
+stop_service() {
        iptables -t nat -F MINIUPNPD 2>/dev/null
        iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
        iptables -t filter -F MINIUPNPD 2>/dev/null
 
-        [ -x /usr/sbin/ip6tables ] && {
-           ip6tables -t filter -F MINIUPNPD 2>/dev/null
-        }
+       [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
+}
+
+start_service() {
+       config_load "upnpd"
+       config_foreach upnpd "upnpd"
+}
+
+service_triggers() {
+       procd_add_reload_trigger "upnpd"
 }
index 299664362dd420f1adfaef8e944c0122ec74b608..4e74664953991e200a7eed0284850b06cf2bc5c6 100644 (file)
@@ -4,8 +4,8 @@ config upnpd config
        option enable_upnp      1
        option secure_mode      1
        option log_output       0
-       option download         1024
-       option upload           512
+       option download         1024
+       option upload           512
 #by default, looked up dynamically from ubus
 #      option external_iface   wan
        option internal_iface   lan
diff --git a/net/miniupnpd/patches/100-no-daemon.patch b/net/miniupnpd/patches/100-no-daemon.patch
new file mode 100644 (file)
index 0000000..3d51fca
--- /dev/null
@@ -0,0 +1,25 @@
+--- a/miniupnpd.c
++++ b/miniupnpd.c
+@@ -1727,21 +1727,7 @@ init(int argc, char * * argv, struct runtime_vars * v)
+               }
+       }
+-      if(debug_flag)
+-      {
+-              pid = getpid();
+-      }
+-      else
+-      {
+-#ifdef USE_DAEMON
+-              if(daemon(0, 0)<0) {
+-                      perror("daemon()");
+-              }
+-              pid = getpid();
+-#else
+-              pid = daemonize();
+-#endif
+-      }
++      pid = getpid();
+       openlog_option = LOG_PID|LOG_CONS;
+       if(debug_flag)