blob: c43647ca6a664b36782c48133973eb2b53e8c4a7 [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
16# Wait until a well known service is network available
17echo "Waiting for network reachability" >&2
18while ! ping -c 1 -W 1 2001:4860:4860::8888 >/dev/null 2>&1; do
19 sleep 1
20done
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070021
22# We need to guarantee we wait at least 5 minutes from reachable in
23# case networking just came up
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080024wait_min=5
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070025echo "Network is reachable, waiting $wait_min min" >&2
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080026sleep $((60 * wait_min))
William A. Kennington IIIe3edde42022-05-25 15:44:03 -070027
28get_dhcp_unit_json() {
29 busctl -j call \
30 org.freedesktop.systemd1 \
31 /org/freedesktop/systemd1/unit/gbmc_2dbr_2ddhcp_2eservice \
32 org.freedesktop.DBus.Properties \
33 GetAll s org.freedesktop.systemd1.Unit
34}
35
36# Follow the process and make sure it idles for at least 5 minutes before
37# shutting down. This allows for failures and retries to happen.
38while true; do
39 json="$(get_dhcp_unit_json)" || exit
40 last_ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
41 if pid="$(cat /run/gbmc-br-dhcp.pid 2>/dev/null)"; then
42 # If the DHCP configuration process is running, wait for it to finish
43 echo "DHCP still running ($pid), waiting" >&2
44 while [[ -e /proc/$pid ]]; do
45 sleep 1
46 done
47 # Wait for systemd to detect the process state change
48 while true; do
49 json="$(get_dhcp_unit_json)" || exit
50 ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
51 (( ms != last_ms )) && break
52 sleep 1
53 done
54 fi
55
56 echo 'Checking DHCP Active State' >&2
57 json="$(get_dhcp_unit_json)" || exit
58 activestr="$(echo "$json" | jq -r '.data[0].ActiveState.data')"
59
60 # The process is already stopped, we are done
61 [[ "$activestr" == 'inactive' ]] && exit
62
63 # If the process is running, give it at least 5 minutes from when it started
64 cur_s="$(cut -d' ' -f1 /proc/uptime)"
65 if [[ "$activestr" == 'active' ]]; then
66 active_ms="$(echo "$json" | jq -r '.data[0].ActiveEnterTimestampMonotonic.data')"
67 else
68 active_ms=$((cur_s*1000*1000))
69 fi
70 w=$((active_ms/1000/1000 + (wait_min*60) - cur_s))
71 [ "$w" -lt 0 ] && break
72 echo "Waiting ${w}s for DHCP process" >&2
73 sleep $w
74done
75
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080076echo "Stopping DHCP processing" >&2
77systemctl stop --no-block gbmc-br-dhcp