Skip to content

Commit b15dca1

Browse files
committed
fix(policy): snapshot state during status reads
1 parent cc2a247 commit b15dca1

1 file changed

Lines changed: 55 additions & 27 deletions

File tree

lib/policy/policy.sh

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ policy_active_ifaces_json() {
5757
local raw item
5858
local -a items=()
5959

60-
raw="$(policy_read_state_value "active_ifaces" || true)"
60+
raw="$(policy_snapshot_get "active_ifaces" "")"
6161
if [[ -z "${raw}" ]]; then
6262
printf '[]'
6363
return 0
@@ -121,6 +121,32 @@ policy_read_state_value() {
121121
awk -F= -v wanted="${key}" '$1==wanted {print substr($0, index($0, "=")+1); exit}' "${state_file}"
122122
}
123123

124+
policy_snapshot_load() {
125+
local state_file line key value
126+
127+
declare -gA POLICY_STATUS_SNAPSHOT=()
128+
state_file="$(policy_state_readonly_file)"
129+
[[ -f "${state_file}" ]] || return 0
130+
131+
while IFS= read -r line; do
132+
[[ "${line}" == *=* ]] || continue
133+
key="${line%%=*}"
134+
value="${line#*=}"
135+
POLICY_STATUS_SNAPSHOT["${key}"]="${value}"
136+
done <"${state_file}"
137+
}
138+
139+
policy_snapshot_get() {
140+
local key="${1:?missing key}"
141+
local default_value="${2:-}"
142+
143+
if [[ -v "POLICY_STATUS_SNAPSHOT[${key}]" ]]; then
144+
printf '%s' "${POLICY_STATUS_SNAPSHOT["${key}"]}"
145+
else
146+
printf '%s' "${default_value}"
147+
fi
148+
}
149+
124150
policy_event_source() {
125151
local ip_tool
126152

@@ -313,7 +339,8 @@ policy_evaluate() {
313339

314340
policy_status_text() {
315341
load_config
316-
local pid_file pid watcher_running state_file
342+
local pid_file pid watcher_running
343+
policy_snapshot_load
317344

318345
pid_file="$(policy_pid_file)"
319346
pid="$(read_pid_file "${pid_file}" || true)"
@@ -324,29 +351,30 @@ policy_status_text() {
324351
pid="0"
325352
fi
326353

327-
printf 'status=%s\n' "$(policy_read_state_value "status" || printf 'inactive')"
354+
printf 'status=%s\n' "$(policy_snapshot_get "status" "inactive")"
328355
printf 'policy_enabled=%s\n' "${BOX_POLICY_ENABLED}"
329356
printf 'watcher_running=%s\n' "${watcher_running}"
330357
printf 'pid=%s\n' "${pid}"
331-
printf 'desired_state=%s\n' "$(policy_read_state_value "desired_state" || printf 'disabled')"
332-
printf 'applied_state=%s\n' "$(policy_read_state_value "applied_state" || printf 'unchanged')"
358+
printf 'desired_state=%s\n' "$(policy_snapshot_get "desired_state" "disabled")"
359+
printf 'applied_state=%s\n' "$(policy_snapshot_get "applied_state" "unchanged")"
333360
printf 'proxy_mode=%s\n' "${BOX_POLICY_PROXY_MODE}"
334361
printf 'debounce_seconds=%s\n' "${BOX_POLICY_DEBOUNCE_SECONDS}"
335-
printf 'active_ifaces=%s\n' "$(policy_read_state_value "active_ifaces" || true)"
336-
printf 'wifi_connected=%s\n' "$(policy_read_state_value "wifi_connected" || printf 'false')"
337-
printf 'ssid=%s\n' "$(policy_read_state_value "ssid" || true)"
338-
printf 'bssid=%s\n' "$(policy_read_state_value "bssid" || true)"
339-
printf 'disable_marker_present=%s\n' "$(policy_read_state_value "disable_marker_present" || printf 'false')"
340-
printf 'last_reason=%s\n' "$(policy_read_state_value "last_reason" || true)"
341-
printf 'last_error=%s\n' "$(policy_read_state_value "last_error" || true)"
342-
printf 'last_event=%s\n' "$(policy_read_state_value "last_event" || true)"
343-
printf 'last_event_ts=%s\n' "$(policy_read_state_value "last_event_ts" || true)"
344-
printf 'last_refresh_ts=%s\n' "$(policy_read_state_value "last_refresh_ts" || true)"
362+
printf 'active_ifaces=%s\n' "$(policy_snapshot_get "active_ifaces" "")"
363+
printf 'wifi_connected=%s\n' "$(policy_snapshot_get "wifi_connected" "false")"
364+
printf 'ssid=%s\n' "$(policy_snapshot_get "ssid" "")"
365+
printf 'bssid=%s\n' "$(policy_snapshot_get "bssid" "")"
366+
printf 'disable_marker_present=%s\n' "$(policy_snapshot_get "disable_marker_present" "false")"
367+
printf 'last_reason=%s\n' "$(policy_snapshot_get "last_reason" "")"
368+
printf 'last_error=%s\n' "$(policy_snapshot_get "last_error" "")"
369+
printf 'last_event=%s\n' "$(policy_snapshot_get "last_event" "")"
370+
printf 'last_event_ts=%s\n' "$(policy_snapshot_get "last_event_ts" "")"
371+
printf 'last_refresh_ts=%s\n' "$(policy_snapshot_get "last_refresh_ts" "")"
345372
}
346373

347374
policy_status_json() {
348375
load_config
349376
local pid_file pid watcher_running
377+
policy_snapshot_load
350378

351379
pid_file="$(policy_pid_file)"
352380
pid="$(read_pid_file "${pid_file}" || true)"
@@ -358,24 +386,24 @@ policy_status_json() {
358386
fi
359387

360388
printf '{%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s}\n' \
361-
"$(json_pair "status" "$(policy_read_state_value "status" || printf 'inactive')")" \
389+
"$(json_pair "status" "$(policy_snapshot_get "status" "inactive")")" \
362390
"$(json_bool_pair "policy_enabled" "${BOX_POLICY_ENABLED}")" \
363391
"$(json_bool_pair "watcher_running" "${watcher_running}")" \
364392
"$(json_num_pair "pid" "${pid}")" \
365-
"$(json_pair "desired_state" "$(policy_read_state_value "desired_state" || printf 'disabled')")" \
366-
"$(json_pair "applied_state" "$(policy_read_state_value "applied_state" || printf 'unchanged')")" \
393+
"$(json_pair "desired_state" "$(policy_snapshot_get "desired_state" "disabled")")" \
394+
"$(json_pair "applied_state" "$(policy_snapshot_get "applied_state" "unchanged")")" \
367395
"$(json_pair "proxy_mode" "${BOX_POLICY_PROXY_MODE}")" \
368396
"$(json_num_pair "debounce_seconds" "${BOX_POLICY_DEBOUNCE_SECONDS}")" \
369397
"\"active_ifaces\":$(policy_active_ifaces_json)" \
370-
"$(json_bool_pair "wifi_connected" "$(policy_read_state_value "wifi_connected" || printf 'false')")" \
371-
"$(json_pair "ssid" "$(policy_read_state_value "ssid" || true)")" \
372-
"$(json_pair "bssid" "$(policy_read_state_value "bssid" || true)")" \
373-
"$(json_bool_pair "disable_marker_present" "$(policy_read_state_value "disable_marker_present" || printf 'false')")" \
374-
"$(json_pair "last_reason" "$(policy_read_state_value "last_reason" || true)")" \
375-
"$(json_pair "last_error" "$(policy_read_state_value "last_error" || true)")" \
376-
"$(json_pair "last_event" "$(policy_read_state_value "last_event" || true)")" \
377-
"$(json_pair "last_event_ts" "$(policy_read_state_value "last_event_ts" || true)")" \
378-
"$(json_pair "last_refresh_ts" "$(policy_read_state_value "last_refresh_ts" || true)")"
398+
"$(json_bool_pair "wifi_connected" "$(policy_snapshot_get "wifi_connected" "false")")" \
399+
"$(json_pair "ssid" "$(policy_snapshot_get "ssid" "")")" \
400+
"$(json_pair "bssid" "$(policy_snapshot_get "bssid" "")")" \
401+
"$(json_bool_pair "disable_marker_present" "$(policy_snapshot_get "disable_marker_present" "false")")" \
402+
"$(json_pair "last_reason" "$(policy_snapshot_get "last_reason" "")")" \
403+
"$(json_pair "last_error" "$(policy_snapshot_get "last_error" "")")" \
404+
"$(json_pair "last_event" "$(policy_snapshot_get "last_event" "")")" \
405+
"$(json_pair "last_event_ts" "$(policy_snapshot_get "last_event_ts" "")")" \
406+
"$(json_pair "last_refresh_ts" "$(policy_snapshot_get "last_refresh_ts" "")")"
379407
}
380408

381409
policy_status() {

0 commit comments

Comments
 (0)