# xinetd - the extended Internet services daemon
description	"xinetd daemon"

start on runlevel [2345]
stop on runlevel [!2345]

## because signal QUIT is sent in pre-stop, respawn cannot be used.
## (LP: #605007)
#respawn

pre-start script
    test -x /usr/sbin/xinetd || { stop; exit 0; }
end script

env PIDFILE=/var/run/xinetd.pid
script
  INETD_COMPAT=Yes
  XINETD_OPTS="-stayalive"

  # per upstart convention, options should be set in the upstart job itself.
  # However, to be friendly, source and respect old defaults file.
  [ ! -f /etc/default/xinetd ] || . /etc/default/xinetd

  case "$INETD_COMPAT" in
    [Yy]*)
      XINETD_OPTS="$XINETD_OPTS -inetd_compat"
      if perl -MSocket -e 'exit (!socket($sock, AF_INET6, SOCK_STREAM, 0))'; then
        XINETD_OPTS="$XINETD_OPTS -inetd_ipv6"
      fi
      ;;
  esac

  checkportmap () {
    if grep "^[^ *#]" /etc/xinetd.conf | grep -q 'rpc/'; then
      if ! rpcinfo -u localhost portmapper >/dev/null 2>&1; then
        echo
        echo "WARNING: portmapper inactive - RPC services unavailable!"
        echo "    Commenting out or removing the RPC services from"
        echo "    the /etc/xinetd.conf file will remove this message."
        echo
      fi
    fi
  }

  checkportmap
  exec /usr/sbin/xinetd -dontfork -pidfile "$PIDFILE" $XINETD_OPTS
end script

pre-stop script
  xinetd_pid=$(status | awk '/stop\/pre-stop/ { print $NF }')
  [ -n "${xinetd_pid}" ] || exit 0
  kill -QUIT "${xinetd_pid}"
end script
