From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Wed, 31 May 2023 17:31:39 +0200 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q4NnY-004QsW-L5 for lore@lore.pengutronix.de; Wed, 31 May 2023 17:31:39 +0200 Received: from localhost ([127.0.0.1] helo=metis.ext.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1q4NnV-0007IC-H4; Wed, 31 May 2023 17:31:37 +0200 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q4NnL-00079s-Op; Wed, 31 May 2023 17:31:27 +0200 Received: from [2a0a:edc0:0:1101:1d::54] (helo=dude05.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1q4NnL-0049CI-3e; Wed, 31 May 2023 17:31:27 +0200 Received: from afa by dude05.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1q4NnK-005uL4-3C; Wed, 31 May 2023 17:31:26 +0200 From: Ahmad Fatoum To: oss-tools@pengutronix.de Date: Wed, 31 May 2023 17:31:22 +0200 Message-Id: <20230531153125.1408092-3-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531153125.1408092-1-a.fatoum@pengutronix.de> References: <20230531153125.1408092-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [OSS-Tools] [PATCH 2/5] state: add option to lock device node X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false Even if /run exists, it may not be world-writable. This is the case on NixOS. Add an alternative option to lock the device node instead. This should eventually be made the default when it has enjoyed more testing. Signed-off-by: Ahmad Fatoum --- configure.ac | 9 +++++++++ meson.build | 1 + src/barebox-state.c | 30 ++++++++++++++++++------------ src/barebox-state/state.c | 4 ++++ src/barebox-state/state.h | 21 +++++++++++++++++++++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index be8967eb0809..117a1e169ba9 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,15 @@ AS_IF([test "x${enable_state_backward_compatibility}" = "xyes"], [ AC_DEFINE(CONFIG_STATE_BACKWARD_COMPATIBLE, [0]) ]) +AC_ARG_ENABLE([lock-device], + AS_HELP_STRING([--enable-lock-device], [barebox-state: lock device node instead of global lock in /run @<:@default=disabled@:>@]), + [], [enable_lock_device=no]) +AS_IF([test "x${enable_lock_device}" = "xyes"], [ + AC_DEFINE(CONFIG_LOCK_DEVICE_NODE, [1], [lock device node backing state.]) +], [ + AC_DEFINE(CONFIG_LOCK_DEVICE_NODE, [0], [use global lock in /run.]) +]) + AC_DEFINE(CONFIG_MTD, [1], [Statically define to be enabled to harmonize barebox' & dt-utils' code base.]) AC_DEFINE(CONFIG_STATE, [1], [Statically define to be enabled to harmonize barebox' & dt-utils' code base.]) diff --git a/meson.build b/meson.build index 22b522ea4d0e..2fc13f55ed62 100644 --- a/meson.build +++ b/meson.build @@ -20,6 +20,7 @@ conf = configuration_data() conf.set10('CONFIG_MTD', true) conf.set10('CONFIG_STATE', true) conf.set10('CONFIG_STATE_BACKWARD_COMPATIBLE', get_option('state-backward-compatibility')) +conf.set10('CONFIG_LOCK_DEVICE_NODE', get_option('lock-device')) meson.add_dist_script( find_program('check-news.sh').path(), diff --git a/src/barebox-state.c b/src/barebox-state.c index c5acd1f7780a..7cf2fbf070a9 100644 --- a/src/barebox-state.c +++ b/src/barebox-state.c @@ -499,6 +499,8 @@ int main(int argc, char *argv[]) printf(PACKAGE_STRING "\n"); printf("Configured with build-time option '--%s-state-backward-compatibility'.\n", (CONFIG_STATE_BACKWARD_COMPATIBLE) ? "enable" : "disable"); + printf(" '--%s-lock-device-node'.\n", + (CONFIG_LOCK_DEVICE_NODE) ? "enable" : "disable"); exit(0); case 'g': sg = xzalloc(sizeof(*sg)); @@ -568,17 +570,19 @@ int main(int argc, char *argv[]) ++nr_states; } - lock_fd = open(BAREBOX_STATE_LOCKFILE, O_CREAT | O_RDWR, 0600); - if (lock_fd < 0) { - pr_err("Failed to open lock-file " BAREBOX_STATE_LOCKFILE "\n"); - exit(1); - } + if (!IS_ENABLED(CONFIG_LOCK_DEVICE_NODE)) { + lock_fd = open(BAREBOX_STATE_LOCKFILE, O_CREAT | O_RDWR, 0600); + if (lock_fd < 0) { + pr_err("Failed to open lock-file " BAREBOX_STATE_LOCKFILE "\n"); + exit(1); + } - ret = flock(lock_fd, LOCK_EX); - if (ret < 0) { - pr_err("Failed to lock " BAREBOX_STATE_LOCKFILE ": %m\n"); - close(lock_fd); - exit(1); + ret = flock(lock_fd, LOCK_EX); + if (ret < 0) { + pr_err("Failed to lock " BAREBOX_STATE_LOCKFILE ": %m\n"); + close(lock_fd); + exit(1); + } } list_for_each_entry(state, &state_list.list, list) { @@ -700,8 +704,10 @@ int main(int argc, char *argv[]) ret = 0; out_unlock: - flock(lock_fd, LOCK_UN); - close(lock_fd); + if (!IS_ENABLED(CONFIG_LOCK_DEVICE_NODE)) { + flock(lock_fd, LOCK_UN); + close(lock_fd); + } return ret; } diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c index 42ce88d3f161..371ae3959d6c 100644 --- a/src/barebox-state/state.c +++ b/src/barebox-state/state.c @@ -664,6 +664,10 @@ struct state *state_new_from_node(struct device_node *node, bool readonly) pr_debug("%s: backend resolved to %s %lld %zu\n", node->full_name, state->backend_path, (long long)offset, size); + /* will be released on program exit */ + if (IS_ENABLED(CONFIG_LOCK_DEVICE_NODE)) + (void)open_exclusive(state->backend_path, readonly ? O_RDONLY : O_RDWR); + state->backend_reproducible_name = of_get_reproducible_name(partition_node); ret = of_property_read_string(node, "backend-type", &backend_type); diff --git a/src/barebox-state/state.h b/src/barebox-state/state.h index 4abfa84285c3..bbbc1892f846 100644 --- a/src/barebox-state/state.h +++ b/src/barebox-state/state.h @@ -3,6 +3,7 @@ #include #include #include +#include struct state; struct mtd_info_user; @@ -269,3 +270,23 @@ static inline int state_string_copy_to_raw(struct state_string *string, return 0; } + +static inline int open_exclusive(const char *path, int flags) +{ + int fd; + + fd = open(path, flags); + if (fd < 0) + return fd; + + if (IS_ENABLED(CONFIG_LOCK_DEVICE_NODE)) { + int ret = flock(fd, LOCK_EX); + if (ret < 0) { + pr_err("Failed to lock %s: %d\n", path, -errno); + close(fd); + return -EWOULDBLOCK; + } + } + + return fd; +} -- 2.39.2