[PATCH 1/2] utils: add nl_has_capability() function

Thomas Haller thaller at redhat.com
Sun Feb 23 07:01:36 EST 2014


Signed-off-by: Thomas Haller <thaller at redhat.com>
---
 include/netlink/utils.h |  8 ++++++++
 lib/utils.c             | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/include/netlink/utils.h b/include/netlink/utils.h
index 502341a..da46a55 100644
--- a/include/netlink/utils.h
+++ b/include/netlink/utils.h
@@ -79,6 +79,14 @@ extern void	nl_new_line(struct nl_dump_params *);
 extern void	nl_dump(struct nl_dump_params *, const char *, ...);
 extern void	nl_dump_line(struct nl_dump_params *, const char *, ...);
 
+enum {
+	NL_CAPABILITY_NONE,
+
+	__NL_CAPABILITY_MAX
+#define NL_CAPABILITY_MAX                               (__NL_CAPABILITY_MAX - 1)
+};
+int nl_has_capability (int capability);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/utils.c b/lib/utils.c
index 1267e87..e85c121 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1111,6 +1111,40 @@ void dump_from_ops(struct nl_object *obj, struct nl_dump_params *params)
 		obj->ce_ops->oo_dump[type](obj, params);
 }
 
+/**
+ * Check whether the loaded libnl library supports a certain @capability.
+ * This is useful so that applications can workaround known issues of
+ * libnl that are fixed in newer library versions, without
+ * having a hard dependency on the new version. It is also useful, for
+ * capabilities that cannot easily detected using autoconf tests.
+ * The capabilities are integer constants with name NL_CAPABILITY_*.
+ *
+ * @return non zero if libnl supports a certain capability, 0 otherwise.
+ **/
+int nl_has_capability (int capability)
+{
+	static const uint8_t caps[ ( NL_CAPABILITY_MAX + 7 ) / 8  ] = {
+#define _NL_ASSERT(expr) ( 0 * sizeof(struct { unsigned int x: ( (!!(expr)) ? 1 : -1 ); }) )
+#define _NL_SETV(i, r, v) \
+		( _NL_ASSERT( (v) == 0 || (i) * 8 + (r) == (v) - 1 ) + \
+		  ( (v) == 0 ? 0 : (1 << (r)) ) )
+#define _NL_SET(i, v0, v1, v2, v3, v4, v5, v6, v7) \
+		[(i)] = ( \
+			_NL_SETV((i), 0, (v0)) | _NL_SETV((i), 4, (v4)) | \
+			_NL_SETV((i), 1, (v1)) | _NL_SETV((i), 5, (v5)) | \
+			_NL_SETV((i), 2, (v2)) | _NL_SETV((i), 6, (v6)) | \
+			_NL_SETV((i), 3, (v3)) | _NL_SETV((i), 7, (v7)) )
+#undef _NL_SET
+#undef _NL_SETV
+#undef _NL_ASSERT
+	};
+
+	if (capability <= 0 || capability > NL_CAPABILITY_MAX)
+		return 0;
+	capability--;
+	return (caps[capability / 8] & (1 << (capability % 8))) != 0;
+}
+
 /** @endcond */
 
 /** @} */
-- 
1.8.5.3




More information about the libnl mailing list