Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 473224 | Differences between
and this patch

Collapse All | Expand All

(-)/usr/portage/net-misc/asterisk/files/1.8.0/asterisk.initd5 (-49 / +97 lines)
Lines 11-48 Link Here
11
}
11
}
12
12
13
is_running() {
13
is_running() {
14
	[ -r /var/run/asterisk/asterisk.pid ] || return 1
14
	[ -r "${ast_rundir}/asterisk.pid" ] || return 1
15
	local ast_pid="`pidof asterisk`"
15
	PID="$(cat "${ast_rundir}/asterisk.pid")"
16
	if [ -z "${ast_pid}" ]; then
16
	[ -d "/proc/${PID}" ] || return 1
17
		return 1
17
	EXE="$(readlink -f /proc/${PID}/exe)"
18
	else
18
	EXE="${EXE% (deleted)}" # in case asterisk got upgraded and we're still looking at an old one.
19
		PID="`cat /var/run/asterisk/asterisk.pid`"
19
	[ "${EXE}" = /usr/sbin/asterisk ] || return 1 # pid got re-used for another process.
20
		for x in ${ast_pid}; do
20
21
			if [ "${x}" = "${PID}" ]; then
21
	# PID reported in pidfile is active, and is still an asterisk instance.
22
				return 0
22
	return 0
23
			fi	
23
}
24
		done
24
25
# Sets up a few variables for us for use
26
# ast_instancename: eg, asterisk when RC_SVCNAME=asterisk, or asterisk(foo) when asterisk.foo.
27
# ast_rundir: directory to be used as run folder (pid and ctl files).
28
# ast_spooldir: 
29
setup_svc_variables()
30
{
31
	local t
32
33
	ast_instancename=asterisk
34
	ast_rundir=/var/run/${RC_SVCNAME}
35
	ast_logdir=/var/log/${RC_SVCNAME}
36
	ast_confdir=/etc/${RC_SVCNAME/.//}
37
38
	if [ "${RC_SVCNAME}" != "asterisk" ]; then
39
		t="${RC_SVCNAME#asterisk.}"
40
		if [ "${RC_SVCNAME}" = "${t}" ]; then
41
			eerror "Invalid SVCNAME of ${RC_SVCNAME}, must be of the format asterisk.name."
42
			return 1
43
		fi
44
		ast_instancename+="(${t})"
25
	fi
45
	fi
26
46
27
	return 1
47
	[ -n "${ASTERISK_RUNDIR}" ] && ast_rundir="${ASTERISK_RUNDIR}"
48
	[ -n "${ASTERISK_LOGDIR}" ] && ast_logdir="${ASTERISK_LOGDIR}"
49
	[ -n "${ASTERISK_CONFDIR}" ] && ast_confdir="${ASTERISK_CONFDIR}"
50
51
	return 0
28
}
52
}
29
53
30
asterisk_run_loop() {
54
asterisk_run_loop() {
31
	local result=0 signal=0
55
	local result=0 signal=0
32
56
33
	echo "Initializing asterisk wrapper"
57
	echo "Initializing ${ast_instancename} wrapper"
34
	OPTS="$*"
58
	OPTS="$*"
35
59
36
	trap "rm -f /var/run/asterisk/wrapper_loop.running" EXIT
60
	trap "rm -f '${ast_rundir}/wrapper_loop.running'" EXIT
37
	touch "/var/run/asterisk/wrapper_loop.running"
61
	touch "${ast_rundir}/wrapper_loop.running"
38
62
39
	while [ -r "/var/run/asterisk/wrapper_loop.running" ]; do
63
	while [ -r "${ast_rundir}/wrapper_loop.running" ]; do
40
		if [ -n "${TTY}" ]; then
64
		if [ -n "${TTY}" ]; then
41
			/usr/bin/stty -F ${TTY} sane
65
			/usr/bin/stty -F "${TTY}" sane
42
			${NICE} /usr/sbin/asterisk ${OPTS} >${TTY} 2>&1 <${TTY}
66
			${NICE} /usr/sbin/asterisk -C "${ast_confdir}/asterisk.conf" ${OPTS} >${TTY} 2>&1 <${TTY}
43
			result=$?
67
			result=$?
44
		else
68
		else
45
			${NICE} /usr/sbin/asterisk ${OPTS} 2>&1 >/dev/null
69
			${NICE} /usr/sbin/asterisk -C "${ast_confdir}/asterisk.conf" ${OPTS} 2>&1 >/dev/null
46
			result=$?
70
			result=$?
47
		fi		
71
		fi		
48
72
Lines 51-60 Link Here
51
			break
75
			break
52
		else
76
		else
53
			if [ $result -gt 128 ]; then
77
			if [ $result -gt 128 ]; then
54
				signal=`expr $result - 128`
78
				signal="$(expr $result - 128)"
55
				MSG="Asterisk terminated with Signal: $signal"
79
				MSG="Asterisk terminated with Signal: $signal"
56
80
57
				CORE_TARGET="core-`date +%Y%m%d-%H%M%S`"
81
				CORE_TARGET="core-$(date "+%Y%m%d-%H%M%S")"
58
82
59
				local CORE_DUMPED=0
83
				local CORE_DUMPED=0
60
				if [ -f "${ASTERISK_CORE_DIR}/core" ]; then
84
				if [ -f "${ASTERISK_CORE_DIR}/core" ]; then
Lines 77-94 Link Here
77
101
78
			# kill left-over tasks
102
			# kill left-over tasks
79
			for X in ${ASTERISK_CLEANUP_ON_CRASH}; do
103
			for X in ${ASTERISK_CLEANUP_ON_CRASH}; do
80
				kill -9 `pidof ${X}`;
104
				kill -9 "$(pidof "${X}")";
81
			done
105
			done
82
		fi
106
		fi
83
107
84
		[ -n "${TTY}" ] \
108
		[ -n "${TTY}" ] \
85
			&& echo "${MSG}" >${TTY} \
109
			&& echo "${MSG}" >"${TTY}" \
86
			|| echo "${MSG}"
110
			|| echo "${MSG}"
87
111
88
112
89
		if [ -n "${ASTERISK_NOTIFY_EMAIL}" ] && \
113
		if [ -n "${ASTERISK_NOTIFY_EMAIL}" ] && \
90
		   [ -x /usr/sbin/sendmail ]; then
114
		   [ -x /usr/sbin/sendmail ]; then
91
			echo -e -n "Subject: Asterisk crashed\n\r${MSG}\n\r" |\
115
			echo -e -n "Subject: Asterisk crashed\r\n${MSG}\r\n" |\
92
				 /usr/sbin/sendmail "${ASTERISK_NOTIFY_EMAIL}"
116
				 /usr/sbin/sendmail "${ASTERISK_NOTIFY_EMAIL}"
93
		fi
117
		fi
94
		sleep "${ASTERISK_RESTART_DELAY}"
118
		sleep "${ASTERISK_RESTART_DELAY}"
Lines 104-124 Link Here
104
	local tmp x
128
	local tmp x
105
129
106
	local OPTS ARGS 
130
	local OPTS ARGS 
107
		
131
108
	ebegin "Starting asterisk PBX"
132
	setup_svc_variables || return $?
133
134
	ebegin "Starting ${ast_instancename} PBX"
109
135
110
	eindent
136
	eindent
111
137
112
	# filter (redundant) arguments
138
	# filter (redundant) arguments
113
	OPTS=`echo "${ASTERISK_OPTS}" | sed -re "s:-[cfF]::g"`
139
	OPTS="$(echo "${ASTERISK_OPTS}" | sed -re "s:-[cfF]::g")"
114
140
115
	# default options
141
	# default options
116
	OPTS="${OPTS} -f"  # don't fork / detach breaks wrapper script...
142
	OPTS="${OPTS} -f"  # don't fork / detach breaks wrapper script...
117
143
118
	# mangle yes/no options
144
	# mangle yes/no options
119
	ASTERISK_CONSOLE="`echo ${ASTERISK_CONSOLE} | tr '[:lower:]' '[:upper:]'`"
145
	ASTERISK_CONSOLE="$(echo ${ASTERISK_CONSOLE} | tr '[:lower:]' '[:upper:]')"
146
	ASTERISK_WAITBOOTED="$(echo "${ASTERISK_WAITBOOTED}" | tr '[:lower:]' '[:upper:]')"
120
147
121
	ASTERISK_RESTART_DELAY="`echo "${ASTERISK_RESTART_DELAY}" | sed -re 's/^([0-9]*).*/\1/'`"
148
	ASTERISK_RESTART_DELAY="$(echo "${ASTERISK_RESTART_DELAY}" | sed -re 's/^([0-9]*).*/\1/')"
122
	[ -z "${ASTERISK_RESTART_DELAY}" ] && ASTERISK_RESTART_DELAY=5
149
	[ -z "${ASTERISK_RESTART_DELAY}" ] && ASTERISK_RESTART_DELAY=5
123
150
124
	if [ -n "${ASTERISK_CORE_SIZE}" ] &&
151
	if [ -n "${ASTERISK_CORE_SIZE}" ] &&
Lines 187-217 Link Here
187
	fi
214
	fi
188
215
189
	if [ -n "${ASTERISK_USER}" ]; then
216
	if [ -n "${ASTERISK_USER}" ]; then
190
		USER=`echo $ASTERISK_USER | sed 's/:.*//'`
217
		USER="$(echo $ASTERISK_USER | sed 's/:.*//')"
191
		GROUP=`echo $ASTERISK_USER | awk -F: '/.*:.*/ { print $2 }'`
218
		GROUP="$(echo $ASTERISK_USER | awk -F: '/.*:.*/ { print $2 }')"
192
		if [ -n "${USER}" ]; then
219
		if [ -n "${USER}" ]; then
220
			if ! getent passwd "${USER}" &>/dev/null; then
221
				eerror "Requested to run asterisk as ${USER}, which doesn't exist."
222
				return 1
223
			fi
193
			OPTS="${OPTS} -U ${USER}"
224
			OPTS="${OPTS} -U ${USER}"
194
		fi
225
		fi
195
		if [ -n "${GROUP}" ]; then
226
		if [ -n "${GROUP}" ]; then
227
			if ! getent group "${GROUP}" &>/dev/null; then
228
				eerror "Requested to run asterisk with group ${USER}, which doesn't exist."
229
				return 1
230
			fi
196
			OPTS="${OPTS} -G ${GROUP}"
231
			OPTS="${OPTS} -G ${GROUP}"
197
			GROUP=":${GROUP}"	# make it look nice...
232
			GROUP=":${GROUP}"	# make it look nice...
198
		fi
233
		fi
199
		checkpath -d -m 0755 -o ${USER}${GROUP} /var/{log,run}/asterisk
234
		checkpath -d -m 0755 -o ${USER}${GROUP} "${ast_logdir}" "${ast_rundir}"
200
		for element in `find /var/{log,run}/asterisk`; do
235
		find "${ast_logdir}" "${ast_rundir}" ! -user "${USER}" | while read element; do
201
			if [ `stat -c %U $element` != "${USER}" ]; then
236
 			ewarn "${USER} is not the owner of $element, or permissions are insufficient, fixing."
202
 				ewarn "${USER} is not the owner of $element, fixing."
237
			chown ${USER} "${element}"
203
				chown -R ${USER} /var/{log,run}/asterisk
238
			chmod u+rX "${element}"
204
				chmod -R u+r /var/{log,run}/asterisk
205
				chmod u+x /var/{log,run}/asterisk
206
			fi;
207
		done;
239
		done;
208
		einfo "Starting asterisk as      : ${USER}${GROUP}"
240
		einfo "Starting asterisk as      : ${USER}${GROUP}"
209
	else
241
	else
210
		checkpath -d -m 0755 -o root:root /var/{log,run}/asterisk
242
		checkpath -d -m 0755 -o root:root "${ast_logdir}" "${ast_rundir}"
211
		ewarn "Starting asterisk as root is not recommended."
243
		ewarn "Starting asterisk as root is not recommended."
212
	fi
244
	fi
213
245
214
	asterisk_run_loop ${OPTS} 2>&1 | logger -t asterisk_wrapper &
246
	asterisk_run_loop ${OPTS} 2>&1 | logger -t "wrapper:${ast_instancename}" &
215
	result=$?
247
	result=$?
216
248
217
	if [ $result -eq 0 ]; then
249
	if [ $result -eq 0 ]; then
Lines 219-243 Link Here
219
		sleep 2 
251
		sleep 2 
220
		is_running
252
		is_running
221
		result=$?
253
		result=$?
254
255
		[ $result -eq 0 ] || wrapperstop
222
	fi
256
	fi
223
257
224
	eoutdent
258
	eoutdent
225
	eend $result
259
	eend $result
260
261
	if [ $result -eq 0 -a "${ASTERISK_WAITBOOTED}" = "YES" ]; then
262
		ebegin "Waiting for ${ast_instancename} to fully boot"
263
		/usr/sbin/asterisk -C "${ast_confdir}/asterisk.conf" -r -x "core waitfullybooted" &>/dev/null
264
		eend $?
265
	fi
266
267
	return $result
226
}
268
}
227
269
228
wrapperstop() {
270
wrapperstop() {
229
	# Accomodate system upgrades (so a previous version of the wrapper script that still uses a pid file may be running).
271
	# Accomodate system upgrades (so a previous version of the wrapper script that still uses a pid file may be running).
230
	if [ -r /var/run/asterisk/wrapper_loop.pid ]; then
272
	if [ -r "${ast_rundir}/wrapper_loop.pid" ]; then
231
		ebegin "Killing wrapper script"
273
		ebegin "Killing wrapper script"
232
		kill `cat /var/run/asterisk/wrapper_loop.pid`
274
		kill "$(cat /var/run/asterisk/wrapper_loop.pid)"
233
		eend $?
275
		eend $?
234
	fi
276
	fi
235
277
236
	# The new one (due to "hardened" requirements) uses a simpler
278
	# The new one (due to "hardened" requirements) uses a simpler
237
	# flag to indicate running or shutting down.
279
	# flag to indicate running or shutting down.
238
	if [ -r /var/run/asterisk/wrapper_loop.running ]; then
280
	if [ -r "${ast_rundir}/wrapper_loop.running" ]; then
239
		ebegin "Signalling wrapper script to terminate"
281
		ebegin "Signalling wrapper script to terminate"
240
		rm /var/run/asterisk/wrapper_loop.running
282
		rm "${ast_rundir}/wrapper_loop.running"
241
		eend $?
283
		eend $?
242
	fi
284
	fi
243
285
Lines 245-250 Link Here
245
}
287
}
246
288
247
forcestop() {
289
forcestop() {
290
	setup_svc_variables || return $?
291
248
	# Just to be sure - when we want to forcestop we should make it all tear down.
292
	# Just to be sure - when we want to forcestop we should make it all tear down.
249
	wrapperstop
293
	wrapperstop
250
294
Lines 254-275 Link Here
254
}
298
}
255
299
256
stop() {
300
stop() {
301
	setup_svc_variables || return $?
302
257
	wrapperstop
303
	wrapperstop
258
304
259
	if ! is_running; then
305
	if ! is_running; then
260
		eerror "Asterisk is not running!"
306
		eerror "Asterisk is not running!"
261
		return 0
307
		return 0
262
	fi
308
	fi
263
309
	
264
	ebegin "Stopping asterisk PBX gracefully"
310
	ebegin "Stopping asterisk PBX gracefully"
265
	/usr/sbin/asterisk -r -x "core stop gracefully" &>/dev/null
311
	/usr/sbin/asterisk -C "${ast_confdir}/asterisk.conf" -r -x "core stop gracefully" &>/dev/null
266
	# Now we have to wait until asterisk has _really_ stopped.
312
	# Now we have to wait until asterisk has _really_ stopped.
267
	sleep 1
313
	sleep 1
268
	if is_running; then
314
	if is_running; then
269
		einfon "Waiting for asterisk to shutdown ."
315
		einfon "Waiting for asterisk to shutdown ."
270
		local cnt=0
316
		local cnt=0
271
		while is_running; do
317
		while is_running; do
272
			cnt=`expr $cnt + 1`
318
			cnt="$(expr $cnt + 1)"
273
			if [ $cnt -gt 60 ] ; then
319
			if [ $cnt -gt 60 ] ; then
274
				# Waited 120 seconds now. Fail.
320
				# Waited 120 seconds now. Fail.
275
				echo
321
				echo
Lines 285-293 Link Here
285
}
331
}
286
332
287
reload() {
333
reload() {
334
	setup_svc_variables || return $?
335
288
	if is_running; then
336
	if is_running; then
289
		ebegin "Forcing asterisk to reload configuration"
337
		ebegin "Forcing asterisk to reload configuration"
290
		/usr/sbin/asterisk -r -x "module reload" &>/dev/null
338
		/usr/sbin/asterisk -C "${ast_confdir}/asterisk.conf" -r -x "module reload" &>/dev/null
291
		eend $?
339
		eend $?
292
	else
340
	else
293
		eerror "Asterisk is not running!"
341
		eerror "Asterisk is not running!"

Return to bug 473224