Debian Codegraph @

tests/ping-localhost.sh

raw
1 #!/bin/sh
2
3 # Copyright (C) 2009-2026 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU Inetutils.
6 #
7 # GNU Inetutils is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # GNU Inetutils is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see `http://www.gnu.org/licenses/'.
19
20 # Prerequisites:
21 #
22 # * Shell: SVR3 Bourne shell, or newer.
23 #
24 # * cut(1), id(1), uname(1).
25
26 . ./tools.sh
27
28 PROTOCOLS=/etc/protocols
29 if test ! -r $PROTOCOLS; then
30 cat <<-EOT >&2
31 This test requires the availability of "$PROTOCOLS",
32 a file which can not be found in the current system.
33 Therefore skipping this test.
34 EOT
35 exit 77
36 fi
37
38 PING=${PING:-../ping/ping$EXEEXT}
39 TARGET=${TARGET:-127.0.0.1}
40
41 PING6=${PING6:-../ping/ping6$EXEEXT}
42 TARGET6=${TARGET6:-::1}
43
44 if [ ! -x $PING ]; then
45 echo 'No executable "'$PING'" available. Skipping test.' >&2
46 exit 77
47 fi
48
49 if [ $VERBOSE ]; then
50 set -x
51 $PING --version
52 fi
53
54 if test "$TEST_IPV4" = "no" && test "$TEST_IPV6" = "no"; then
55 echo >&2 "Inet socket testing is disabled. Skipping test."
56 exit 77
57 fi
58
59 have_privs='no'
60 test "`func_id_uid`" = 0 && have_privs='yes'
61
62 # some systems allow ping without root privileges
63 need_privs='yes'
64
65 # the kernel Linux requires specific configuration for unprivileged ping
66 PING_GROUP_RANGE=/proc/sys/net/ipv4/ping_group_range
67 if test "`uname -s`" = 'Linux' && test -f "$PING_GROUP_RANGE"
68 then
69 low=`cut -f1 "$PING_GROUP_RANGE"`
70 high=`cut -f2 "$PING_GROUP_RANGE"`
71 for grp_id in `id -G`; do
72 test "$low" -le "$grp_id" && test "$high" -ge "$grp_id" &&
73 need_privs='no' && break
74 done
75 fi
76
77 # macOS generally allows unprivileged ping
78 test "`uname -s`" = 'Darwin' && need_privs='no'
79
80 if test "$need_privs" = 'yes' && test "$have_privs" = 'no'; then
81 echo >&2 "ping needs to run as root"
82 exit 77
83 fi
84
85 # the ping6 program still requires privileges
86 test "$need_privs" = 'no' && test "$have_privs" = 'no' &&
87 test "$TEST_IPV6" != 'no' && TEST_IPV6='no' &&
88 echo >&2 'ping6 needs to run as root, disabling IPv6 test' &&
89 test "$TEST_IPV4" = 'no' &&
90 echo >&2 'Testing of IPv4 and IPv6 is disabled. Skipping test.' &&
91 exit 77
92
93 errno=0
94 errno2=0
95
96 test "$TEST_IPV4" != "no" && test -x $PING &&
97 { $PING -n -c 1 $TARGET || errno=$?; }
98
99 test $errno -eq 0 || echo "Failed at pinging $TARGET." >&2
100
101 # Host might not have been built with IPv6 support.
102 test "$TEST_IPV6" != "no" && test -x $PING6 &&
103 { $PING6 -n -c 1 $TARGET6 || errno2=$?; }
104
105 test $errno2 -eq 0 || echo "Failed at pinging $TARGET6." >&2
106
107 test $errno -eq 0 || exit $errno
108
109 exit $errno2