summaryrefslogtreecommitdiff
blob: 890ded531f52ee48b039bad0b1e11769d3e08a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh

APACHE2="/usr/sbin/apache2"
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"


is_systemd() {
        [ $(ps -o comm= -p 1) = "systemd" ] && return 0
        return 1
}

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}"
                exit 1
        fi
        . ${APACHE_RC_CONF}
        export APACHE2_OPTS
        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
reload() {
        RELOAD_TYPE="${RELOAD_TYPE:-graceful}"

        if [ "${RELOAD_TYPE}" = "restart" ]; then
                ${APACHE2} ${APACHE2_OPTS} -k restart
        elif [ "${RELOAD_TYPE}" = "graceful" ]; then
                ${APACHE2} ${APACHE2_OPTS} -k graceful
        else
                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
fullstatus() {
        LYNX="${LYNX:-lynx -dump}"
        STATUSURL="${STATUSURL:-http://localhost/server-status}"

        if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
                echo 'lynx not found! you need to emerge www-client/lynx'
        else
                ${LYNX} ${STATUSURL}
        fi
        return $?
}

# Basically the code from '/etc/init.d/apache2::checkconfd()', updated to run without open-rc
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
configtest() {
        checkconfd || return 1

        local ret
        OUTPUT="$(${APACHE2} ${APACHE2_OPTS} -t 2>&1)"
        ret=$?
        if [ ${ret} -ne 0 ]; then
                echo "apache2 has detected an error in your setup:"
                printf "%s\n" "${OUTPUT}"
        fi

        return ${ret}
}

# Basically the code from '/etc/init.d/apache2::configdump()', updated to run without open-rc
configdump() {
        INFOURL="${INFOURL:-http://localhost/server-info}"

        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
        fi
}


if ! is_systemd ; then
	# 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} -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