The jansson and json-glib libraries both export symbols with a json_
name prefix and json_object_iter_next() clashes between them.
Unfortunately json_glib is linked in by GTK, so any app using GTK and
libvirt will get a clash, resulting in SEGV. This also affects the NSS
module provided by libvirt
Instead of directly linking to jansson, use dlopen() with the RTLD_LOCAL
flag which allows us to hide the symbols from the application that loads
libvirt or the NSS module.
Some preprocessor black magic and wrapper functions are used to redirect
calls into the dlopen resolved symbols.
Signed-off-by: Daniel P. Berrangé
---
libvirt.spec.in | 2 +
src/Makefile.am | 5 +-
src/util/Makefile.inc.am | 3 +-
src/util/virjson.c | 9 +-
src/util/virjsoncompat.c | 274 +++
src/util/virjsoncompat.h | 88 +
6 files changed, 377 insertions(+), 4 deletions(-)
create mode 100644 src/util/virjsoncompat.c
create mode 100644 src/util/virjsoncompat.h
diff --git a/libvirt.spec.in b/libvirt.spec.in
index 4113579e47..19ae55cdaf 100644
--- a/libvirt.spec.in
+++ b/libvirt.spec.in
@@ -898,6 +898,8 @@ Requires: ncurses
Requires: gettext
# Needed by virt-pki-validate script.
Requires: gnutls-utils
+# We dlopen(libjansson.so.4), so need an explicit dep
+Requires: jansson
%if %{with_bash_completion}
Requires: %{name}-bash-completion = %{version}-%{release}
%endif
diff --git a/src/Makefile.am b/src/Makefile.am
index 83263e69e5..4b6366bb4c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -552,7 +552,6 @@ libvirt_admin_la_CFLAGS += \
libvirt_admin_la_LIBADD += \
$(CAPNG_LIBS) \
- $(JANSSON_LIBS) \
$(DEVMAPPER_LIBS) \
$(LIBXML_LIBS) \
$(SSH2_LIBS) \
@@ -690,6 +689,7 @@ libvirt_setuid_rpc_client_la_SOURCES = \
util/virhashcode.c \
util/virhostcpu.c \
util/virjson.c \
+ util/virjsoncompat.c \
util/virlog.c \
util/virobject.c \
util/virpidfile.c \
@@ -961,6 +961,8 @@ libvirt_nss_la_SOURCES = \
util/virhashcode.h \
util/virjson.c \
util/virjson.h \
+ util/virjsoncompat.c \
+ util/virjsoncompat.h \
util/virkmod.c \
util/virkmod.h \
util/virlease.c \
@@ -1001,7 +1003,6 @@ libvirt_nss_la_LDFLAGS = \
$(NULL)
libvirt_nss_la_LIBADD = \
- $(JANSSON_LIBS) \
$(NULL)
endif WITH_NSS
diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
index 71b2b93c2d..8ef9ee1dfa 100644
--- a/src/util/Makefile.inc.am
+++ b/src/util/Makefile.inc.am
@@ -86,6 +86,8 @@ UTIL_SOURCES = \
util/viriscsi.h \
util/virjson.c \
util/virjson.h \
+ util/virjsoncompat.c \
+ util/virjsoncompat.h \
util/virkeycode.c \
util/virkeycode.h \
util/virkeyfile.c \
@@ -264,7 +266,6 @@ libvirt_util_la_CFLAGS = \
$(NULL)
libvirt_util_la_LIBADD = \
$(CAPNG_LIBS) \
- $(JANSSON_LIBS) \
$(LIBNL_LIBS) \
$(THREAD_LIBS) \
$(AUDIT_LIBS) \
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 01a387b2f7..5bab662cd3 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1437,7 +1437,8 @@ virJSONValueCopy(const virJSONValue *in)
#if WITH_JANSSON
-# include
+
+# include "virjsoncompat.h"
static virJSONValuePtr
virJSONValueFromJansson(json_t *json)
@@ -1524,6 +1525,9 @@ virJSONValueFromString(const char *jsonstring)
size_t flags = JSON_REJECT_DUPLICATES |
JSON_DECODE_ANY;
+if (virJSONInitialize() < 0)
+return NULL;
+
if (!(json = json_loads(jsonstring, flags, ))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("failed to parse JSON %d:%d: %s"),
@@ -1630,6 +1634,9 @@ virJSONValueToString(virJSONValuePtr object,
json_t *json;
char *str = NULL;
+if (virJSONInitialize() < 0)
+return NULL;
+
if (pretty)
flags |= JSON_INDENT(2);
else
diff --git a/src/util/virjsoncompat.c b/src/util/virjsoncompat.c
new file mode 100644
index 00..6c853e9bb5
--- /dev/null
+++ b/src/util/virjsoncompat.c
@@ -0,0 +1,274 @@
+/*
+ * virjsoncompat.c: JSON object parsing/formatting
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for