restund: add procd init script
authorSebastian Kemper <[email protected]>
Tue, 26 Dec 2017 13:58:30 +0000 (14:58 +0100)
committerSebastian Kemper <[email protected]>
Tue, 26 Dec 2017 13:58:39 +0000 (14:58 +0100)
- add procd init script
- script starts the daemon as user "restund" instead of root
- add /etc/default/restund
- declare /etc/init.d/restund a configuration file
- prevent autostart after upgrade by using ENABLE_RESTUND variable in
  /etc/default/restund

Signed-off-by: Sebastian Kemper <[email protected]>
net/restund/Makefile
net/restund/files/restund.default [new file with mode: 0644]
net/restund/files/restund.init

index 28537e923f74500e5351de3d8d16bf25b7d229da..fa53d2e2fc70105fd7f7c9208248cb8c0bf20d34 100644 (file)
@@ -39,6 +39,7 @@ define Package/restund
 $(call Package/restund/Default)
   TITLE:=Modular STUN/TURN server
   DEPENDS:=+libre
+  USERID:=$(PKG_NAME)=373:$(PKG_NAME)=373
 endef
 
 restund-mod-mysql:=USE_MYSQL
@@ -78,14 +79,31 @@ define Package/restund/install
        $(SED) \
                's|^\(module_path\)\([ \t]\+\).*$$$$|\1\2/usr/lib/restund/modules|g' \
                $(1)/etc/restund.conf
+       $(INSTALL_DIR) $(1)/etc/default
+       $(INSTALL_CONF) ./files/restund.default $(1)/etc/default/restund
        $(INSTALL_DIR) $(1)/etc/init.d
        $(INSTALL_BIN) ./files/restund.init $(1)/etc/init.d/restund
 endef
 
 define Package/restund/conffiles
+/etc/default/restund
+/etc/init.d/restund
 /etc/restund.conf
 endef
 
+define Package/restund/postinst
+#!/bin/sh
+if [ -z "$${IPKG_INSTROOT}" ]; then
+  chown $(PKG_NAME):$(PKG_NAME) /etc/restund.conf
+
+  # Prevent $(PKG_NAME) from auto-starting after an upgrade. The modules may
+  # not be upgraded yet and the user configuration may need a revision.
+  sed -i '/^ENABLE_RESTUND="yes"/s/^/#/' \
+    /etc/default/$(PKG_NAME)
+fi
+exit 0
+endef
+
 #
 # 1. Name
 # 2. Title
diff --git a/net/restund/files/restund.default b/net/restund/files/restund.default
new file mode 100644 (file)
index 0000000..3e7cbc6
--- /dev/null
@@ -0,0 +1,8 @@
+### restund init configuration ###
+
+# Uncomment once you verified your configuration, otherwise the init script will
+# not start restund.
+#ENABLE_RESTUND="yes"
+
+# The following is added to the command line when starting restund:
+OPTIONS=""
index d6360ff83afaf26efb3e21255ab7b068ed9a51c4..ee722ca62e0635d9dcbe37c507fffc4d7c94d310 100644 (file)
@@ -1,15 +1,91 @@
 #!/bin/sh /etc/rc.common
-#
-# Copyright (C) 2010-2011 OpenWrt.org
-# Copyright (C) 2010 Alfred E. Heggestad
-#
+# Copyright (C) 2017 OpenWrt.org
 
 START=60
 
-start() {
-       service_start /usr/sbin/restund
+USE_PROCD=1
+
+#PROCD_DEBUG=1
+
+DAEMON=restund
+DEFAULT=/etc/default/$DAEMON
+LOGGER="/usr/bin/logger -p user.err -s -t $DAEMON"
+OPTIONS=
+PROG=/usr/sbin/$DAEMON
+TIMEOUT=30
+
+[ -f $DEFAULT ] && . $DEFAULT
+
+start_service() {
+  local dir=
+
+  if [ "$ENABLE_RESTUND" != yes ]; then
+    $LOGGER User configuration incomplete - not starting $DAEMON
+    $LOGGER Check ENABLE_RESTUND in $DEFAULT
+    exit 1
+  fi
+
+  procd_open_instance
+  procd_set_param command $PROG
+  procd_append_param command \
+    -n \
+    $OPTIONS
+  procd_set_param pidfile /var/run/${DAEMON}.pid
+  # forward stderr to logd
+  procd_set_param stderr 1
+  # forward stdout to logd
+  procd_set_param stdout 1
+  procd_set_param user $DAEMON
+  procd_close_instance
 }
 
-stop() {
-       service_stop /usr/sbin/restund
+stop_service() {
+  local retval=
+  local mypid=
+  local timeout=$TIMEOUT
+
+  pgrep $DAEMON &> /dev/null
+  [ $? -ne 0 ] && exit 0
+
+  [ -f /var/run/${DAEMON}.pid ]
+  retval=$?
+
+  # init script could find itself in a scenario where restund was started
+  # very recently, so make it wait a while for a pid file to appear
+  while [ $retval -ne 0 -a $timeout -gt 0 ]; do
+    sleep 1
+    [ -f /var/run/${DAEMON}.pid ]
+    retval=$?
+    timeout=$(($timeout-1))
+  done
+
+  [ $retval -eq 0 ] || {
+    $LOGGER PID file does not exist
+    exit 1
+  }
+
+  mypid=$(cat /var/run/${DAEMON}.pid)
+
+  [ "$mypid" -gt 1 ] 2> /dev/null || {
+    $LOGGER PID file contains garbage
+    exit 1
+  }
+
+  timeout=$TIMEOUT
+  kill $mypid 2>/dev/null
+  pgrep $DAEMON | grep -w $mypid &>/dev/null
+  retval=$?
+
+  while [ $retval -eq 0 -a $timeout -gt 0 ]; do
+    sleep 10
+    pgrep $DAEMON | grep -w $mypid &>/dev/null
+    retval=$?
+    [ $retval -eq 0 ] && kill $mypid 2>/dev/null
+    timeout=$(($timeout-10))
+  done
+
+  [ $retval -ne 1 ] && {
+    $LOGGER Failed to stop $DAEMON
+    exit 1
+  }
 }