[rbldnsd] Use native systemd init from F-17 onwards (see README.systemd)

Paul Howarth pghmcfc at fedoraproject.org
Tue Apr 17 16:52:53 UTC 2012


commit 482aa3ee3f6d44ff3d27a532cc8d8c86623a4e53
Author: Paul Howarth <paul at city-fan.org>
Date:   Tue Apr 17 17:52:18 2012 +0100

    Use native systemd init from F-17 onwards (see README.systemd)

 README.systemd    |   76 +++++++++++++++++++++++
 rbldnsd.conf      |   15 +++++
 rbldnsd.spec      |   80 +++++++++++++++++++++---
 systemctl-rbldnsd |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 337 insertions(+), 9 deletions(-)
---
diff --git a/README.systemd b/README.systemd
new file mode 100644
index 0000000..e56cef1
--- /dev/null
+++ b/README.systemd
@@ -0,0 +1,76 @@
+Using rbldnsd with systemd
+==========================
+
+The traditional SysV initscript shipped with rbldnsd supports using
+multiple instances with arbitrary command lines. This is difficult
+to achieve with systemd so a wrapper script, systemctl-rbldnsd, has
+been provided to assist with the creation and use of native systemd
+unit files for each required instance, using exactly the same
+configuration in /etc/sysconfig/rbldnsd as the SysV initscript used.
+
+Having edited /etc/sysconfig/rbldnsd to specify the rbldnsd
+instance(s) required, individual unit files for each instance can be
+created by:
+
+  systemctl-rbldnsd create
+
+This creates unit files /etc/systemd/system/rbldnsd-<instancename>.service
+for each required instance. These units can then be manipulated
+individually using /bin/systemctl as usual, or all at once by using
+other systemctl-rbldnsd commands:
+
+  systemctl-rbldnsd enable
+
+This enables all configured rbldnsd instances to start at boot time
+(equivalent of chkconfig rbldnsd on).
+ 
+  systemctl-rbldnsd disable
+
+This disables all configured rbldnsd instances from starting at boot time
+(equivalent of chkconfig rbldnsd off).
+ 
+  systemctl-rbldnsd start
+
+This starts all configured rbldnsd instances immediately
+(equivalent of service rbldnsd start).
+ 
+  systemctl-rbldnsd stop
+
+This stops all configured rbldnsd instances
+(equivalent of service rbldnsd stop).
+ 
+  systemctl-rbldnsd reload
+
+This reloads all configured rbldnsd instances
+(equivalent of service rbldnsd reload).
+ 
+  systemctl-rbldnsd restart
+
+This restarts all configured rbldnsd instances
+(equivalent of service rbldnsd restart).
+ 
+  systemctl-rbldnsd condrestart
+  systemctl-rbldnsd try-restart
+
+These restart all configured rbldnsd instances immediately, if they are
+already running (equivalent of service rbldnsd condrestart).
+ 
+  systemctl-rbldnsd status
+
+This shows status of all configured rbldnsd instances
+(equivalent of service rbldnsd status).
+
+When editing /etc/sysconfig/rbldnsd to specify rbldnsd instance options,
+bear in mind:
+
+* systemd prefers the daemons it controls not to fork, so the unit files
+  created by systemctl-rbldnsd start rbldnsd with the "-n" option to prevent
+  it from forking; this also means that systemd knows the PIDs of all
+  instances itself, so there is no need to specify any forking/pidfile related
+  options in /etc/sysconfig/rbldnsd
+
+* if you want just a single instance of rbldnsd and use the traditional "-"
+  identifier for it, systemctl-rbldnsd will create a unit file
+  /etc/systemd/system/rbldnsd-single.service, i.e. the instance name will be
+  set to "single"
+
diff --git a/rbldnsd.conf b/rbldnsd.conf
new file mode 100644
index 0000000..46a8c33
--- /dev/null
+++ b/rbldnsd.conf
@@ -0,0 +1,15 @@
+# This is boilerplate text for rbldnsd unit files, which can be
+# created using "systemctl-rbldnsd create" based on the traditional
+# configuration variable RBLDNSD as defined in /etc/sysconfig/rbldnsd
+
+[Unit]
+Description=DNS daemon for DNSBLs
+After=network.target
+
+[Service]
+ExecReload=/bin/kill -HUP $MAINPID
+
+[Install]
+WantedBy=multi-user.target
+
+
diff --git a/rbldnsd.spec b/rbldnsd.spec
index d8d476f..805ab63 100644
--- a/rbldnsd.spec
+++ b/rbldnsd.spec
@@ -1,21 +1,38 @@
+# This package uses systemd init from Fedora 17, but can use it for
+# Fedora 15/16 if built using --with systemd
+%if 0%{?fedora} > 16 || 0%{?rhel} > 6
+%global _with_systemd --with-systemd
+%endif
+%global use_systemd %{!?_with_systemd:0}%{?_with_systemd:1}
+
 # Build hardened (PIE) where possible
 %define _hardened_build 1
 
 Summary:	Small, fast daemon to serve DNSBLs
 Name:		rbldnsd
 Version:	0.996b
-Release:	6%{?dist}
+Release:	7%{?dist}
 License:	GPLv2+
 Group:		System Environment/Daemons
 URL:		http://www.corpit.ru/mjt/rbldnsd.html
 Source0:	http://www.corpit.ru/mjt/rbldnsd/rbldnsd_%{version}.tar.gz
 Source1:	rbldnsd.init
+Source2:	rbldnsd.conf
+Source3:	systemctl-rbldnsd
+Source4:	README.systemd
 BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(id -nu)
 BuildRequires:	gawk, zlib-devel
-
 Requires(pre):	shadow-utils
+%if %{use_systemd}
+BuildRequires:	systemd-units
+Requires(pre):	systemd-sysv, /sbin/chkconfig
+Requires(post):	systemd-units
+Requires(preun): systemd-units
+Requires(postun): systemd-units
+%else
 Requires(post):	/sbin/chkconfig
 Requires(preun): /sbin/chkconfig
+%endif
 
 %description
 Rbldnsd is a small, authoritative-only DNS nameserver designed to serve
@@ -26,6 +43,7 @@ blocklists.
 %setup -q -n %{name}-%{version}
 sed -i	-e 's@/var/lib/rbldns\([/ ]\)@%{_localstatedir}/lib/rbldnsd\1 at g' \
 	-e 's@\(-r/[a-z/]*\) -b@\1 -q -b at g' debian/rbldnsd.default
+cp -p %{SOURCE1} %{SOURCE2} %{SOURCE3} %{SOURCE4} ./
 
 %build
 # this is not an autotools-generated configure script, and does not support --libdir
@@ -37,11 +55,16 @@ make
 %install
 rm -rf %{buildroot}
 mkdir -p %{buildroot}{%{_sbindir},%{_mandir}/man8,%{_initrddir},%{_sysconfdir}/sysconfig}
-mkdir -p %{buildroot}%{_localstatedir}/lib/rbldnsd
-install -m 755 rbldnsd			%{buildroot}%{_sbindir}
-install -m 644 rbldnsd.8		%{buildroot}%{_mandir}/man8
-install -m 644 debian/rbldnsd.default	%{buildroot}%{_sysconfdir}/sysconfig/rbldnsd
-install -m 755 %{SOURCE1}		%{buildroot}%{_initrddir}/rbldnsd
+mkdir -p %{buildroot}{/etc/systemd,%{_localstatedir}/lib/rbldnsd}
+install -p -m 755 rbldnsd			%{buildroot}%{_sbindir}/
+install -p -m 644 rbldnsd.8			%{buildroot}%{_mandir}/man8/
+install -p -m 644 debian/rbldnsd.default	%{buildroot}%{_sysconfdir}/sysconfig/rbldnsd
+%if %{use_systemd}
+install -p -m 644 rbldnsd.conf			%{buildroot}/etc/systemd/
+install -p -m 755 systemctl-rbldnsd		%{buildroot}%{_sbindir}/
+%else
+install -p -m 755 rbldnsd.init			%{buildroot}%{_initrddir}/rbldnsd
+%endif
 
 %clean
 rm -rf %{buildroot}
@@ -51,19 +74,49 @@ rm -rf %{buildroot}
 /usr/bin/getent passwd rbldns >/dev/null || \
 	/usr/sbin/useradd -r -g rbldns -d %{_localstatedir}/lib/rbldnsd \
 		-s /sbin/nologin -c "rbldns daemon" rbldns
+%if %{use_systemd}
+# SysV-to-systemd migration
+if [ $1 -gt 1 -a ! -e /etc/systemd/rbldnsd.conf -a -e %{_initrddir}/rbldnsd ]; then
+	/usr/bin/systemd-sysv-convert --save rbldnsd &>/dev/null || :
+	/sbin/chkconfig --del rbldnsd &>/dev/null || :
+fi
+%endif
 exit 0
 
 %post
-/sbin/chkconfig --add rbldnsd || :
+%if %{use_systemd}
+/bin/systemctl daemon-reload &>/dev/null || :
+%else
+if [ $1 -eq 1 ]; then
+	# Initial installation
+	/sbin/chkconfig --add rbldnsd || :
+fi
+%endif
 
 %preun
 if [ $1 -eq 0 ]; then
+	# Package removal, not upgrade
+%if %{use_systemd}
+	%{_sbindir}/systemctl-rbldnsd stop &>/dev/null || :
+	%{_sbindir}/systemctl-rbldnsd disable &>/dev/null || :
+%else
 	%{_initrddir}/rbldnsd stop &>/dev/null || :
 	/sbin/chkconfig --del rbldnsd || :
+%endif
 fi
 
 %postun
-%{_initrddir}/rbldnsd condrestart &>/dev/null || :
+%if %{use_systemd}
+/bin/systemctl daemon-reload &>/dev/null || :
+%endif
+if [ $1 -ge 1 ]; then
+	# Package upgrade, not uninstall
+%if %{use_systemd}
+	%{_sbindir}/systemctl-rbldnsd try-restart &>/dev/null || :
+%else
+	%{_initrddir}/rbldnsd condrestart &>/dev/null || :
+%endif
+fi
 
 %files
 %defattr (-,root,root,-)
@@ -72,9 +125,18 @@ fi
 %{_mandir}/man8/rbldnsd.8*
 %dir %{_localstatedir}/lib/rbldnsd/
 %config(noreplace) %{_sysconfdir}/sysconfig/rbldnsd
+%if %{use_systemd}
+%doc README.systemd
+%config(noreplace) %{_sysconfdir}/systemd/rbldnsd.conf
+%{_sbindir}/systemctl-rbldnsd
+%else
 %{_initrddir}/rbldnsd
+%endif
 
 %changelog
+* Tue Apr 17 2012 Paul Howarth <paul at city-fan.org> 0.996b-7
+- use native systemd init from F-17 onwards (see README.systemd)
+
 * Mon Apr 16 2012 Paul Howarth <paul at city-fan.org> 0.996b-6
 - fix some initscript issues (#807504)
 - do a hardened build (PIE) where possible
diff --git a/systemctl-rbldnsd b/systemctl-rbldnsd
new file mode 100644
index 0000000..a3fc118
--- /dev/null
+++ b/systemctl-rbldnsd
@@ -0,0 +1,175 @@
+#!/bin/bash
+#
+# systemctl-rbldnsd : helper script for managing rbldnsd instances with systemd
+#
+# Configuration is via the RBLDNSD variable in /etc/sysconfig/rbldnsd,
+# exactly as used with the traditional sysv initscript.
+#
+# Usage:
+#
+# systemctl-rbldnsd create
+#   Creates unit files /etc/systemd/system/rbldnsd-<instance>.service
+#   for each configured rbldnsd instance
+#
+# systemctl-rbldnsd enable
+#   Enables (via systemctl) all configured rbldnsd instances to start
+#   at boot time
+#
+# systemctl-rbldnsd disable
+#   Disables (via systemctl) all configured rbldnsd instances from
+#   starting at boot time
+#
+# systemctl-rbldnsd start
+#   Starts (via systemctl) all configured rbldnsd instances immediately
+#
+# systemctl-rbldnsd stop
+#   Stop (via systemctl) all configured rbldnsd instances immediately
+#
+# systemctl-rbldnsd reload
+#   Reloads (via systemctl) all configured rbldnsd instances immediately
+#
+# systemctl-rbldnsd restart
+#   Restarts (via systemctl) all configured rbldnsd instances immediately
+#
+# systemctl-rbldnsd condrestart
+# systemctl-rbldnsd try-restart
+#   Restarts (via systemctl) all configured rbldnsd instances immediately,
+#   if they are already running
+#
+# systemctl-rbldnsd status
+#   Shows status of all configured rbldnsd instances
+#
+PATH=/sbin:/bin:/usr/bin:/usr/sbin
+
+# Get config and check that RBLDNSD is set
+[ -f /etc/sysconfig/rbldnsd ] && . /etc/sysconfig/rbldnsd
+
+# Check that configuration has been set up (RBLDNSD set in /etc/sysconfig/rbldnsd)
+if [ -z "$RBLDNSD" ]; then
+	echo "systemctl-rbldnsd: RBLDNSD not configured in /etc/sysconfig/rbldnsd" >&2
+	exit 6
+fi
+
+# Set an exit status
+set_status() {
+	exit $1
+}
+
+# Process multiple instances of the daemon (see /etc/sysconfig/rbldnsd)
+for_all_daemons() {
+	ret=0
+	echo "$RBLDNSD" |
+		while read name args; do
+			# For a single instance (name = "-"), use "single" as name
+			case "$name" in
+				""|\#*)	continue;;
+				-)	name=single;;
+				*)	;;
+			esac
+			# Process this instance
+			$1
+			thisret=$?
+			if [ "$1" = "check_one_daemon" -o "$1" = "reload_one_daemon" ]; then
+				if [ $thisret -ne 0 ]; then
+					ret=$thisret
+				fi
+			else
+				if [ $thisret -ne 0 ]; then
+					ret=1
+				fi
+			fi
+		done
+	set_status $ret
+}
+
+start_one_daemon() {
+	/bin/systemctl start "rbldnsd-${name}.service"
+}
+
+stop_one_daemon() {
+	/bin/systemctl stop "rbldnsd-${name}.service"
+}
+
+reload_one_daemon() {
+	/bin/systemctl reload "rbldnsd-${name}.service"
+}
+
+check_one_daemon() {
+	/bin/systemctl status "rbldnsd-${name}.service"
+}
+
+restart_one_daemon() {
+	/bin/systemctl restart "rbldnsd-${name}.service"
+}
+
+condrestart_one_daemon() {
+	/bin/systemctl try-restart "rbldnsd-${name}.service"
+}
+
+create_one_daemon() {
+	cat > "/etc/systemd/system/rbldnsd-${name}.service" <<-END_OF_UNIT
+	.include /etc/systemd/rbldnsd.conf
+
+	[Unit]
+	Description=DNSBL (rbldnsd) ${name} instance
+
+	[Service]
+	ExecStart=/sbin/rbldnsd -n ${args}
+	END_OF_UNIT
+	echo "Created unit file /etc/systemd/system/rbldnsd-${name}.service"
+}
+
+enable_one_daemon() {
+	/bin/systemctl enable "rbldnsd-${name}.service"
+}
+
+disable_one_daemon() {
+	/bin/systemctl disable "rbldnsd-${name}.service"
+}
+
+# See how we were called.
+case "$1" in
+	create)
+		for_all_daemons create_one_daemon
+		RETVAL=$?
+		/bin/systemctl daemon-reload
+		;;
+	enable)
+		for_all_daemons enable_one_daemon
+		RETVAL=$?
+		;;
+	disable)
+		for_all_daemons disable_one_daemon
+		RETVAL=$?
+		;;
+	start)
+		for_all_daemons start_one_daemon
+		RETVAL=$?
+		;;
+	restart)
+		for_all_daemons restart_one_daemon
+		RETVAL=$?
+		;;
+	reload)
+		for_all_daemons reload_one_daemon
+		RETVAL=$?
+		;;
+	stop)
+		for_all_daemons stop_one_daemon
+		RETVAL=$?
+		;;
+	status)
+		for_all_daemons check_one_daemon
+		RETVAL=$?
+		;;
+	condrestart|try-restart)
+		for_all_daemons condrestart_one_daemon
+		RETVAL=$?
+		;;
+	*)
+		echo $"Usage: $0 {create|enable|disable|start|stop|restart|try-restart|reload|status}" >&2
+		RETVAL=1
+		;;
+esac
+
+exit $RETVAL


More information about the scm-commits mailing list