[ovs-dev] [mcgroup 1/4] nln: Allow unregister when register fails.

Ethan Jackson ethan at nicira.com
Thu Sep 15 00:55:31 UTC 2011


Quite a bit of code in OVS ignores the return code of
nln_notifier_register() and will nln_notifier_unregister() on
notifiers which failed to register in the first place.  This can
cause segmentation faults.

Instead of forcing every caller to remember whether or not
registration failed, this patch allows unregistration of notifiers
which failed to register.
---
 lib/netlink-notifier.c |   12 +++++++++++-
 lib/netlink-notifier.h |    1 +
 2 files changed, 12 insertions(+), 1 deletions(-)

diff --git a/lib/netlink-notifier.c b/lib/netlink-notifier.c
index 1b7529d..cb6fd87 100644
--- a/lib/netlink-notifier.c
+++ b/lib/netlink-notifier.c
@@ -92,6 +92,8 @@ int
 nln_notifier_register(struct nln *nln, struct nln_notifier *notifier,
                       nln_notify_func *cb, void *aux)
 {
+    notifier->registered = false;
+
     if (!nln->notify_sock) {
         struct nl_sock *sock;
         int error;
@@ -115,14 +117,22 @@ nln_notifier_register(struct nln *nln, struct nln_notifier *notifier,
     list_push_back(&nln->all_notifiers, &notifier->node);
     notifier->cb = cb;
     notifier->aux = aux;
+    notifier->registered = true;
     return 0;
 }
 
 /* Cancels notification on 'notifier', which must have previously been
- * registered with nln_notifier_register(). */
+ * registered with nln_notifier_register().
+ *
+ * May optionally be called on 'notifier' even if nln_notifier_register()
+ * failed with an error. */
 void
 nln_notifier_unregister(struct nln *nln, struct nln_notifier *notifier)
 {
+    if (!notifier->registered) {
+        return;
+    }
+
     list_remove(&notifier->node);
     if (list_is_empty(&nln->all_notifiers)) {
         nl_sock_destroy(nln->notify_sock);
diff --git a/lib/netlink-notifier.h b/lib/netlink-notifier.h
index 60b5991..a5f2d3d 100644
--- a/lib/netlink-notifier.h
+++ b/lib/netlink-notifier.h
@@ -41,6 +41,7 @@ struct nln_notifier {
     struct list node;
     nln_notify_func *cb;
     void *aux;
+    bool registered;
 };
 
 struct nln *nln_create(int protocol, int multicast_group, nln_parse_func *,
-- 
1.7.6.1




More information about the dev mailing list