mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: oss-tools@pengutronix.de, rhi@pengutronix.de
Subject: [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file
Date: Thu,  9 Jul 2020 15:31:03 +0200	[thread overview]
Message-ID: <20200709133103.31615-1-a.fatoum@pengutronix.de> (raw)

barebox-state may be run in restricted contexts where /var/lock is not
yet mounted. Introduce a --no-lock option for users that guarantee by
other means that multiple instances of barebox-state won't run concurrently.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Correct operation can be verified with
$ strace barebox-state -i file.dtb --no-lock 2>&1 | grep /var/lock
---
 src/barebox-state.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/src/barebox-state.c b/src/barebox-state.c
index c7f6dee90d4a..cd56ce7192c3 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -381,6 +381,7 @@ struct state *state_get(const char *name, const char *filename, bool readonly, b
 enum opt {
 	OPT_DUMP_SHELL = UCHAR_MAX + 1,
 	OPT_VERSION    = UCHAR_MAX + 2,
+	OPT_NO_LOCK    = UCHAR_MAX + 3,
 };
 
 static struct option long_options[] = {
@@ -391,6 +392,7 @@ static struct option long_options[] = {
 	{"dump",	no_argument,		0,	'd' },
 	{"dump-shell",	no_argument,		0,	OPT_DUMP_SHELL },
 	{"force",	no_argument,		0,	'f' },
+	{"no-lock",	no_argument,		0,	OPT_NO_LOCK },
 	{"verbose",	no_argument,		0,	'v' },
 	{"quiet",	no_argument,		0,	'q' },
 	{"version",	no_argument,		0,	OPT_VERSION },
@@ -410,6 +412,7 @@ static void usage(char *name)
 "-d, --dump                                dump the state\n"
 "--dump-shell                              dump the state suitable for shell sourcing\n"
 "-f, --force                               do not check for state manipulation via the HMAC\n"
+"--no-lock                                 do not use lock file to guard access\n"
 "-v, --verbose                             increase verbosity\n"
 "-q, --quiet                               decrease verbosity\n"
 "--version                                 display version\n"
@@ -447,6 +450,7 @@ int main(int argc, char *argv[])
 	int pr_level = 5;
 	int auth = 1;
 	const char *dtb = NULL;
+	bool use_lock_file = true;
 
 	INIT_LIST_HEAD(&sg_list);
 	INIT_LIST_HEAD(&state_list.list);
@@ -478,6 +482,9 @@ int main(int argc, char *argv[])
 		case 'f':
 			auth = 0;
 			break;
+		case OPT_NO_LOCK:
+			use_lock_file = false;
+			break;
 		case 'd':
 			do_dump = 1;
 			break;
@@ -530,17 +537,19 @@ int main(int argc, char *argv[])
 		++nr_states;
 	}
 
-	lock_fd = open("/var/lock/barebox-state", O_CREAT | O_RDWR, 0600);
-	if (lock_fd < 0) {
-		pr_err("Failed to open lock-file /var/lock/barebox-state\n");
-		exit(1);
-	}
+	if (use_lock_file) {
+		lock_fd = open("/var/lock/barebox-state", O_CREAT | O_RDWR, 0600);
+		if (lock_fd < 0) {
+			pr_err("Failed to open lock-file /var/lock/barebox-state\n");
+			exit(1);
+		}
 
-	ret = flock(lock_fd, LOCK_EX);
-	if (ret < 0) {
-		pr_err("Failed to lock /var/lock/barebox-state: %m\n");
-		close(lock_fd);
-		exit(1);
+		ret = flock(lock_fd, LOCK_EX);
+		if (ret < 0) {
+			pr_err("Failed to lock /var/lock/barebox-state: %m\n");
+			close(lock_fd);
+			exit(1);
+		}
 	}
 
 	list_for_each_entry(state, &state_list.list, list) {
@@ -662,8 +671,10 @@ int main(int argc, char *argv[])
 
 	ret = 0;
 out_unlock:
-	flock(lock_fd, LOCK_UN);
-	close(lock_fd);
+	if (use_lock_file) {
+		flock(lock_fd, LOCK_UN);
+		close(lock_fd);
+	}
 
 	return ret;
 }
-- 
2.27.0


_______________________________________________
OSS-Tools mailing list
OSS-Tools@pengutronix.de

             reply	other threads:[~2020-07-09 13:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09 13:31 Ahmad Fatoum [this message]
2021-02-10  9:43 ` Ahmad Fatoum

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=20200709133103.31615-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    --cc=rhi@pengutronix.de \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox