From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: From: Ahmad Fatoum Date: Thu, 9 Jul 2020 15:31:03 +0200 Message-Id: <20200709133103.31615-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Subject: [OSS-Tools] [PATCH] barebox-state: introduce --no-lock option to avoid lock file List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: oss-tools-bounces@pengutronix.de Sender: "OSS-Tools" To: oss-tools@pengutronix.de, rhi@pengutronix.de 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 --- 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