mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
* [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file
@ 2020-07-09 13:31 Ahmad Fatoum
  2021-02-10  9:43 ` Ahmad Fatoum
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2020-07-09 13:31 UTC (permalink / raw)
  To: oss-tools, rhi

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file
  2020-07-09 13:31 [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file Ahmad Fatoum
@ 2021-02-10  9:43 ` Ahmad Fatoum
  0 siblings, 0 replies; 2+ messages in thread
From: Ahmad Fatoum @ 2021-02-10  9:43 UTC (permalink / raw)
  To: oss-tools, rhi

Hello,

On 09.07.20 15:31, Ahmad Fatoum wrote:
> 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.

Please dismiss. This patch was for running barebox-state in a systemd-generator,
which didn't have /var/lock available. /run is however available that early,
so I replaced this patch with Agner's lock in /run patch on my side and
I no longer see a need for this to be merged.

> 
> 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;
>  }
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-10  9:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-09 13:31 [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file Ahmad Fatoum
2021-02-10  9:43 ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox