mwan3: replace $(cat ..) with readfile

Remove a fork+exec

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
This commit is contained in:
Etienne Champetier
2025-06-27 19:18:51 -04:00
committed by Florian Eckert
parent de98fdebef
commit ecd2470ddb
5 changed files with 28 additions and 19 deletions

View File

@@ -45,7 +45,7 @@ config_get_bool enabled $INTERFACE 'enabled' '0'
config_get initial_state $INTERFACE initial_state "online" config_get initial_state $INTERFACE initial_state "online"
if [ "$initial_state" = "offline" ]; then if [ "$initial_state" = "offline" ]; then
status=$(cat $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS 2>/dev/null || echo unknown) readfile status $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS 2>/dev/null || status="unknown"
[ "$status" = "online" ] || status=offline [ "$status" = "online" ] || status=offline
else else
status=online status=online

View File

@@ -141,7 +141,7 @@ mwan3_init()
# mwan3's MARKing mask (at least 3 bits should be set) # mwan3's MARKing mask (at least 3 bits should be set)
if [ -e "${MWAN3_STATUS_DIR}/mmx_mask" ]; then if [ -e "${MWAN3_STATUS_DIR}/mmx_mask" ]; then
MMX_MASK=$(cat "${MWAN3_STATUS_DIR}/mmx_mask") readfile MMX_MASK "${MWAN3_STATUS_DIR}/mmx_mask"
MWAN3_INTERFACE_MAX=$(uci_get_state mwan3 globals iface_max) MWAN3_INTERFACE_MAX=$(uci_get_state mwan3 globals iface_max)
else else
config_get MMX_MASK globals mmx_mask '0x3F00' config_get MMX_MASK globals mmx_mask '0x3F00'
@@ -208,14 +208,15 @@ mwan3_count_one_bits()
} }
get_uptime() { get_uptime() {
local uptime=$(cat /proc/uptime) local uptime
readfile uptime /proc/uptime
echo "${uptime%%.*}" echo "${uptime%%.*}"
} }
get_online_time() { get_online_time() {
local time_n time_u iface local time_n time_u iface
iface="$1" iface="$1"
time_u="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/ONLINE" 2>/dev/null)" readfile time_u "$MWAN3TRACK_STATUS_DIR/${iface}/ONLINE" 2>/dev/null
[ -z "${time_u}" ] || [ "${time_u}" = "0" ] || { [ -z "${time_u}" ] || [ "${time_u}" = "0" ] || {
time_n="$(get_uptime)" time_n="$(get_uptime)"
echo $((time_n-time_u)) echo $((time_n-time_u))

View File

@@ -996,7 +996,7 @@ mwan3_interface_hotplug_shutdown()
interface="$1" interface="$1"
ifdown="$2" ifdown="$2"
[ -f $MWAN3TRACK_STATUS_DIR/$interface/STATUS ] && { [ -f $MWAN3TRACK_STATUS_DIR/$interface/STATUS ] && {
status=$(cat $MWAN3TRACK_STATUS_DIR/$interface/STATUS) readfile status $MWAN3TRACK_STATUS_DIR/$interface/STATUS
} }
[ "$status" != "online" ] && [ "$ifdown" != 1 ] && return [ "$status" != "online" ] && [ "$ifdown" != 1 ] && return
@@ -1076,8 +1076,9 @@ mwan3_set_iface_hotplug_state() {
mwan3_get_iface_hotplug_state() { mwan3_get_iface_hotplug_state() {
local iface=$1 local iface=$1
local state=offline
cat "$MWAN3_STATUS_DIR/iface_state/$iface" 2>/dev/null || echo "offline" readfile state "$MWAN3_STATUS_DIR/iface_state/$iface"
echo "$state"
} }
mwan3_report_iface_status() mwan3_report_iface_status()
@@ -1101,7 +1102,7 @@ mwan3_report_iface_status()
fi fi
if [ -f "$MWAN3TRACK_STATUS_DIR/${1}/STATUS" ]; then if [ -f "$MWAN3TRACK_STATUS_DIR/${1}/STATUS" ]; then
status="$(cat "$MWAN3TRACK_STATUS_DIR/${1}/STATUS")" readfile status "$MWAN3TRACK_STATUS_DIR/${1}/STATUS"
else else
status="unknown" status="unknown"
fi fi

View File

@@ -68,7 +68,7 @@ report_policies_v6() {
get_age() { get_age() {
local time_p time_u local time_p time_u
iface="$1" iface="$1"
time_p="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TIME")" readfile time_p "$MWAN3TRACK_STATUS_DIR/${iface}/TIME"
[ -z "${time_p}" ] || { [ -z "${time_p}" ] || {
time_n="$(get_uptime)" time_n="$(get_uptime)"
echo $((time_n-time_p)) echo $((time_n-time_p))
@@ -78,7 +78,7 @@ get_age() {
get_offline_time() { get_offline_time() {
local time_n time_d iface local time_n time_d iface
iface="$1" iface="$1"
time_d="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/OFFLINE")" readfile time_d "$MWAN3TRACK_STATUS_DIR/${iface}/OFFLINE"
[ -z "${time_d}" ] || [ "${time_d}" = "0" ] || { [ -z "${time_d}" ] || [ "${time_d}" = "0" ] || {
time_n="$(get_uptime)" time_n="$(get_uptime)"
echo $((time_n-time_d)) echo $((time_n-time_d))
@@ -92,7 +92,7 @@ get_mwan3_status() {
local age=0 local age=0
local online=0 local online=0
local offline=0 local offline=0
local enabled time_p time_n time_u time_d status track_status up uptime local enabled time_p time_n time_u time_d status track_status up uptime temp
if [ "${iface}" != "${iface_select}" ] && [ "${iface_select}" != "" ]; then if [ "${iface}" != "${iface_select}" ] && [ "${iface_select}" != "" ]; then
return return
@@ -109,7 +109,7 @@ get_mwan3_status() {
if [ -f "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS" ]; then if [ -f "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS" ]; then
network_get_uptime uptime "$iface" network_get_uptime uptime "$iface"
network_is_up "$iface" && up="1" network_is_up "$iface" && up="1"
status="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS")" readfile status "$MWAN3TRACK_STATUS_DIR/${iface}/STATUS"
else else
uptime=0 uptime=0
up=0 up=0
@@ -121,9 +121,12 @@ get_mwan3_status() {
json_add_int online "${online}" json_add_int online "${online}"
json_add_int offline "${offline}" json_add_int offline "${offline}"
json_add_int uptime "${uptime}" json_add_int uptime "${uptime}"
json_add_int "score" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/SCORE")" readfile temp "$MWAN3TRACK_STATUS_DIR/${iface}/SCORE"
json_add_int "lost" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LOST")" json_add_int "score" "$temp"
json_add_int "turn" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/TURN")" readfile temp "$MWAN3TRACK_STATUS_DIR/${iface}/LOST"
json_add_int "lost" "$temp"
readfile temp "$MWAN3TRACK_STATUS_DIR/${iface}/TURN"
json_add_int "turn" "$temp"
json_add_string "status" "${status}" json_add_string "status" "${status}"
json_add_boolean "enabled" "${enabled}" json_add_boolean "enabled" "${enabled}"
json_add_boolean "running" "${running}" json_add_boolean "running" "${running}"
@@ -136,9 +139,12 @@ get_mwan3_status() {
track="${file#*/TRACK_}" track="${file#*/TRACK_}"
json_add_object json_add_object
json_add_string ip "${track}" json_add_string ip "${track}"
json_add_string status "$(cat "${file}")" readfile temp "${file}"
json_add_int latency "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LATENCY_${track}")" json_add_string status "$temp"
json_add_int packetloss "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}/LOSS_${track}")" readfile temp "$MWAN3TRACK_STATUS_DIR/${iface}/LATENCY_${track}"
json_add_int latency "$temp"
readfile temp "$MWAN3TRACK_STATUS_DIR/${iface}/LOSS_${track}"
json_add_int packetloss "$temp"
json_close_object json_close_object
done done
json_close_array json_close_array

View File

@@ -111,7 +111,8 @@ validate_wrap() {
} }
disconnected() { disconnected() {
local status="$(cat ${MWAN3TRACK_STATUS_DIR}/${INTERFACE}/STATUS)" local status
readfile status ${MWAN3TRACK_STATUS_DIR}/${INTERFACE}/STATUS
STATUS='offline' STATUS='offline'
echo "offline" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS echo "offline" > $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS