install_prereq: Download latest Jansson.
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
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 -n` (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 770 "${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         for script in "${ASTETCDIR}/startup.d/"*.sh; do
164                 if test -r "${script}"; then
165                         . "${script}"
166                 fi
167         done
168 fi
169
170 run_asterisk()
171 {
172         while :; do
173                 if test -n "$TTY" && test "$TTY" != "no"; then
174                         cd "${RUNDIR}"
175                         stty sane </dev/${TTY}
176                         nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
177                 else
178                         cd "${RUNDIR}"
179                         nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
180                 fi
181                 EXITSTATUS=$?
182                 message "Asterisk ended with exit status $EXITSTATUS"
183                 if test $EXITSTATUS -eq 0; then
184                         # Properly shutdown....
185                         message "Asterisk shutdown normally."
186                         exit 0
187                 elif test $EXITSTATUS -gt 128; then
188                         EXITSIGNAL=$((EXITSTATUS - 128))
189                         message "Asterisk exited on signal $EXITSIGNAL."
190                         if test -n "$NOTIFY"; then
191                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
192                                 mail -s "Asterisk on $MACHINE died (sig $EXITSIGNAL)" $NOTIFY
193                         fi
194                         if test -n "$EXEC"; then
195                                 $EXEC
196                         fi
197
198                         PID=`cat ${ASTPIDFILE}`
199                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
200                         if test -f "${RUNDIR}/core.${PID}"; then
201                                 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
202                         elif test -f "${RUNDIR}/core"; then
203                                 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
204                         fi
205                 else
206                         message "Asterisk died with code $EXITSTATUS."
207
208                         PID=`cat ${ASTPIDFILE}`
209                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
210                         if test -f "${RUNDIR}/core.${PID}"; then
211                                 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
212                         elif test -f "${RUNDIR}/core"; then
213                                 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
214                         fi
215                 fi
216                 message "Automatically restarting Asterisk."
217                 sleep $SLEEPSECS
218                 if test "0$KILLALLMPG123" -gt 0; then
219                         pkill -9 mpg123
220                 fi
221         done
222 }
223
224 if test -n "$ASTSAFE_FOREGROUND"; then
225         run_asterisk
226 else
227         run_asterisk &
228 fi