Morphy99
Posts: 14
Joined: Mon Jan 13, 2014 9:51 pm

Hostapd starting before Dnsmasq is ready

Sun Feb 05, 2017 4:25 pm

Hi

I've set up a hotspot on my raspbian lite install however am having an issue with immediate authentication after boot. I get authentication failures until dnsmasq is started completely. Is there a way to delay hostapd to run after dnsmasq? They're both starting from init.d scripts.

Code: Select all

 sudo /etc/init.d/dnsmasq status -l
â— dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
   Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled)
  Drop-In: /run/systemd/generator/dnsmasq.service.d
           └─50-dnsmasq-$named.conf, 50-insserv.conf-$named.conf
   Active: active (running) since Sun 2017-02-05 16:18:53 GMT; 58s ago
  Process: 539 ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf (code=exited, status=0/SUCCESS)
  Process: 486 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=0/SUCCESS)
  Process: 393 ExecStartPre=/usr/sbin/dnsmasq --test (code=exited, status=0/SUCCESS)
 Main PID: 537 (dnsmasq)
   CGroup: /system.slice/dnsmasq.service
           └─537 /usr/sbin/dnsmasq -x /var/run/dnsmasq/dnsmasq.pid -u dnsmasq -r /var/run/dnsmasq/resolv.conf -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new -...

Feb 05 16:19:08 raspberrypi dnsmasq[537]: reading /var/run/dnsmasq/resolv.conf
Feb 05 16:19:08 raspberrypi dnsmasq[537]: using nameserver 8.8.8.8#53
Feb 05 16:19:08 raspberrypi dnsmasq[537]: using nameserver 212.159.6.10#53
Feb 05 16:19:08 raspberrypi dnsmasq[537]: using nameserver 212.159.6.9#53
Feb 05 16:19:11 raspberrypi dnsmasq-dhcp[537]: DHCPDISCOVER(wlan0) ac:37:43:xx:xx:xx
Feb 05 16:19:11 raspberrypi dnsmasq-dhcp[537]: DHCPOFFER(wlan0) 192.168.41.134 ac:37:43:xx:xx:xx
Feb 05 16:19:11 raspberrypi dnsmasq-dhcp[537]: DHCPREQUEST(wlan0) 192.168.41.134 ac:37:43:xx:xx:xx
Feb 05 16:19:11 raspberrypi dnsmasq-dhcp[537]: DHCPACK(wlan0) 192.168.41.134 ac:37:43:xx:xx:xx android-xxxxxxx
Feb 05 16:19:31 raspberrypi dnsmasq-dhcp[537]: DHCPREQUEST(wlan0) 192.168.41.110 6c:ad:f8:xx:xx:xx
Feb 05 16:19:31 raspberrypi dnsmasq-dhcp[537]: DHCPACK(wlan0) 192.168.41.110 6c:ad:f8:xx:xx:xx Chromecast 

Code: Select all

 sudo /etc/init.d/hostapd status -l
â— hostapd.service - LSB: Advanced IEEE 802.11 management daemon
   Loaded: loaded (/etc/init.d/hostapd)
   Active: active (running) since Sun 2017-02-05 16:18:53 GMT; 59s ago
  Process: 513 ExecStart=/etc/init.d/hostapd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/hostapd.service
           └─647 /usr/sbin/hostapd -B -P /run/hostapd.pid /etc/hostapd/hostapd.conf

Feb 05 16:18:52 raspberrypi systemd[1]: Starting LSB: Advanced IEEE 802.11 management daemon...
Feb 05 16:18:53 raspberrypi hostapd[513]: Starting advanced IEEE 802.11 management: hostapd.
Feb 05 16:18:53 raspberrypi systemd[1]: Started LSB: Advanced IEEE 802.11 management daemon.
Feb 05 16:18:57 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx IEEE 802.11: associated
Feb 05 16:18:58 raspberrypi hostapd[647]: wlan0: STA ac:37:43:xx:xx:xx IEEE 802.11: associated
Feb 05 16:19:02 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx IEEE 802.11: deauthenticated due to local deauth request
Feb 05 16:19:03 raspberrypi hostapd[647]: wlan0: STA ac:37:43:xx:xx:xx IEEE 802.11: deauthenticated due to local deauth request
Feb 05 16:19:07 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx IEEE 802.11: disassociated
Feb 05 16:19:08 raspberrypi hostapd[647]: wlan0: STA ac:37:43:4e:cc:e7 IEEE 802.11: disassociated
Feb 05 16:19:11 raspberrypi hostapd[647]: wlan0: STA ac:37:43:xx:xx:xx IEEE 802.11: associated
Feb 05 16:19:11 raspberrypi hostapd[647]: wlan0: STA ac:37:43:xx:xx:xx RADIUS: starting accounting session 5897506D-00000002
Feb 05 16:19:11 raspberrypi hostapd[647]: wlan0: STA ac:37:43:xx:xx:xx WPA: pairwise key handshake completed (RSN)
Feb 05 16:19:30 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx IEEE 802.11: associated
Feb 05 16:19:31 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx RADIUS: starting accounting session 5897506D-00000003
Feb 05 16:19:31 raspberrypi hostapd[647]: wlan0: STA 6c:ad:f8:xx:xx:xx WPA: pairwise key handshake completed (RSN)
Last edited by Morphy99 on Mon Feb 06, 2017 10:11 pm, edited 1 time in total.

pcmanbob
Posts: 4536
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Hostapd starting before Dnsmasq is ready

Sun Feb 05, 2017 9:14 pm

Hi

might be a good idea to post the scripts you are using to call both programs so people can see what you are doing.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Morphy99
Posts: 14
Joined: Mon Jan 13, 2014 9:51 pm

Re: Hostapd starting before Dnsmasq is ready

Mon Feb 06, 2017 10:10 pm

pcmanbob wrote:Hi

might be a good idea to post the scripts you are using to call both programs so people can see what you are doing.
Thanks for the reply. They're the standard startup scripts that get installed with both packages. I can post them if needs be but they're out of the box as it were.

pcmanbob
Posts: 4536
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Hostapd starting before Dnsmasq is ready

Tue Feb 07, 2017 5:56 pm

Hi.
The reason I ask is because I don't have this software running on any of my Pi's and I wanted to see if we could add a sleep line to the hostapd script to delay its start-up by 30 sec or so to allow the other program to finish loading.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Morphy99
Posts: 14
Joined: Mon Jan 13, 2014 9:51 pm

Re: Hostapd starting before Dnsmasq is ready

Fri Feb 10, 2017 7:49 pm

Ah ok yea that makes sense sorry.
Here's hostapd:

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides:             hostapd
# Required-Start:       $all
# Required-Stop:        $remote_fs
# Should-Start:         $network
# Should-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Advanced IEEE 802.11 management daemon
# Description:          Userspace IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP
#                       Authenticator
### END INIT INFO
wait=30
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd
DAEMON_DEFS=/etc/default/hostapd
DAEMON_CONF=
NAME=hostapd
DESC="advanced IEEE 802.11 management"
PIDFILE=/run/hostapd.pid

[ -x "$DAEMON_SBIN" ] || exit 0
[ -s "$DAEMON_DEFS" ] && . /etc/default/hostapd
[ -n "$DAEMON_CONF" ] || exit 0

DAEMON_OPTS="-B -P $PIDFILE $DAEMON_OPTS $DAEMON_CONF"

. /lib/lsb/init-functions

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
        log_end_msg "$?"
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        start-stop-daemon --stop --signal HUP --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  restart|force-reload)
        $0 stop
        sleep 8
        $0 start
        ;;
  status)
        status_of_proc "$DAEMON_SBIN" "$NAME"
        exit $?
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
        exit 1
        ;;
esac

exit 0
And dnsmasq although I gather its hostapd I need to delay:

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides:       dnsmasq
# Required-Start: $hostapd $network $remote_fs $syslog
# Required-Stop:  $network $remote_fs $syslog
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    DHCP and DNS server
### END INIT INFO

set +e   # Don't exit on error status

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/dnsmasq
NAME=dnsmasq
DESC="DNS forwarder and DHCP server"

# Most configuration options in /etc/default/dnsmasq are deprecated
# but still honoured.
ENABLED=1
if [ -r /etc/default/$NAME ]; then
        . /etc/default/$NAME
fi

# Get the system locale, so that messages are in the correct language, and the
# charset for IDN is correct
if [ -r /etc/default/locale ]; then
        . /etc/default/locale
        export LANG
fi

# /etc/dnsmasq.d/README is a non-conffile installed by the dnsmasq package.
# Should the dnsmasq package be removed, the following test ensures that
# the daemon is no longer started, even if the dnsmasq-base package is
# still in place.
test -e /etc/dnsmasq.d/README || exit 0

test -x $DAEMON || exit 0

# Provide skeleton LSB log functions for backports which don't have LSB functions.
if [ -f /lib/lsb/init-functions ]; then
         . /lib/lsb/init-functions
else
         log_warning_msg () {
            echo "${@}."
         }

         log_success_msg () {
            echo "${@}."
         }

         log_daemon_msg () {
            echo -n "${1}: $2"
         }

         log_end_msg () {
            if [ $1 -eq 0 ]; then
              echo "."
            elif [ $1 -eq 255 ]; then
              /bin/echo -e " (warning)."
            else
              /bin/echo -e " failed!"
            fi
         }
fi

# RESOLV_CONF:
# If the resolvconf package is installed then use the resolv conf file
# that it provides as the default.  Otherwise use /etc/resolv.conf as
# the default.
#
# If IGNORE_RESOLVCONF is set in /etc/default/dnsmasq or an explicit
# filename is set there then this inhibits the use of the resolvconf-provided
# information.
#
# Note that if the resolvconf package is installed it is not possible to
# override it just by configuration in /etc/dnsmasq.conf, it is necessary
# to set IGNORE_RESOLVCONF=yes in /etc/default/dnsmasq.

if [ ! "$RESOLV_CONF" ] &&
 [ -x /sbin/resolvconf ]
then
        RESOLV_CONF=/var/run/dnsmasq/resolv.conf
fi

for INTERFACE in $DNSMASQ_INTERFACE; do
        DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -i $INTERFACE"
done

for INTERFACE in $DNSMASQ_EXCEPT; do
        DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -I $INTERFACE"
done

if [ ! "$DNSMASQ_USER" ]; then
   DNSMASQ_USER="dnsmasq"
fi

# This tells dnsmasq to ignore DNS requests that don't come from a local network.
# It's automatically ignored if  --interface --except-interface, --listen-address
# or --auth-server exist in the configuration, so for most installations, it will
# have no effect, but for otherwise-unconfigured installations, it stops dnsmasq
# from being vulnerable to DNS-reflection attacks.

DNSMASQ_OPTS="$DNSMASQ_OPTS --local-service"

# If the dns-root-data package is installed, then the trust anchors will be
# available in $ROOT_DS, in BIND zone-file format. Reformat as dnsmasq
# --trust-anchor options.

ROOT_DS="/usr/share/dns/root.ds"

if [ -f $ROOT_DS ]; then
   DNSMASQ_OPTS="$DNSMASQ_OPTS `sed -e s/". IN DS "/--trust-anchor=.,/ -e s/" "/,/g $ROOT_DS | tr '\n' ' '`"
fi

start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        # /var/run may be volatile, so we need to ensure that
        # /var/run/dnsmasq exists here as well as in postinst
        if [ ! -d /var/run/dnsmasq ]; then
           mkdir /var/run/dnsmasq || return 2
           chown dnsmasq:nogroup /var/run/dnsmasq || return 2
        fi

        start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null || return 1
        start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON -- \
                -x /var/run/dnsmasq/$NAME.pid \
                ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
                ${MAILTARGET:+ -t $MAILTARGET} \
                ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
                ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
                ${DHCP_LEASE:+ -l $DHCP_LEASE} \
                ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
                ${RESOLV_CONF:+ -r $RESOLV_CONF} \
                ${CACHESIZE:+ -c $CACHESIZE} \
                ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
                ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} \
                || return 2
}

start_resolvconf()
{
# If interface "lo" is explicitly disabled in /etc/default/dnsmasq
# Then dnsmasq won't be providing local DNS, so don't add it to
# the resolvconf server set.
        for interface in $DNSMASQ_EXCEPT
        do
                [ $interface = lo ] && return
        done

        if [ -x /sbin/resolvconf ] ; then
                echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.$NAME
        fi
        return 0
}
stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile /var/run/dnsmasq/$NAME.pid --name $NAME
}

stop_resolvconf()
{
        if [ -x /sbin/resolvconf ] ; then
                /sbin/resolvconf -d lo.$NAME
        fi
        return 0
}

status()
{
        # Return
        #   0 if daemon is running
        #   1 if daemon is dead and pid file exists
        #   3 if daemon is not running
        #   4 if daemon status is unknown
        start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null
        case "$?" in
                0) [ -e "/var/run/dnsmasq/$NAME.pid" ] && return 1 ; return 3 ;;
                1) return 0 ;;
                *) return 4 ;;
        esac
}

case "$1" in
  start)
        test "$ENABLED" != "0" || exit 0
        log_daemon_msg "Starting $DESC" "$NAME"
        start
        case "$?" in
                0)
                        log_end_msg 0
                        start_resolvconf
                        exit 0
                        ;;
                1)
                        log_success_msg "(already running)"
                        exit 0
                        ;;
                *)
                        log_end_msg 1
                        exit 1
                        ;;
        esac
 ;;
  stop)
        stop_resolvconf
        if [ "$ENABLED" != "0" ]; then
             log_daemon_msg "Stopping $DESC" "$NAME"
        fi
        stop
        RETVAL="$?"
        if [ "$ENABLED" = "0" ]; then
            case "$RETVAL" in
               0) log_daemon_msg "Stopping $DESC" "$NAME"; log_end_msg 0 ;;
            esac
            exit 0
        fi
        case "$RETVAL" in
                0) log_end_msg 0 ; exit 0 ;;
                1) log_warning_msg "(not running)" ; exit 0 ;;
                *) log_end_msg 1; exit 1 ;;
        esac
        ;;
  restart|force-reload)
        test "$ENABLED" != "0" || exit 1
        $DAEMON --test ${CONFIG_DIR:+ -7 $CONFIG_DIR} ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            NAME="configuration syntax check"
            RETVAL="2"
        else
            stop_resolvconf
            stop
            RETVAL="$?"
        fi
        log_daemon_msg "Restarting $DESC" "$NAME"
        case "$RETVAL" in
                0|1)
                        sleep 2
                        start
                        case "$?" in
                                0)
                                        log_end_msg 0
                                        start_resolvconf
                                        exit 0
                                        ;;
                                *)
                                        log_end_msg 1
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        log_end_msg 1
                        exit 1
                        ;;
        esac
        ;;
 status)
        log_daemon_msg "Checking $DESC" "$NAME"
        status
        case "$?" in
                0) log_success_msg "(running)" ; exit 0 ;;
                1) log_success_msg "(dead, pid file exists)" ; exit 1 ;;
                3) log_success_msg "(not running)" ; exit 3 ;;
                *) log_success_msg "(unknown)" ; exit 4 ;;
        esac
        ;;
  dump-stats)
        kill -s USR1 `cat /var/run/dnsmasq/$NAME.pid`
        ;;
  systemd-start-resolvconf)
        start_resolvconf
        ;;
  systemd-stop-resolvconf)
        stop_resolvconf
        ;;
  systemd-exec)
# /var/run may be volatile, so we need to ensure that
        # /var/run/dnsmasq exists here as well as in postinst
        if [ ! -d /var/run/dnsmasq ]; then
           mkdir /var/run/dnsmasq || return 2
           chown dnsmasq:nogroup /var/run/dnsmasq || return 2
        fi
        exec $DAEMON -x /var/run/dnsmasq/$NAME.pid \
 ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
            ${MAILTARGET:+ -t $MAILTARGET} \
            ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
            ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
            ${DHCP_LEASE:+ -l $DHCP_LEASE} \
            ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
            ${RESOLV_CONF:+ -r $RESOLV_CONF} \
            ${CACHESIZE:+ -c $CACHESIZE} \
            ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
            ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
        ;;
  *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|dump-stats|status}" >&2
        exit 3
        ;;
esac

exit 0

pcmanbob
Posts: 4536
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Hostapd starting before Dnsmasq is ready

Fri Feb 10, 2017 8:08 pm

Morphy99 wrote:

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides:             hostapd
# Required-Start:       $all
# Required-Stop:        $remote_fs
# Should-Start:         $network
# Should-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Advanced IEEE 802.11 management daemon
# Description:          Userspace IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP
#                       Authenticator
### END INIT INFO
wait=30
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd
DAEMON_DEFS=/etc/default/hostapd
DAEMON_CONF=
NAME=hostapd
DESC="advanced IEEE 802.11 management"
PIDFILE=/run/hostapd.pid

[ -x "$DAEMON_SBIN" ] || exit 0
[ -s "$DAEMON_DEFS" ] && . /etc/default/hostapd
[ -n "$DAEMON_CONF" ] || exit 0

DAEMON_OPTS="-B -P $PIDFILE $DAEMON_OPTS $DAEMON_CONF"

. /lib/lsb/init-functions

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
        log_end_msg "$?"
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        start-stop-daemon --stop --signal HUP --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  restart|force-reload)
        $0 stop
        sleep 8
        $0 start
        ;;
  status)
        status_of_proc "$DAEMON_SBIN" "$NAME"
        exit $?
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
        exit 1
        ;;
esac

exit 0
HI.

now this is just a guess so cant be sure it will work, how about adding "sleep 30"
at the start of the script like this.

Code: Select all

#!/bin/sh
sleep 30
### BEGIN INIT INFO
that should delay the rest of the script by 30 sec giving Dnsmasq that bit of extra time to finish starting up.
if it works then you can fine turn the time delay as 30 sec may be to long. its worth a try if it does not work you have lost nothing.
I done this before with both bash scripts and python programs when I needed to make them wait for some other software to finish what it was doing..
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

SurferTim
Posts: 1767
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Hostapd starting before Dnsmasq is ready

Fri Feb 10, 2017 8:28 pm

I haven't used this in a while, but you can set dependencies in /etc/init.d/.depend.start
http://unix.stackexchange.com/questions ... -on-debian

edit: According to your Required-Start in dnsmasq, hostapd must be started first.

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides:       dnsmasq
# Required-Start: $hostapd $network $remote_fs $syslog

User avatar
elkberry
Posts: 167
Joined: Wed Dec 28, 2016 9:21 pm

Re: Hostapd starting before Dnsmasq is ready

Sat Feb 11, 2017 5:49 pm

Dependencies in systemd may not exactly work out you may want them to, because systemd often cannot tell when a service unit really is ready, but only when it has started and daemonized. My understanding is that this also applies to initd also to some extent, albeit aggressive parallelism in systemd may make this more prominent.

I had problems with the dependency of dhcpcd to wpa supplicant and hostap. A sleep wasn't really helping in my case with systemd. What I'm now doing with success is to configure the service unit to restart a service automatically after a few seconds in case it stopped. This helps for those services that like to terminate early when they detect they have yet nothing to do, instead of waiting. They then kick in a few seconds later, finding thr system state they expect. Not perfect, but may be a solution.
From ZX81 to Raspberry Pi, but wait ... where's the 7805 gone?

epoch1970
Posts: 2126
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Hostapd starting before Dnsmasq is ready

Sat Feb 11, 2017 6:58 pm

SurferTim wrote:edit: According to your Required-Start in dnsmasq, hostapd must be started first.

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides:       dnsmasq
# Required-Start: $hostapd $network $remote_fs $syslog
Right. The default init.d file for dnsmasq simply requires "$network $remote_fs $syslog". I think $remote_fs is overkill. Perhaps even $network because dnsmasq defaults to listening on lo.
I won't try messing with this myself because none of my Pis are currently hooked to a screen, but at least you could revert your change and return to the standard list of facilities.

If you don't "bind-interfaces" in dnsmasq.conf AFAIK you can start dnsmasq about any time you want. Or, since this is an AP, put the wifi interface in a bridge and have dnsmasq listen on the bridge. The bridge will be there before the wifi interface for sure.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Morphy99
Posts: 14
Joined: Mon Jan 13, 2014 9:51 pm

Re: Hostapd starting before Dnsmasq is ready

Sat Feb 18, 2017 11:21 am

pcmanbob wrote:
Morphy99 wrote:

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides:             hostapd
# Required-Start:       $all
# Required-Stop:        $remote_fs
# Should-Start:         $network
# Should-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Advanced IEEE 802.11 management daemon
# Description:          Userspace IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP
#                       Authenticator
### END INIT INFO
wait=30
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd
DAEMON_DEFS=/etc/default/hostapd
DAEMON_CONF=
NAME=hostapd
DESC="advanced IEEE 802.11 management"
PIDFILE=/run/hostapd.pid

[ -x "$DAEMON_SBIN" ] || exit 0
[ -s "$DAEMON_DEFS" ] && . /etc/default/hostapd
[ -n "$DAEMON_CONF" ] || exit 0

DAEMON_OPTS="-B -P $PIDFILE $DAEMON_OPTS $DAEMON_CONF"

. /lib/lsb/init-functions

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
        log_end_msg "$?"
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        start-stop-daemon --stop --signal HUP --exec "$DAEMON_SBIN" \
                --pidfile "$PIDFILE"
        log_end_msg "$?"
        ;;
  restart|force-reload)
        $0 stop
        sleep 8
        $0 start
        ;;
  status)
        status_of_proc "$DAEMON_SBIN" "$NAME"
        exit $?
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
        exit 1
        ;;
esac

exit 0
HI.

now this is just a guess so cant be sure it will work, how about adding "sleep 30"
at the start of the script like this.

Code: Select all

#!/bin/sh
sleep 30
### BEGIN INIT INFO
that should delay the rest of the script by 30 sec giving Dnsmasq that bit of extra time to finish starting up.
if it works then you can fine turn the time delay as 30 sec may be to long. its worth a try if it does not work you have lost nothing.
I done this before with both bash scripts and python programs when I needed to make them wait for some other software to finish what it was doing..
I thought adding a sleep would be the way but I wondering if there was more elegant way. Tried this anyway and a 15s seems to work great, maybe even less. Thanks :)

Return to “Networking and servers”