Debian Codegraph @

tests/ts/hwclock/systohc

raw
1 #!/bin/bash
2
3 #
4 # Copyright (C) 2007 Karel Zak <[email protected]>
5 #
6 # This file is part of util-linux.
7 #
8 # This file is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License or
11 # (at your option) any later version.
12 #
13 # This file is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18
19 TS_TOPDIR="${0%/*}/../.."
20 TS_DESC="system to hw"
21 NTP_SERVER="0.fedora.pool.ntp.org"
22
23 . "$TS_TOPDIR"/functions.sh
24 ts_init "$*"
25
26 ts_check_test_command "$TS_CMD_HWCLOCK"
27
28 ts_skip_nonroot
29 if [ "$TRAVIS_DIST" == "precise" ]; then
30 ts_skip "https://github.com/util-linux/util-linux/issues/1082"
31 fi
32 if [ "$GITHUB_ACTIONS" == "true" ]; then
33 ts_skip "virtual machine"
34 fi
35
36 ts_check_prog "bc"
37 ts_check_prog "sntp"
38
39 function get_offset_sys_ntp
40 {
41 local ip="$@"
42 local out
43
44 # using hostname instead of IP could give us more than one offset
45 out=$(sntp --timeout 1 "$ip") || return 1
46
47 # sed must deliver a signed float or empty string for sure
48 out=$(echo "$out" | \
49 sed -n 's/.* \(\(+\|-\)[0-9]\{1,\}\.[0-9]\{1,\}\).*/\1/1p')
50
51 [ -n "$out" ] || return 1
52 echo "$out"
53 }
54
55 function check_diff_offset
56 {
57 local a=${1#+}
58 local b=${2#+}
59 local max="$3"
60 local tmp
61
62 tmp=$(echo "$a - $b" | bc | tr -d '-')
63 echo "$tmp"
64
65 tmp=$(echo "$tmp < $max" | bc)
66 [ $tmp -eq 1 ]
67 }
68
69
70 # we need fixed ntp IP to get comparable offsets
71 NTP_IP=$(ts_resolve_host "$NTP_SERVER") \
72 || ts_skip "can't resolve hostname $NTP_SERVER"
73
74 OFFSET_A=$(get_offset_sys_ntp "$NTP_IP") \
75 || ts_skip "can't get ntp offset 1st, $NTP_IP"
76 OFFSET_B=$(get_offset_sys_ntp "$NTP_IP") \
77 || ts_skip "can't get ntp offset 2nd, $NTP_IP"
78
79 diff=$(check_diff_offset $OFFSET_A $OFFSET_B 0.02) \
80 || ts_skip "unreliable ntp or sys clock offsets: $NTP_IP $OFFSET_A $OFFSET_B +/-$diff"
81
82 # hwclock --show should work if we have a hw clock
83 tmp=$($TS_CMD_HWCLOCK --show 2>&1)
84 if [ $? != "0" ]; then
85 echo "$tmp" | grep -q "Cannot access the Hardware Clock via" \
86 && ts_skip "no hardware clock found"
87 ts_failed "hwclock --show"
88 fi
89
90 # call hwclock
91 for i in `seq 1 10`; do
92 # only *skip* on failure for now
93 $TS_CMD_HWCLOCK --systohc || ts_skip "hwclock --systohc failed, $i"
94 $TS_CMD_HWCLOCK --hctosys || ts_skip "hwclock --hctosys failed, $i"
95 done
96
97 OFFSET_C=$(get_offset_sys_ntp "$NTP_IP") \
98 || ts_skip "can't get ntp offset 3rd, $NTP_IP"
99
100 diff=$(check_diff_offset "$OFFSET_B" "$OFFSET_C" 1.0) \
101 || ts_failed "offsets $NTP_IP: $OFFSET_B $OFFSET_C +/-$diff"
102
103 ts_ok "offsets $NTP_IP: $OFFSET_B $OFFSET_C +/-$diff"