[make-initrd] [PATCH 1/3] runtime: Add locking functions based on flock utility
Alexey Gladkov
gladkov.alexey at gmail.com
Wed May 17 19:04:40 MSK 2023
Signed-off-by: Alexey Gladkov <gladkov.alexey at gmail.com>
---
data/bin/shell-locks | 40 +++++++++++++++
testing/units/runtime-locks/ts0001/expect | 24 +++++++++
testing/units/runtime-locks/ts0001/lock-1 | 0
testing/units/runtime-locks/ts0001/lock-2 | 0
testing/units/runtime-locks/ts0001/lock-3 | 0
testing/units/runtime-locks/ts0001/run | 61 +++++++++++++++++++++++
testing/units/runtime-locks/ts0002/expect | 5 ++
testing/units/runtime-locks/ts0002/lock-1 | 0
testing/units/runtime-locks/ts0002/lock-2 | 0
testing/units/runtime-locks/ts0002/lock-3 | 0
testing/units/runtime-locks/ts0002/run | 25 ++++++++++
testing/units/runtime-locks/ts0003/expect | 9 ++++
testing/units/runtime-locks/ts0003/lock-1 | 0
testing/units/runtime-locks/ts0003/lock-2 | 0
testing/units/runtime-locks/ts0003/lock-3 | 0
testing/units/runtime-locks/ts0003/run | 38 ++++++++++++++
16 files changed, 202 insertions(+)
create mode 100644 data/bin/shell-locks
create mode 100644 testing/units/runtime-locks/ts0001/expect
create mode 100644 testing/units/runtime-locks/ts0001/lock-1
create mode 100644 testing/units/runtime-locks/ts0001/lock-2
create mode 100644 testing/units/runtime-locks/ts0001/lock-3
create mode 100755 testing/units/runtime-locks/ts0001/run
create mode 100644 testing/units/runtime-locks/ts0002/expect
create mode 100644 testing/units/runtime-locks/ts0002/lock-1
create mode 100644 testing/units/runtime-locks/ts0002/lock-2
create mode 100644 testing/units/runtime-locks/ts0002/lock-3
create mode 100755 testing/units/runtime-locks/ts0002/run
create mode 100644 testing/units/runtime-locks/ts0003/expect
create mode 100644 testing/units/runtime-locks/ts0003/lock-1
create mode 100644 testing/units/runtime-locks/ts0003/lock-2
create mode 100644 testing/units/runtime-locks/ts0003/lock-3
create mode 100755 testing/units/runtime-locks/ts0003/run
diff --git a/data/bin/shell-locks b/data/bin/shell-locks
new file mode 100644
index 00000000..1b855761
--- /dev/null
+++ b/data/bin/shell-locks
@@ -0,0 +1,40 @@
+#!/bin/bash -efu
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+if [ -z "${__included_shell_locks-}" ]; then
+__included_shell_locks=1
+
+_fd_lock()
+{
+ local fd="$1"; shift
+ local fn="$1"; shift
+ eval "exec $fd<\"$fn\""
+ flock "$@" "$fd"
+}
+
+fd_rlock() { _fd_lock "$1" "$2" -s; }
+fd_lock() { _fd_lock "$1" "$2" -x; }
+fd_trylock() { _fd_lock "$1" "$2" -n; }
+
+fd_unlock()
+{
+ eval "exec $1<&-"
+}
+
+fd_is_locked()
+{
+ local fd=0
+ while [ -e "/proc/self/fd/$fd" ]; do
+ fd=$(( $fd + 1 ))
+ done
+
+ if fd_trylock "$fd" "$1"; then
+ fd_unlock "$fd"
+ return 1
+ fi
+
+ eval "exec $fd<&-"
+ return 0
+}
+
+fi #__included_shell_locks
diff --git a/testing/units/runtime-locks/ts0001/expect b/testing/units/runtime-locks/ts0001/expect
new file mode 100644
index 00000000..7272e014
--- /dev/null
+++ b/testing/units/runtime-locks/ts0001/expect
@@ -0,0 +1,24 @@
+=== step 1 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/92:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/93:lock: 1: FLOCK ADVISORY WRITE
+=== step 2 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/93:lock: 1: FLOCK ADVISORY WRITE
+=== step 3 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/92:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/93:lock: 1: FLOCK ADVISORY WRITE
+=== step 4 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/92:lock: 1: FLOCK ADVISORY WRITE
+/proc/self/fdinfo/93:lock: 1: FLOCK ADVISORY WRITE
+=== step 5 ===
+=== step 6 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY READ
+/proc/self/fdinfo/92:lock: 1: FLOCK ADVISORY READ
+/proc/self/fdinfo/93:lock: 1: FLOCK ADVISORY READ
+unable to take write lock
+=== step 7 ===
+/proc/self/fdinfo/94:lock: 1: FLOCK ADVISORY WRITE
+rc=0
diff --git a/testing/units/runtime-locks/ts0001/lock-1 b/testing/units/runtime-locks/ts0001/lock-1
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0001/lock-2 b/testing/units/runtime-locks/ts0001/lock-2
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0001/lock-3 b/testing/units/runtime-locks/ts0001/lock-3
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0001/run b/testing/units/runtime-locks/ts0001/run
new file mode 100755
index 00000000..be71fbea
--- /dev/null
+++ b/testing/units/runtime-locks/ts0001/run
@@ -0,0 +1,61 @@
+#!/bin/bash -eu
+
+cwd="${0%/*}"
+
+. data/bin/shell-locks
+
+show_self_locks()
+{
+ grep -E -o -e "^lock:.* FLOCK .* (READ|WRITE) " "/proc/self/fdinfo"/* 2>/dev/null ||:
+}
+
+fd_lock 91 "$cwd/lock-1"
+fd_lock 92 "$cwd/lock-2"
+fd_lock 93 "$cwd/lock-3"
+
+echo === step 1 ===
+show_self_locks
+
+fd_unlock 92
+
+echo === step 2 ===
+show_self_locks
+
+fd_lock 92 "$cwd/lock-2"
+
+echo === step 3 ===
+show_self_locks
+
+fd_trylock 92 "$cwd/lock-2"
+
+echo === step 4 ===
+show_self_locks
+
+fd_unlock 91
+fd_unlock 92
+fd_unlock 93
+
+echo === step 5 ===
+show_self_locks
+
+fd_rlock 91 "$cwd/lock-1"
+fd_rlock 92 "$cwd/lock-1"
+fd_rlock 93 "$cwd/lock-1"
+
+echo === step 6 ===
+show_self_locks
+
+fd_trylock 94 "$cwd/lock-1" ||
+ echo "unable to take write lock"
+
+fd_unlock 91
+fd_unlock 92
+fd_unlock 93
+
+fd_trylock 94 "$cwd/lock-1" ||
+ echo "unable to take write lock"
+
+echo === step 7 ===
+show_self_locks
+
+
diff --git a/testing/units/runtime-locks/ts0002/expect b/testing/units/runtime-locks/ts0002/expect
new file mode 100644
index 00000000..51f65a47
--- /dev/null
+++ b/testing/units/runtime-locks/ts0002/expect
@@ -0,0 +1,5 @@
+=== step 1 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+=== step 2 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+rc=0
diff --git a/testing/units/runtime-locks/ts0002/lock-1 b/testing/units/runtime-locks/ts0002/lock-1
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0002/lock-2 b/testing/units/runtime-locks/ts0002/lock-2
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0002/lock-3 b/testing/units/runtime-locks/ts0002/lock-3
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0002/run b/testing/units/runtime-locks/ts0002/run
new file mode 100755
index 00000000..70c8257a
--- /dev/null
+++ b/testing/units/runtime-locks/ts0002/run
@@ -0,0 +1,25 @@
+#!/bin/bash -eu
+
+cwd="${0%/*}"
+
+. data/bin/shell-locks
+
+show_self_locks()
+{
+ grep -E -o -e "^lock:.* FLOCK .* (READ|WRITE) " "/proc/self/fdinfo"/* 2>/dev/null ||:
+}
+
+fd_lock 91 "$cwd/lock-1"
+fd_lock 91 "$cwd/lock-2"
+fd_lock 91 "$cwd/lock-3"
+
+echo === step 1 ===
+show_self_locks
+
+fd_lock 91 "$cwd/lock-1"
+fd_lock 91 "$cwd/lock-1"
+fd_lock 91 "$cwd/lock-1"
+
+echo === step 2 ===
+show_self_locks
+
diff --git a/testing/units/runtime-locks/ts0003/expect b/testing/units/runtime-locks/ts0003/expect
new file mode 100644
index 00000000..0cd65029
--- /dev/null
+++ b/testing/units/runtime-locks/ts0003/expect
@@ -0,0 +1,9 @@
+=== step 1 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY WRITE
+lock-1 locked
+=== step 2 ===
+lock-1 unlocked
+=== step 3 ===
+/proc/self/fdinfo/91:lock: 1: FLOCK ADVISORY READ
+lock-1 locked
+rc=0
diff --git a/testing/units/runtime-locks/ts0003/lock-1 b/testing/units/runtime-locks/ts0003/lock-1
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0003/lock-2 b/testing/units/runtime-locks/ts0003/lock-2
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0003/lock-3 b/testing/units/runtime-locks/ts0003/lock-3
new file mode 100644
index 00000000..e69de29b
diff --git a/testing/units/runtime-locks/ts0003/run b/testing/units/runtime-locks/ts0003/run
new file mode 100755
index 00000000..dd972973
--- /dev/null
+++ b/testing/units/runtime-locks/ts0003/run
@@ -0,0 +1,38 @@
+#!/bin/bash -eu
+
+cwd="${0%/*}"
+
+. data/bin/shell-locks
+
+show_self_locks()
+{
+ grep -E -o -e "^lock:.* FLOCK .* (READ|WRITE) " "/proc/self/fdinfo"/* 2>/dev/null ||:
+}
+
+fd_lock 91 "$cwd/lock-1"
+
+echo === step 1 ===
+show_self_locks
+
+fd_is_locked "$cwd/lock-1" &&
+ echo lock-1 locked ||
+ echo lock-1 unlocked
+
+fd_unlock 91
+
+echo === step 2 ===
+show_self_locks
+
+fd_is_locked "$cwd/lock-1" &&
+ echo lock-1 locked ||
+ echo lock-1 unlocked
+
+fd_rlock 91 "$cwd/lock-1"
+
+echo === step 3 ===
+show_self_locks
+
+fd_is_locked "$cwd/lock-1" &&
+ echo lock-1 locked ||
+ echo lock-1 unlocked
+
--
2.33.8
More information about the Make-initrd
mailing list