]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nss: Move logging into a separate file and turn it temporarily on
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 18 Jun 2025 06:49:29 +0000 (08:49 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 18 Jul 2025 13:05:41 +0000 (15:05 +0200)
Currently, when somebody wants to debug the NSS plugin, they have
to change a line in libvirt_nss.h (to enable debug printings) and
recompile the module. This may work for us, developers, but we
can not expect this from users.

For now, this turns debug printings unconditionally on. Making it
conditional on an envvar is handled in the next commit.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
build-aux/syntax-check.mk
tools/nss/libvirt_nss.h
tools/nss/libvirt_nss_log.c [new file with mode: 0644]
tools/nss/libvirt_nss_log.h [new file with mode: 0644]
tools/nss/meson.build

index 27eabe65659e6de046947d75f2e080707a885ba3..4ba059b56bd4fa8db5979b75aca98d8c64a7fc4f 100644 (file)
@@ -1430,7 +1430,7 @@ exclude_file_name_regexp--sc_prohibit_realpath = \
   ^(build-aux/syntax-check\.mk|src/cpu_map/sync_qemu_features_i386\.py|tests/virfilemock\.c)$$
 
 exclude_file_name_regexp--sc_prohibit_raw_allocation = \
-  ^(docs/advanced-tests\.rst|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c|tools/nss/libvirt_nss(_leases|_macs)?\.[ch])$$
+  ^(docs/advanced-tests\.rst|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c|tools/nss/libvirt_nss(_leases|_log|_macs)?\.[ch])$$
 
 exclude_file_name_regexp--sc_prohibit_readlink = \
   ^src/(util/virutil|lxc/lxc_container)\.c$$
index 84db0444094a3c5239a2c45f47d11134104622e8..e09fce9715485ac609dfdfe77af7fc5c59fed87a 100644 (file)
 #include <netdb.h>
 #include <stdlib.h>
 
-
-#if 0
-# include <errno.h>
-# include <stdio.h>
-# include <string.h>
-# define NULLSTR(s) ((s) ? (s) : "<null>")
-# define ERROR(...) \
-do { \
-    int saved_errno = errno; \
-    const size_t ebuf_size = 512; \
-    g_autofree char *ebuf = calloc(ebuf_size, sizeof(*ebuf)); \
-    if (ebuf) \
-        strerror_r(saved_errno, ebuf, ebuf_size); \
-    fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
-    fprintf(stderr, __VA_ARGS__); \
-    fprintf(stderr, " : %s\n", NULLSTR(ebuf)); \
-    fprintf(stderr, "\n"); \
-} while (0)
-
-# define DEBUG(...) \
-do { \
-    fprintf(stderr, "DEBUG %s:%d : ", __FUNCTION__, __LINE__); \
-    fprintf(stderr, __VA_ARGS__); \
-    fprintf(stderr, "\n"); \
-} while (0)
-#else
-# define ERROR(...) do { } while (0)
-# define DEBUG(...) do { } while (0)
-#endif
+#include "libvirt_nss_log.h"
 
 #if !defined(LIBVIRT_NSS_GUEST)
 # define NSS_NAME(s) _nss_libvirt_##s##_r
diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c
new file mode 100644 (file)
index 0000000..672a170
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * libvirt_nss_log: Logging for Name Service Switch plugin
+ *
+ * Copyright (C) 2025 Red Hat, Inc.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <config.h>
+
+#undef _GNU_SOURCE
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "libvirt_nss_log.h"
+#include "libvirt_nss.h"
+
+#define NULLSTR(s) ((s) ? (s) : "<null>")
+
+static const char *  __attribute__((returns_nonnull))
+nssLogPriorityToString(nssLogPriority prio)
+{
+    switch (prio) {
+    case NSS_DEBUG:
+        return "DEBUG";
+    case NSS_ERROR:
+        return "ERROR";
+    }
+
+    return "";
+}
+
+void
+nssLog(nssLogPriority prio,
+       const char *func,
+       int linenr,
+       const char *fmt, ...)
+{
+    int saved_errno = errno;
+    const size_t ebuf_size = 512;
+    g_autofree char *ebuf = NULL;
+    va_list ap;
+
+    fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr);
+
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+
+    switch (prio) {
+    case NSS_DEBUG:
+        break;
+
+    case NSS_ERROR:
+        ebuf = calloc(ebuf_size, sizeof(*ebuf));
+        if (ebuf)
+            strerror_r(saved_errno, ebuf, ebuf_size);
+        fprintf(stderr, " : %s", NULLSTR(ebuf));
+        break;
+    }
+
+    fprintf(stderr, "\n");
+}
diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h
new file mode 100644 (file)
index 0000000..d99d88e
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * libvirt_nss_log: Logging for Name Service Switch plugin
+ *
+ * Copyright (C) 2025 Red Hat, Inc.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#pragma once
+
+typedef enum {
+    NSS_DEBUG,
+    NSS_ERROR,
+} nssLogPriority;
+
+#define DEBUG(...) \
+    nssLog(NSS_DEBUG, __FUNCTION__, __LINE__, __VA_ARGS__)
+
+#define ERROR(...) \
+    nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__)
+
+void
+nssLog(nssLogPriority prio,
+       const char *func,
+       int linenr,
+       const char *fmt, ...) __attribute__ ((format(printf, 4, 5)));
index 38bba2d616a3f3181e59d954dab2fc5478d0b7f9..5893952adc8216cb29cda80170fc18fa45163038 100644 (file)
@@ -13,6 +13,7 @@ endif
 nss_sources = [
   'libvirt_nss.c',
   'libvirt_nss_leases.c',
+  'libvirt_nss_log.c',
 ]
 
 nss_guest_sources = [