Debian Codegraph @

contrib/scripts/safe_asterisk

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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=`hostnamescip-shell system . . 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 messagescip-shell shell shell-project 0.0.0 message().() {
40 if testscip-shell system . . test. -n "$TTY" && testscip-shell system . . test. "$TTY" != "no"; then
41 echoscip-shell system . . echo. "$1" >/dev/${TTY}
42 fi
43 if testscip-shell system . . test. -n "$SYSLOG"; then
44 loggerscip-shell system . . logger. -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
45 fi
46 if testscip-shell system . . test. -n "$LOGFILE"; then
47 echoscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`.`
54 if testscip-shell system . . test. "`echoscip-shell system . . echo. $VERSION | cutscip-shell system . . cut. -c 1-8`" = "Asterisk"; then
55 messagescip-shell shell shell-project 0.0.0 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 testscip-shell system . . test. `idscip-shell system . . id. -u` != 0; then
64 echoscip-shell system . . echo. "Oops. I'm not root. Falling back to standard prio and file max." >&2
65 echoscip-shell system . . echo. "This is NOT suitable for large systems." >&2
66 PRIORITY=0
67 messagescip-shell shell shell-project 0.0.0 message(). "safe_asterisk was started by `idscip-shell system . . id. -un` (uid `idscip-shell system . . id. -u`)."
68 else
69 if `unamescip-shell system . . uname. -s | grepscip-shell system . . grep. Linux >/dev/nullscip-shell filesystem . . `/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 testscip-shell system . . test. -z "$MAXFILES"; then
73 # just check if file-max is readable
74 if testscip-shell system . . test. -r /proc/sys/fs/file-maxscip-shell filesystem . . `/proc/sys/fs/file-max`.; then
75 MAXFILES=$((`catscip-shell system . . cat. /proc/sys/fs/file-maxscip-shell filesystem . . `/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 testscip-shell system . . test. $MAXFILES -gt 1048576; then
79 MAXFILES=1048576
80 fi
81 fi
82 fi
83 SYSCTL_MAXFILES="fs.file-max"
84 elif `unamescip-shell system . . uname. -s | grepscip-shell system . . grep. Darwin >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1`; then
85 SYSCTL_MAXFILES="kern.maxfiles"
86 fi
87
88
89 if testscip-shell system . . test. -n "$SYSMAXFILES"; then
90 if testscip-shell system . . test. -n "$SYSCTL_MAXFILES"; then
91 sysctlscip-shell system . . 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 testscip-shell system . . test. ! -d "${ASTVARRUNDIR}"; then
99 mkdirscip-shell system . . mkdir. -p "${ASTVARRUNDIR}"
100 chmodscip-shell system . . chmod. 755 "${ASTVARRUNDIR}"
101 fi
102
103 fi
104
105 if testscip-shell system . . 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 testscip-shell system . . test. -n "$TTY" && testscip-shell system . . test. "$TTY" != "no"; then
119 if testscip-shell system . . test. -c /dev/tty${TTY}; then
120 TTY=tty${TTY}
121 elif testscip-shell system . . test. -c /dev/vc/${TTY}; then
122 TTY=vc/${TTY}
123 elif testscip-shell system . . 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 messagescip-shell shell shell-project 0.0.0 message(). "Cannot find specified TTY (${TTY})"
130 exit 1
131 fi
132 if testscip-shell system . . test. -n "$TTY"; then
133 ASTARGS="${ASTARGS} -vvvg"
134 if testscip-shell system . . test. "$CONSOLE" != "no"; then
135 ASTARGS="${ASTARGS} -c"
136 fi
137 fi
138 fi
139
140 if testscip-shell system . . test. ! -d "${RUNDIR}"; then
141 messagescip-shell shell shell-project 0.0.0 message(). "${RUNDIR} does not exist, creating"
142 if ! mkdirscip-shell system . . mkdir. -p "${RUNDIR}"; then
143 messagescip-shell shell shell-project 0.0.0 message(). "Unable to create ${RUNDIR}"
144 exit 1
145 fi
146 fi
147
148 if testscip-shell system . . test. ! -w "${DUMPDROP}"; then
149 messagescip-shell shell shell-project 0.0.0 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 testscip-shell system . . 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 testscip-shell system . . test. `idscip-shell system . . id. -u` = 0; then
166 dir_owner=$(statscip-shell system . . stat. -c '%u' "${ASTETCDIR}/startup.d" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)
167 if testscip-shell system . . test. "${dir_owner}" != 0 ; then
168 messagescip-shell shell shell-project 0.0.0 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 testscip-shell system . . test. -r "${script}"; then
175 script_owner=$(statscip-shell system . . stat. -c '%u' "${script}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)
176 if testscip-shell system . . test. "$script_owner" != 0 ; then
177 messagescip-shell shell shell-project 0.0.0 message(). "FATAL: Script $(basenamescip-shell system . . 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_asteriskscip-shell shell shell-project 0.0.0 run_asterisk().()
190 {
191 while :; do
192 if testscip-shell system . . test. -n "$TTY" && testscip-shell system . . test. "$TTY" != "no"; then
193 cd "${RUNDIR}"
194 sttyscip-shell system . . stty. sane </dev/${TTY}
195 nicescip-shell system . . nice. -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
196 else
197 cd "${RUNDIR}"
198 nicescip-shell system . . nice. -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 </dev/nullscip-shell filesystem . . `/dev/null`.
199 fi
200 EXITSTATUS=$?
201 messagescip-shell shell shell-project 0.0.0 message(). "Asterisk ended with exit status $EXITSTATUS"
202 if testscip-shell system . . test. $EXITSTATUS -eq 0; then
203 # Properly shutdown....
204 messagescip-shell shell shell-project 0.0.0 message(). "Asterisk shutdown normally."
205 exit 0
206 elif testscip-shell system . . test. $EXITSTATUS -gt 128; then
207 EXITSIGNAL=$((EXITSTATUS - 128))
208 messagescip-shell shell shell-project 0.0.0 message(). "Asterisk exited on signal $EXITSIGNAL."
209 if testscip-shell system . . test. -n "$NOTIFY"; then
210 echoscip-shell system . . 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 testscip-shell system . . test. -n "$EXEC"; then
214 $EXEC
215 fi
216
217 PID=`catscip-shell system . . cat. ${ASTPIDFILE}`
218 DATE=`datescip-shell system . . date. "+%Y-%m-%dT%H:%M:%S%z"`
219 if testscip-shell system . . test. -f "${RUNDIR}/core.${PID}"; then
220 mvscip-shell system . . mv. "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostnamescip-shell system . . hostname.`-$DATE" &
221 elif testscip-shell system . . test. -f "${RUNDIR}/core"; then
222 mvscip-shell system . . mv. "${RUNDIR}/core" "${DUMPDROP}/core.`hostnamescip-shell system . . hostname.`-$DATE" &
223 fi
224 else
225 messagescip-shell shell shell-project 0.0.0 message(). "Asterisk died with code $EXITSTATUS."
226
227 PID=`catscip-shell system . . cat. ${ASTPIDFILE}`
228 DATE=`datescip-shell system . . date. "+%Y-%m-%dT%H:%M:%S%z"`
229 if testscip-shell system . . test. -f "${RUNDIR}/core.${PID}"; then
230 mvscip-shell system . . mv. "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostnamescip-shell system . . hostname.`-$DATE" &
231 elif testscip-shell system . . test. -f "${RUNDIR}/core"; then
232 mvscip-shell system . . mv. "${RUNDIR}/core" "${DUMPDROP}/core.`hostnamescip-shell system . . hostname.`-$DATE" &
233 fi
234 fi
235 messagescip-shell shell shell-project 0.0.0 message(). "Automatically restarting Asterisk."
236 sleepscip-shell system . . sleep. $SLEEPSECS
237 if testscip-shell system . . test. "0$KILLALLMPG123" -gt 0; then
238 pkillscip-shell system . . pkill. -9 mpg123
239 fi
240 done
241 }
242
243 if testscip-shell system . . test. -n "$ASTSAFE_FOREGROUND"; then
244 run_asteriskscip-shell shell shell-project 0.0.0 run_asterisk().
245 else
246 run_asteriskscip-shell shell shell-project 0.0.0 run_asterisk(). &
247 fi