From 5c9adf0752f58f6884ce7f241c76cdd91dca5e5d Mon Sep 17 00:00:00 2001
From: William Brown <firstyear@redhat.com>
Date: Wed, 20 Jul 2016 13:13:19 +1000
Subject: [PATCH] Ticket 48931 - start-ds-admin should use systemctl

Bug Description:  Due to the design of start-ds-admin, it can be annoying
to get selinux policy to work with this. We should use systemctl when avaliable
to correctly have our admin process labeled.

Fix Description:  When the configure flag specifies a unit dir, we set a flag
in the start / stop / restart scripts that uses systemctl instead.

https://fedorahosted.org/389/ticket/48931

Author: wibrown

Review by: ???
---
 Makefile.am                          |  4 ++
 admserv/cfgstuff/restart-ds-admin.in | 12 +++--
 admserv/cfgstuff/start-ds-admin.in   | 87 +++++++++++++++++++-----------------
 admserv/cfgstuff/stop-ds-admin.in    | 55 +++++++++++++----------
 4 files changed, 91 insertions(+), 67 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index e3f119a..a0afb6e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -189,8 +189,10 @@ selinux-built/dirsrv-admin.fc: selinux-built
 
 if SYSTEMD
 systemdsystemunit_DATA = wrappers/$(PACKAGE_NAME).service
+SYSTEMD_ENABLE=1
 else
 init_SCRIPTS = wrappers/$(PACKAGE_NAME)
+SYSTEMD_ENABLE=0
 endif
 
 initconfig_DATA = admserv/cfgstuff/$(PACKAGE_NAME)
@@ -566,6 +568,7 @@ fixupcmd = sed \
         -e 's,@updatedir\@,$(updatedir),g' \
 	-e 's,@with_selinux\@,@with_selinux@,g' \
 	-e 's,@enable_service\@,@enable_service@,g' \
+	-e 's,@systemd_enable\@,$(SYSTEMD_ENABLE),g' \
 	-e 's,@systemdsystemunitdir\@,$(systemdsystemunitdir),g' \
 	-e 's,@systemddirsrvgroupname\@,$(systemddirsrvgroupname),g' \
 	-e 's,@dsgwcgibindir\@,$(dsgwcgibindir),g' \
@@ -628,6 +631,7 @@ fixupcmd = sed \
 	-e 's,@updatedir\@,$(updatedir),g' \
 	-e 's,@with_selinux\@,@with_selinux@,g' \
 	-e 's,@enable_service\@,@enable_service@,g' \
+	-e 's,@systemd_enable\@,$(SYSTEMD_ENABLE),g' \
 	-e 's,@systemdsystemunitdir\@,$(systemdsystemunitdir),g' \
 	-e 's,@systemddirsrvgroupname\@,$(systemddirsrvgroupname),g' \
 	-e 's,@dsgwcgibindir\@,$(dsgwcgibindir),g' \
diff --git a/admserv/cfgstuff/restart-ds-admin.in b/admserv/cfgstuff/restart-ds-admin.in
index b7a42f0..a4c0a43 100755
--- a/admserv/cfgstuff/restart-ds-admin.in
+++ b/admserv/cfgstuff/restart-ds-admin.in
@@ -21,6 +21,12 @@
 
 # END COPYRIGHT BLOCK
 
-@cmdbindir@/stop-ds-admin
-sleep 3
-@cmdbindir@/start-ds-admin
+SYSTEMD_ENABLE=@systemd_enable@
+
+if [ ${SYSTEMD_ENABLE} -eq 1 ] ; then
+    systemctl restart @package_name@.service
+else
+    @cmdbindir@/stop-ds-admin
+    sleep 3
+    @cmdbindir@/start-ds-admin
+fi
diff --git a/admserv/cfgstuff/start-ds-admin.in b/admserv/cfgstuff/start-ds-admin.in
index 2d0e4bf..3e6ac31 100644
--- a/admserv/cfgstuff/start-ds-admin.in
+++ b/admserv/cfgstuff/start-ds-admin.in
@@ -67,56 +67,63 @@ export LIBPATH
 
 HTTPD=@HTTPD@
 OS=`uname -s`
-
-# see if httpd is linked with the openldap libraries - we need to override them if
-# using mozldap
-if [ -z "@use_openldap@" ] ; then
-    if [ $OS = "Linux" ]; then
-        hasol=0
-
-        /usr/bin/ldd $HTTPD 2>&1 | grep libldap > /dev/null 2>&1 && hasol=1
-
-        if [ $hasol -eq 1 ] ; then
-            LD_PRELOAD="@ldapsdk_libdir@/libldap60.so"
-            ssl_lib="@nss_libdir@/libssl3.so"
-            if [ -f "$ssl_lib" ] ; then
-                LD_PRELOAD="$ssl_lib $LD_PRELOAD"
+SYSTEMD_ENABLE=@systemd_enable@
+
+if [ ${SYSTEMD_ENABLE} -eq 1 ] ; then
+    systemctl start @package_name@.service
+
+else
+    # see if httpd is linked with the openldap libraries - we need to override them if
+    # using mozldap
+    if [ -z "@use_openldap@" ] ; then
+        if [ $OS = "Linux" ]; then
+            hasol=0
+
+            /usr/bin/ldd $HTTPD 2>&1 | grep libldap > /dev/null 2>&1 && hasol=1
+
+            if [ $hasol -eq 1 ] ; then
+                LD_PRELOAD="@ldapsdk_libdir@/libldap60.so"
+                ssl_lib="@nss_libdir@/libssl3.so"
+                if [ -f "$ssl_lib" ] ; then
+                    LD_PRELOAD="$ssl_lib $LD_PRELOAD"
+                fi
+                export LD_PRELOAD
             fi
-            export LD_PRELOAD
         fi
     fi
-fi
 
-# mod_deflate is not supported on HP-UX using the /opt/hpws apache
-if [ $OS = "HP-UX" ] ; then
-    OMIT_DEFLATE="-DOmitDeflate"
-fi
+    # mod_deflate is not supported on HP-UX using the /opt/hpws apache
+    if [ $OS = "HP-UX" ] ; then
+        OMIT_DEFLATE="-DOmitDeflate"
+    fi
 
-# source env. for admin server
-[ -f @initconfigdir@/@package_name@ ] && . @initconfigdir@/@package_name@
+    # source env. for admin server
+    [ -f @initconfigdir@/@package_name@ ] && . @initconfigdir@/@package_name@
 
-if [ -z "@with_selinux@" ] ; then
-    if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
-        SELINUX_CMD="runcon -t unconfined_t --"
+    if [ -z "@with_selinux@" ] ; then
+        if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
+            SELINUX_CMD="runcon -t unconfined_t --"
+        fi
     fi
-fi
 
-$SELINUX_CMD $HTTPD $OMIT_DEFLATE -k start -f @configdir@/httpd.conf "$@"
+    $SELINUX_CMD $HTTPD $OMIT_DEFLATE -k start -f @configdir@/httpd.conf "$@"
 
-PIDFILE=@localstatedir@/run/@PACKAGE_BASE_NAME@/admin-serv.pid
+    PIDFILE=@localstatedir@/run/@PACKAGE_BASE_NAME@/admin-serv.pid
 
-loop_counter=1
-# wait for 10 seconds for the pid file to appear
-max_count=10
-while test $loop_counter -le $max_count; do
-    loop_counter=`expr $loop_counter + 1`
+    loop_counter=1
+    # wait for 10 seconds for the pid file to appear
+    max_count=10
+    while test $loop_counter -le $max_count; do
+        loop_counter=`expr $loop_counter + 1`
+        if test ! -f $PIDFILE ; then
+            sleep 1;
+        else
+            PID=`cat $PIDFILE`
+        fi
+    done
     if test ! -f $PIDFILE ; then
-        sleep 1;
-    else
-        PID=`cat $PIDFILE`
+        echo Server failed to start !!! Please check errors log for problems
+        exit 1
     fi
-done
-if test ! -f $PIDFILE ; then
-    echo Server failed to start !!! Please check errors log for problems
-    exit 1
+
 fi
diff --git a/admserv/cfgstuff/stop-ds-admin.in b/admserv/cfgstuff/stop-ds-admin.in
index be63bec..e00a7ae 100755
--- a/admserv/cfgstuff/stop-ds-admin.in
+++ b/admserv/cfgstuff/stop-ds-admin.in
@@ -20,31 +20,38 @@
 #
 
 # END COPYRIGHT BLOCK
+SYSTEMD_ENABLE=@systemd_enable@
 
-PID_FILE=@piddir@/@pidfile@
-if test -f $PID_FILE ; then
-	kill -TERM `cat $PID_FILE`
-	if test $? -ne 0 ; then
-		exit 1
-	fi
+if [ ${SYSTEMD_ENABLE} -eq 1 ] ; then
+    systemctl stop @package_name@.service
 else
-	echo server not running
-	exit 1
-fi
 
-loop_counter=1
-max_count=30
-while test $loop_counter -le $max_count; do
-	loop_counter=`expr $loop_counter + 1`
-	if test -f $PID_FILE ; then
-		sleep 2
-	else
-		exit 0
-	fi
-done
-echo server not responding to exit command
-echo killing process group
-kill -9 `cat $PID_FILE`
-rm $PID_FILE
+    PID_FILE=@piddir@/@pidfile@
+    if test -f $PID_FILE ; then
+        kill -TERM `cat $PID_FILE`
+        if test $? -ne 0 ; then
+            exit 1
+        fi
+    else
+        echo server not running
+        exit 1
+    fi
+
+    loop_counter=1
+    max_count=30
+    while test $loop_counter -le $max_count; do
+        loop_counter=`expr $loop_counter + 1`
+        if test -f $PID_FILE ; then
+            sleep 2
+        else
+            exit 0
+        fi
+    done
+    echo server not responding to exit command
+    echo killing process group
+    kill -9 `cat $PID_FILE`
+    rm $PID_FILE
 
-exit 1
+    exit 1
+
+fi
-- 
1.8.3.1

