Skip to content

Commit 35a4b9a

Browse files
Added F/W Update Lock File
Added a mutually exclusive, non-blocking FLOCK mechanism in MerlinAU so that whenever AMTM initiates script updates or MerlinAU starts an F/W Update, each one can check the FLOCK mutex to make sure only one of them can perform its intended operation without "stepping on each other's toes" and avoid any possible corruption of a script update.
1 parent 1cc1448 commit 35a4b9a

2 files changed

Lines changed: 79 additions & 10 deletions

File tree

MerlinAU.sh

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#
55
# Original Creation Date: 2023-Oct-01 by @ExtremeFiretop.
66
# Official Co-Author: @Martinski W. - Date: 2023-Nov-01
7-
# Last Modified: 2026-Mar-17
7+
# Last Modified: 2026-Mar-18
88
###################################################################
99
set -u
1010

1111
## Set version for each Production Release ##
1212
readonly SCRIPT_VERSION=1.6.0
13-
readonly SCRIPT_VERSTAG="26031700"
13+
readonly SCRIPT_VERSTAG="26031800"
1414
readonly SCRIPT_NAME="MerlinAU"
1515
## Set to "master" for Production Releases ##
1616
SCRIPT_BRANCH="dev"
@@ -476,8 +476,7 @@ _AcquireLock_()
476476
retCode=1
477477
lockTypeFound=""
478478
waitTimeoutSecs=0
479-
savedVerbose="$isVerbose"
480-
isVerbose=true
479+
savedVerbose="$isVerbose" ; isVerbose=true
481480

482481
while true
483482
do
@@ -531,6 +530,66 @@ _AcquireLock_()
531530
return "$retCode"
532531
}
533532

533+
##-------------------------------------##
534+
## Added by Martinski W. [2026-Mar-18] ##
535+
##-------------------------------------##
536+
fwupMutexFLock_FD=576
537+
fwupMutexFLock_FN="/tmp/var/${ScriptFNameTag}_FW_Update.FLock"
538+
539+
_ReleaseMutexFLock_()
540+
{
541+
printf '' > "$fwupMutexFLock_FN"
542+
flock -u "$fwupMutexFLock_FD" 2>/dev/null
543+
}
544+
545+
##-------------------------------------##
546+
## Added by Martinski W. [2026-Mar-18] ##
547+
##-------------------------------------##
548+
#--------------------------------------------------------------#
549+
# This is a mutually exclusive, non-blocking FLOCK mechanism
550+
# to be used when MerlinAU is perforning a F/W Update so that
551+
# AMTM can check and prevent running automatic script updates
552+
# while the F/W Update is in progress.
553+
#--------------------------------------------------------------#
554+
_AcquireMutexFLock_()
555+
{
556+
local retCode savedVerbose
557+
local procInfo procName="" procIDno="" procIDof=""
558+
559+
savedVerbose="$isVerbose" ; isVerbose=true
560+
561+
if [ -s "$fwupMutexFLock_FN" ]
562+
then
563+
procInfo="$(head -n1 "$fwupMutexFLock_FN")"
564+
procName="$(echo "$procInfo" | cut -d'|' -f1)"
565+
procIDno="$(echo "$procInfo" | cut -d'|' -f2)"
566+
if [ -n "$procName" ] && [ -n "$procIDno" ]
567+
then procIDof="$(pidof "$procName")"
568+
fi
569+
if [ -z "$procIDof" ] || ! echo "$procIDof" | grep -qow "$procIDno"
570+
then
571+
Say "Stale F/W Update Lock Found. Resetting lock file..."
572+
_ReleaseMutexFLock_
573+
fi
574+
fi
575+
576+
[ ! -s "$fwupMutexFLock_FN" ] && \
577+
eval exec "$fwupMutexFLock_FD>$fwupMutexFLock_FN"
578+
579+
if flock -x -n "$fwupMutexFLock_FD" 2>/dev/null
580+
then
581+
printf "$(basename "$0")|$$\n" > "$fwupMutexFLock_FN"
582+
retCode=0
583+
else
584+
procInfo="$(head -n1 "$fwupMutexFLock_FN")"
585+
Say "${REDct}**ERROR**${NOct}: Another process [$procInfo] has the F/W Update Lock file."
586+
retCode=1
587+
fi
588+
589+
isVerbose="$savedVerbose"
590+
return "$retCode"
591+
}
592+
534593
##-------------------------------------##
535594
## Added by Martinski W. [2025-Sep-01] ##
536595
##-------------------------------------##
@@ -9126,10 +9185,12 @@ _RunOfflineUpdateNow_()
91269185
FW_DL_FPATH="${FW_ZIP_DIR}/${FW_FileName}.${extension}"
91279186
_GnutonBuildSelection_
91289187
fi
9129-
if _AcquireLock_ cliFileLock
9188+
if _AcquireLock_ cliFileLock && \
9189+
_AcquireMutexFLock_
91309190
then
91319191
_RunFirmwareUpdateNow_
91329192
_ReleaseLock_ cliFileLock
9193+
_ReleaseMutexFLock_
91339194
fi
91349195
_ClearOfflineUpdateState_
91359196
else
@@ -9953,10 +10014,12 @@ _PostRebootRunNow_()
995310014

995410015
Say "END of $logMsg [$curWaitDelaySecs sec.]"
995510016
sleep 30 ## Let's wait a bit & proceed ##
9956-
if _AcquireLock_ cliFileLock
10017+
if _AcquireLock_ cliFileLock && \
10018+
_AcquireMutexFLock_
995710019
then
995810020
_RunFirmwareUpdateNow_
995910021
_ReleaseLock_ cliFileLock
10022+
_ReleaseMutexFLock_
996010023
fi
996110024
}
996210025

@@ -11615,10 +11678,12 @@ _MainMenu_()
1161511678
HIDE_ROUTER_SECTION=true
1161611679
fi
1161711680
;;
11618-
1) if _AcquireLock_ cliFileLock
11681+
1) if _AcquireLock_ cliFileLock && \
11682+
_AcquireMutexFLock_
1161911683
then
1162011684
_RunFirmwareUpdateNow_
1162111685
_ReleaseLock_ cliFileLock
11686+
_ReleaseMutexFLock_
1162211687
FlashStarted=false
1162311688
fi
1162411689
;;
@@ -11822,10 +11887,12 @@ then
1182211887

1182311888
case "$1" in
1182411889
run_now)
11825-
if _AcquireLock_ cliFileLock
11890+
if _AcquireLock_ cliFileLock && \
11891+
_AcquireMutexFLock_
1182611892
then
1182711893
_RunFirmwareUpdateNow_
1182811894
_ReleaseLock_ cliFileLock
11895+
_ReleaseMutexFLock_
1182911896
fi
1183011897
;;
1183111898
processNodes) _ProcessMeshNodes_ false
@@ -11914,7 +11981,8 @@ then
1191411981
;;
1191511982
"${SCRIPT_NAME}checkfwupdate" | \
1191611983
"${SCRIPT_NAME}checkfwupdate_bypassDays")
11917-
if _AcquireLock_ cliFileLock
11984+
if _AcquireLock_ cliFileLock && \
11985+
_AcquireMutexFLock_
1191811986
then
1191911987
if [ "$3" = "${SCRIPT_NAME}checkfwupdate_bypassDays" ]
1192011988
then bypassPostponedDays=true
@@ -11923,6 +11991,7 @@ then
1192311991
webguiMode=true
1192411992
_RunFirmwareUpdateNow_
1192511993
_ReleaseLock_ cliFileLock
11994+
_ReleaseMutexFLock_
1192611995
fi
1192711996
;;
1192811997
"${SCRIPT_NAME}scrptupdate" | \

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# MerlinAU - AsusWRT-Merlin Firmware Auto Updater
22

33
## v1.6.0
4-
## 2026-Mar-17
4+
## 2026-Mar-18
55

66
## WebUI:
77
![image](https://github.com/user-attachments/assets/9c1dff99-9c13-491b-a7fa-aff924d5f02e)

0 commit comments

Comments
 (0)