summaryrefslogtreecommitdiff
path: root/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-dhcp-term.sh
blob: c3b5a33659f4d9d4640f1503a1618248f9e6faa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# If we don't have a saved IP, we keep running indefinitely
if ! srcip="$(cat /var/google/gbmc-br-ip 2>/dev/null)"; then
  echo 'Missing saved gbmc-br IP' >&2
  exit 0
fi

# Wait until a well known service is network available
echo "Waiting for network reachability" >&2
while ! ping -I "$srcip" -c 1 -W 1 2001:4860:4860::8888 >/dev/null 2>&1; do
  sleep 1
done

# We need to guarantee we wait at least 5 minutes from reachable in
# case networking just came up
wait_min=5
echo "Network is reachable, waiting $wait_min min" >&2
sleep $((60 * wait_min))

get_dhcp_unit_json() {
  busctl -j call \
    org.freedesktop.systemd1 \
    /org/freedesktop/systemd1/unit/gbmc_2dbr_2ddhcp_2eservice \
    org.freedesktop.DBus.Properties \
    GetAll s org.freedesktop.systemd1.Unit
}

# Follow the process and make sure it idles for at least 5 minutes before
# shutting down. This allows for failures and retries to happen.
while true; do
  json="$(get_dhcp_unit_json)" || exit
  last_ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
  if pid="$(cat /run/gbmc-br-dhcp.pid 2>/dev/null)"; then
    # If the DHCP configuration process is running, wait for it to finish
    echo "DHCP still running ($pid), waiting" >&2
    while [[ -e /proc/$pid ]]; do
      sleep 1
    done
    # Wait for systemd to detect the process state change
    while true; do
      json="$(get_dhcp_unit_json)" || exit
      ms="$(echo "$json" | jq -r '.data[0].StateChangeTimestampMonotonic.data')"
      (( ms != last_ms )) && break
      sleep 1
    done
  fi

  echo 'Checking DHCP Active State' >&2
  json="$(get_dhcp_unit_json)" || exit
  activestr="$(echo "$json" | jq -r '.data[0].ActiveState.data')"

  # The process is already stopped, we are done
  [[ "$activestr" == 'inactive' ]] && exit

  # If the process is running, give it at least 5 minutes from when it started
  cur_s="$(cut -d' ' -f1 /proc/uptime)"
  # Remove floating point if applied since bash can't perform float arithmetic
  cur_s="${cur_s%.*}"
  if [[ "$activestr" == 'active' ]]; then
    active_ms="$(echo "$json" | jq -r '.data[0].ActiveEnterTimestampMonotonic.data')"
  else
    active_ms=$((cur_s*1000*1000))
  fi
  w=$((active_ms/1000/1000 + (wait_min*60) - cur_s))
  [ "$w" -lt 0 ] && break
  echo "Waiting ${w}s for DHCP process" >&2
  sleep $w
done

echo "Stopping DHCP processing" >&2
systemctl stop --no-block gbmc-br-dhcp