Blob Blame History Raw
From 6b9e9cd87e41bc07cfbfd5a607a739cfebb2e2ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
Date: Wed, 24 Feb 2016 02:15:47 +0100
Subject: [PATCH 4/6] Feature: alternative "nametag" provider: libsystemd

---
 booth.spec                   |  2 ++
 configure.ac                 | 22 ++++++++++++
 src/Makefile.am              | 10 +++++-
 src/alt/nametag_libsystemd.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
 src/alt/nametag_libsystemd.h | 23 +++++++++++++
 src/main.c                   |  6 +++-
 6 files changed, 142 insertions(+), 2 deletions(-)
 create mode 100644 src/alt/nametag_libsystemd.c
 create mode 100644 src/alt/nametag_libsystemd.h

diff --git a/booth.spec b/booth.spec
index b88ff4c..18387f7 100644
--- a/booth.spec
+++ b/booth.spec
@@ -71,6 +71,8 @@ BuildRequires:  libglue-devel
 BuildRequires:  pkgconfig(libqb)
 # random2range provider
 BuildRequires:  pkgconfig(glib-2.0)
+# nametag provider
+BuildRequires:  pkgconfig(libsystemd)
 %endif
 BuildRequires:  libxml2-devel
 BuildRequires:  zlib-devel
diff --git a/configure.ac b/configure.ac
index 10e131d..bb2e3a8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -268,6 +268,27 @@ glib)
 esac
 AM_CONDITIONAL([RANGE2RANDOM_GLIB], [test "x$range2random_provider" = "xglib"])
 
+# figure out nametag/distinguished-role provider
+nametag_provider=""
+if test "x$nametag_provider" = "x" && test "x$with_glue" != "xno"; then
+       AC_CHECK_LIB([plumbgpl], [set_proc_title], [nametag_provider="libplumbgpl"])
+fi
+if test "x$nametag_provider" = "x" && test "x$with_glue" = "xno"; then
+       AC_CHECK_LIB([systemd], [sd_notify], [nametag_provider="libsystemd"])
+fi
+case "$nametag_provider" in
+libplumbgpl)
+	;;
+libsystemd)
+	PKG_CHECK_MODULES([LIBSYSTEMD], [libsystemd])
+	AC_DEFINE([NAMETAG_LIBSYSTEMD], [], [use libsystemd as a nametag provider])
+	;;
+*)
+	AC_MSG_ERROR([nametag provider required (libplumbgpl, or libsystemd when --without-glue)])
+	;;
+esac
+AM_CONDITIONAL([NAMETAG_LIBSYSTEMD], [test "x$nametag_provider" = "xlibsystemd"])
+
 # OS detection
 # THIS SECTION MUST DIE!
 CP=cp
@@ -498,6 +519,7 @@ AC_MSG_RESULT([  SOCKETDIR                = ${SOCKETDIR}])
 AC_MSG_RESULT([  Features                 = ${PACKAGE_FEATURES}])
 AC_MSG_RESULT([  Logging provider         = ${logging_provider}])
 AC_MSG_RESULT([  Range2random provider    = ${range2random_provider}])
+AC_MSG_RESULT([  Nametag provider         = ${nametag_provider}])
 AC_MSG_RESULT([])
 AC_MSG_RESULT([$PACKAGE build info:])
 AC_MSG_RESULT([  Library SONAME           = ${SONAME}])
diff --git a/src/Makefile.am b/src/Makefile.am
index 317710e..69b7b48 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,7 +22,7 @@ boothd_SOURCES += auth.c
 endif
 
 boothd_LDFLAGS		= $(OS_DYFLAGS) -L./
-boothd_LDADD		= -lplumb -lplumbgpl -lm $(GLIB_LIBS) $(ZLIB_LIBS)
+boothd_LDADD		= -lplumb -lm $(GLIB_LIBS) $(ZLIB_LIBS)
 boothd_CFLAGS		= $(GLIB_CFLAGS)
 
 if !LOGGING_LIBQB
@@ -41,5 +41,13 @@ boothd_SOURCES		+= alt/range2random_glib.c
 noinst_HEADERS		+= alt/range2random_glib.h
 endif
 
+if !NAMETAG_LIBSYSTEMD
+boothd_LDADD		+= -lplumbgpl
+else
+boothd_LDADD		+= $(LIBSYSTEMD_LIBS)
+boothd_SOURCES		+= alt/nametag_libsystemd.c
+noinst_HEADERS		+= alt/nametag_libsystemd.h
+endif
+
 lint:
 	-splint $(INCLUDES) $(LINT_FLAGS) $(CFLAGS) *.c
diff --git a/src/alt/nametag_libsystemd.c b/src/alt/nametag_libsystemd.c
new file mode 100644
index 0000000..1fb9ffa
--- /dev/null
+++ b/src/alt/nametag_libsystemd.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 Jan Pokorny <jpokorny@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <assert.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <systemd/sd-daemon.h>
+
+#include "nametag_libsystemd.h"
+#include "booth.h"
+#include "log.h"
+#include "transport.h"
+
+/* assume first argument after "fmt" is for DAEMON_NAME, that is
+   really not of interest in our "nametag" function based on
+   sd_notify (that very data point is provided implicitly) */
+void sd_notify_wrapper(const char *fmt, ...)
+{
+	/* assume that first %s in fmt is intended for DAEMON_NAME,
+	   i.e., for first argument following fmt in original
+	   set_proc_title invocation, which has already been dropped
+	   before it boils down here (using the wrapping macro trick);
+	   we now simply append the reset after that first %s
+	   (with whitespace stripped) to the "Running: " prefix */
+	int rv;
+	char buffer[255];
+	char *fmt_iter;
+	char *suffix = NULL;
+	va_list ap;
+
+	switch (local->type) {
+		case ARBITRATOR:
+		case GEOSTORE:
+			break;
+		default:
+			return;  /* not expected to be run as system service */
+	}
+
+	fmt_iter = strchr(fmt, '%');
+	while (fmt_iter) {
+		switch (*++fmt_iter) {
+			case 's': suffix = fmt_iter;
+				  /* fall through */
+			default: fmt_iter = NULL;
+		}
+	}
+	if (!suffix) {
+		log_warn("%s:%d: invalid format: %s", __FILE__, __LINE__, fmt);
+		return;
+	}
+	while (isspace(*++suffix)) /* noop */ ;
+
+	va_start(ap, fmt);
+	fmt_iter = va_arg(ap, char *);  /* just shift by one */
+	assert(!strcmp(fmt_iter, DAEMON_NAME));
+	rv = vsnprintf(buffer, sizeof(buffer), suffix, ap);
+	va_end(ap);
+
+	rv = sd_notifyf(0, "READY=1\n"
+			"STATUS=Running: %s",
+			buffer);
+	if (rv < 0)
+		log_warn("%s:%d: sd_notifyf fail", __FILE__, __LINE__);
+}
diff --git a/src/alt/nametag_libsystemd.h b/src/alt/nametag_libsystemd.h
new file mode 100644
index 0000000..2c1dc1e
--- /dev/null
+++ b/src/alt/nametag_libsystemd.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2016 Jan Pokorny <jpokorny@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+void
+sd_notify_wrapper(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
+
+#define init_set_proc_title(c, a, e)  /* omitted */
+#define set_proc_title  sd_notify_wrapper
diff --git a/src/main.c b/src/main.c
index 498718b..27d285c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,7 +34,6 @@
 #include <sys/poll.h>
 #include <sys/wait.h>
 #include <pacemaker/crm/services.h>
-#include <clplumbing/setproctitle.h>
 #include <sys/prctl.h>
 #include <clplumbing/coredumps.h>
 #include <fcntl.h>
@@ -49,6 +48,11 @@
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include "b_config.h"
+#ifndef NAMETAG_LIBSYSTEMD
+#include <clplumbing/setproctitle.h>
+#else
+#include "alt/nametag_libsystemd.h"
+#endif
 #include "log.h"
 #include "booth.h"
 #include "config.h"
-- 
2.4.11