beautysh: re-format
beautysh is enabled in the openbmc-build-scripts on Bash/Zsh/POSIX-sh
files to have a consistent formatting. Re-run the formatter on the
whole repository.
Change-Id: I6a61656dd6033329cba124ac0754152cbd1e9de3
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/subprojects/ncsid/src/ncsid_lib.sh b/subprojects/ncsid/src/ncsid_lib.sh
index 9330008..52c73d0 100644
--- a/subprojects/ncsid/src/ncsid_lib.sh
+++ b/subprojects/ncsid/src/ncsid_lib.sh
@@ -15,289 +15,289 @@
# Internal handler used for signalling child processes that they should
# terminate.
-HandleTerm() {
- GOT_TERM=1
- if ShouldTerm && (( ${#CHILD_PIDS[@]} > 0 )); then
- kill -s TERM "${!CHILD_PIDS[@]}"
- fi
+function HandleTerm() {
+ GOT_TERM=1
+ if ShouldTerm && (( ${#CHILD_PIDS[@]} > 0 )); then
+ kill -s TERM "${!CHILD_PIDS[@]}"
+ fi
}
# Sets up the signal handler and global variables needed to run interruptible
# services that can be killed gracefully.
-InitTerm() {
- declare -g -A CHILD_PIDS=()
- declare -g GOT_TERM=0
- declare -g SUPPRESS_TERM=0
- trap HandleTerm TERM
+function InitTerm() {
+ declare -g -A CHILD_PIDS=()
+ declare -g GOT_TERM=0
+ declare -g SUPPRESS_TERM=0
+ trap HandleTerm TERM
}
# Used to suppress the handling of SIGTERM for critical components that should
# not respect SIGTERM. To finish suppressing, use UnsuppressTerm()
-SuppressTerm() {
- SUPPRESS_TERM=$((SUPPRESS_TERM + 1))
+function SuppressTerm() {
+ SUPPRESS_TERM=$((SUPPRESS_TERM + 1))
}
# Stops suppressing SIGTERM for a single invocation of SuppresssTerm()
-UnsuppressTerm() {
- SUPPRESS_TERM=$((SUPPRESS_TERM - 1))
+function UnsuppressTerm() {
+ SUPPRESS_TERM=$((SUPPRESS_TERM - 1))
}
# Determines if we got a SIGTERM and should respect it
-ShouldTerm() {
- (( GOT_TERM == 1 && SUPPRESS_TERM == 0 ))
+function ShouldTerm() {
+ (( GOT_TERM == 1 && SUPPRESS_TERM == 0 ))
}
# Internal, ensures that functions called in a subprocess properly initialize
# their SIGTERM handling logic
-RunInterruptibleFunction() {
- CHILD_PIDS=()
- trap HandleTerm TERM
- "$@"
+function RunInterruptibleFunction() {
+ CHILD_PIDS=()
+ trap HandleTerm TERM
+ "$@"
}
# Runs the provided commandline in the background, and passes any received
# SIGTERMS to the child. Can be waited on using WaitInterruptibleBg
-RunInterruptibleBg() {
- if ShouldTerm; then
- return 143
- fi
- if [ "$(type -t "$1")" = "function" ]; then
- RunInterruptibleFunction "$@" &
- else
- "$@" &
- fi
- CHILD_PIDS["$!"]=1
+function RunInterruptibleBg() {
+ if ShouldTerm; then
+ return 143
+ fi
+ if [ "$(type -t "$1")" = "function" ]; then
+ RunInterruptibleFunction "$@" &
+ else
+ "$@" &
+ fi
+ CHILD_PIDS["$!"]=1
}
# Runs the provided commandline to completion, and passes any received
# SIGTERMS to the child.
-RunInterruptible() {
- RunInterruptibleBg "$@" || return
- local child_pid="$!"
- wait "$child_pid" || true
- unset CHILD_PIDS["$child_pid"]
- wait "$child_pid"
+function RunInterruptible() {
+ RunInterruptibleBg "$@" || return
+ local child_pid="$!"
+ wait "$child_pid" || true
+ unset CHILD_PIDS["$child_pid"]
+ wait "$child_pid"
}
# Waits until all of the RunInterruptibleBg() jobs have terminated
-WaitInterruptibleBg() {
- local wait_on=("${!CHILD_PIDS[@]}")
- if (( ${#wait_on[@]} > 0 )); then
- wait "${wait_on[@]}" || true
- CHILD_PIDS=()
- local rc=0
- local id
- for id in "${wait_on[@]}"; do
- wait "$id" || rc=$?
- done
- return $rc
- fi
+function WaitInterruptibleBg() {
+ local wait_on=("${!CHILD_PIDS[@]}")
+ if (( ${#wait_on[@]} > 0 )); then
+ wait "${wait_on[@]}" || true
+ CHILD_PIDS=()
+ local rc=0
+ local id
+ for id in "${wait_on[@]}"; do
+ wait "$id" || rc=$?
+ done
+ return $rc
+ fi
}
# Runs the provided commandline to completion, capturing stdout
# into a variable
-CaptureInterruptible() {
- local var="$1"
- shift
- if ShouldTerm; then
- return 143
- fi
- coproc "$@" || return
- local child_pid="$COPROC_PID"
- CHILD_PIDS["$child_pid"]=1
- exec {COPROC[1]}>&-
- read -d $'\0' -ru "${COPROC[0]}" "$var" || true
- wait "$child_pid" || true
- unset CHILD_PIDS[$child_pid]
- wait "$child_pid"
+function CaptureInterruptible() {
+ local var="$1"
+ shift
+ if ShouldTerm; then
+ return 143
+ fi
+ coproc "$@" || return
+ local child_pid="$COPROC_PID"
+ CHILD_PIDS["$child_pid"]=1
+ exec {COPROC[1]}>&-
+ read -d $'\0' -ru "${COPROC[0]}" "$var" || true
+ wait "$child_pid" || true
+ unset CHILD_PIDS[$child_pid]
+ wait "$child_pid"
}
# Determines if an address could be a valid IPv4 address
# NOTE: this doesn't sanitize invalid IPv4 addresses
-IsIPv4() {
- local ip="$1"
+function IsIPv4() {
+ local ip="$1"
- [[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
+ [[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
}
# Takes lines of text from an application on stdin and parses out a single
# MAC address per line of input.
-ParseMACFromLine() {
- sed -n 's,.*\(\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}\).*,\1,p'
+function ParseMACFromLine() {
+ sed -n 's,.*\(\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}\).*,\1,p'
}
# Looks up the MAC address of the IPv4 neighbor using ARP
-DetermineNeighbor4() {
- local netdev="$1"
- local ip="$2"
+function DetermineNeighbor4() {
+ local netdev="$1"
+ local ip="$2"
- # Grep intentionally prevented from returning an error to preserve the error
- # value of arping
- RunInterruptible arping -f -c 5 -w 5 -I "$netdev" "$ip" | \
- { grep 'reply from' || true; } | ParseMACFromLine
+ # Grep intentionally prevented from returning an error to preserve the error
+ # value of arping
+ RunInterruptible arping -f -c 5 -w 5 -I "$netdev" "$ip" | \
+ { grep 'reply from' || true; } | ParseMACFromLine
}
# Looks up the MAC address of the IPv6 neighbor using ICMPv6 ND
-DetermineNeighbor6() {
- local netdev="$1"
- local ip="$2"
+function DetermineNeighbor6() {
+ local netdev="$1"
+ local ip="$2"
- RunInterruptible ndisc6 -1 -r 5 -w 1000 -q "$ip" "$netdev"
+ RunInterruptible ndisc6 -1 -r 5 -w 1000 -q "$ip" "$netdev"
}
# Looks up the MAC address of the neighbor regardless of type
-DetermineNeighbor() {
- local netdev="$1"
- local ip="$2"
+function DetermineNeighbor() {
+ local netdev="$1"
+ local ip="$2"
- if IsIPv4 "$ip"; then
- DetermineNeighbor4 "$netdev" "$ip"
- else
- DetermineNeighbor6 "$netdev" "$ip"
- fi
+ if IsIPv4 "$ip"; then
+ DetermineNeighbor4 "$netdev" "$ip"
+ else
+ DetermineNeighbor6 "$netdev" "$ip"
+ fi
}
# Performs a mapper call to get the subroot for the object root
# with a maxdepth and list of required interfaces. Returns a streamed list
# of JSON objects that contain an { object, service }.
-GetSubTree() {
- local root="$1"
- shift
- local max_depth="$1"
- shift
+function GetSubTree() {
+ local root="$1"
+ shift
+ local max_depth="$1"
+ shift
- busctl --json=short call \
- 'xyz.openbmc_project.ObjectMapper' \
- '/xyz/openbmc_project/object_mapper' \
- 'xyz.openbmc_project.ObjectMapper' \
- 'GetSubTree' sias "$root" "$max_depth" "$#" "$@" | \
- jq -c '.data[0] | to_entries[] | { object: .key, service: (.value | keys[0]) }'
+ busctl --json=short call \
+ 'xyz.openbmc_project.ObjectMapper' \
+ '/xyz/openbmc_project/object_mapper' \
+ 'xyz.openbmc_project.ObjectMapper' \
+ 'GetSubTree' sias "$root" "$max_depth" "$#" "$@" | \
+ jq -c '.data[0] | to_entries[] | { object: .key, service: (.value | keys[0]) }'
}
# Returns all of the properties for a DBus interface on an object as a JSON
# object where the keys are the property names
-GetProperties() {
- local service="$1"
- local object="$2"
- local interface="$3"
+function GetProperties() {
+ local service="$1"
+ local object="$2"
+ local interface="$3"
- busctl --json=short call \
- "$service" \
- "$object" \
- 'org.freedesktop.DBus.Properties' \
- 'GetAll' s "$interface" | \
- jq -c '.data[0] | with_entries({ key, value: .value.data })'
+ busctl --json=short call \
+ "$service" \
+ "$object" \
+ 'org.freedesktop.DBus.Properties' \
+ 'GetAll' s "$interface" | \
+ jq -c '.data[0] | with_entries({ key, value: .value.data })'
}
# Returns the property for a DBus interface on an object
-GetProperty() {
- local service="$1"
- local object="$2"
- local interface="$3"
- local property="$4"
+function GetProperty() {
+ local service="$1"
+ local object="$2"
+ local interface="$3"
+ local property="$4"
- busctl --json=short call \
- "$service" \
- "$object" \
- 'org.freedesktop.DBus.Properties' \
- 'Get' ss "$interface" "$property" | \
- jq -r '.data[0].data'
+ busctl --json=short call \
+ "$service" \
+ "$object" \
+ 'org.freedesktop.DBus.Properties' \
+ 'Get' ss "$interface" "$property" | \
+ jq -r '.data[0].data'
}
# Deletes any OpenBMC DBus object from a service
-DeleteObject() {
- local service="$1"
- local object="$2"
+function DeleteObject() {
+ local service="$1"
+ local object="$2"
- busctl call \
- "$service" \
- "$object" \
- 'xyz.openbmc_project.Object.Delete' \
- 'Delete'
+ busctl call \
+ "$service" \
+ "$object" \
+ 'xyz.openbmc_project.Object.Delete' \
+ 'Delete'
}
# Transforms the given JSON dictionary into bash local variable
# statements that can be directly evaluated by the interpreter
-JSONToVars() {
- jq -r 'to_entries[] | @sh "local \(.key)=\(.value)"'
+function JSONToVars() {
+ jq -r 'to_entries[] | @sh "local \(.key)=\(.value)"'
}
# Returns the DBus object root for the ethernet interface
-EthObjRoot() {
- local netdev="$1"
+function EthObjRoot() {
+ local netdev="$1"
- echo "/xyz/openbmc_project/network/$netdev"
+ echo "/xyz/openbmc_project/network/$netdev"
}
# Returns the DBus object root for the static neighbors of an intrerface
-StaticNeighborObjRoot() {
- local netdev="$1"
+function StaticNeighborObjRoot() {
+ local netdev="$1"
- echo "$(EthObjRoot "$netdev")/static_neighbor"
+ echo "$(EthObjRoot "$netdev")/static_neighbor"
}
# Returns all of the neighbor { service, object } data for an interface as if
# a call to GetSubTree() was made
-GetNeighborObjects() {
- local netdev="$1"
+function GetNeighborObjects() {
+ local netdev="$1"
- GetSubTree "$(StaticNeighborObjRoot "$netdev")" 0 \
- 'xyz.openbmc_project.Network.Neighbor'
+ GetSubTree "$(StaticNeighborObjRoot "$netdev")" 0 \
+ 'xyz.openbmc_project.Network.Neighbor'
}
# Returns the neighbor properties as a JSON object
-GetNeighbor() {
- local service="$1"
- local object="$2"
+function GetNeighbor() {
+ local service="$1"
+ local object="$2"
- GetProperties "$service" "$object" 'xyz.openbmc_project.Network.Neighbor'
+ GetProperties "$service" "$object" 'xyz.openbmc_project.Network.Neighbor'
}
# Adds a static neighbor to the system network daemon
-AddNeighbor() {
- local service="$1"
- local netdev="$2"
- local ip="$3"
- local mac="$4"
+function AddNeighbor() {
+ local service="$1"
+ local netdev="$2"
+ local ip="$3"
+ local mac="$4"
- busctl call \
- "$service" \
- "$(EthObjRoot "$netdev")" \
- 'xyz.openbmc_project.Network.Neighbor.CreateStatic' \
- 'Neighbor' ss "$ip" "$mac" >/dev/null
+ busctl call \
+ "$service" \
+ "$(EthObjRoot "$netdev")" \
+ 'xyz.openbmc_project.Network.Neighbor.CreateStatic' \
+ 'Neighbor' ss "$ip" "$mac" >/dev/null
}
# Returns all of the IP { service, object } data for an interface as if
# a call to GetSubTree() was made
-GetIPObjects() {
- local netdev="$1"
+function GetIPObjects() {
+ local netdev="$1"
- GetSubTree "$(EthObjRoot "$netdev")" 0 \
- 'xyz.openbmc_project.Network.IP'
+ GetSubTree "$(EthObjRoot "$netdev")" 0 \
+ 'xyz.openbmc_project.Network.IP'
}
# Returns the IP properties as a JSON object
-GetIP() {
- local service="$1"
- local object="$2"
+function GetIP() {
+ local service="$1"
+ local object="$2"
- GetProperties "$service" "$object" 'xyz.openbmc_project.Network.IP'
+ GetProperties "$service" "$object" 'xyz.openbmc_project.Network.IP'
}
# Returns the Gateway address for the interface and type
-GetGateways() {
- local service="$1"
- local netdev="$2"
+function GetGateways() {
+ local service="$1"
+ local netdev="$2"
- # We fetch both the system properties and the netdev specific properties
- # as OpenBMC is in the process of transitioning these to the netdev object
- # but the migration is not yet complete.
- {
- GetProperties "$service" '/xyz/openbmc_project/network/config' \
- 'xyz.openbmc_project.Network.SystemConfiguration'
- GetProperties "$service" "$(EthObjRoot "$netdev")" \
- 'xyz.openbmc_project.Network.EthernetInterface'
- } | jq -s '
+ # We fetch both the system properties and the netdev specific properties
+ # as OpenBMC is in the process of transitioning these to the netdev object
+ # but the migration is not yet complete.
+ {
+ GetProperties "$service" '/xyz/openbmc_project/network/config' \
+ 'xyz.openbmc_project.Network.SystemConfiguration'
+ GetProperties "$service" "$(EthObjRoot "$netdev")" \
+ 'xyz.openbmc_project.Network.EthernetInterface'
+ } | jq -s '
. | map(
if .DefaultGateway != "" then
{DefaultGateway: .DefaultGateway}
@@ -309,178 +309,178 @@
else
{}
end
- ) | {DefaultGateway: "", DefaultGateway6: ""} + add'
+ ) | {DefaultGateway: "", DefaultGateway6: ""} + add'
}
# Adds a static IP to the system network daemon
-AddIP() {
- local service="$1"
- local netdev="$2"
- local ip="$3"
- local prefix="$4"
+function AddIP() {
+ local service="$1"
+ local netdev="$2"
+ local ip="$3"
+ local prefix="$4"
- local protocol='xyz.openbmc_project.Network.IP.Protocol.IPv4'
- if ! IsIPv4 "$ip"; then
- protocol='xyz.openbmc_project.Network.IP.Protocol.IPv6'
- fi
+ local protocol='xyz.openbmc_project.Network.IP.Protocol.IPv4'
+ if ! IsIPv4 "$ip"; then
+ protocol='xyz.openbmc_project.Network.IP.Protocol.IPv6'
+ fi
- busctl call \
- "$service" \
- "$(EthObjRoot "$netdev")" \
- 'xyz.openbmc_project.Network.IP.Create' \
- 'IP' ssys "$protocol" "$ip" "$prefix" '' >/dev/null
+ busctl call \
+ "$service" \
+ "$(EthObjRoot "$netdev")" \
+ 'xyz.openbmc_project.Network.IP.Create' \
+ 'IP' ssys "$protocol" "$ip" "$prefix" '' >/dev/null
}
# Determines if two IP addresses have the same address family
# IE: Both are IPv4 or both are IPv6
-MatchingAF() {
- local rc1=0 rc2=0
- IsIPv4 "$1" || rc1=$?
- IsIPv4 "$2" || rc2=$?
- (( rc1 == rc2 ))
+function MatchingAF() {
+ local rc1=0 rc2=0
+ IsIPv4 "$1" || rc1=$?
+ IsIPv4 "$2" || rc2=$?
+ (( rc1 == rc2 ))
}
# Checks to see if the machine has the provided IP address information
# already configured. If not, it deletes all of the information for that
# address family and adds the provided IP address.
-UpdateIP() {
- local service="$1"
- local netdev="$2"
- local ip="$3"
- local prefix="$4"
+function UpdateIP() {
+ local service="$1"
+ local netdev="$2"
+ local ip="$3"
+ local prefix="$4"
- local should_add=1
- local delete_services=()
- local delete_objects=()
- local entry
- while read entry; do
- eval "$(echo "$entry" | JSONToVars)" || return $?
- eval "$(GetIP "$service" "$object" | JSONToVars)" || return $?
- if [ "$(normalize_ip "$Address")" = "$(normalize_ip "$ip")" ] && \
- [ "$PrefixLength" = "$prefix" ]; then
- should_add=0
- elif MatchingAF "$ip" "$Address"; then
- echo "Deleting spurious IP: $Address/$PrefixLength" >&2
- delete_services+=("$service")
- delete_objects+=("$object")
+ local should_add=1
+ local delete_services=()
+ local delete_objects=()
+ local entry
+ while read entry; do
+ eval "$(echo "$entry" | JSONToVars)" || return $?
+ eval "$(GetIP "$service" "$object" | JSONToVars)" || return $?
+ if [ "$(normalize_ip "$Address")" = "$(normalize_ip "$ip")" ] && \
+ [ "$PrefixLength" = "$prefix" ]; then
+ should_add=0
+ elif MatchingAF "$ip" "$Address"; then
+ echo "Deleting spurious IP: $Address/$PrefixLength" >&2
+ delete_services+=("$service")
+ delete_objects+=("$object")
+ fi
+ done < <(GetIPObjects "$netdev")
+
+ local i
+ for (( i=0; i<${#delete_objects[@]}; ++i )); do
+ DeleteObject "${delete_services[$i]}" "${delete_objects[$i]}" || true
+ done
+
+ if (( should_add == 0 )); then
+ echo "Not adding IP: $ip/$prefix" >&2
+ else
+ echo "Adding IP: $ip/$prefix" >&2
+ AddIP "$service" "$netdev" "$ip" "$prefix" || return $?
fi
- done < <(GetIPObjects "$netdev")
-
- local i
- for (( i=0; i<${#delete_objects[@]}; ++i )); do
- DeleteObject "${delete_services[$i]}" "${delete_objects[$i]}" || true
- done
-
- if (( should_add == 0 )); then
- echo "Not adding IP: $ip/$prefix" >&2
- else
- echo "Adding IP: $ip/$prefix" >&2
- AddIP "$service" "$netdev" "$ip" "$prefix" || return $?
- fi
}
# Sets the system gateway property to the provided IP address if not already
# set to the current value.
-UpdateGateway() {
- local service="$1"
- local netdev="$2"
- local ip="$3"
+function UpdateGateway() {
+ local service="$1"
+ local netdev="$2"
+ local ip="$3"
- local object="$(EthObjRoot "$netdev")"
- local interface='xyz.openbmc_project.Network.EthernetInterface'
- local property='DefaultGateway'
- if ! IsIPv4 "$ip"; then
- property='DefaultGateway6'
- fi
+ local object="$(EthObjRoot "$netdev")"
+ local interface='xyz.openbmc_project.Network.EthernetInterface'
+ local property='DefaultGateway'
+ if ! IsIPv4 "$ip"; then
+ property='DefaultGateway6'
+ fi
- local current_ip
- current_ip="$(GetProperty "$service" "$object" "$interface" "$property")" || \
- return $?
- if [ -n "$current_ip" ] && \
- [ "$(normalize_ip "$ip")" = "$(normalize_ip "$current_ip")" ]; then
- echo "Not reconfiguring gateway: $ip" >&2
- return 0
- fi
+ local current_ip
+ current_ip="$(GetProperty "$service" "$object" "$interface" "$property")" || \
+ return $?
+ if [ -n "$current_ip" ] && \
+ [ "$(normalize_ip "$ip")" = "$(normalize_ip "$current_ip")" ]; then
+ echo "Not reconfiguring gateway: $ip" >&2
+ return 0
+ fi
- echo "Setting gateway: $ip" >&2
- busctl set-property "$service" "$object" "$interface" "$property" s "$ip"
+ echo "Setting gateway: $ip" >&2
+ busctl set-property "$service" "$object" "$interface" "$property" s "$ip"
}
# Checks to see if the machine has the provided neighbor information
# already configured. If not, it deletes all of the information for that
# address family and adds the provided neighbor entry.
-UpdateNeighbor() {
- local service="$1"
- local netdev="$2"
- local ip="$3"
- local mac="$4"
+function UpdateNeighbor() {
+ local service="$1"
+ local netdev="$2"
+ local ip="$3"
+ local mac="$4"
- local should_add=1
- local delete_services=()
- local delete_objects=()
- local entry
- while read entry; do
- eval "$(echo "$entry" | JSONToVars)" || return $?
- eval "$(GetNeighbor "$service" "$object" | JSONToVars)" || return $?
- if [ "$(normalize_ip "$IPAddress")" = "$(normalize_ip "$ip")" ] && \
- [ "$(normalize_mac "$MACAddress")" = "$(normalize_mac "$mac")" ]; then
- should_add=0
- elif MatchingAF "$ip" "$IPAddress"; then
- echo "Deleting spurious neighbor: $IPAddress $MACAddress" >&2
- delete_services+=("$service")
- delete_objects+=("$object")
+ local should_add=1
+ local delete_services=()
+ local delete_objects=()
+ local entry
+ while read entry; do
+ eval "$(echo "$entry" | JSONToVars)" || return $?
+ eval "$(GetNeighbor "$service" "$object" | JSONToVars)" || return $?
+ if [ "$(normalize_ip "$IPAddress")" = "$(normalize_ip "$ip")" ] && \
+ [ "$(normalize_mac "$MACAddress")" = "$(normalize_mac "$mac")" ]; then
+ should_add=0
+ elif MatchingAF "$ip" "$IPAddress"; then
+ echo "Deleting spurious neighbor: $IPAddress $MACAddress" >&2
+ delete_services+=("$service")
+ delete_objects+=("$object")
+ fi
+ done < <(GetNeighborObjects "$netdev" 2>/dev/null)
+
+ local i
+ for (( i=0; i<${#delete_objects[@]}; ++i )); do
+ DeleteObject "${delete_services[$i]}" "${delete_objects[$i]}" || true
+ done
+
+ if (( should_add == 0 )); then
+ echo "Not adding neighbor: $ip $mac" >&2
+ else
+ echo "Adding neighbor: $ip $mac" >&2
+ AddNeighbor "$service" "$netdev" "$ip" "$mac" || return $?
fi
- done < <(GetNeighborObjects "$netdev" 2>/dev/null)
-
- local i
- for (( i=0; i<${#delete_objects[@]}; ++i )); do
- DeleteObject "${delete_services[$i]}" "${delete_objects[$i]}" || true
- done
-
- if (( should_add == 0 )); then
- echo "Not adding neighbor: $ip $mac" >&2
- else
- echo "Adding neighbor: $ip $mac" >&2
- AddNeighbor "$service" "$netdev" "$ip" "$mac" || return $?
- fi
}
# Determines the ip and mac of the IPv6 router
-DiscoverRouter6() {
- local netdev="$1"
- local retries="$2"
- local timeout="$3"
- local router="${4-}"
+function DiscoverRouter6() {
+ local netdev="$1"
+ local retries="$2"
+ local timeout="$3"
+ local router="${4-}"
- local output
- local st=0
- local args=(-1 -w "$timeout" -n $router "$netdev")
- if (( retries < 0 )); then
- args+=(-d)
- else
- args+=(-r "$retries")
- fi
- CaptureInterruptible output rdisc6 "${args[@]}" || st=$?
- if (( st != 0 )); then
- echo "rdisc6 failed with: " >&2
- echo "$output" >&2
- return $st
- fi
+ local output
+ local st=0
+ local args=(-1 -w "$timeout" -n $router "$netdev")
+ if (( retries < 0 )); then
+ args+=(-d)
+ else
+ args+=(-r "$retries")
+ fi
+ CaptureInterruptible output rdisc6 "${args[@]}" || st=$?
+ if (( st != 0 )); then
+ echo "rdisc6 failed with: " >&2
+ echo "$output" >&2
+ return $st
+ fi
- local ip="$(echo "$output" | grep 'from' | awk '{print $2}')"
- local mac="$(echo "$output" | grep 'Source link-layer' | ParseMACFromLine)"
- local staddr="$(echo "$output" | grep 'Stateful address conf.*Yes')"
- printf '{"router_ip":"%s","router_mac":"%s","stateful_address":"%s"}\n' \
- "$ip" "$mac" "$staddr"
+ local ip="$(echo "$output" | grep 'from' | awk '{print $2}')"
+ local mac="$(echo "$output" | grep 'Source link-layer' | ParseMACFromLine)"
+ local staddr="$(echo "$output" | grep 'Stateful address conf.*Yes')"
+ printf '{"router_ip":"%s","router_mac":"%s","stateful_address":"%s"}\n' \
+ "$ip" "$mac" "$staddr"
}
# Sets the network configuration of an interface to be static
-SetStatic() {
- local service="$1"
- local netdev="$2"
+function SetStatic() {
+ local service="$1"
+ local netdev="$2"
- echo "Disabling DHCP" >&2
- busctl set-property "$service" "$(EthObjRoot "$netdev")" \
- xyz.openbmc_project.Network.EthernetInterface DHCPEnabled \
- s xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none
+ echo "Disabling DHCP" >&2
+ busctl set-property "$service" "$(EthObjRoot "$netdev")" \
+ xyz.openbmc_project.Network.EthernetInterface DHCPEnabled \
+ s xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none
}
diff --git a/subprojects/ncsid/src/ncsid_udhcpc4.script b/subprojects/ncsid/src/ncsid_udhcpc4.script
index bb6fa6a..fbf90f1 100644
--- a/subprojects/ncsid/src/ncsid_udhcpc4.script
+++ b/subprojects/ncsid/src/ncsid_udhcpc4.script
@@ -1,72 +1,72 @@
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
-DetermineRouterMac() {
- # Attempt to find the neighbor once, in case our configuration is already
- # valid. Errors are silenced to make the logs more clear. The next call
- # will print any real errors.
- if DetermineNeighbor4 "$interface" "$router" 2>/dev/null; then
- return 0
- fi
-
- # arping might not have a valid source address, so we need to assign
- # the given address so arping has a source to write into the request
- # packet. We don't want a persistent configuration yet so we modify
- # the kernel directly.
- if ! ip -4 addr flush dev "$interface"; then
- echo "Failed to flush $interface" >&2
- return 1
- fi
- if ! ip addr add "$ip/$mask" dev "$interface"; then
- echo "Failed to assign $ip/$mask to $interface" >&2
- # Don't return, because we need to reset networkd
- fi
-
- local rc=0
- DetermineNeighbor4 "$interface" "$router" || rc=$?
-
- # We need to ensure that our old network configuration gets
- # restored, in case our early flushing breaks things.
- systemctl restart systemd-networkd || return $?
- return $rc
-}
-
-HandleDHCP4() {
- local op="$1"
-
- if [ "$op" = "bound" ]; then
- echo "INTF: $interface" >&2
- echo "IP: $ip/$mask" >&2
- echo "GW: $router" >&2
-
- local router_mac
- if ! router_mac="$(DetermineRouterMac "$interface" "$router")"; then
- echo "Failed to acquire gateway mac for $router" >&2
- return 1
+function DetermineRouterMac() {
+ # Attempt to find the neighbor once, in case our configuration is already
+ # valid. Errors are silenced to make the logs more clear. The next call
+ # will print any real errors.
+ if DetermineNeighbor4 "$interface" "$router" 2>/dev/null; then
+ return 0
fi
- echo "GW_MAC: $router_mac" >&2
- SuppressTerm
- local service='xyz.openbmc_project.Network'
+ # arping might not have a valid source address, so we need to assign
+ # the given address so arping has a source to write into the request
+ # packet. We don't want a persistent configuration yet so we modify
+ # the kernel directly.
+ if ! ip -4 addr flush dev "$interface"; then
+ echo "Failed to flush $interface" >&2
+ return 1
+ fi
+ if ! ip addr add "$ip/$mask" dev "$interface"; then
+ echo "Failed to assign $ip/$mask to $interface" >&2
+ # Don't return, because we need to reset networkd
+ fi
+
local rc=0
- SetStatic "$service" "$interface" && \
- UpdateIP "$service" "$interface" "$ip" "$mask" && \
- UpdateGateway "$service" "$interface" "$router" && \
- UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \
- rc=$?
- UnsuppressTerm
- touch /run/dhcp4.done
+ DetermineNeighbor4 "$interface" "$router" || rc=$?
+
+ # We need to ensure that our old network configuration gets
+ # restored, in case our early flushing breaks things.
+ systemctl restart systemd-networkd || return $?
return $rc
- fi
}
-Main() {
- set -o nounset
- set -o errexit
- set -o pipefail
+function HandleDHCP4() {
+ local op="$1"
- InitTerm
- HandleDHCP4 "$@"
+ if [ "$op" = "bound" ]; then
+ echo "INTF: $interface" >&2
+ echo "IP: $ip/$mask" >&2
+ echo "GW: $router" >&2
+
+ local router_mac
+ if ! router_mac="$(DetermineRouterMac "$interface" "$router")"; then
+ echo "Failed to acquire gateway mac for $router" >&2
+ return 1
+ fi
+ echo "GW_MAC: $router_mac" >&2
+
+ SuppressTerm
+ local service='xyz.openbmc_project.Network'
+ local rc=0
+ SetStatic "$service" "$interface" && \
+ UpdateIP "$service" "$interface" "$ip" "$mask" && \
+ UpdateGateway "$service" "$interface" "$router" && \
+ UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \
+ rc=$?
+ UnsuppressTerm
+ touch /run/dhcp4.done
+ return $rc
+ fi
+}
+
+function Main() {
+ set -o nounset
+ set -o errexit
+ set -o pipefail
+
+ InitTerm
+ HandleDHCP4 "$@"
}
return 0 2>/dev/null
diff --git a/subprojects/ncsid/src/ncsid_udhcpc6.script b/subprojects/ncsid/src/ncsid_udhcpc6.script
index c913ebe..e78a0ca 100644
--- a/subprojects/ncsid/src/ncsid_udhcpc6.script
+++ b/subprojects/ncsid/src/ncsid_udhcpc6.script
@@ -1,45 +1,45 @@
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
-HandleDHCP6() {
- local op="$1"
+function HandleDHCP6() {
+ local op="$1"
- if [ "$op" = "bound" ]; then
- echo "INTF: $interface" >&2
- echo "IP: $ipv6/128" >&2
+ if [ "$op" = "bound" ]; then
+ echo "INTF: $interface" >&2
+ echo "IP: $ipv6/128" >&2
- local disc
- if ! disc="$(DiscoverRouter6 "$interface" 5 1000)"; then
- echo "Failed to discover router" >&2
- return 1
+ local disc
+ if ! disc="$(DiscoverRouter6 "$interface" 5 1000)"; then
+ echo "Failed to discover router" >&2
+ return 1
+ fi
+ local vars
+ vars="$(echo "$disc" | JSONToVars)" || return
+ eval "$vars" || return
+ echo "GW: $router_ip" >&2
+ echo "GW_MAC: $router_mac" >&2
+
+ SuppressTerm
+ local service='xyz.openbmc_project.Network'
+ local rc=0
+ SetStatic "$service" "$interface" && \
+ UpdateIP "$service" "$interface" "$ipv6" '128' && \
+ UpdateGateway "$service" "$interface" "$router_ip" && \
+ UpdateNeighbor "$service" "$interface" "$router_ip" "$router_mac" || \
+ rc=$?
+ UnsuppressTerm
+ touch /run/dhcp6.done
+ return $rc
fi
- local vars
- vars="$(echo "$disc" | JSONToVars)" || return
- eval "$vars" || return
- echo "GW: $router_ip" >&2
- echo "GW_MAC: $router_mac" >&2
-
- SuppressTerm
- local service='xyz.openbmc_project.Network'
- local rc=0
- SetStatic "$service" "$interface" && \
- UpdateIP "$service" "$interface" "$ipv6" '128' && \
- UpdateGateway "$service" "$interface" "$router_ip" && \
- UpdateNeighbor "$service" "$interface" "$router_ip" "$router_mac" || \
- rc=$?
- UnsuppressTerm
- touch /run/dhcp6.done
- return $rc
- fi
}
-Main() {
- set -o nounset
- set -o errexit
- set -o pipefail
+function Main() {
+ set -o nounset
+ set -o errexit
+ set -o pipefail
- InitTerm
- HandleDHCP6 "$@"
+ InitTerm
+ HandleDHCP6 "$@"
}
return 0 2>/dev/null
diff --git a/subprojects/ncsid/src/update_ra_gw.sh b/subprojects/ncsid/src/update_ra_gw.sh
index 6d2ef3a..e8f3599 100644
--- a/subprojects/ncsid/src/update_ra_gw.sh
+++ b/subprojects/ncsid/src/update_ra_gw.sh
@@ -20,52 +20,52 @@
old_rtr=
old_mac=
-set_rtr() {
- [ -n "$rtr" -a -n "$lifetime" ] || return
- [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return
- # Only valid default routers can be considered, 0 lifetime implies
- # a non-default router
- (( lifetime > 0 )) || return
+function set_rtr() {
+ [ -n "$rtr" -a -n "$lifetime" ] || return
+ [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return
+ # Only valid default routers can be considered, 0 lifetime implies
+ # a non-default router
+ (( lifetime > 0 )) || return
- echo "Setting default router: $rtr at $mac" >&2
- local svc=xyz.openbmc_project.Network
- SetStatic "$svc" "$NCSI_IF" || return
- UpdateGateway "$svc" "$NCSI_IF" "$rtr" || return
- UpdateNeighbor "$svc" "$NCSI_IF" "$rtr" "$mac" || return
+ echo "Setting default router: $rtr at $mac" >&2
+ local svc=xyz.openbmc_project.Network
+ SetStatic "$svc" "$NCSI_IF" || return
+ UpdateGateway "$svc" "$NCSI_IF" "$rtr" || return
+ UpdateNeighbor "$svc" "$NCSI_IF" "$rtr" "$mac" || return
- retries=-1
- old_mac="$mac"
- old_rtr="$rtr"
+ retries=-1
+ old_mac="$mac"
+ old_rtr="$rtr"
}
retries=1
w=60
while true; do
- start=$SECONDS
- args=(-m "$NCSI_IF" -w $(( w * 1000 )))
- if (( retries > 0 )); then
- args+=(-r "$retries")
- else
- args+=(-d)
- fi
- while read line; do
- if [ -z "$line" ]; then
- lifetime=
- mac=
- elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then
- lifetime="${BASH_REMATCH[1]}"
- elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then
- mac="${BASH_REMATCH[1]}"
- elif [[ "$line" =~ ^from' '(.*)$ ]]; then
- rtr="${BASH_REMATCH[1]}"
- set_rtr || true
- lifetime=
- mac=
- rtr=
+ start=$SECONDS
+ args=(-m "$NCSI_IF" -w $(( w * 1000 )))
+ if (( retries > 0 )); then
+ args+=(-r "$retries")
+ else
+ args+=(-d)
fi
- done < <(exec rdisc6 "${args[@]}" 2>/dev/null)
- # If rdisc6 exits early we still want to wait the full `w` time before
- # starting again.
- (( timeout = start + w - SECONDS ))
- sleep $(( timeout < 0 ? 0 : timeout ))
+ while read line; do
+ if [ -z "$line" ]; then
+ lifetime=
+ mac=
+ elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then
+ lifetime="${BASH_REMATCH[1]}"
+ elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then
+ mac="${BASH_REMATCH[1]}"
+ elif [[ "$line" =~ ^from' '(.*)$ ]]; then
+ rtr="${BASH_REMATCH[1]}"
+ set_rtr || true
+ lifetime=
+ mac=
+ rtr=
+ fi
+ done < <(exec rdisc6 "${args[@]}" 2>/dev/null)
+ # If rdisc6 exits early we still want to wait the full `w` time before
+ # starting again.
+ (( timeout = start + w - SECONDS ))
+ sleep $(( timeout < 0 ? 0 : timeout ))
done
diff --git a/subprojects/ncsid/src/update_ra_neighbor.sh b/subprojects/ncsid/src/update_ra_neighbor.sh
index 1fb6b58..8d0a3c9 100644
--- a/subprojects/ncsid/src/update_ra_neighbor.sh
+++ b/subprojects/ncsid/src/update_ra_neighbor.sh
@@ -15,50 +15,50 @@
source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
-UpdateRA() {
- local netdev="$1"
+function UpdateRA() {
+ local netdev="$1"
- local service='xyz.openbmc_project.Network'
- local gateways
- if ! gateways="$(GetGateways "$service" "$netdev")"; then
- echo "Failed to look up gateways" >&2
- return 1
- fi
- local vars
- vars="$(echo "$gateways" | JSONToVars)" || return
- eval "$vars" || return
+ local service='xyz.openbmc_project.Network'
+ local gateways
+ if ! gateways="$(GetGateways "$service" "$netdev")"; then
+ echo "Failed to look up gateways" >&2
+ return 1
+ fi
+ local vars
+ vars="$(echo "$gateways" | JSONToVars)" || return
+ eval "$vars" || return
- echo "GW($netdev): ${DefaultGateway6:-(none)}" >&2
- if [ -z "$DefaultGateway6" ]; then
- return 0
- fi
+ echo "GW($netdev): ${DefaultGateway6:-(none)}" >&2
+ if [ -z "$DefaultGateway6" ]; then
+ return 0
+ fi
- local st=0
- local disc
- CaptureInterruptible disc DiscoverRouter6 "$netdev" -1 360000 "$DefaultGateway6" || st=$?
- if (( st != 0 )); then
- echo "Failed to discover router" >&2
- return 1
- fi
- local vars
- vars="$(echo "$disc" | JSONToVars)" || return
- eval "$vars" || return
- echo "GW($netdev) MAC: $router_mac" >&2
+ local st=0
+ local disc
+ CaptureInterruptible disc DiscoverRouter6 "$netdev" -1 360000 "$DefaultGateway6" || st=$?
+ if (( st != 0 )); then
+ echo "Failed to discover router" >&2
+ return 1
+ fi
+ local vars
+ vars="$(echo "$disc" | JSONToVars)" || return
+ eval "$vars" || return
+ echo "GW($netdev) MAC: $router_mac" >&2
- SuppressTerm
- local rc=0
- UpdateNeighbor "$service" "$netdev" "$router_ip" "$router_mac" || rc=$?
- UnsuppressTerm
- return $rc
+ SuppressTerm
+ local rc=0
+ UpdateNeighbor "$service" "$netdev" "$router_ip" "$router_mac" || rc=$?
+ UnsuppressTerm
+ return $rc
}
-Main() {
- set -o nounset
- set -o errexit
- set -o pipefail
+function Main() {
+ set -o nounset
+ set -o errexit
+ set -o pipefail
- InitTerm
- UpdateRA "$@"
+ InitTerm
+ UpdateRA "$@"
}
return 0 2>/dev/null
diff --git a/subprojects/ncsid/src/update_static_neighbors.sh b/subprojects/ncsid/src/update_static_neighbors.sh
index 8a6c72f..2f46f82 100644
--- a/subprojects/ncsid/src/update_static_neighbors.sh
+++ b/subprojects/ncsid/src/update_static_neighbors.sh
@@ -15,48 +15,48 @@
source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
-UpdateNeighbor() {
- local netdev="$1"
- local service="$2"
- local object="$3"
+function UpdateNeighbor() {
+ local netdev="$1"
+ local service="$2"
+ local object="$3"
- eval "$(GetNeighbor "$service" "$object" | JSONToVars)" || return $?
- local new_mac
- if ! new_mac="$(DetermineNeighbor "$netdev" "$IPAddress")"; then
- echo "Faild to find $IPAddress" >&2
- return 10
- fi
- new_mac=$(normalize_mac "$new_mac") || return
- if [ "$new_mac" != "$(normalize_mac "$MACAddress")" ]; then
- echo "Updating $IPAddress: $MACAddress -> $new_mac" >&2
- SuppressTerm
- local rc=0
- DeleteObject "$service" "$object" && \
- AddNeighbor "$service" "$netdev" "$IPAddress" "$new_mac" || \
- rc=$?
- UnsuppressTerm
- return $rc
- fi
+ eval "$(GetNeighbor "$service" "$object" | JSONToVars)" || return $?
+ local new_mac
+ if ! new_mac="$(DetermineNeighbor "$netdev" "$IPAddress")"; then
+ echo "Faild to find $IPAddress" >&2
+ return 10
+ fi
+ new_mac=$(normalize_mac "$new_mac") || return
+ if [ "$new_mac" != "$(normalize_mac "$MACAddress")" ]; then
+ echo "Updating $IPAddress: $MACAddress -> $new_mac" >&2
+ SuppressTerm
+ local rc=0
+ DeleteObject "$service" "$object" && \
+ AddNeighbor "$service" "$netdev" "$IPAddress" "$new_mac" || \
+ rc=$?
+ UnsuppressTerm
+ return $rc
+ fi
}
-UpdateNeighbors() {
- local netdev="$1"
+function UpdateNeighbors() {
+ local netdev="$1"
- local entry
- while read entry; do
- eval "$(echo "$entry" | JSONToVars)"
- RunInterruptibleBg UpdateNeighbor "$netdev" "$service" "$object"
- done < <(GetNeighborObjects "$netdev" 2>/dev/null)
- WaitInterruptibleBg
+ local entry
+ while read entry; do
+ eval "$(echo "$entry" | JSONToVars)"
+ RunInterruptibleBg UpdateNeighbor "$netdev" "$service" "$object"
+ done < <(GetNeighborObjects "$netdev" 2>/dev/null)
+ WaitInterruptibleBg
}
-Main() {
- set -o nounset
- set -o errexit
- set -o pipefail
+function Main() {
+ set -o nounset
+ set -o errexit
+ set -o pipefail
- InitTerm
- UpdateNeighbors "$@"
+ InitTerm
+ UpdateNeighbors "$@"
}
return 0 2>/dev/null