From af35ab5042c9ad5cac23c880cbe75d8986b798f0 Mon Sep 17 00:00:00 2001 From: jinsaihang Date: Thu, 26 Jun 2025 13:05:28 +0000 Subject: [PATCH] make sure make_hotpath is running in a single instance Signed-off-by: jinsaihang (cherry picked from commit 7d447617065d4dcc37f664cf05c11ece220ce9bc) --- kpatch.spec | 8 +++++++- make_hotpatch | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/kpatch.spec b/kpatch.spec index e3db1f9..c0fbc39 100644 --- a/kpatch.spec +++ b/kpatch.spec @@ -1,7 +1,7 @@ Name: kpatch Epoch: 1 Version: 0.9.9 -Release: 13 +Release: 14 Summary: A Linux dynamic kernel patching infrastructure License: GPLv2 @@ -127,6 +127,12 @@ popd %{_mandir}/man1/*.1.gz %changelog +* Thu Jun 26 2025 jinsaihang - 1:0.9.9-14 +- Type:bugfix +- CVE:NA +- SUG:NA +- DESC:make sure make_hotpath is running in a single instance + * Tue Jun 03 2025 jinsaihang - 1:0.9.9-13 - Type:bugfix - CVE:NA diff --git a/make_hotpatch b/make_hotpatch index a95022b..358754c 100644 --- a/make_hotpatch +++ b/make_hotpatch @@ -121,27 +121,27 @@ function fn_check_reg_char() echo "error: the string is empty, check string failed" return 1 fi - ##字符串长度 + ## character string length if [ -n "$l_str_maxlen" ] && [ -z "`echo $l_str_maxlen | sed 's/[0-9]//g'`" ];then if [ $len_str -gt $l_str_maxlen ]; then echo "error: The length of $l_str exceed max length $l_str_maxlen." return 1 fi fi - #不能包含其他字符 + # cannot contain other characters if [ -n "$l_str_ex" ] && [ -n "`echo $l_str | grep -E [$l_str_ex] 2>/dev/null`" ];then echo "error: string $l_str included characters $l_str_ex." return 1 fi - ##数字和字母开头 + # starting with numbers and letters if [ -z "`echo $l_str | grep -E '^[a-z0-9A-Z]'`" ];then echo "error: string $l_str must start with a character ('0-9' or 'A-Z' or 'a-z')." return 1 fi - ##只能数字和字母 -_ + # only '-' and '_' numbers and letters are allowed if [ -n "`echo $l_str | grep -E '[^a-z0-9A-Z_-]'`" ];then if [ -n "$l_str_ex" ] ;then - ##这个日志写不好 + # starting with numbers and letters echo "error: string $l_str can only contain characters included by ('0-9', 'a-z', 'A-Z')" else echo "error: string $l_str can only contain characters included by ('0-9', 'a-z', 'A-Z', '-', '_')." @@ -669,9 +669,37 @@ function fn_prepare() touch $G_KPATCH_FLAGS } -G_NUM=`pidof -x make_hotpatch | wc -w` -if [ $G_NUM -gt 2 ];then - echo "[$0]someone is making, please try again later." +# define the lock file +LOCK_FILE="/var/run/make_hotpatch.lock" +# define the lock value +LOCK_FD=200 + +# get the pid +make_hotpatch_pid=$$ + +if [ -e "$LOCK_FILE" ]; then + lock_pid=$(cat "$LOCK_FILE") + # check if the lock process is running + if ! kill -0 "$lock_pid" 2>/dev/null; then + # if the process is not running, remove lock file + rm -f "$LOCK_FILE" + else + # if the lock process is running, exit the script + echo "[$0] Someone is making, please try again later." + exit 1 + fi +fi + +# trap exception exit and delete lock file +trap 'rm -f "$LOCK_FILE"' EXIT TERM INT + +# binding the lock value to Lock File +exec {LOCK_FD}> "$LOCK_FILE" +echo $$ > "$LOCK_FILE" + +# obtaining a Lock in Non-Block Mode +if ! flock -n $LOCK_FD; then + echo "[$0] Someone is making, please try again later." exit 1 fi -- Gitee