* [make-initrd] [PATCH 0/3] New locking functions based on flock utility
@ 2023-05-17 16:04 Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 1/3] runtime: Add " Alexey Gladkov
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Alexey Gladkov @ 2023-05-17 16:04 UTC (permalink / raw)
To: make-initrd
Привет!
Чтобы избавиться от лишнего поллинга в реализации блокировок я сделал другой
API основанный на flock из busybox. Добавилась возможность делать shared locks.
Я не стал использовать bash-builtin-lockf, который использует hasher, потому что
не хочу завязываться на версию bash и тянуть зависимость на bash-devel.
Как следствие блокировка может утечь в запускаемый под блокировкой код и теперь
за этим нужно следить.
Далее нужно будет провести рефакторинг и посмотреть нельзя ли сократить код под
блокировкой.
Alexey Gladkov (3):
runtime: Add locking functions based on flock utility
Rewrite console locking
feature/network: Use flock-based locking
data/bin/rdshell | 12 ----
data/bin/rdshell-locked | 1 -
data/bin/rdshell-sh-functions | 29 ---------
data/bin/shell-locks | 40 ++++++++++++
data/etc/rc.d/init.d/cmdline | 2 -
data/etc/rc.d/rc | 15 +++--
data/lib/initrd/boot/scripts/console-inactive | 7 +--
data/lib/initrd/spawn-shell | 7 ++-
data/lib/uevent/each/pre/.gitignore | 0
data/lib/uevent/each/pre/console | 25 --------
data/lib/uevent/handlers/poll/400-rootdelay | 21 ++++---
.../lib/initrd/boot/method/bootloader/action | 18 +++---
.../data/lib/uevent/handlers/040-mountdev | 13 ++--
.../luks/data/lib/uevent/handlers/085-luks | 11 ++--
.../network/data/etc/rc.d/init.d/network-up | 10 +++
features/network/data/lib/network/config | 8 ---
features/network/data/lib/network/hostname | 10 +--
features/network/data/lib/network/resolvconf | 10 +--
.../network/data/lib/network/udhcpc4.script | 12 ++--
.../data/lib/uevent/handlers/040-sshfsroot | 13 ++--
.../zfs/data/lib/uevent/extenders/200-zfs | 12 ++--
testing/sh-functions | 4 +-
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 ++++++++++++
.../units/sort-services/ts0007/data00-cmdline | 2 -
.../units/sort-services/ts0008/data00-cmdline | 2 -
39 files changed, 283 insertions(+), 163 deletions(-)
delete mode 120000 data/bin/rdshell-locked
delete mode 100644 data/bin/rdshell-sh-functions
create mode 100644 data/bin/shell-locks
create mode 100644 data/lib/uevent/each/pre/.gitignore
delete mode 100755 data/lib/uevent/each/pre/console
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
--
2.33.8
^ permalink raw reply [flat|nested] 4+ messages in thread
* [make-initrd] [PATCH 1/3] runtime: Add locking functions based on flock utility
2023-05-17 16:04 [make-initrd] [PATCH 0/3] New locking functions based on flock utility Alexey Gladkov
@ 2023-05-17 16:04 ` Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 2/3] Rewrite console locking Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 3/3] feature/network: Use flock-based locking Alexey Gladkov
2 siblings, 0 replies; 4+ messages in thread
From: Alexey Gladkov @ 2023-05-17 16:04 UTC (permalink / raw)
To: make-initrd
Signed-off-by: Alexey Gladkov <gladkov.alexey@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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [make-initrd] [PATCH 2/3] Rewrite console locking
2023-05-17 16:04 [make-initrd] [PATCH 0/3] New locking functions based on flock utility Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 1/3] runtime: Add " Alexey Gladkov
@ 2023-05-17 16:04 ` Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 3/3] feature/network: Use flock-based locking Alexey Gladkov
2 siblings, 0 replies; 4+ messages in thread
From: Alexey Gladkov @ 2023-05-17 16:04 UTC (permalink / raw)
To: make-initrd
We have migrated from a directory as a locking mechanism to use the
flock utility. This allows you to get rid of active polling.
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
---
data/bin/rdshell | 12 --------
data/bin/rdshell-locked | 1 -
data/bin/rdshell-sh-functions | 29 -------------------
data/etc/rc.d/init.d/cmdline | 2 --
data/etc/rc.d/rc | 15 +++++-----
data/lib/initrd/boot/scripts/console-inactive | 7 ++---
data/lib/initrd/spawn-shell | 7 +++--
data/lib/uevent/each/pre/.gitignore | 0
data/lib/uevent/each/pre/console | 25 ----------------
data/lib/uevent/handlers/poll/400-rootdelay | 21 ++++++++------
.../lib/initrd/boot/method/bootloader/action | 18 ++++++------
.../data/lib/uevent/handlers/040-mountdev | 13 ++++-----
.../luks/data/lib/uevent/handlers/085-luks | 11 +++----
.../network/data/etc/rc.d/init.d/network-up | 10 +++++++
.../data/lib/uevent/handlers/040-sshfsroot | 13 ++++-----
.../zfs/data/lib/uevent/extenders/200-zfs | 12 ++++----
testing/sh-functions | 4 +--
.../units/sort-services/ts0007/data00-cmdline | 2 --
.../units/sort-services/ts0008/data00-cmdline | 2 --
19 files changed, 69 insertions(+), 135 deletions(-)
delete mode 120000 data/bin/rdshell-locked
delete mode 100644 data/bin/rdshell-sh-functions
create mode 100644 data/lib/uevent/each/pre/.gitignore
delete mode 100755 data/lib/uevent/each/pre/console
diff --git a/data/bin/rdshell b/data/bin/rdshell
index 59c3e6c1..bb040f15 100755
--- a/data/bin/rdshell
+++ b/data/bin/rdshell
@@ -2,7 +2,6 @@
. shell-error
. shell-source
-. rdshell-sh-functions
source_if_exists /.initrd/initenv
@@ -27,14 +26,6 @@ esac
exec </dev/console >/dev/console 2>&1
-console_locked=
-if [ -n "$0" ] && [ -z "${0##*-locked}" ]; then
- while ! console_lock; do
- sleep 0.1
- done
- console_locked=1
-fi
-
for f in /lib/shell/*; do
[ -x "$f" ] || break
"$f" ||:
@@ -44,6 +35,3 @@ done
printf '%s: %s\n' "${0##*/}" "$1"
setsid -c $shcmd
-
-[ -z "$console_locked" ] ||
- console_unlock
diff --git a/data/bin/rdshell-locked b/data/bin/rdshell-locked
deleted file mode 120000
index ee18ff81..00000000
--- a/data/bin/rdshell-locked
+++ /dev/null
@@ -1 +0,0 @@
-rdshell
\ No newline at end of file
diff --git a/data/bin/rdshell-sh-functions b/data/bin/rdshell-sh-functions
deleted file mode 100644
index b2b7b2aa..00000000
--- a/data/bin/rdshell-sh-functions
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/bash -eu
-
-if [ -z "${__rdshell_sh_functions-}" ]; then
-__rdshell_sh_functions=1
-
-_console_lock=/.initrd/rdshell.lock
-
-console_lock()
-{
- mkdir "$_console_lock" >/dev/null 2>&1 ||
- return 1
-}
-
-console_unlock()
-{
- rmdir "$_console_lock"
-}
-
-console_inactive()
-{
- [ ! -d "$_console_lock" ]
-}
-
-console_open()
-{
- rdshell-locked "$@" &
-}
-
-fi
diff --git a/data/etc/rc.d/init.d/cmdline b/data/etc/rc.d/init.d/cmdline
index 7199c4aa..4d2af343 100755
--- a/data/etc/rc.d/init.d/cmdline
+++ b/data/etc/rc.d/init.d/cmdline
@@ -15,8 +15,6 @@
. /etc/init.d/functions
-. rdshell-sh-functions
-
. shell-error
. shell-var
. shell-cmdline
diff --git a/data/etc/rc.d/rc b/data/etc/rc.d/rc
index 9e261958..02f85ae5 100755
--- a/data/etc/rc.d/rc
+++ b/data/etc/rc.d/rc
@@ -8,7 +8,7 @@
#
. /etc/init.d/functions
-. rdshell-sh-functions
+. shell-locks
# Get first argument. Set new runlevel to this argument.
[ $# -eq 0 ] ||
@@ -21,14 +21,13 @@ rcd="/etc/rc.d/rc$runlevel.d"
[ -d "$rcd" ] ||
exit 0
+exec </dev/console 2>&1
+fd_lock 90 /dev/console
+
msg "INIT: Entering runlevel: $runlevel"
export INITLOG_FILE='/var/log/boot.log'
-while ! console_lock; do
- sleep 0.5
-done
-
run_scripts() {
local s n="$1"
shift
@@ -59,7 +58,7 @@ for i in "$rcd"/K*; do
rc="$?"
run_scripts "post/$subsys" stop "$subsys" "$rc"
-done
+done 90<&-
# Now run the START scripts.
for i in "$rcd"/S*; do
@@ -111,6 +110,6 @@ for i in "$rcd"/S*; do
rc="$?"
run_scripts "post/$subsys" start "$subsys" "$rc"
-done
+done 90<&-
-console_unlock
+fd_unlock 90
diff --git a/data/lib/initrd/boot/scripts/console-inactive b/data/lib/initrd/boot/scripts/console-inactive
index b40bdde9..ab41ef21 100755
--- a/data/lib/initrd/boot/scripts/console-inactive
+++ b/data/lib/initrd/boot/scripts/console-inactive
@@ -1,6 +1,5 @@
-#!/bin/bash
+#!/bin/bash -e
-. rdshell-sh-functions
+. shell-locks
-console_inactive ||
- exit
+fd_trylock 90 /dev/console
diff --git a/data/lib/initrd/spawn-shell b/data/lib/initrd/spawn-shell
index 55dbdbfe..ed8df4a3 100755
--- a/data/lib/initrd/spawn-shell
+++ b/data/lib/initrd/spawn-shell
@@ -1,6 +1,7 @@
#!/bin/bash -efu
-. rdshell-sh-functions
+. shell-locks
-console_inactive &&
- console_open "Emergency shell" ||:
+if fd_trylock 90 /dev/console; then
+ rdshell "Emergency shell" &
+fi
diff --git a/data/lib/uevent/each/pre/.gitignore b/data/lib/uevent/each/pre/.gitignore
new file mode 100644
index 00000000..e69de29b
diff --git a/data/lib/uevent/each/pre/console b/data/lib/uevent/each/pre/console
deleted file mode 100755
index dbf16cbb..00000000
--- a/data/lib/uevent/each/pre/console
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/bash -efu
-
-. rdshell-sh-functions
-
-QUEUE="$1"
-
-[ "$QUEUE" = udev ] ||
- exit 0
-
-. shell-error
-
-PROG="${QUEUE:--}: session=${SESSION:-0}: $PROG"
-message_time=1
-
-show_msg=
-while ! console_inactive; do
- if [ -z "$show_msg" ]; then
- message "waiting for console to be unlocked."
- show_msg=1
- fi
- sleep 0.1
-done
-
-[ -z "$show_msg" ] ||
- message "console is unlocked."
diff --git a/data/lib/uevent/handlers/poll/400-rootdelay b/data/lib/uevent/handlers/poll/400-rootdelay
index cac86622..50173947 100755
--- a/data/lib/uevent/handlers/poll/400-rootdelay
+++ b/data/lib/uevent/handlers/poll/400-rootdelay
@@ -1,12 +1,11 @@
#!/bin/bash -efu
-. rdshell-sh-functions
-
# Waiting for cmdline service
[ -f /.initrd/initenv ] ||
exit 0
. /.initrd/initenv
+. shell-locks
disabled="/.initrd/rootdelay/disabled"
@@ -15,7 +14,10 @@ disabled="/.initrd/rootdelay/disabled"
if [ -n "${RDSHELL-}" ]; then
mkdir -p -- "$disabled"
- console_open "Emergency shell"
+
+ fd_lock 90 /dev/console
+ rdshell "Emergency shell" &
+
exit
fi
@@ -58,23 +60,24 @@ if [ "$delay" -le 0 ]; then
msg="$(cat "$consmsg")"
mkdir -p -- "$disabled"
- console_open "${msg:-The waiting time expired!}"
+
+ fd_lock 90 /dev/console
+ rdshell "${msg:-The waiting time expired!}" &
+
exit 0
fi
if [ -z "${QUIET-}" ] && [ -z "$first_iter" ] && [ $(( $delay % 15 )) -eq 0 ]; then
. shell-error
- while ! console_lock; do
- sleep 0.2
- done
+ fd_lock 90 /dev/console
resume_checked &&
msg="Waiting for root ${ROOT:+($ROOT) }..." ||
msg="Waiting for resume device ${RESUME:+($RESUME) }..."
PROG=initramfs \
- message "$msg" >/dev/console 2>&1 ||:
+ message "$msg" >/dev/console 2>&1 90<&- ||:
- console_unlock
+ fd_unlock 90
fi
diff --git a/features/bootloader/data/lib/initrd/boot/method/bootloader/action b/features/bootloader/data/lib/initrd/boot/method/bootloader/action
index b11b98d7..20fd15e9 100755
--- a/features/bootloader/data/lib/initrd/boot/method/bootloader/action
+++ b/features/bootloader/data/lib/initrd/boot/method/bootloader/action
@@ -1,22 +1,22 @@
#!/bin/bash -efu
-. shell-error
. /.initrd/initenv
+. shell-error
+. shell-locks
+
[ -n "${BOOT_CONFIG-}" ] ||
fatal "config file not found"
-. rdshell-sh-functions
-
-console_lock
exec < /dev/console >/dev/console 2>&1
+fd_lock 90 /dev/console
export NEWT_COLORS_FILE="/home/root/.newtrc"
mkdir -p /tmp/bootmenu
cd /tmp/bootmenu
-setsid -c /bin/bootmenu "$BOOT_CONFIG" .
+setsid -c /bin/bootmenu "$BOOT_CONFIG" . 90<&-
# shellcheck disable=SC2094,SC2162
read kernel < kernel
@@ -32,9 +32,9 @@ ln -sf -- "${BOOT_CONFIG%/*}" /boot
kexec \
--load "/boot/$kernel" \
${initrd:+--initrd="/boot/$initrd"} \
- ${append:+--append="$append"}
-kexec -x -e
+ ${append:+--append="$append"} 90<&-
+kexec -x -e 90<&-
-setsid /bin/sh -l
+setsid /bin/sh -l 90<&-
-console_unlock
+fd_unlock 90
diff --git a/features/fsck/data/lib/uevent/handlers/040-mountdev b/features/fsck/data/lib/uevent/handlers/040-mountdev
index 8ca70ded..fab73b4b 100755
--- a/features/fsck/data/lib/uevent/handlers/040-mountdev
+++ b/features/fsck/data/lib/uevent/handlers/040-mountdev
@@ -1,9 +1,11 @@
#!/bin/bash
. /.initrd/initenv
+
+. shell-locks
+
. /etc/init.d/functions
. uevent-sh-functions
-. rdshell-sh-functions
. initrd-sh-functions
. state-sh-functions
@@ -50,15 +52,12 @@ handler()
reboot
}
-while ! console_lock; do
- sleep 0.5
-done
-
exec 0</dev/console >/dev/console 2>&1
+fd_lock 90 /dev/console
for e in "$eventdir"/mountdev.*; do
[ -e "$e" ] || break
( . "$e"; handler; ) ||:
-done
+done 90<&-
-console_unlock
+fd_unlock 90
diff --git a/features/luks/data/lib/uevent/handlers/085-luks b/features/luks/data/lib/uevent/handlers/085-luks
index 5dd038e1..d2025a50 100755
--- a/features/luks/data/lib/uevent/handlers/085-luks
+++ b/features/luks/data/lib/uevent/handlers/085-luks
@@ -4,10 +4,10 @@
. shell-error
. shell-var
+. shell-locks
. uevent-sh-functions
. initrd-sh-functions
-. rdshell-sh-functions
. crypttab-sh-functions
PROG="${QUEUE:--}: session=${SESSION:-0}: $PROG"
@@ -407,11 +407,8 @@ handler() {
fi
}
-while ! console_lock; do
- sleep 0.5
-done
-
exec 0</dev/console >/dev/console 2>&1
+fd_lock 90 /dev/console
rc=0
for e in "$eventdir"/luks.*; do
@@ -423,7 +420,7 @@ for e in "$eventdir"/luks.*; do
1) rc=1 ;;
0) done_event "$e" ;;
esac
-done
+done 90<&-
-console_unlock
+fd_unlock 90
exit $rc
diff --git a/features/network/data/etc/rc.d/init.d/network-up b/features/network/data/etc/rc.d/init.d/network-up
index 684d5a3d..9ec3b978 100755
--- a/features/network/data/etc/rc.d/init.d/network-up
+++ b/features/network/data/etc/rc.d/init.d/network-up
@@ -13,11 +13,14 @@
msg="Network up"
+. shell-locks
. network-sh-functions
# wait for network modules
udevadm settle --timeout=3
+fd_rlock 90 /dev/console
+
ifaces=' '
while :; do
set -- /sys/class/net/*
@@ -46,3 +49,10 @@ while :; do
sleep 0.1
done
+
+if [ "${RDLOG-}" = 'console' ]; then
+ msg="all interfaces are up ($ifaces):"
+
+ echo_msg "$msg"
+ success "$msg"
+fi
diff --git a/features/sshfsroot/data/lib/uevent/handlers/040-sshfsroot b/features/sshfsroot/data/lib/uevent/handlers/040-sshfsroot
index 99d1416c..3a4b0ef4 100755
--- a/features/sshfsroot/data/lib/uevent/handlers/040-sshfsroot
+++ b/features/sshfsroot/data/lib/uevent/handlers/040-sshfsroot
@@ -1,10 +1,12 @@
#!/bin/bash
. /.initrd/initenv
+
+. shell-locks
+
. initrd-sh-functions
. uevent-sh-functions
. network-sh-functions
-. rdshell-sh-functions
PROG="${QUEUE:--}: session=${SESSION:-0}: $PROG"
message_time=1
@@ -38,11 +40,8 @@ handler() {
done
}
-while ! console_lock; do
- sleep 0.5
-done
-
exec 0</dev/console >/dev/console 2>&1
+fd_lock 90 /dev/console
rc=0
for e in "$eventdir"/sshfsroot.*; do
@@ -50,7 +49,7 @@ for e in "$eventdir"/sshfsroot.*; do
( . "$e"; handler; ) ||
rc=1
done_event "$e"
-done
+done 90<&-
-console_unlock
+fd_unlock 90
exit $rc
diff --git a/features/zfs/data/lib/uevent/extenders/200-zfs b/features/zfs/data/lib/uevent/extenders/200-zfs
index ec626234..723ac827 100755
--- a/features/zfs/data/lib/uevent/extenders/200-zfs
+++ b/features/zfs/data/lib/uevent/extenders/200-zfs
@@ -7,8 +7,9 @@
. /.initrd/initenv
. shell-error
+. shell-locks
+
. initrd-sh-functions
-. rdshell-sh-functions
. uevent-sh-functions
message_time=1
@@ -90,13 +91,12 @@ mount_zfs()
resume_checked ||
exit 0
-while ! console_lock; do
- sleep 0.5
-done
+exec 0</dev/console >/dev/console 2>&1
+fd_lock 90 /dev/console
findmnt -sno TARGET -t zfs | sort -d -k1,1 |
while read -r mnt; do
- mount_zfs "$mnt" </dev/null ||:
+ mount_zfs "$mnt" </dev/null 90<&- ||:
done
-console_unlock
+fd_unlock 90
diff --git a/testing/sh-functions b/testing/sh-functions
index da515ee9..18a8c8c2 100644
--- a/testing/sh-functions
+++ b/testing/sh-functions
@@ -360,8 +360,8 @@ qemu_watchdog()
for err in \
"(or press Control-D to continue):" \
"/.initrd/uevent/queues/udev.startup: No such file or directory" \
- "rdshell-locked: The waiting time expired!" \
- "rdshell-locked: The init program \`/sbin/init' not found in the root directory." \
+ "rdshell: The waiting time expired!" \
+ "rdshell: The init program \`/sbin/init' not found in the root directory." \
;
do
if grep -qsF -e "$err" "$logfile"; then
diff --git a/testing/units/sort-services/ts0007/data00-cmdline b/testing/units/sort-services/ts0007/data00-cmdline
index 95ea8e05..f68ba8a2 100755
--- a/testing/units/sort-services/ts0007/data00-cmdline
+++ b/testing/units/sort-services/ts0007/data00-cmdline
@@ -15,8 +15,6 @@
. /etc/init.d/functions
-. rdshell-sh-functions
-
. shell-error
. shell-var
. shell-cmdline
diff --git a/testing/units/sort-services/ts0008/data00-cmdline b/testing/units/sort-services/ts0008/data00-cmdline
index 95ea8e05..f68ba8a2 100755
--- a/testing/units/sort-services/ts0008/data00-cmdline
+++ b/testing/units/sort-services/ts0008/data00-cmdline
@@ -15,8 +15,6 @@
. /etc/init.d/functions
-. rdshell-sh-functions
-
. shell-error
. shell-var
. shell-cmdline
--
2.33.8
^ permalink raw reply [flat|nested] 4+ messages in thread
* [make-initrd] [PATCH 3/3] feature/network: Use flock-based locking
2023-05-17 16:04 [make-initrd] [PATCH 0/3] New locking functions based on flock utility Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 1/3] runtime: Add " Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 2/3] Rewrite console locking Alexey Gladkov
@ 2023-05-17 16:04 ` Alexey Gladkov
2 siblings, 0 replies; 4+ messages in thread
From: Alexey Gladkov @ 2023-05-17 16:04 UTC (permalink / raw)
To: make-initrd
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
---
features/network/data/lib/network/config | 8 --------
features/network/data/lib/network/hostname | 10 +++-------
features/network/data/lib/network/resolvconf | 10 +++-------
features/network/data/lib/network/udhcpc4.script | 12 ++++++------
4 files changed, 12 insertions(+), 28 deletions(-)
diff --git a/features/network/data/lib/network/config b/features/network/data/lib/network/config
index c3a8dbb1..418b03b6 100755
--- a/features/network/data/lib/network/config
+++ b/features/network/data/lib/network/config
@@ -1,14 +1,6 @@
#!/bin/bash -efu
. shell-error
-
-lock="/tmp/$PROG.lock"
-while ! mkdir -- "$lock" >/dev/null 2>&1; do
- sleep 0.1
-done
-unlock() { rmdir "$lock"; }
-trap unlock EXIT
-
. network-sh-functions
PROG="$PROG: $NET_IF"
diff --git a/features/network/data/lib/network/hostname b/features/network/data/lib/network/hostname
index 1160fba0..3597a495 100755
--- a/features/network/data/lib/network/hostname
+++ b/features/network/data/lib/network/hostname
@@ -1,18 +1,12 @@
#!/bin/bash -efu
. shell-error
+. shell-locks
. network-sh-functions
[ "$NET_EV_ACTION" = update ] ||
exit 0
-lock="/tmp/$PROG.lock"
-while ! mkdir -- "$lock" >/dev/null 2>&1; do
- sleep 0.1
-done
-unlock() { rmdir "$lock"; }
-trap unlock EXIT
-
PROG="$PROG: $NET_IF"
message_time=1
@@ -20,6 +14,8 @@ get_iface_confdir &&
[ -f "$confdir/hostname" ] ||
exit 0
+fd_lock 90 "$0"
+
name=
read -r name < "$confdir/hostname" ||:
diff --git a/features/network/data/lib/network/resolvconf b/features/network/data/lib/network/resolvconf
index e6e14602..67ccb8ab 100755
--- a/features/network/data/lib/network/resolvconf
+++ b/features/network/data/lib/network/resolvconf
@@ -1,18 +1,12 @@
#!/bin/bash -efu
. shell-error
+. shell-locks
. network-sh-functions
[ "$NET_EV_ACTION" = update ] ||
exit 0
-lock="/tmp/$PROG.lock"
-while ! mkdir -- "$lock" >/dev/null 2>&1; do
- sleep 0.1
-done
-unlock() { rmdir "$lock"; }
-trap unlock EXIT
-
PROG="$PROG: $NET_IF"
message_time=1
@@ -20,6 +14,8 @@ get_iface_confdir &&
[ -f "$confdir/resolv.conf" ] ||
exit 0
+fd_lock 90 "$0"
+
message "write resolv.conf"
tmp="$(mktemp "/etc/resolv.conf.XXXXXX")"
diff --git a/features/network/data/lib/network/udhcpc4.script b/features/network/data/lib/network/udhcpc4.script
index dcb1f74c..bd1b4779 100755
--- a/features/network/data/lib/network/udhcpc4.script
+++ b/features/network/data/lib/network/udhcpc4.script
@@ -1,5 +1,7 @@
#!/bin/bash -efu
+. shell-locks
+
STATE="$1"
PROG="${0##*/}"
@@ -9,11 +11,9 @@ V="${CMDNAME##udhcpc}"
INTERFACE="${interface-}"
lock="/tmp/$CMDNAME.$INTERFACE.lock"
-while ! mkdir -- "$lock" >/dev/null 2>&1; do
- sleep 0.1
-done
-unlock() { rmdir "$lock"; }
-trap unlock EXIT
+[ -d "$lock" ] || mkdir -p -- "$lock"
+
+fd_lock 90 "$lock"
logfile=$(
. /.initrd/initenv
@@ -148,4 +148,4 @@ case "$STATE" in
*)
exit 0
;;
-esac
+esac 90<&-
--
2.33.8
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-05-17 16:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-17 16:04 [make-initrd] [PATCH 0/3] New locking functions based on flock utility Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 1/3] runtime: Add " Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 2/3] Rewrite console locking Alexey Gladkov
2023-05-17 16:04 ` [make-initrd] [PATCH 3/3] feature/network: Use flock-based locking Alexey Gladkov
Make-initrd development discussion
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/make-initrd/0 make-initrd/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 make-initrd make-initrd/ http://lore.altlinux.org/make-initrd \
make-initrd@lists.altlinux.org make-initrd@lists.altlinux.ru make-initrd@lists.altlinux.com
public-inbox-index make-initrd
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.make-initrd
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git