# [WORKAROUND] net.wlan0 doesn't wait for dhcpcd

## Antti Alien

Hi! I just bought a new usb wlan card, and I even noticed that there's a new driver for it in the 2.6.37 kernel. Everything works very well, but it seems that the init script is too hasty to wait for an IP address, which causes problems with ntp.

When net.wlan0 (symlink to net.lo) is started, it outputs (with RC_VERBOSE="yes"):

```

 * Caching service dependencies ...                                       [ ok ]

 * Bringing up interface wlan0

 *   Loaded modules: apipa arping bonding tuntap ccwgroup macchanger macnet wpa_supplicant ssidnet ifconfig pppd system dhcpcd ip6to4

 *   Configuring wlan0 for MAC address 00:1a:9f:93:1a:79 ...              [ ok ]

 *   Starting wpa_supplicant on wlan0 ...

 *   Detaching to start `/usr/sbin/wpa_supplicant' ...                    [ ok ]

 *   Starting wpa_cli on wlan0 ...

 *   Detaching to start `/usr/bin/wpa_cli' ...                            [ ok ]

 *   Backgrounding ... ...

 * WARNING: net.wlan0 has started, but is inactive

```

At that point, dhcpcd isn't even running yet, but something waits until wpa_supplicant has associated with the access-point, and after that dhcpcd gets run in the background. Now, because the network init script returns almost immediately, ntpd gets run before an IP address has been acquired.

So, how can I tell net.wlan0 script to wait for dhcpcd?

/etc/conf.d/net:

```

modules=( "wpa_supplicant" )

wpa_supplicant_wlan0="-Dwext"

preferred_aps=( "v1b28" )

associate_order=( "preferredonly" )

config_wlan0=( "dhcp" )

enable_ipv6_wlan0="false"

auto_wlan0="true"

```

/etc/wpa_supplicant/wpa_supplicant.conf:

```

ctrl_interface=/var/run/wpa_supplicant

ctrl_interface_group=0

ap_scan=1

network={

   ssid="v1b28"

   proto=RSN

   key_mgmt=WPA-PSK

   pairwise=CCMP TKIP

   group=CCMP TKIP

   psk="secret"

}

```

Last edited by Antti Alien on Fri Jan 07, 2011 3:02 pm; edited 1 time in total

----------

## cdstealer

I have this line in my /etc/conf.d/net

```
dhcpcd_wlan0="-t 10"
```

hope it helps.

----------

## Antti Alien

Unfortunately it didn't. Adding that line only made dhcpcd run with -t 10, but it still executes in the background after net.wlan0 has already returned and ntpd has tried to start.

----------

## cdstealer

I have amended the ntp-client init script to wait for dhcp on my box at home.  I'll post it when I get home.

It tests for connectivity and only starts ntp once connected or ntp is skipped after 30 seconds.

----------

## Antti Alien

I got the exact same idea  :Smile:  Here's my solution:

```
wait_for_ip() {

        einfo "Waiting for network connection ..."

        local i=0

        while [ $i -lt 30 ]; do

                if ping -c 1 192.168.0.1 &> /dev/null; then

                        return 0

                else

                        let i=i+1

                fi

                sleep 1

        done

        return 1

}
```

...and then in start():

```
wait_for_ip && "${NTPCLIENT_CMD}" ${NTPCLIENT_OPTS}
```

Just a quick hack, so there might be better ways to do this. Nevertheless it works. I'm marking this partially solved, in case someone knows the Real(tm) solution.

----------

## cdstealer

kudos to you  :Smile:   I had the following which works for me.

```
#!/sbin/runscript

# Copyright 1999-2007 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Header: /var/cvsroot/gentoo-x86/net-misc/ntp/files/ntp-client.rc,v 1.11 2007/03/09 17:24:28 vapier Exp $

depend() {

        before cron portmap

        need net

        use dns logger

}

checkconfig() {

        if ! type "${NTPCLIENT_CMD}" >/dev/null 2>/dev/null ; then

                eerror "Please edit /etc/conf.d/ntp-client"

                eerror "Unable to locate the client command ${NTPCLIENT_CMD}!"

                return 1

        fi

        if [ -z "${NTPCLIENT_OPTS}" ] ; then

                eerror "Please edit /etc/conf.d/ntp-client"

                eerror "I need to know what server/options to use!"

                return 1

        fi

        return 0

}

start() {

        checkconfig || return $?

        A="0"

        B="0"

        until [ "$A" -eq 1 ]; do

        echo "waiting for connectivity..."

        A=`ifconfig | grep "inet addr"|grep 192|cut -d':' -f2|sed 's/\ Bcast//g'|wc -l`

        sleep 1

        let B=$[$B+1]

        if [ $B -ge 30 ]; then eend $? "Failed to set clock" && exit 1

        elif [ "$A" -eq 1 ]; then ebegin "Setting clock via the NTP client '${NTPCLIENT_CMD}'"

        "${NTPCLIENT_CMD}" ${NTPCLIENT_OPTS}

        fi

        done

}
```

----------

