mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] include: add support for NOTIFY_BAD
@ 2025-04-22  7:56 Ahmad Fatoum
  2025-04-22  9:01 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-04-22  7:56 UTC (permalink / raw)
  To: barebox

From: Ahmad Fatoum <a.fatoum@barebox.org>

When notified, returning NOTIFY_BAD or NOTIFY_STOP can be used stop
further calls and inform the original notification site.

Provide the same in barebox.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 include/notifier.h | 16 ++++++++++++++++
 lib/notifier.c     | 10 +++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/include/notifier.h b/include/notifier.h
index 093fedb0e8e4..822f8a3d6103 100644
--- a/include/notifier.h
+++ b/include/notifier.h
@@ -37,5 +37,21 @@ int clock_notifier_call_chain(void);
 
 #define NOTIFY_DONE		0x0000		/* Don't care */
 #define NOTIFY_OK		0x0001		/* Suits me */
+#define NOTIFY_STOP_MASK	0x8000		/* Don't call further */
+#define NOTIFY_BAD		(NOTIFY_STOP_MASK|0x0002)
+						/* Bad/Veto action */
+/*
+ * Clean way to return from the notifier and stop further calls.
+ */
+#define NOTIFY_STOP		(NOTIFY_OK|NOTIFY_STOP_MASK)
+
+/* Encapsulate (negative) errno value (in particular, NOTIFY_BAD <=> EPERM). */
+static inline int notifier_from_errno(int err)
+{
+	if (err)
+		return NOTIFY_STOP_MASK | (NOTIFY_OK - err);
+
+	return NOTIFY_OK;
+}
 
 #endif /* __NOTIFIER_H */
diff --git a/lib/notifier.c b/lib/notifier.c
index 3ca557f76df2..775619d3c06f 100644
--- a/lib/notifier.c
+++ b/lib/notifier.c
@@ -19,11 +19,15 @@ int notifier_chain_unregister(struct notifier_head *nh, struct notifier_block *n
 int notifier_call_chain(struct notifier_head *nh, unsigned long val, void *v)
 {
 	struct notifier_block *entry, *tmp;
+	int ret = NOTIFY_DONE;
 
-	list_for_each_entry_safe(entry, tmp, &nh->blocks, list)
-		entry->notifier_call(entry, val, v);
+	list_for_each_entry_safe(entry, tmp, &nh->blocks, list) {
+		ret = entry->notifier_call(entry, val, v);
+		if (ret & NOTIFY_STOP_MASK)
+			break;
+	}
 
-	return 0;
+	return ret;
 }
 
 /*
-- 
2.39.5




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

end of thread, other threads:[~2025-04-22 10:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-22  7:56 [PATCH] include: add support for NOTIFY_BAD Ahmad Fatoum
2025-04-22  9:01 ` Sascha Hauer

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