Blame SOURCES/0004-add-nl_has_capability.patch

4c8f2e
From 02f989353861bca76e0bf228b89e3b2a38c851be Mon Sep 17 00:00:00 2001
4c8f2e
From: Thomas Haller <thaller@redhat.com>
4c8f2e
Date: Thu, 13 Mar 2014 13:16:05 +0100
4c8f2e
Subject: [PATCH 1/1] utils: add nl_has_capability() function
4c8f2e
4c8f2e
Acked-by: Thomas Graf <tgraf@suug.ch>
4c8f2e
Signed-off-by: Thomas Haller <thaller@redhat.com>
4c8f2e
(cherry picked from commit 68d6bd7f37dc9a0c004b4355770e9c475fb964cd)
4c8f2e
---
4c8f2e
 include/netlink/utils.h |  8 ++++++++
4c8f2e
 lib/utils.c             | 43 +++++++++++++++++++++++++++++++++++++++++++
4c8f2e
 2 files changed, 51 insertions(+)
4c8f2e
4c8f2e
diff --git a/include/netlink/utils.h b/include/netlink/utils.h
4c8f2e
index 502341a..da46a55 100644
4c8f2e
--- a/include/netlink/utils.h
4c8f2e
+++ b/include/netlink/utils.h
4c8f2e
@@ -79,6 +79,14 @@ extern void	nl_new_line(struct nl_dump_params *);
4c8f2e
 extern void	nl_dump(struct nl_dump_params *, const char *, ...);
4c8f2e
 extern void	nl_dump_line(struct nl_dump_params *, const char *, ...);
4c8f2e
 
4c8f2e
+enum {
4c8f2e
+	NL_CAPABILITY_NONE,
4c8f2e
+
4c8f2e
+	__NL_CAPABILITY_MAX
4c8f2e
+#define NL_CAPABILITY_MAX                               (__NL_CAPABILITY_MAX - 1)
4c8f2e
+};
4c8f2e
+int nl_has_capability (int capability);
4c8f2e
+
4c8f2e
 #ifdef __cplusplus
4c8f2e
 }
4c8f2e
 #endif
4c8f2e
diff --git a/lib/utils.c b/lib/utils.c
4c8f2e
index 3bfa604..7354956 100644
4c8f2e
--- a/lib/utils.c
4c8f2e
+++ b/lib/utils.c
4c8f2e
@@ -1097,6 +1097,49 @@ void dump_from_ops(struct nl_object *obj, struct nl_dump_params *params)
4c8f2e
 		obj->ce_ops->oo_dump[type](obj, params);
4c8f2e
 }
4c8f2e
 
4c8f2e
+/**
4c8f2e
+ * Check for library capabilities
4c8f2e
+ *
4c8f2e
+ * @arg	capability	capability identifier
4c8f2e
+ *
4c8f2e
+ * Check whether the loaded libnl library supports a certain capability.
4c8f2e
+ * This is useful so that applications can workaround known issues of
4c8f2e
+ * libnl that are fixed in newer library versions, without
4c8f2e
+ * having a hard dependency on the new version. It is also useful, for
4c8f2e
+ * capabilities that cannot easily be detected using autoconf tests.
4c8f2e
+ * The capabilities are integer constants with name NL_CAPABILITY_*.
4c8f2e
+ *
4c8f2e
+ * As this function is intended to detect capabilities at runtime,
4c8f2e
+ * you might not want to depend during compile time on the NL_CAPABILITY_*
4c8f2e
+ * names. Instead you can use their numeric values which are guaranteed not to
4c8f2e
+ * change meaning.
4c8f2e
+ *
4c8f2e
+ * @return non zero if libnl supports a certain capability, 0 otherwise.
4c8f2e
+ **/
4c8f2e
+int nl_has_capability (int capability)
4c8f2e
+{
4c8f2e
+	static const uint8_t caps[ ( NL_CAPABILITY_MAX + 7 ) / 8  ] = {
4c8f2e
+#define _NL_ASSERT(expr) ( 0 * sizeof(struct { unsigned int x: ( (!!(expr)) ? 1 : -1 ); }) )
4c8f2e
+#define _NL_SETV(i, r, v) \
4c8f2e
+		( _NL_ASSERT( (v) == 0 || (i) * 8 + (r) == (v) - 1 ) + \
4c8f2e
+		  ( (v) == 0 ? 0 : (1 << (r)) ) )
4c8f2e
+#define _NL_SET(i, v0, v1, v2, v3, v4, v5, v6, v7) \
4c8f2e
+		[(i)] = ( \
4c8f2e
+			_NL_SETV((i), 0, (v0)) | _NL_SETV((i), 4, (v4)) | \
4c8f2e
+			_NL_SETV((i), 1, (v1)) | _NL_SETV((i), 5, (v5)) | \
4c8f2e
+			_NL_SETV((i), 2, (v2)) | _NL_SETV((i), 6, (v6)) | \
4c8f2e
+			_NL_SETV((i), 3, (v3)) | _NL_SETV((i), 7, (v7)) )
4c8f2e
+#undef _NL_SET
4c8f2e
+#undef _NL_SETV
4c8f2e
+#undef _NL_ASSERT
4c8f2e
+	};
4c8f2e
+
4c8f2e
+	if (capability <= 0 || capability > NL_CAPABILITY_MAX)
4c8f2e
+		return 0;
4c8f2e
+	capability--;
4c8f2e
+	return (caps[capability / 8] & (1 << (capability % 8))) != 0;
4c8f2e
+}
4c8f2e
+
4c8f2e
 /** @endcond */
4c8f2e
 
4c8f2e
 /** @} */
4c8f2e
-- 
4c8f2e
1.8.5.3
4c8f2e