🎉 Sync 2026-11-02 12:48:14

This commit is contained in:
github-actions[bot] committed 2026-11-02 12:48:14 +08:00
1 parent 42d5cbfa43
commit db798bdf5e
53 files changed
+1895 -568

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@ IPKG_INSTROOT="${IPKG_INSTROOT:-}"
export IPKG_INSTROOT
readonly PKG_NAME='safeshield'
readonly PKG_VERSION='0.3.25-r1'
readonly PKG_VERSION='0.3.26-r1'
if type extra_command >/dev/null 2>&1; then
extra_command 'apply_local_rules' 'Apply local allow/block rules using the cached Hub artifact'
@@ -876,7 +876,80 @@ ss_merge_sorted_api_sources() {
sort -m -u "$@" >"$out"
}
# Build the final blocked-rule stream without two full-sized domain copies.
# Keep the ancestor/equal precedence used by ss_filter_domains_shadowed_by_ancestors:
# local allow > local block > Hub allow > Hub block.
ss_write_effective_block_rules() {
local shadow_file="$1"
local local_blocks="$2"
local api_blocks="$3"
local output="$4"
local prefix="$5"
local suffix="$6"
# With no effective allow rules there is nothing to shadow the Hub blocks.
# Avoid building an AWK lookup table and splitting every domain, but keep
# the same local-first ordering and input validation as the normal path.
if [ ! -s "$shadow_file" ]; then
awk -v local_blocks="$local_blocks" -v prefix="$prefix" -v suffix="$suffix" '
FILENAME == local_blocks {
if ($0 != "") print prefix $0 suffix
next
}
/^[a-z0-9._-]+$/ { print prefix $0 suffix }
' "$local_blocks" "$api_blocks" >"$output"
return $?
fi
awk -v shadow_file="$shadow_file" -v local_blocks="$local_blocks" \
-v prefix="$prefix" -v suffix="$suffix" '
FILENAME == shadow_file {
if ($0 != "") shadow[$0] = 1
next
}
FILENAME == local_blocks {
if ($0 != "") print prefix $0 suffix
next
}
/^[a-z0-9._-]+$/ {
if ($0 in shadow) next
n = split($0, labels, ".")
ancestor = labels[n]
for (i = n - 1; i >= 1; i--) {
ancestor = labels[i] "." ancestor
if (ancestor in shadow) next
}
print prefix $0 suffix
}
' "$shadow_file" "$local_blocks" "$api_blocks" >"$output"
}
# Merge scratch files live on tmpfs. API shards and local lists are retained
# for refresh retry/reuse, but these derived copies are never reused.
ss_cleanup_merge_scratch() {
rm -f \
"${SS_TMP_DIR}/api-blocks.domains" \
"${SS_TMP_DIR}/api-allows.domains" \
"${SS_TMP_DIR}/effective-local-blocks.domains" \
"${SS_TMP_DIR}/effective-api-allows.domains" \
"${SS_TMP_DIR}/effective-allows.domains" \
"${SS_TMP_DIR}/effective-api-blocks.domains" \
"${SS_TMP_DIR}/final-blocks.domains"
}
# Always release derived tmpfs storage, including when validation fails.
ss_merge_lists() {
local result=0
# Firmware refresh uses the C locale. Enforce it here too so direct
# calls (including CI) retain the exact same bytewise domain order.
local LC_ALL=C
export LC_ALL
ss_merge_lists_inner "$@" || result=$?
ss_cleanup_merge_scratch
return "$result"
}
ss_merge_lists_inner() {
local final
local api_blocks="${SS_TMP_DIR}/api-blocks.domains"
local api_allows="${SS_TMP_DIR}/api-allows.domains"
@@ -885,10 +958,9 @@ ss_merge_lists() {
local effective_local_blocks="${SS_TMP_DIR}/effective-local-blocks.domains"
local effective_api_allows="${SS_TMP_DIR}/effective-api-allows.domains"
local effective_allows="${SS_TMP_DIR}/effective-allows.domains"
local effective_api_blocks="${SS_TMP_DIR}/effective-api-blocks.domains"
local final_blocks="${SS_TMP_DIR}/final-blocks.domains"
local final_size_kb
local valid_count
local rule_prefix rule_suffix
final="$(ss_blocklist_tmp_path)" || return 1
: >"$final" || return 1
@@ -929,39 +1001,39 @@ ss_merge_lists() {
rm -f "$final"
return 1
}
ss_filter_domains_shadowed_by_ancestors "$effective_allows" "$api_blocks" "$effective_api_blocks" || {
# Produce final rule lines directly rather than materializing both a full
# filtered API blocklist and a second combined domain list. The input API
# shards were already merged and sorted above; the final sort preserves the
# exact dnsmasq rule order/deduplication of the old pipeline.
if ss_dnsmasq_safeshield_block_supported; then
rule_prefix='safeshield-block=/'
rule_suffix='/'
else
rule_prefix='address=/'
rule_suffix='/#'
fi
ss_write_effective_block_rules \
"$effective_allows" "$effective_local_blocks" "$api_blocks" \
"$final" "$rule_prefix" "$rule_suffix" || {
rm -f "$final"
return 1
}
{
[ -f "$effective_local_blocks" ] && cat "$effective_local_blocks"
[ -f "$effective_api_blocks" ] && cat "$effective_api_blocks"
} | sort -u >"$final_blocks" || {
# Sort by the domain field (between slashes), not the whole dnsmasq rule.
# Sorting full rules changes legacy ordering for domain-prefix pairs such
# as foo and foo-bar, causing otherwise identical refreshes to differ.
# The domain field cannot contain '/' after normalization. -o keeps the
# output in-place without allocating a second full-size domain copy.
# Bound GNU sort's in-memory buffer on low-memory routers. External sort
# runs may use more temporary disk space; the final order is unchanged.
sort -S 1M -u -t/ -k2,2 -o "$final" "$final" || {
rm -f "$final"
return 1
}
# Preserve the cancellation point from the original merge pipeline.
ss_should_stop && {
rm -f "$final"
return 130
}
if [ -s "$final_blocks" ]; then
if ss_dnsmasq_safeshield_block_supported; then
awk '{ print "safeshield-block=/" $0 "/" }' "$final_blocks" >"$final" || {
rm -f "$final"
return 1
}
else
# Stock dnsmasq does not understand the SafeShield extension. Preserve
# protection with the standard address rule and leave statistics off.
awk '{ print "address=/" $0 "/#" }' "$final_blocks" >"$final" || {
rm -f "$final"
return 1
}
fi
fi
if [ -s "$effective_allows" ]; then
awk '{ print "server=/" $0 "/#" }' "$effective_allows" >>"$final" || {
rm -f "$final"
+2 -10
View File
@@ -46,7 +46,6 @@ readonly SS_MAX_ARTIFACT_SOURCES=16
readonly SS_PREV_BLOCKLIST_GZ="/tmp/safeshield.prev.blocklist.gz"
readonly SS_RUNTIME_OUT="${SS_TMP_DIR}/runtime.out"
readonly SS_REFRESH_LOCK='/var/lock/safeshield-refresh.lock'
readonly SS_REFRESH_LOCK_FD=307
readonly SS_LOCAL_APPLY_STATE="${SS_TMP_DIR}/local-apply.state"
readonly SS_STATISTICS_DIR="${SS_TMP_DIR}/statistics"
readonly SS_STATISTICS_STATE_FILE="${SS_STATISTICS_DIR}/state.tsv"
@@ -444,7 +443,6 @@ safeshield_apply_local_rules() {
*)
log_error "Failed to merge cached Hub artifact with local rules"
ss_mark_local_apply_failure "local_merge_failed"
ss_restore_and_restart
ss_refresh_lock_close
return 1
;;
@@ -647,9 +645,7 @@ safeshield_force_download() {
*)
failure_code="$(ss_resolve_error_code)"
ss_status_mark_failure "$failure_code"
if [ "$failure_code" != "safeshield_upgrade_required" ]; then
ss_restore_and_restart
fi
# No blocklist replacement has happened; keep the running DNS service.
ss_refresh_lock_close
return 1
;;
@@ -672,9 +668,7 @@ safeshield_force_download() {
*)
failure_code="$(ss_artifact_download_error_code)"
ss_status_mark_failure "$failure_code"
if [ "$failure_code" != "safeshield_upgrade_required" ]; then
ss_restore_and_restart
fi
# No blocklist replacement has happened; keep the running DNS service.
ss_refresh_lock_close
return 1
;;
@@ -689,7 +683,6 @@ safeshield_force_download() {
ss_prepare_local_rule_files || {
log_error "Failed to build allowlist"
ss_status_mark_failure "allowlist_build_failed"
ss_restore_and_restart
ss_refresh_lock_close
return 1
}
@@ -711,7 +704,6 @@ safeshield_force_download() {
*)
log_error "Failed to merge API artifact with local overrides"
ss_status_mark_failure "merge_failed"
ss_restore_and_restart
ss_refresh_lock_close
return 1
;;
@@ -0,0 +1,119 @@
# shellcheck shell=ash
# Directory locks work with the OpenWrt BusyBox base utilities; flock(1) is
# intentionally not required. A pid-less directory is treated as initializing
# (or abandoned) and must age before it can be reclaimed.
ss_lock_pid_alive() {
case "${1:-}" in
'' | *[!0-9]* | 0) return 1 ;;
esac
kill -0 "$1" 2>/dev/null
}
ss_lock_stale() {
local path="$1" owner='' modified='' now=''
[ -d "$path" ] || return 1
if [ -r "$path/pid" ]; then
IFS= read -r owner <"$path/pid" || true
# A live owner always wins, even if the directory appears old.
ss_lock_pid_alive "$owner" && return 1
# Never reclaim an invalid pid that could be mid-write without a grace period.
case "$owner" in
'' | *[!0-9]* | 0) ;;
*) return 0 ;;
esac
fi
# stat is available in the supported BusyBox builds. If it is missing,
# fail closed rather than destroying a possibly initializing lock.
modified="$(stat -c %Y "$path" 2>/dev/null)" || return 1
now="$(date +%s 2>/dev/null)" || return 1
case "$modified:$now" in
*[!0-9:]* | :* | *:) return 1 ;;
esac
[ "$((now - modified))" -ge 30 ] 2>/dev/null
}
ss_lock_reclaim() {
local path="$1" guard="${1}.reclaim" owner=''
# Only one contender may inspect/remove a stale directory at a time.
mkdir "$guard" 2>/dev/null || return 1
if ! ss_lock_stale "$path"; then
rmdir "$guard" 2>/dev/null || true
return 1
fi
if [ -r "$path/pid" ]; then
IFS= read -r owner <"$path/pid" || true
fi
if ss_lock_pid_alive "$owner"; then
rmdir "$guard" 2>/dev/null || true
return 1
fi
# A foreign file makes this fail safely instead of deleting arbitrary data.
rm -f "$path/pid" 2>/dev/null || true
if ! rmdir "$path" 2>/dev/null; then
rmdir "$guard" 2>/dev/null || true
return 1
fi
rmdir "$guard" 2>/dev/null || true
return 0
}
ss_lock_try() {
local path="$1" attempt=0
[ -n "$path" ] || return 1
mkdir -p "${path%/*}" || return 1
while [ "$attempt" -lt 2 ]; do
# Prevent new acquisitions while stale-lock removal is in progress.
if [ -d "${path}.reclaim" ]; then
# A crashed reclaimer must not block all future acquisitions forever.
# Refuse to touch a recent guard still owned by an active reclaimer.
ss_lock_stale "${path}.reclaim" && rmdir "${path}.reclaim" 2>/dev/null || return 1
fi
if mkdir "$path" 2>/dev/null; then
if ! printf '%s\n' "$$" >"$path/pid"; then
rmdir "$path" 2>/dev/null || true
return 1
fi
if [ -e "${path}.reclaim" ]; then
ss_lock_release "$path"
return 1
fi
return 0
fi
[ "$attempt" -eq 0 ] || break
ss_lock_reclaim "$path" || return 1
attempt=$((attempt + 1))
done
return 1
}
ss_lock_wait() {
local path="$1" timeout="${2:-10}" stop_cb="${3:-}" waited=0
case "$timeout" in
'' | *[!0-9]*) timeout=10 ;;
esac
while ! ss_lock_try "$path"; do
if [ "$waited" -ge "$timeout" ]; then
return 1
fi
if [ -n "$stop_cb" ] && "$stop_cb"; then
return 130
fi
sleep 1
waited=$((waited + 1))
done
return 0
}
ss_lock_release() {
local path="$1" owner=''
[ -n "$path" ] && [ -r "$path/pid" ] || return 0
IFS= read -r owner <"$path/pid" || true
[ "$owner" = "$$" ] || return 1
# Do not remove someone else's data even if the lock has extra contents.
rm -f "$path/pid" || return 1
rmdir "$path" 2>/dev/null
}
@@ -30,6 +30,16 @@ function device_bucket_key(key, bucket) {
function prune_buckets(now, cutoff, bucket, composite, parts) {
cutoff = hour_start(now) - ((retention_hours - 1) * 3600)
# Existing snapshots only add counters to the active hour. Repeating the
# full expired-bucket sweep every minute does no work until the cutoff
# advances. A collector restart starts with an empty cache and prunes once.
if (cutoff == last_prune_cutoff) {
return
}
last_prune_cutoff = cutoff
# A new retention window can invalidate cached totals even without DNS
# traffic; the full aggregate is rebuilt at the next snapshot.
aggregate_dirty = 1
for (bucket in queries) {
if ((bucket + 0) < cutoff) {
delete queries[bucket]
@@ -136,6 +146,7 @@ function register_device(key, mac, ip, hostname, client_id, duid, selected, o
# device record structurally complete, this avoids passing a missing awk
# associative-array element to helper functions during serialization.
device_seen[selected] = 1
aggregate_dirty = 1
device_mac[selected] = ""
device_ip[selected] = ""
device_hostname[selected] = ""
@@ -282,6 +282,7 @@ function migrate_device(from_key, to_key, mac, ip, hostname, client_id, duid,
if (!journal_replaying) {
journal_deleted_device[from_key] = 1
}
aggregate_dirty = 1
remove_device_metadata(from_key)
selected = register_device(to_key, mac, ip, hostname, client_id, duid)
if (selected == "") {
@@ -533,6 +534,7 @@ function record_device(ip, query_delta, blocked_delta, now, key, bucket, comp
bucket = hour_start(now)
composite = device_bucket_key(key, bucket)
aggregate_dirty = 1
device_hour_queries[composite] += 0
device_hour_blocked[composite] += 0
if (query_delta > 0) {
@@ -53,6 +53,12 @@ function save_json_range(path, now, first_hour, last_hour, include_empty_devices
if (!is_unknown_output_device(key)) {
continue
}
# An upload contains only a short recent window. Ignore provisional
# devices whose retained buckets fall completely outside that window.
if ((device_last_bucket[key] + 0) < first_hour ||
(device_first_bucket[key] + 0) > last_hour) {
continue
}
for (bucket = first_hour; bucket <= last_hour; bucket += 3600) {
composite = device_bucket_key(key, bucket)
if (!((composite in device_hour_queries) || (composite in device_hour_blocked))) {
@@ -228,9 +234,16 @@ function save_snapshot(now, force_persistent, force_snapshot, serialize_snaps
serialize_snapshot = snapshot_dirty || force_snapshot || (force_persistent && persistence_configured)
if (serialize_snapshot) {
prune_buckets(now)
recompute_totals()
# Error-only and unchanged UBus snapshots still refresh source metadata
# but do not change retained counters. Avoid reaggregating 128 devices
# across up to 168 hours unless counters, identities, or cutoff changed.
if (aggregate_dirty) {
recompute_totals()
aggregate_dirty = 0
}
if (compact_stale_provisional_devices(now) > 0) {
recompute_totals()
aggregate_dirty = 0
}
updated_at = now
if (!ensure_snapshot_sequence_reservation(now)) {
@@ -9,6 +9,7 @@ function record_event(query_delta, blocked_delta, client_ip, now, bucket) {
now = current_time()
bucket = hour_start(now)
snapshot_dirty = 1
aggregate_dirty = 1
queries[bucket] += 0
blocked[bucket] += 0
@@ -110,6 +111,7 @@ function apply_source_snapshot( now, bucket, had_source, same_epoch, query_de
}
if (query_delta > 0 || blocked_delta > 0) {
aggregate_dirty = 1
queries[bucket] += 0
blocked[bucket] += 0
queries[bucket] += query_delta
@@ -184,6 +186,9 @@ BEGIN {
}
event_index = 0
snapshot_dirty = 0
# BEGIN restores state and reconciles device identities before the first
# snapshot, so never trust persisted aggregates as an in-memory cache.
aggregate_dirty = 1
device_count = 0
devices_truncated = 0
last_lease_refresh = 0
@@ -62,6 +62,19 @@ else if (cmd == 'set') {
obj.data[key] = value;
}
else if (cmd == 'set_many') {
// Reject incomplete input before modifying any field; one JSON write is
// committed only after every key/value pair has been validated.
if (length(ARGV) < 4 || (length(ARGV) - 2) % 2 != 0)
exit(2);
for (let i = 2; i < length(ARGV); i += 2) {
if (!ARGV[i])
exit(2);
}
for (let i = 2; i < length(ARGV); i += 2)
obj.data[ARGV[i]] = ARGV[i + 1];
}
else if (cmd == 'add_error') {
let code = ARGV[2];
+90 -118
View File
@@ -10,26 +10,19 @@ ss_status_exec() {
}
ss_status_apply() {
local tmp="${RUNNING_STATUS_FILE}.tmp"
local tmp="${RUNNING_STATUS_FILE}.tmp.$$" rc=0
mkdir -p "${RUNNING_STATUS_FILE%/*}" || return 1
json_lock_open || return 1
ss_status_exec "$@" >"${tmp}" || {
rm -f "${tmp}"
return 1
}
[ -s "${tmp}" ] || {
rm -f "${tmp}"
return 1
}
mv -f "${tmp}" "${RUNNING_STATUS_FILE}" || {
rm -f "${tmp}"
return 1
}
return 0
ss_status_exec "$@" >"$tmp" || rc=1
[ "$rc" -eq 0 ] && [ -s "$tmp" ] || rc=1
if [ "$rc" -eq 0 ]; then
mv -f "$tmp" "$RUNNING_STATUS_FILE" || rc=1
fi
[ "$rc" -eq 0 ] || rm -f "$tmp"
json_lock_close
return "$rc"
}
ss_now() {
@@ -40,66 +33,77 @@ ss_status_set_now() {
ss_status_set "$1" "$(ss_now)"
}
# A single status-store invocation updates all pairs while holding the same
# JSON lock. This avoids spawning ucode once per field during a refresh reset.
ss_status_set_many() {
[ "$#" -gt 0 ] || return 0
ss_status_apply set_many "$@" >/dev/null 2>&1 || true
}
ss_status_reset_health_fields() {
ss_status_set health_dnsmasq_binary ""
ss_status_set health_dnsmasq_version ""
ss_status_set health_dnsmasq_features ""
ss_status_set health_dnsmasq_confdir ""
ss_status_set health_dnsmasq_initial_restart ""
ss_status_set health_dnsmasq_final_restart ""
ss_status_set health_dns_runtime ""
ss_status_set health_blocklist_verify ""
ss_status_set health_min_valid_line_count ""
ss_status_set health_max_file_size ""
ss_status_set health_api_resolve ""
ss_status_set health_artifact_download ""
ss_status_set health_artifact_sha256 ""
ss_status_set dnsmasq_version ""
ss_status_set dnsmasq_min_version "${SS_MIN_DNSMASQ_VERSION:-2.93}"
ss_status_set_many \
health_dnsmasq_binary "" \
health_dnsmasq_version "" \
health_dnsmasq_features "" \
health_dnsmasq_confdir "" \
health_dnsmasq_initial_restart "" \
health_dnsmasq_final_restart "" \
health_dns_runtime "" \
health_blocklist_verify "" \
health_min_valid_line_count "" \
health_max_file_size "" \
health_api_resolve "" \
health_artifact_download "" \
health_artifact_sha256 "" \
dnsmasq_version "" \
dnsmasq_min_version "${SS_MIN_DNSMASQ_VERSION:-2.93}"
}
ss_status_reset_blocklist_fields() {
ss_status_set blocklist_installed "0"
ss_status_set blocklist_file_size_kb "0"
ss_status_set blocklist_verification_ok "0"
ss_status_set blocklist_test_domain ""
ss_status_set blocklist_test_domain_sample_count "0"
ss_status_set blocklist_test_domain_success_count "0"
ss_status_set blocklist_test_domains ""
ss_status_set blocklist_backup_available "0"
ss_status_set_many \
blocklist_installed "0" \
blocklist_file_size_kb "0" \
blocklist_verification_ok "0" \
blocklist_test_domain "" \
blocklist_test_domain_sample_count "0" \
blocklist_test_domain_success_count "0" \
blocklist_test_domains "" \
blocklist_backup_available "0"
}
ss_status_reset_artifact_fields() {
ss_status_set entitlement_plan ""
ss_status_set entitlement_status ""
ss_status_set physical_fingerprint ""
ss_status_set fingerprint_version ""
ss_status_set identity_provider ""
ss_status_set identity_source ""
ss_status_set identity_strength ""
ss_status_set identity_profile ""
ss_status_set device_code ""
ss_status_set device_code_source ""
ss_status_set installation_id ""
ss_status_set device_profile ""
ss_status_set artifact_tier ""
ss_status_set artifact_version ""
ss_status_set artifact_sha256 ""
ss_status_set artifact_unique_domains "0"
ss_status_set artifact_rules "0"
ss_status_set artifact_download_url_present "0"
ss_status_set artifact_source_count "0"
ss_status_set artifact_block_source_count "0"
ss_status_set artifact_allow_source_count "0"
ss_status_set_many \
entitlement_plan "" \
entitlement_status "" \
physical_fingerprint "" \
fingerprint_version "" \
identity_provider "" \
identity_source "" \
identity_strength "" \
identity_profile "" \
device_code "" \
device_code_source "" \
installation_id "" \
device_profile "" \
artifact_tier "" \
artifact_version "" \
artifact_sha256 "" \
artifact_unique_domains "0" \
artifact_rules "0" \
artifact_download_url_present "0" \
artifact_source_count "0" \
artifact_block_source_count "0" \
artifact_allow_source_count "0"
}
ss_status_mark_failure() {
local code="$1"
ss_status_set status "error"
ss_status_set_now last_failure
ss_status_set last_result "error"
ss_status_set last_error_code "$code"
ss_status_set_many \
status "error" \
last_failure "$(ss_now)" \
last_result "error" \
last_error_code "$code"
ss_status_add_error "$code"
}
@@ -109,14 +113,15 @@ ss_status_mark_success() {
now="$(ss_now)"
ss_status_apply clear_messages >/dev/null 2>&1 || true
ss_status_set status "ready"
ss_status_set stage "done"
ss_status_set last_success "$now"
ss_status_set last_result "success"
ss_status_set last_error_code ""
if is_valid_integer "$interval" && [ "$interval" -gt 0 ] 2>/dev/null; then
ss_status_set next_refresh_at "$((now + interval))"
ss_status_set_many \
status "ready" stage "done" last_success "$now" \
last_result "success" last_error_code "" \
next_refresh_at "$((now + interval))"
else
ss_status_set_many \
status "ready" stage "done" last_success "$now" \
last_result "success" last_error_code ""
fi
}
@@ -131,9 +136,10 @@ ss_status_prepare_refresh() {
is_valid_integer "$last_local_apply_failure" || last_local_apply_failure=0
ss_status_reset
ss_status_set last_success "$last_success"
ss_status_set last_local_apply "$last_local_apply"
ss_status_set last_local_apply_failure "$last_local_apply_failure"
ss_status_set_many \
last_success "$last_success" \
last_local_apply "$last_local_apply" \
last_local_apply_failure "$last_local_apply_failure"
}
ss_status_set() {
@@ -159,22 +165,14 @@ ss_status_add_warning() {
}
ss_status_reset() {
rm -f "${RUNNING_STATUS_FILE}" "${RUNNING_STATUS_FILE}.tmp"
ss_status_apply reset >/dev/null 2>&1 || return 1
ss_status_set status "idle"
ss_status_set stage ""
ss_status_set valid_line_count "0"
ss_status_set last_result "idle"
ss_status_set last_error_code ""
ss_status_set last_attempt "0"
ss_status_set last_success "0"
ss_status_set last_failure "0"
ss_status_set last_local_apply "0"
ss_status_set last_local_apply_failure "0"
ss_status_set next_refresh_at "0"
ss_status_set_many \
status "idle" stage "" valid_line_count "0" \
last_result "idle" last_error_code "" \
last_attempt "0" last_success "0" last_failure "0" \
last_local_apply "0" last_local_apply_failure "0" \
next_refresh_at "0"
ss_status_reset_health_fields
ss_status_reset_blocklist_fields
@@ -262,41 +260,15 @@ ss_restore_previous_blocklist() {
}
ss_refresh_lock_open() {
mkdir -p "${SS_REFRESH_LOCK%/*}" || return 1
eval "exec ${SS_REFRESH_LOCK_FD}>\"${SS_REFRESH_LOCK}\"" || return 1
flock -n "${SS_REFRESH_LOCK_FD}" || {
eval "exec ${SS_REFRESH_LOCK_FD}>&-"
return 1
}
ss_lock_try "${SS_REFRESH_LOCK}.d"
}
ss_refresh_lock_open_wait() {
local timeout="${1:-120}"
local waited=0
is_valid_integer "$timeout" || timeout=120
mkdir -p "${SS_REFRESH_LOCK%/*}" || return 1
eval "exec ${SS_REFRESH_LOCK_FD}>\"${SS_REFRESH_LOCK}\"" || return 1
while ! flock -n "${SS_REFRESH_LOCK_FD}"; do
if [ "$waited" -ge "$timeout" ] 2>/dev/null; then
eval "exec ${SS_REFRESH_LOCK_FD}>&-" 2>/dev/null || true
return 1
fi
ss_should_stop && {
eval "exec ${SS_REFRESH_LOCK_FD}>&-" 2>/dev/null || true
return 130
}
sleep 1
waited=$((waited + 1))
done
return 0
ss_lock_wait "${SS_REFRESH_LOCK}.d" "$timeout" ss_should_stop
}
ss_refresh_lock_close() {
flock -u "${SS_REFRESH_LOCK_FD}" 2>/dev/null || true
eval "exec ${SS_REFRESH_LOCK_FD}>&-" 2>/dev/null || true
ss_lock_release "${SS_REFRESH_LOCK}.d"
}
+20 -12
View File
@@ -1,7 +1,9 @@
# shellcheck shell=ash
readonly LOCK_FD=309
readonly RUNNING_STATUS_LOCK="/var/lock/${PKG_NAME}.lock"
readonly RUNNING_STATUS_LOCK="/var/lock/${PKG_NAME}.lock.d"
# shellcheck disable=SC1090
. "${SS_LOCKING_LIB:-${IPKG_INSTROOT:-}/usr/lib/safeshield/locking.sh}"
readonly RUNNING_STATUS_FILE="/dev/shm/${PKG_NAME}.status.json"
command_exists() {
@@ -21,17 +23,23 @@ is_greater_equal() {
}
json_lock_open() {
mkdir -p "${RUNNING_STATUS_LOCK%/*}" || return 1
eval "exec ${LOCK_FD}>\"${RUNNING_STATUS_LOCK}\"" || return 1
flock -x "${LOCK_FD}" || {
eval "exec ${LOCK_FD}>&-"
return 1
}
ss_lock_wait "$RUNNING_STATUS_LOCK" 10
}
json_lock_close() {
flock -u "${LOCK_FD}" 2>/dev/null || true
eval "exec ${LOCK_FD}>&-" 2>/dev/null || true
ss_lock_release "$RUNNING_STATUS_LOCK"
}
_json_commit() {
local tmp="${RUNNING_STATUS_FILE}.tmp.$$"
json_dump >"$tmp" || {
rm -f "$tmp"
return 1
}
mv -f "$tmp" "$RUNNING_STATUS_FILE" || {
rm -f "$tmp"
return 1
}
}
_json_init_data_object() {
@@ -62,7 +70,7 @@ _json_set() {
json_add_string "$key" "$value"
json_select .. 2>/dev/null || true
json_dump >"${RUNNING_STATUS_FILE}"
_json_commit
}
_json_add_message() {
@@ -82,7 +90,7 @@ _json_add_message() {
json_add_string info "$value"
json_close_object
json_select .. 2>/dev/null || true
json_dump >"${RUNNING_STATUS_FILE}"
_json_commit
}
json() {
@@ -14,20 +14,26 @@ export IPKG_INSTROOT
. /usr/lib/safeshield/core.sh
readonly SS_REFRESHD_LOCK='/var/lock/safeshield-refreshd.lock'
readonly SS_REFRESHD_LOCK_FD=308
refreshd_owned=0
ss_should_terminate=0
ss_sleep_pid=''
refreshd_lock() {
mkdir -p "${SS_REFRESHD_LOCK%/*}" || return 1
eval "exec ${SS_REFRESHD_LOCK_FD}>\"${SS_REFRESHD_LOCK}\"" || return 1
flock -n "${SS_REFRESHD_LOCK_FD}" || return 1
ss_lock_try "${SS_REFRESHD_LOCK}.d" || return 1
refreshd_owned=1
}
refreshd_unlock() {
flock -u "${SS_REFRESHD_LOCK_FD}" 2>/dev/null || true
eval "exec ${SS_REFRESHD_LOCK_FD}>&-" 2>/dev/null || true
[ "${refreshd_owned:-0}" -eq 1 ] || return 0
ss_lock_release "${SS_REFRESHD_LOCK}.d"
refreshd_owned=0
}
refreshd_cleanup() {
[ "${refreshd_owned:-0}" -eq 1 ] || return 0
clear_next_refresh_at
refreshd_unlock
}
handle_term() {
@@ -253,6 +259,6 @@ main() {
}
trap "handle_term" TERM INT
trap "refreshd_unlock; clear_next_refresh_at" EXIT
trap "refreshd_cleanup" EXIT
main "$@"
+7 -21
View File
@@ -7,6 +7,7 @@ LC_ALL=C
export LC_ALL
umask 077
SS_STATSD_LOCK_LIB="${SS_STATSD_LOCK_LIB:-/usr/lib/safeshield/locking.sh}"
SS_STATSD_FUNCTIONS_LIB="${SS_STATSD_FUNCTIONS_LIB:-/lib/functions.sh}"
SS_STATSD_CORE_LIB="${SS_STATSD_CORE_LIB:-/usr/lib/safeshield/core.sh}"
SS_STATSD_AWK_DIR="${SS_STATSD_AWK_DIR:-/usr/lib/safeshield/statistics}"
@@ -24,6 +25,8 @@ SS_STATSD_PERSISTENT_COMPACT_INTERVAL_S="${SS_STATSD_PERSISTENT_COMPACT_INTERVAL
. "$SS_STATSD_FUNCTIONS_LIB"
# shellcheck disable=SC1090
. "$SS_STATSD_CORE_LIB"
# shellcheck disable=SC1090
. "$SS_STATSD_LOCK_LIB"
stats_poller_pid=''
stats_awk_pid=''
@@ -74,7 +77,8 @@ stats_cleanup() {
[ -n "$stats_fifo" ] && rm -f "$stats_fifo"
if [ "$stats_lock_owned" -eq 1 ] && [ -n "$stats_lock_dir" ]; then
rm -rf "$stats_lock_dir"
ss_lock_release "$stats_lock_dir" || true
stats_lock_owned=0
fi
}
@@ -85,30 +89,12 @@ stats_terminate() {
}
stats_acquire_lock() {
local owner_pid=''
stats_lock_dir="${SS_STATISTICS_DIR}/collector.lock"
if mkdir "$stats_lock_dir" 2>/dev/null; then
stats_lock_owned=1
printf '%s\n' "$$" >"${stats_lock_dir}/pid"
return 0
fi
if [ -r "${stats_lock_dir}/pid" ]; then
owner_pid="$(cat "${stats_lock_dir}/pid" 2>/dev/null || true)"
fi
if stats_process_running "$owner_pid"; then
log_warn "Statistics collector is already running (pid=${owner_pid})"
if ! ss_lock_try "$stats_lock_dir"; then
log_warn 'Statistics collector lock is already held or unavailable'
return 1
fi
log_warn 'Removing stale statistics collector lock'
rm -rf "$stats_lock_dir" || return 1
mkdir "$stats_lock_dir" || return 1
stats_lock_owned=1
printf '%s\n' "$$" >"${stats_lock_dir}/pid"
return 0
}
@@ -7,6 +7,7 @@
"status",
"config",
"statistics",
"statistics_summary",
"rules_list"
]
}
@@ -24,6 +24,10 @@ return {
call: statistics.build
},
statistics_summary: {
call: statistics.summary
},
config_update: {
args: {
values: {}
@@ -84,7 +84,7 @@ function sanitize_source(item) {
};
}
function build_statistics() {
function build_statistics(include_detail) {
reload_uci();
let enabled = to_bool(cfg('statistics_enabled', '1'), true);
let retention_hours = to_int(cfg('statistics_retention_hours', '168'), 168);
@@ -98,7 +98,7 @@ function build_statistics() {
if (!effective_enabled) {
source.available = false;
}
return {
let result = {
schema: {
name: STATISTICS_SCHEMA_NAME,
version: STATISTICS_SCHEMA_VERSION
@@ -133,13 +133,26 @@ function build_statistics() {
totals: {
queries: to_int(totals.queries, 0),
blocked: to_int(totals.blocked, 0)
},
hourly: sanitize_hourly(data.hourly),
device_limit: to_int(data.device_limit, 128),
devices_truncated: to_bool(data.devices_truncated, false),
devices: sanitize_devices(data.devices)
}
};
// Preserve the legacy detailed RPC field insertion order as well as
// names/values: totals -> hourly -> device_limit -> devices_truncated ->
// devices. Summary callers omit only the two large detail arrays.
if (include_detail !== false) {
result.hourly = sanitize_hourly(data.hourly);
}
result.device_limit = to_int(data.device_limit, 128);
result.devices_truncated = to_bool(data.devices_truncated, false);
if (include_detail !== false) {
result.devices = sanitize_devices(data.devices);
}
return result;
}
function build_statistics_summary() {
return build_statistics(false);
}
return {
build: build_statistics
build: build_statistics,
summary: build_statistics_summary
};