Debian Codegraph @

contrib/scripts/safe_asterisk

raw symbols SCIP
1 #!/bin/sh
2
3 ASTETCDIR="__ASTERISK_ETC_DIR__"
4 ASTSBINDIR="__ASTERISK_SBIN_DIR__"
5 ASTVARRUNDIR="__ASTERISK_VARRUN_DIR__"
6 ASTVARLOGDIR="__ASTERISK_LOG_DIR__"
7
8 CLIARGS="$*" # Grab any args passed to safe_asterisk
9 TTY=9 # TTY (if you want one) for Asterisk to run on
10 CONSOLE=yes # Whether or not you want a console
11 #NOTIFY=root@localhost # Who to notify about crashes
12 #EXEC=/path/to/somescript # Run this command if Asterisk crashes
13 #LOGFILE="${ASTVARLOGDIR}/safe_asterisk.log" # Where to place the normal logfile (disabled if blank)
14 #SYSLOG=local0 # Which syslog facility to use (disabled if blank)
15 MACHINE=`hostname` # To specify which machine has crashed when getting the mail
16 DUMPDROP="${DUMPDROP:-/tmp}"
17 RUNDIR="${RUNDIR:-/tmp}"
18 SLEEPSECS=4
19 ASTPIDFILE="${ASTVARRUNDIR}/asterisk.pid"
20
21 # comment this line out to have this script _not_ kill all mpg123 processes when
22 # asterisk exits
23 KILLALLMPG123=1
24
25 # run asterisk with this priority
26 PRIORITY=0
27
28 # set system filemax on supported OSes if this variable is set
29 # SYSMAXFILES=262144
30
31 # Asterisk allows full permissions by default, so set a umask, if you want
32 # restricted permissions.
33 #UMASK=022
34
35 # set max files open with ulimit. On linux systems, this will be automatically
36 # set to the system's maximum files open devided by two, if not set here.
37 # MAXFILES=32768
38
39 message() {
40 if test -n "$TTY" && test "$TTY" != "no"; then
41 echo "$1" >/dev/${TTY}
42 fi
43 if test -n "$SYSLOG"; then
44 logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
45 fi
46 if test -n "$LOGFILE"; then
47 echo "safe_asterisk[$$]: $1" >>"$LOGFILE"
48 fi
49 }
50
51 # Check if Asterisk is already running. If it is, then bug out, because
52 # starting safe_asterisk when Asterisk is running is very bad.
53 VERSION=`"${ASTSBINDIR}/asterisk" -nrx 'core show version' 2>/dev/null`
54 if test "`echo $VERSION | cut -c 1-8`" = "Asterisk"; then
55 message "Asterisk is already running. $0 will exit now."
56 exit 1
57 fi
58
59 # since we're going to change priority and open files limits, we need to be
60 # root. if running asterisk as other users, pass that to asterisk on the command
61 # line.
62 # if we're not root, fall back to standard everything.
63 if test `id -u` != 0; then
64 echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
65 echo "This is NOT suitable for large systems." >&2
66 PRIORITY=0
67 message "safe_asterisk was started by `id -un` (uid `id -u`)."
68 else
69 if `uname -s | grep Linux >/dev/null 2>&1`; then
70 # maximum number of open files is set to the system maximum
71 # divided by two if MAXFILES is not set.
72 if test -z "$MAXFILES"; then
73 # just check if file-max is readable
74 if test -r /proc/sys/fs/file-max; then
75 MAXFILES=$((`cat /proc/sys/fs/file-max` / 2))
76 # don't exceed upper limit of 2^20 for open
77 # files on systems where file-max is > 2^21
78 if test $MAXFILES -gt 1048576; then
79 MAXFILES=1048576
80 fi
81 fi
82 fi
83 SYSCTL_MAXFILES="fs.file-max"
84 elif `uname -s | grep Darwin >/dev/null 2>&1`; then
85 SYSCTL_MAXFILES="kern.maxfiles"
86 fi
87
88
89 if test -n "$SYSMAXFILES"; then
90 if test -n "$SYSCTL_MAXFILES"; then
91 sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
92 fi
93 fi
94
95 # set the process's filemax to whatever set above
96 ulimit -n $MAXFILES
97
98 if test ! -d "${ASTVARRUNDIR}"; then
99 mkdir -p "${ASTVARRUNDIR}"
100 chmod 755 "${ASTVARRUNDIR}"
101 fi
102
103 fi
104
105 if test -n "$UMASK"; then
106 umask $UMASK
107 fi
108
109 #
110 # Let Asterisk dump core
111 #
112 ulimit -c unlimited
113
114 #
115 # Don't fork when running "safely"
116 #
117 ASTARGS=""
118 if test -n "$TTY" && test "$TTY" != "no"; then
119 if test -c /dev/tty${TTY}; then
120 TTY=tty${TTY}
121 elif test -c /dev/vc/${TTY}; then
122 TTY=vc/${TTY}
123 elif test "$TTY" = "9"; then # ignore default if it was untouched
124 # If there is no /dev/tty9 and not /dev/vc/9 we don't
125 # necessarily want to die at this point. Pretend that
126 # TTY wasn't set.
127 TTY=
128 else
129 message "Cannot find specified TTY (${TTY})"
130 exit 1
131 fi
132 if test -n "$TTY"; then
133 ASTARGS="${ASTARGS} -vvvg"
134 if test "$CONSOLE" != "no"; then
135 ASTARGS="${ASTARGS} -c"
136 fi
137 fi
138 fi
139
140 if test ! -d "${RUNDIR}"; then
141 message "${RUNDIR} does not exist, creating"
142 if ! mkdir -p "${RUNDIR}"; then
143 message "Unable to create ${RUNDIR}"
144 exit 1
145 fi
146 fi
147
148 if test ! -w "${DUMPDROP}"; then
149 message "Cannot write to ${DUMPDROP}"
150 exit 1
151 fi
152
153 #
154 # Don't die if stdout/stderr can't be written to
155 #
156 trap '' PIPE
157
158 #
159 # Run scripts to set any environment variables or do any other system-specific setup needed
160 #
161
162 if test -d "${ASTETCDIR}/startup.d"; then
163 # If this script is run by root, the startup.d directory and all scripts in it
164 # must be owned by root.
165 if test `id -u` = 0; then
166 dir_owner=$(stat -c '%u' "${ASTETCDIR}/startup.d" 2>/dev/null)
167 if test "${dir_owner}" != 0 ; then
168 message "FATAL: ${ASTETCDIR}/startup.d is not owned by root"
169 exit 1
170 fi
171
172 # Check all scripts for proper ownership before sourcing any of them.
173 for script in "${ASTETCDIR}/startup.d/"*.sh ; do
174 if test -r "${script}"; then
175 script_owner=$(stat -c '%u' "${script}" 2>/dev/null)
176 if test "$script_owner" != 0 ; then
177 message "FATAL: Script $(basename "$script") is not owned by root"
178 exit 1
179 fi
180 fi
181 done
182 fi
183
184 for script in "${ASTETCDIR}/startup.d/"*.sh ; do
185 . "${script}"
186 done
187 fi
188
189 run_asterisk()
190 {
191 while :; do
192 if test -n "$TTY" && test "$TTY" != "no"; then
193 cd "${RUNDIR}"
194 stty sane </dev/${TTY}
195 nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
196 else
197 cd "${RUNDIR}"
198 nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
199 fi
200 EXITSTATUS=$?
201 message "Asterisk ended with exit status $EXITSTATUS"
202 if test $EXITSTATUS -eq 0; then
203 # Properly shutdown....
204 message "Asterisk shutdown normally."
205 exit 0
206 elif test $EXITSTATUS -gt 128; then
207 EXITSIGNAL=$((EXITSTATUS - 128))
208 message "Asterisk exited on signal $EXITSIGNAL."
209 if test -n "$NOTIFY"; then
210 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
211 mail -s "Asterisk on $MACHINE died (sig $EXITSIGNAL)" $NOTIFY
212 fi
213 if test -n "$EXEC"; then
214 $EXEC
215 fi
216
217 PID=`cat ${ASTPIDFILE}`
218 DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
219 if test -f "${RUNDIR}/core.${PID}"; then
220 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
221 elif test -f "${RUNDIR}/core"; then
222 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
223 fi
224 else
225 message "Asterisk died with code $EXITSTATUS."
226
227 PID=`cat ${ASTPIDFILE}`
228 DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
229 if test -f "${RUNDIR}/core.${PID}"; then
230 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
231 elif test -f "${RUNDIR}/core"; then
232 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
233 fi
234 fi
235 message "Automatically restarting Asterisk."
236 sleep $SLEEPSECS
237 if test "0$KILLALLMPG123" -gt 0; then
238 pkill -9 mpg123
239 fi
240 done
241 }
242
243 if test -n "$ASTSAFE_FOREGROUND"; then
244 run_asterisk
245 else
246 run_asterisk &
247 fi