Make-initrd development discussion
 help / color / mirror / Atom feed
From: Alexey Gladkov <gladkov.alexey@gmail.com>
To: make-initrd@lists.altlinux.org
Subject: [make-initrd] [PATCH 1/3] runtime: Add locking functions based on flock utility
Date: Wed, 17 May 2023 18:04:40 +0200
Message-ID: <437d45dddaf15bd1151aaa058a6f7f3a41b5929a.1684332365.git.gladkov.alexey@gmail.com> (raw)
In-Reply-To: <cover.1684332365.git.gladkov.alexey@gmail.com>

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



  reply	other threads:[~2023-05-17 16:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 16:04 [make-initrd] [PATCH 0/3] New " Alexey Gladkov
2023-05-17 16:04 ` Alexey Gladkov [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=437d45dddaf15bd1151aaa058a6f7f3a41b5929a.1684332365.git.gladkov.alexey@gmail.com \
    --to=gladkov.alexey@gmail.com \
    --cc=make-initrd@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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