Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 650584 - sys-apps/openrc: net-online when host is unreachable ping waits for 3 seconds but only one second is counted
Summary: sys-apps/openrc: net-online when host is unreachable ping waits for 3 seconds...
Status: CONFIRMED
Alias: None
Product: Gentoo Hosted Projects
Classification: Unclassified
Component: OpenRC (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: OpenRC Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-15 17:45 UTC by nnnn20430
Modified: 2022-01-14 02:40 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description nnnn20430 2018-03-15 17:45:52 UTC
In init.d/net-online "ping -c 1 $ping_test_host" command waits for 3 seconds when host is unreachable but only 1 second is subtracted from the timeout variable ": $((timeout -= 1))".

It seems to wait for 3 seconds based on value of "mcast_solicit" (/proc/sys/net/ipv4/neigh/$IFNAME/mcast_solicit) which defaults to 3. Instead of assuming it to always be the default we can simply calculate how long the command took:

ping_start="$(date +%s)"
ping -c 1 $ping_test_host > /dev/null 2>&1
rc=$?
ping_end="$(date +%s)"
[ $rc -eq 0 ] && break
: $((timeout -= (ping_end - ping_start)))
Comment 1 pva 2021-07-03 08:47:12 UTC
(In reply to nnnn20430 from comment #0)
> In init.d/net-online "ping -c 1 $ping_test_host" command waits for 3 seconds

By default, ping makes a reverse DNS request for every ping reply, so my guess is that it is DNS reply that ping waits for 3 seconds. So to fix this problem it is required to add -n for ping command.


Yet we have another bug here. If ping receives reply very fast script waits much less that timeout value. In my case it fails with error less then second, while I put 120 seconds of timeout:

=======================================================
 ~ # time /etc/init.d/net-online start
 * Caching service dependencies ...                                                                                                         [ ok ]
 * Checking to see if the network is online ...
 * The network is offline                                                                                                                   [ !! ]
 * ERROR: net-online failed to start

real	0m0.938s
user	0m0.669s
sys	0m0.185s
=======================================================

To fix we need to add sleep 1 before we subtract second with the following line:
: $((timeout -= 1))
Comment 2 pva 2021-07-03 08:49:18 UTC
Patch to fix both issues:

--- net-online	2021-07-03 11:47:59.916318058 +0300
+++ net-online	2021-07-03 11:47:52.406308782 +0300
@@ -65,9 +65,10 @@
         ping_test_host="${ping_test_host:-google.com}"
         if [ -n "$ping_test_host" ]; then
                 while $infinite || [ $timeout -gt 0 ]; do
-                        ping -c 1 $ping_test_host > /dev/null 2>&1
+                        ping -n -c 1 $ping_test_host > /dev/null 2>&1
                         rc=$?
                         [ $rc -eq 0 ] && break
+                        sleep 1
                         : $((timeout -= 1))
                 done
         fi
Comment 3 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-01-14 02:40:50 UTC
See https://github.com/OpenRC/openrc/commit/e21b01b97e84f81589f52e087d2b2ac53f0bf144 but maybe we want -n too.