On a Friday in 2025, Michal Privoznik via Devel wrote:
From: Michal Privoznik <mprivozn(a)redhat.com>
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(a)redhat.com>
---
build-aux/syntax-check.mk | 2 +-
tools/nss/libvirt_nss.h | 30 +--------------
tools/nss/libvirt_nss_log.c | 76 +++++++++++++++++++++++++++++++++++++
tools/nss/libvirt_nss_log.h | 38 +++++++++++++++++++
tools/nss/meson.build | 1 +
5 files changed, 117 insertions(+), 30 deletions(-)
create mode 100644 tools/nss/libvirt_nss_log.c
create mode 100644 tools/nss/libvirt_nss_log.h
diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c
new file mode 100644
index 0000000000..0863897c07
--- /dev/null
+++ b/tools/nss/libvirt_nss_log.c
@@ -0,0 +1,76 @@
+/*
+ * libvirt_nss_log: Logging for Name Service Switch plugin
+ *
+ * Copyright (C) 2025 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 more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <
http://www.gnu.org/licenses/>.
Can we replace this with just:
SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <config.h>
+
+#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);
clang complains:
../../work/libvirt/tools/nss/libvirt_nss_log.c:79:13: error: ignoring return value of
function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]
79 | strerror_r(saved_errno, ebuf, ebuf_size);
| ^~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It seems we're building with GNU_SOURCE, so we should print the char*
returned by the function; not our allocated buffer.
+ 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
index 0000000000..bac64d4bc7
--- /dev/null
+++ b/tools/nss/libvirt_nss_log.h
@@ -0,0 +1,38 @@
+/*
+ * libvirt_nss_log: Logging for Name Service Switch plugin
+ *
+ * Copyright (C) 2025 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 more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <
http://www.gnu.org/licenses/>.
>+ */
Same comment about SPDX here.
Jano
+
+#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)));
diff --git a/tools/nss/meson.build b/tools/nss/meson.build
index 38bba2d616..5893952adc 100644
--- a/tools/nss/meson.build
+++ b/tools/nss/meson.build
@@ -13,6 +13,7 @@ endif
nss_sources = [
'libvirt_nss.c',
'libvirt_nss_leases.c',
+ 'libvirt_nss_log.c',
]
nss_guest_sources = [
--
2.49.0