William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 1 | #!/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 III | 618c109 | 2023-02-28 16:11:43 -0800 | [diff] [blame^] | 16 | # If we don't have a saved IP, we keep running indefinitely |
| 17 | if ! srcip="$(cat /var/google/gbmc-br-ip 2>/dev/null)"; then |
| 18 | echo 'Missing saved gbmc-br IP' >&2 |
| 19 | exit 0 |
| 20 | fi |
| 21 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 22 | # Wait until a well known service is network available |
| 23 | echo "Waiting for network reachability" >&2 |
William A. Kennington III | 618c109 | 2023-02-28 16:11:43 -0800 | [diff] [blame^] | 24 | while ! ping -I "$srcip" -c 1 -W 1 2001:4860:4860::8888 >/dev/null 2>&1; do |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 25 | sleep 1 |
| 26 | done |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 27 | |
| 28 | # We need to guarantee we wait at least 5 minutes from reachable in |
| 29 | # case networking just came up |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 30 | wait_min=5 |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 31 | echo "Network is reachable, waiting $wait_min min" >&2 |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 32 | sleep $((60 * wait_min)) |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 33 | |
| 34 | get_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. |
| 44 | while 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 III | 9275d85 | 2022-08-26 14:38:02 -0700 | [diff] [blame] | 71 | # Remove floating point if applied since bash can't perform float arithmetic |
| 72 | cur_s="${cur_s%.*}" |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 73 | 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 |
| 82 | done |
| 83 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 84 | echo "Stopping DHCP processing" >&2 |
| 85 | systemctl stop --no-block gbmc-br-dhcp |