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 | |
| 16 | # Wait until a well known service is network available |
| 17 | echo "Waiting for network reachability" >&2 |
| 18 | while ! ping -c 1 -W 1 2001:4860:4860::8888 >/dev/null 2>&1; do |
| 19 | sleep 1 |
| 20 | done |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 21 | |
| 22 | # We need to guarantee we wait at least 5 minutes from reachable in |
| 23 | # case networking just came up |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 24 | wait_min=5 |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 25 | echo "Network is reachable, waiting $wait_min min" >&2 |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 26 | sleep $((60 * wait_min)) |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 27 | |
| 28 | get_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. |
| 38 | while 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)" |
William A. Kennington III | 9275d85 | 2022-08-26 14:38:02 -0700 | [diff] [blame] | 65 | # Remove floating point if applied since bash can't perform float arithmetic |
| 66 | cur_s="${cur_s%.*}" |
William A. Kennington III | e3edde4 | 2022-05-25 15:44:03 -0700 | [diff] [blame] | 67 | if [[ "$activestr" == 'active' ]]; then |
| 68 | active_ms="$(echo "$json" | jq -r '.data[0].ActiveEnterTimestampMonotonic.data')" |
| 69 | else |
| 70 | active_ms=$((cur_s*1000*1000)) |
| 71 | fi |
| 72 | w=$((active_ms/1000/1000 + (wait_min*60) - cur_s)) |
| 73 | [ "$w" -lt 0 ] && break |
| 74 | echo "Waiting ${w}s for DHCP process" >&2 |
| 75 | sleep $w |
| 76 | done |
| 77 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 78 | echo "Stopping DHCP processing" >&2 |
| 79 | systemctl stop --no-block gbmc-br-dhcp |