From: Ahmad Fatoum <a.fat...@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.fat...@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


Reply via email to