Lines 1-4
Link Here
|
1 |
#! /bin/bash |
1 |
#! /bin/sh |
2 |
# Copyright 1999-2022 Gentoo Authors |
2 |
# Copyright 1999-2022 Gentoo Authors |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
|
4 |
|
Lines 7-12
Link Here
|
7 |
T="$(getopt -o "h" --long "help,corecompress:,corerename:,crashemail:,dumpcore:,instance:,maxfd:,nicelevel:,restartdelay:,rundir:,syslog,tty:" -n "$progname" -- "$@")" |
7 |
T="$(getopt -o "h" --long "help,corecompress:,corerename:,crashemail:,dumpcore:,instance:,maxfd:,nicelevel:,restartdelay:,rundir:,syslog,tty:" -n "$progname" -- "$@")" |
8 |
eval set -- "${T}" |
8 |
eval set -- "${T}" |
9 |
|
9 |
|
|
|
10 |
# bash builtins are special ... |
11 |
echo_e="$([ "${SHELL}" = "/bin/bash" ] && echo "echo -e" || echo echo)" |
12 |
|
10 |
rundir=/run/asterisk |
13 |
rundir=/run/asterisk |
11 |
restartdelay=5 |
14 |
restartdelay=5 |
12 |
nicelevel=0 |
15 |
nicelevel=0 |
Lines 14-20
Link Here
|
14 |
dumpcore=0 |
17 |
dumpcore=0 |
15 |
unset tty instance syslog corecompress corerename crashemail |
18 |
unset tty instance syslog corecompress corerename crashemail |
16 |
|
19 |
|
17 |
function usage() { |
20 |
usage() { |
18 |
cat <<USAGE |
21 |
cat <<USAGE |
19 |
USAGE: $progname [options] -- asterisk options" |
22 |
USAGE: $progname [options] -- asterisk options" |
20 |
OPTIONS: |
23 |
OPTIONS: |
Lines 60-73
Link Here
|
60 |
USAGE |
63 |
USAGE |
61 |
} |
64 |
} |
62 |
|
65 |
|
|
|
66 |
matchreg() { |
67 |
local v=$1 |
68 |
shift |
69 |
echo "$v" | grep -q "$@" |
70 |
} |
71 |
|
63 |
while [ "$1" != "--" ]; do |
72 |
while [ "$1" != "--" ]; do |
64 |
case "$1" in |
73 |
case "$1" in |
65 |
--corecompress|--corerename|--crashemail|--dumpcore|--instance|--maxfd|--nicelevel|--restartdelay|--rundir|--tty) |
74 |
--corecompress|--corerename|--crashemail|--dumpcore|--instance|--maxfd|--nicelevel|--restartdelay|--rundir|--tty) |
66 |
eval "${1:2}=\"\${2}\"" |
75 |
eval "${1#--}=\"\${2}\"" |
67 |
shift 2 |
76 |
shift 2 |
68 |
;; |
77 |
;; |
69 |
--syslog) |
78 |
--syslog) |
70 |
eval "${1:2}=1" |
79 |
eval "${1#--}=1" |
71 |
shift |
80 |
shift |
72 |
;; |
81 |
;; |
73 |
--help|-h) |
82 |
--help|-h) |
Lines 83-114
Link Here
|
83 |
done |
92 |
done |
84 |
shift # -- |
93 |
shift # -- |
85 |
|
94 |
|
86 |
ast_opts=("$@") |
95 |
if ! matchreg "${restartdelay}" "^[1-9][0-9]*$"; then |
87 |
|
|
|
88 |
if ! [[ "${restartdelay}" =~ ^[1-9][0-9]*$ ]]; then |
89 |
echo "Invalid --restartdelay value ${restartdelay}, resetting to 5." >&2 |
96 |
echo "Invalid --restartdelay value ${restartdelay}, resetting to 5." >&2 |
90 |
restartdelay=5 |
97 |
restartdelay=5 |
91 |
fi |
98 |
fi |
92 |
|
99 |
|
93 |
if [[ -n "${corecompress}" && ! -x "${corecompress}" ]]; then |
100 |
if ! matchreg "${restartdelay}" "^[1-9][0-9]*$"; then |
|
|
101 |
echo "Invalid --maxfd value, resetting to 4096." >&2 |
102 |
maxfd=4096 |
103 |
fi |
104 |
|
105 |
if [ $maxfd -lt 1024 ]; then |
106 |
echo "maxfd is guaranteed too low, bumping to at least 1024" >&2 |
107 |
maxfd=1024 |
108 |
fi |
109 |
|
110 |
if [ -n "${nicelevel}" ] && ! matchreg "${nicelevel}" -E "^-?[0-9]+$"; then |
111 |
echo "Invalid --nicelevel which much be a valid integer (values from -20 to 20 makes sense)." |
112 |
exit 1 |
113 |
fi |
114 |
|
115 |
if [ -n "${corecompress}" -a ! -x "${corecompress}" ]; then |
94 |
corecompress=$(which "${corecompress}" 2>/dev/null) |
116 |
corecompress=$(which "${corecompress}" 2>/dev/null) |
95 |
[[ -z "${corecompress}" ]] && echo "Error locating core compression tool, disabling core compression." >&2 |
117 |
[ -z "${corecompress}" ] && echo "Error locating core compression tool, disabling core compression." >&2 |
96 |
fi |
118 |
fi |
97 |
|
119 |
|
98 |
# Before here will still be output (potentially munged, to the terminal). |
120 |
# Before here will still be output (potentially munged, to the terminal). |
99 |
if [[ -n "${syslog}" ]]; then |
121 |
if [ -n "${syslog}" ]; then |
100 |
exec 1> >(logger -t "asterisk_wrapper${instance:+:}${instance}" &>/dev/null) |
122 |
tdir="$(mktemp -d)" |
|
|
123 |
tfifo="${tdir}/asterisk_wrapper.logger.fifo" |
124 |
mkfifo "${tfifo}" |
125 |
logger -t "asterisk_wrapper${instance:+:}${instance}" --id=$$ >/dev/null 2>&1 <"${tfifo}" & |
126 |
exec 1>"${tfifo}" |
101 |
exec 2>&1 |
127 |
exec 2>&1 |
|
|
128 |
|
129 |
rm "${tfifo}" |
130 |
rmdir "${tdir}" |
102 |
fi |
131 |
fi |
103 |
|
132 |
|
104 |
echo "Initializing ${progname}" |
133 |
echo "Initializing ${progname}" |
105 |
|
134 |
|
106 |
function cleanup(){ |
135 |
cleanup(){ |
107 |
# There is a tiny race here, if this gets replaced inbetween the read and the rm. |
136 |
# There is a tiny race here, if this gets replaced inbetween the read and the rm. |
108 |
# To fix this is quite complex in that we need to keep an fd, compare inode numbers |
137 |
# To fix this is quite complex in that we need to keep an fd, compare inode numbers |
109 |
# and manage flock's. |
138 |
# and manage flock's. |
110 |
[[ -r "${rundir}/${progname}.pid" ]] && \ |
139 |
[ -r "${rundir}/${progname}.pid" ] && \ |
111 |
[[ "$(<"${rundir}/${progname}.pid")" = $$ ]] && \ |
140 |
[ "$(cat "${rundir}/${progname}.pid")" = $$ ] && \ |
112 |
rm "${rundir}/${progname}.pid" |
141 |
rm "${rundir}/${progname}.pid" |
113 |
} |
142 |
} |
114 |
trap cleanup EXIT |
143 |
trap cleanup EXIT |
Lines 117-153
Link Here
|
117 |
# it's next iteration. Towards this end, if asterisk.pid exists, attempt to find it's |
146 |
# it's next iteration. Towards this end, if asterisk.pid exists, attempt to find it's |
118 |
# config file and request a core stop when convenient so that we can take over. |
147 |
# config file and request a core stop when convenient so that we can take over. |
119 |
echo $$ > "${rundir}/${progname}.pid" |
148 |
echo $$ > "${rundir}/${progname}.pid" |
120 |
if [[ -r "${rundir}/asterisk.pid" ]]; then |
149 |
if [ -r "${rundir}/asterisk.pid" ]; then |
121 |
ast_pid="$(<"${rundir}/asterisk.pid")" |
150 |
ast_pid="$(cat "${rundir}/asterisk.pid")" |
122 |
[[ -r "/proc/${ast_pid}/cmdline" ]] && ast_conf="$(tr '\0' '\n' < "/proc/${ast_pid}/cmdline" | grep -A1 '^-C$' | tail -n1)" && /usr/sbin/asterisk -C "${ast_conf:-/etc/asterisk/asterisk.conf}" -rx "core stop when convenient" |
151 |
[ -r "/proc/${ast_pid}/cmdline" ] && ast_conf="$(tr '\0' '\n' < "/proc/${ast_pid}/cmdline" | grep -A1 '^-C$' | tail -n1)" && /usr/sbin/asterisk -C "${ast_conf:-/etc/asterisk/asterisk.conf}" -rx "core stop when convenient" |
123 |
# We may hit a few (depending on how busy the server is a great many number) loop failures still ... |
152 |
# We may hit a few (depending on how busy the server is a great many number) loop failures still ... |
124 |
fi |
153 |
fi |
125 |
|
154 |
|
126 |
ulimit -c "${dumpcore}" |
155 |
prlimit --core=${dumpcore} |
127 |
ulimit -n "${maxfd}" |
156 |
prlimit --nofile=${maxfd} |
128 |
|
157 |
|
129 |
ast_cmd=() |
158 |
ast_cmd=/usr/sbin/asterisk |
130 |
if [ -n "${nicelevel}" ]; then |
159 |
if [ -n "${nicelevel}" ]; then |
131 |
ast_cmd+=(nice -n "${nicelevel}") |
160 |
ast_cmd="nice -n ${nicelevel} ${ast_cmd}" |
132 |
fi |
161 |
fi |
133 |
ast_cmd+=(/usr/sbin/asterisk "${ast_opts[@]}") |
|
|
134 |
|
162 |
|
135 |
while [[ -r "${rundir}/${progname}.pid" ]]; do |
163 |
while [ -r "${rundir}/${progname}.pid" ]; do |
136 |
# Another instance is looking to replace us, so terminate. |
164 |
# Another instance is looking to replace us, so terminate. |
137 |
if [[ "$(<"${rundir}/${progname}.pid")" != $$ ]]; then |
165 |
if [ "$(cat "${rundir}/${progname}.pid")" != $$ ]; then |
138 |
break |
166 |
break |
139 |
fi |
167 |
fi |
140 |
|
168 |
|
141 |
echo "Starting asterisk with ${ast_opts[*]}" |
169 |
echo "Starting asterisk with ${ast_cmd} $*" |
142 |
if [ -n "${tty+yes}" ]; then |
170 |
if [ -n "${tty+yes}" ]; then |
143 |
/bin/stty -F "${tty}" sane |
171 |
/bin/stty -F "${tty}" sane |
144 |
"${ast_cmd[@]}" >"${tty}" 2>&1 <"${tty}" |
172 |
${ast_cmd} "$@" >"${tty}" 2>&1 <"${tty}" |
145 |
result=$? |
173 |
result=$? |
146 |
else |
174 |
else |
147 |
# Purposefully leave stderr alone, this will under certain odd cases (like exceptions, |
175 |
# Purposefully leave stderr alone, this will under certain odd cases (like exceptions, |
148 |
# and other odd cases logged from glibc) result in those logs at least being captured |
176 |
# and other odd cases logged from glibc) result in those logs at least being captured |
149 |
# in syslog. |
177 |
# in syslog. |
150 |
"${ast_cmd[@]}" </dev/null >/dev/null |
178 |
${ast_cmd} "$@" </dev/null >/dev/null |
151 |
result=$? |
179 |
result=$? |
152 |
fi |
180 |
fi |
153 |
|
181 |
|
Lines 163-183
Link Here
|
163 |
|
191 |
|
164 |
# TODO: figure out how to use /proc/sys/kernel/core_pattern here, but if someone is using |
192 |
# TODO: figure out how to use /proc/sys/kernel/core_pattern here, but if someone is using |
165 |
# that, chances are they're already dealing with what we want here. |
193 |
# that, chances are they're already dealing with what we want here. |
166 |
if [[ -r core ]]; then |
194 |
if [ -r core ]; then |
167 |
if [[ -n "${corerename+yes}" ]]; then |
195 |
if [ -n "${corerename+yes}" ]; then |
168 |
core_target="${core_pattern}" |
196 |
core_target="$(echo "${core_pattern}" | sed -e "s/%h/$(hostname)/" \ |
169 |
core_target="${core_target//%h/"$(hostname)"}" |
197 |
-e "s/%D/$(date +%Y%m%d)/" -e "s/%T/$(date +%H%M%S)/")" |
170 |
core_target="${core_target//%D/"$(date +%Y%m%d)"}" |
|
|
171 |
core_target="${core_target//%T/"$(date +%H%M%S)"}" |
172 |
mv core "${core_target}" |
198 |
mv core "${core_target}" |
173 |
core_target=$(readlink -f "${core_target}") |
199 |
core_target=$(readlink -f "${core_target}") |
174 |
else |
200 |
else |
175 |
core_target=$(readlink -f core) |
201 |
core_target=$(readlink -f core) |
176 |
fi |
202 |
fi |
177 |
|
203 |
|
178 |
if [[ -n "${corecompress}" && -x "${corecompress}" ]]; then |
204 |
if [ -n "${corecompress}" && -x "${corecompress}" ]; then |
179 |
"${corecompress}" "${core_target}" |
205 |
"${corecompress}" "${core_target}" |
180 |
# TODO: Figure out a way to tag the extension onto core_target. |
|
|
181 |
fi |
206 |
fi |
182 |
|
207 |
|
183 |
MSG="${MSG}\r\nCore dumped: ${core_target}" |
208 |
MSG="${MSG}\r\nCore dumped: ${core_target}" |
Lines 190-197
Link Here
|
190 |
&& echo "${MSG}" >"${tty}" \ |
215 |
&& echo "${MSG}" >"${tty}" \ |
191 |
|| echo "${MSG}" |
216 |
|| echo "${MSG}" |
192 |
|
217 |
|
193 |
if [[ -n "${crashemail+yes}" && -x /usr/sbin/sendmail ]]; then |
218 |
if [ -n "${crashemail+yes}" && -x /usr/sbin/sendmail ]; then |
194 |
echo -e -n "Subject: Asterisk crashed\r\n${MSG}\r\n" |\ |
219 |
$echo_e -n "Subject: Asterisk crashed\r\n${MSG}\r\n" |\ |
195 |
/usr/sbin/sendmail "${crashemail}" |
220 |
/usr/sbin/sendmail "${crashemail}" |
196 |
fi |
221 |
fi |
197 |
echo "Restarting asterisk after ${restartdelay}s ..." |
222 |
echo "Restarting asterisk after ${restartdelay}s ..." |