Blame 0004-Feature-alternative-nametag-provider-libsystemd.patch

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