blob: c3b5a33659f4d9d4640f1503a1618248f9e6faa7 [file] [log] [blame]
William A. Kennington IIIbef990f2022-02-08 16:50:30 -08001#!/bin/bash
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
William A. Kennington III618c1092023-02-28 16:11:43 -080016# If we don't have a saved IP, we keep running indefinitely
17if ! srcip="$(cat /var/google/gbmc-br-ip 2>/dev/null)"; then
18 echo 'Missing saved gbmc-br IP' >&2
19 exit 0
20fi
21
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080022# Wait until a well known service is network available
23echo "Waiting for network reachability" >&2
William A. Kennington III618c1092023-02-28 16:11:43 -080024while ! ping -I "$srcip" -c 1 -W 1 2001:4860:4860::8888 >/dev/null 2>&1; do
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080025 sleep 1
26done
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070027
28# We need to guarantee we wait at least 5 minutes from reachable in
29# case networking just came up
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080030wait_min=5
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070031echo "Network is reachable, waiting $wait_min min" >&2
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080032sleep $((60 * wait_min))
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070033
34get_dhcp_unit_json() {
35 busctl -j call \
36 org.freedesktop.systemd1 \
37 /org/freedesktop/systemd1/unit/gbmc_2dbr_2ddhcp_2eservice \
38 org.freedesktop.DBus.Properties \
39 GetAll s org.freedesktop.systemd1.Unit
40}
41
42# Follow the process and make sure it idles for at least 5 minutes before
43# shutting down. This allows for failures and retries to happen.
44while true; do
45 json="$(get_dhcp_unit_json)" || exit
46 last_ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
47 if pid="$(cat /run/gbmc-br-dhcp.pid 2>/dev/null)"; then
48 # If the DHCP configuration process is running, wait for it to finish
49 echo "DHCP still running ($pid), waiting" >&2
50 while [[ -e /proc/$pid ]]; do
51 sleep 1
52 done
53 # Wait for systemd to detect the process state change
54 while true; do
55 json="$(get_dhcp_unit_json)" || exit
56 ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
57 (( ms != last_ms )) && break
58 sleep 1
59 done
60 fi
61
62 echo 'Checking DHCP Active State' >&2
63 json="$(get_dhcp_unit_json)" || exit
64 activestr="$(echo "$json" | jq -r '.data[0].ActiveState.data')"
65
66 # The process is already stopped, we are done
67 [[ "$activestr" == 'inactive' ]] && exit
68
69 # If the process is running, give it at least 5 minutes from when it started
70 cur_s="$(cut -d' ' -f1 /proc/uptime)"
William A. Kennington III9275d852022-08-26 14:38:02 -070071 # Remove floating point if applied since bash can't perform float arithmetic
72 cur_s="${cur_s%.*}"
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070073 if [[ "$activestr" == 'active' ]]; then
74 active_ms="$(echo "$json" | jq -r '.data[0].ActiveEnterTimestampMonotonic.data')"
75 else
76 active_ms=$((cur_s*1000*1000))
77 fi
78 w=$((active_ms/1000/1000 + (wait_min*60) - cur_s))
79 [ "$w" -lt 0 ] && break
80 echo "Waiting ${w}s for DHCP process" >&2
81 sleep $w
82done
83
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080084echo "Stopping DHCP processing" >&2
85systemctl stop --no-block gbmc-br-dhcp