cmdlib.sh: Move daemon functions to daemonlib.sh

Move the daemon start/stop functions to separate daemonlib.sh library.

Add documentation to test-writing-guidelines

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index f637da6..337a6a8 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -764,6 +764,56 @@
 tst_exit
 -------------------------------------------------------------------------------
 
+2.3.4 Restarting daemons
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+Restarting system daemons is a complicated task for two reasons.
+
+* There are different init systems
+  (SysV init, systemd, etc...)
+
+* Daemon names are not unified between distributions
+  (apache vs httpd, cron vs crond, various syslog variations)
+
+To solve these problems LTP has 'testcases/lib/daemonlib.sh' library that
+provides functions to start/stop/query daemons as well as variables that store
+correct daemon name.
+
+.Supported operations
+|==============================================================================
+| start_daemon()   | Starts daemon, name is passed as first parameter.
+| stop_daemon()    | Stops daemon, name is passed as first parameter.
+| restart_daemon() | Restarts daemon, name is passed as first parameter.
+| status_daemon()  | Returns daemon status, TODO: what is return value?
+|==============================================================================
+
+.Variables with detected names
+|==============================================================================
+| CROND_DAEMON | Cron daemon name (cron, crond).
+| SYSLOG_DAEMON | Syslog daemon name (syslog, syslog-ng, rsyslog).
+|==============================================================================
+
+.Cron daemon restart example
+[source,sh]
+-------------------------------------------------------------------------------
+#!/bin/sh
+#
+# Cron daemon restart example
+#
+TCID=cron01
+TST_COUNT=1
+. test.sh
+. daemonlib.sh
+
+...
+
+restart_daemon $CROND_DAEMON
+
+...
+
+tst_exit
+-------------------------------------------------------------------------------
+
 
 3. Common problems
 ------------------
diff --git a/testcases/lib/cmdlib.sh b/testcases/lib/cmdlib.sh
index 412fe23..104504f 100644
--- a/testcases/lib/cmdlib.sh
+++ b/testcases/lib/cmdlib.sh
@@ -154,63 +154,4 @@
 [ -z "$TCID" ] && TCID=${0##*/}
 TC=$(echo "$TCID" | awk '{ sub( /[0-9]+$/,""); print; }')
 
-# running under systemd?
-if command -v systemctl >/dev/null 2>&1; then
-	HAVE_SYSTEMCTL=1
-else
-	HAVE_SYSTEMCTL=0
-fi
-
-# Check to see if syslogd, syslog-ng or rsyslogd exists
-SYSLOG_DAEMON=""
-if command -v syslogd >/dev/null 2>&1; then
-	SYSLOG_DAEMON="syslog"
-elif command -v syslog-ng >/dev/null 2>&1; then
-	SYSLOG_DAEMON="syslog-ng"
-elif command -v rsyslogd >/dev/null 2>&1; then
-	SYSLOG_DAEMON="rsyslog"
-fi
-
-# Check to see if cron or crond exists
-CROND_DAEMON=""
-if command -v crond >/dev/null 2>&1; then
-	CROND_DAEMON="crond"
-elif command -v cron >/dev/null 2>&1; then
-	CROND_DAEMON="cron"
-fi
-
-start_daemon()
-{
-	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
-		systemctl start $1.service > /dev/null 2>&1
-	else
-		service $1 start > /dev/null 2>&1
-	fi
-}
-
-stop_daemon()
-{
-	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
-		systemctl stop $1.service > /dev/null 2>&1
-	else
-		service $1 stop > /dev/null 2>&1
-	fi
-}
-
-status_daemon()
-{
-	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
-		systemctl status $1.service > /dev/null 2>&1
-	else
-		service $1 status > /dev/null 2>&1
-	fi
-}
-
-restart_daemon()
-{
-	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
-		systemctl restart $1.service > /dev/null 2>&1
-	else
-		service $1 restart > /dev/null 2>&1
-	fi
-}
+. daemonlib.sh
diff --git a/testcases/lib/daemonlib.sh b/testcases/lib/daemonlib.sh
new file mode 100644
index 0000000..b924e17
--- /dev/null
+++ b/testcases/lib/daemonlib.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+#
+# Copyright (C) 2009, Cisco Systems Inc.
+#  Garrett Cooper, August 2009
+# Copyright (C) 2012-2014 Linux Test Project
+#
+# 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 of the License, or
+# (at your option) any later version.
+#
+# This program 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 program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+
+# running under systemd?
+if command -v systemctl >/dev/null 2>&1; then
+	HAVE_SYSTEMCTL=1
+else
+	HAVE_SYSTEMCTL=0
+fi
+
+# Check to see if syslogd, syslog-ng or rsyslogd exists
+SYSLOG_DAEMON=""
+if command -v syslogd >/dev/null 2>&1; then
+	SYSLOG_DAEMON="syslog"
+elif command -v syslog-ng >/dev/null 2>&1; then
+	SYSLOG_DAEMON="syslog-ng"
+elif command -v rsyslogd >/dev/null 2>&1; then
+	SYSLOG_DAEMON="rsyslog"
+fi
+
+# Check to see if cron or crond exists
+CROND_DAEMON=""
+if command -v crond >/dev/null 2>&1; then
+	CROND_DAEMON="crond"
+elif command -v cron >/dev/null 2>&1; then
+	CROND_DAEMON="cron"
+fi
+
+start_daemon()
+{
+	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
+		systemctl start $1.service > /dev/null 2>&1
+	else
+		service $1 start > /dev/null 2>&1
+	fi
+}
+
+stop_daemon()
+{
+	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
+		systemctl stop $1.service > /dev/null 2>&1
+	else
+		service $1 stop > /dev/null 2>&1
+	fi
+}
+
+status_daemon()
+{
+	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
+		systemctl status $1.service > /dev/null 2>&1
+	else
+		service $1 status > /dev/null 2>&1
+	fi
+}
+
+restart_daemon()
+{
+	if [ $HAVE_SYSTEMCTL -eq 1 ]; then
+		systemctl restart $1.service > /dev/null 2>&1
+	else
+		service $1 restart > /dev/null 2>&1
+	fi
+}