commit:     fcc7da95dd75910085a8c688b8ec5d25ba11bd1e
Author:     Lars Wendler <polynomial-c <AT> gentoo <DOT> org>
AuthorDate: Tue Apr  2 08:31:45 2019 +0000
Commit:     Lars Wendler <polynomial-c <AT> gentoo <DOT> org>
CommitDate: Tue Apr  2 08:39:12 2019 +0000
URL:        https://gitweb.gentoo.org/proj/apache.git/commit/?id=fcc7da95

Several fixes to previous apache2ctl systemd enhancement

- Attempt to keep the script POSIX sh compliant
- Don't use "function" prefix
- Use curly braces for variables
- Fixed indentation
- Put code for systemd into an "else" part
- Renamed "ERROR" variable to "retval"
- Made some variables local
- Put the systemd apache2 service file reference into a variable

Bug: https://bugs.gentoo.org/673530
Signed-off-by: Lars Wendler <polynomial-c <AT> gentoo.org>

 2.4/scripts/apache2ctl | 198 ++++++++++++++++++++++++-------------------------
 1 file changed, 97 insertions(+), 101 deletions(-)

diff --git a/2.4/scripts/apache2ctl b/2.4/scripts/apache2ctl
index 392ac4c..55975d4 100644
--- a/2.4/scripts/apache2ctl
+++ b/2.4/scripts/apache2ctl
@@ -1,18 +1,18 @@
 #!/bin/sh
 
 APACHE2="/usr/sbin/apache2"
-APACHE_OPTS=""
+APACHE2_OPTS=""
 APACHE_RC_CONF="/etc/conf.d/apache2"
 # List of init script verbs that should be passed forward
 RC_VERBS="start stop restart checkconfd configtest modules virtualhosts 
configdump fullstatus graceful gracefulstop reload"
 
 
-function is_systemd() {
-        [ $(ps --no-headers -o comm 1) == "systemd" ] && return 0
+is_systemd() {
+        [ $(ps -o comm= 1) = "systemd" ] && return 0
         return 1
 }
 
-function load_rc_config() {
+load_rc_config() {
         [ -f "${APACHE_RC_CONF}" ] || return 1
         if ! grep -q '^[[:space:]]*APACHE2_OPTS' ${APACHE_RC_CONF} ; then
                 echo "Cannot find APACHE2_OPTS in ${APACHE_RC_CONF}"
@@ -20,12 +20,12 @@ function load_rc_config() {
         fi
         . ${APACHE_RC_CONF}
         export APACHE2_OPTS
-        export SERVERROOT="${SERVERROOT:-/usr/lib64/apache2}"
+        export SERVERROOT="${SERVERROOT:-/usr/@LIBDIR@/apache2}"
         export CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}"
 }
 
 # Basically the code from '/etc/init.d/apache2::reload()', updated to run 
without open-rc
-function reload() {
+reload() {
         RELOAD_TYPE="${RELOAD_TYPE:-graceful}"
 
         if [ "${RELOAD_TYPE}" = "restart" ]; then
@@ -33,12 +33,12 @@ function reload() {
         elif [ "${RELOAD_TYPE}" = "graceful" ]; then
                 ${APACHE2} ${APACHE2_OPTS} -k graceful
         else
-                echo "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit 
/etc/conf.d/apache2"
+                echo "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit 
${APACHE_RC_CONF}"
         fi
 }
 
 # Basically the code from '/etc/init.d/apache2::fullstatus()', updated to run 
without open-rc
-function fullstatus() {
+fullstatus() {
         LYNX="${LYNX:-lynx -dump}"
         STATUSURL="${STATUSURL:-http://localhost/server-status}";
 
@@ -51,35 +51,37 @@ function fullstatus() {
 }
 
 # Basically the code from '/etc/init.d/apache2::checkconfd()', updated to run 
without open-rc
-function checkconfd() {
-        if [ ! -d ${SERVERROOT} ]; then
+checkconfd() {
+        if [ ! -d "${SERVERROOT}" ]; then
                 echo "SERVERROOT does not exist: ${SERVERROOT}"
                 return 1
         fi
 }
 
 # Basically the code from '/etc/init.d/apache2::checkconfig()', updated to run 
without open-rc
-function configtest() {
+configtest() {
         checkconfd || return 1
 
-        OUTPUT=$( ${APACHE2} ${APACHE2_OPTS} -t 2>&1 )
+        local ret
+        OUTPUT="$(${APACHE2} ${APACHE2_OPTS} -t 2>&1)"
         ret=$?
-        if [ $ret -ne 0 ]; then
+        if [ ${ret} -ne 0 ]; then
                 echo "apache2 has detected an error in your setup:"
                 printf "%s\n" "${OUTPUT}"
         fi
 
-        return $ret
+        return ${ret}
 }
 
 # Basically the code from '/etc/init.d/apache2::configdump()', updated to run 
without open-rc
-function configdump() {
+configdump() {
         INFOURL="${INFOURL:-http://localhost/server-info}";
 
-        if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
+        if ! command -v $(set -- ${LYNX}; echo ${1}) 2>&1 >/dev/null; then
                 echo "lynx not found! you need to emerge www-client/lynx"
         else
                 echo "${APACHE2} started with '${APACHE2_OPTS}'"
+                local i
                 for i in config server list; do
                         ${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server 
Information/d;/^[[:space:]]\+[_]\+$/Q'
                 done
@@ -87,90 +89,84 @@ function configdump() {
 }
 
 
-# If systemd IS NOT detected, run the legacy apache2ctl code
 if ! is_systemd; then
-        # If first parameter is a verb defined in $RC_VERBS, pass the command 
to init script.
-        # In other cases, compile command line and run the command on apache 
binary.
-        if echo "${RC_VERBS}" | grep -q -- "${1}" ; then
-                exec /etc/init.d/apache2 "${@}"
-        else
-                load_rc_config || exit 1
-                ${APACHE2} ${APACHE2_OPTS} -d ${SERVERROOT} -f ${CONFIGFILE} 
"${@}"
-        fi
-        exit 0
+       # If systemd IS NOT detected, run the legacy apache2ctl code
+
+       # If first parameter is a verb defined in $RC_VERBS, pass the command 
to init script.
+       # In other cases, compile command line and run the command on apache 
binary.
+       if echo "${RC_VERBS}" | grep -q -- "${1}" ; then
+               exec /etc/init.d/apache2 "${@}"
+       else
+               load_rc_config || exit 1
+               ${APACHE2} ${APACHE2_OPTS} -d ${SERVERROOT} -f ${CONFIGFILE} 
"${@}"
+       fi
+else
+       # If systemd IS detected, load the config and parse the argument
+
+       # Yes, we load config from apache's openrc conf.d file.
+       # Feel free to provide a more suitable solution.
+       load_rc_config || exit 1
+
+       # Append the server root and configuration file parameters to the
+       # user's APACHE2_OPTS.
+       APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}"
+       APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}"
+
+       apache_service="apache2.service"
+
+       case ${1} in
+               # Original apachectl options
+               # See: https://httpd.apache.org/docs/2.4/programs/apachectl.html
+               start|stop|restart|status)
+                       systemctl ${1} ${apache_service}
+                       retval=$?
+               ;;
+               reload)
+                       reload
+                       retval=$?
+               ;;
+               fullstatus)
+                       fullstatus
+                       retval=$?
+               ;;
+               graceful)
+                       configtest || exit 1
+                       systemctl reload ${apache_service}
+                       retval=$?
+               ;;
+               gracefulstop|graceful-stop)
+                       configtest || exit 1
+                       systemctl stop ${apache_service}
+                       retval=$?
+               ;;
+               configtest)
+                       configtest
+                       retval=$?
+               ;;
+               checkconfd)
+                       checkconfd
+                       retval=$?
+               ;;
+               configdump)
+                       configtest || exit 1
+                       configdump
+                       retval=$?
+               ;;
+               virtualhosts)
+                       configtest || exit 1
+                       ${APACHE2} ${APACHE2_OPTS} -S
+                       retval=$?
+               ;;
+               modules)
+                       configtest || exit 1
+                       ${APACHE2} ${APACHE2_OPTS} -M 2>&1
+                       retval=$?
+               ;;
+               # For all other options fall back to the legacy way of handling 
them
+               *)
+                       ${APACHE2} ${APACHE2_OPTS} "${@}"
+                       retval=$?
+               ;;
+       esac
+       exit ${retval}
 fi
-
-# If systemd IS detected, load the config and parse the argument
-load_rc_config || exit 1
-
-# Append the server root and configuration file parameters to the
-# user's APACHE2_OPTS.
-APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}"
-APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}"
-
-case $1 in
-# Original apachectl options
-# See: https://httpd.apache.org/docs/2.4/programs/apachectl.html
-start|stop|restart|status)
-        /bin/systemctl $1 apache2.service
-        ERROR=$?
-        ;;
-
-reload)
-        reload
-        ERROR=$?
-        ;;
-
-fullstatus)
-        fullstatus
-        ERROR=$?
-        ;;
-
-graceful)
-        configtest || exit 1
-        /bin/systemctl reload apache2.service
-        ERROR=$?
-        ;;
-
-gracefulstop|graceful-stop)
-        configtest || exit 1
-        /bin/systemctl stop apache2.service
-        ERROR=$?
-        ;;
-
-configtest)
-        configtest
-        ERROR=$?
-        ;;
-
-checkconfd)
-        checkconfd
-        ERROR=$?
-        ;;
-
-configdump)
-        configtest || exit 1
-        configdump
-        ERROR=$?
-        ;;
-
-virtualhosts)
-        configtest || exit 1
-        ${APACHE2} ${APACHE2_OPTS} -S
-        ERROR=$?
-        ;;
-
-modules)
-        configtest || exit 1
-        ${APACHE2} ${APACHE2_OPTS} -M 2>&1
-        ERROR=$?
-        ;;
-
-# For all other options fall back to the legacy way of handling them
-*)
-        ${APACHE2} ${APACHE2_OPTS} "${@}"
-        ERROR=$?
-        ;;
-esac
-
-exit $ERROR

Reply via email to