safe_asterisk: Don't automatically exceed MAXFILES value of 2^20.
[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         else
124                 message "Cannot find specified TTY (${TTY})"
125                 exit 1
126         fi
127         ASTARGS="${ASTARGS} -vvvg"
128         if test "$CONSOLE" != "no"; then
129                 ASTARGS="${ASTARGS} -c"
130         fi
131 fi
132
133 if test ! -d "${RUNDIR}"; then
134         message "${RUNDIR} does not exist, creating"
135         if ! mkdir -p "${RUNDIR}"; then
136                 message "Unable to create ${RUNDIR}"
137                 exit 1
138         fi
139 fi
140
141 if test ! -w "${DUMPDROP}"; then
142         message "Cannot write to ${DUMPDROP}"
143         exit 1
144 fi
145
146 #
147 # Don't die if stdout/stderr can't be written to
148 #
149 trap '' PIPE
150
151 #
152 # Run scripts to set any environment variables or do any other system-specific setup needed
153 #
154
155 if test -d "${ASTETCDIR}/startup.d"; then
156         for script in "${ASTETCDIR}/startup.d/"*.sh; do
157                 if test -r "${script}"; then
158                         . "${script}"
159                 fi
160         done
161 fi
162
163 run_asterisk()
164 {
165         while :; do
166                 if test -n "$TTY" && test "$TTY" != "no"; then
167                         cd "${RUNDIR}"
168                         stty sane </dev/${TTY}
169                         nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
170                 else
171                         cd "${RUNDIR}"
172                         nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
173                 fi
174                 EXITSTATUS=$?
175                 message "Asterisk ended with exit status $EXITSTATUS"
176                 if test $EXITSTATUS -eq 0; then
177                         # Properly shutdown....
178                         message "Asterisk shutdown normally."
179                         exit 0
180                 elif test $EXITSTATUS -gt 128; then
181                         EXITSIGNAL=$((EXITSTATUS - 128))
182                         message "Asterisk exited on signal $EXITSIGNAL."
183                         if test -n "$NOTIFY"; then
184                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
185                                 mail -s "Asterisk on $MACHINE died (sig $EXITSIGNAL)" $NOTIFY
186                         fi
187                         if test -n "$EXEC"; then
188                                 $EXEC
189                         fi
190
191                         PID=`cat ${ASTPIDFILE}`
192                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
193                         if test -f "${RUNDIR}/core.${PID}"; then
194                                 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
195                         elif test -f "${RUNDIR}/core"; then
196                                 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
197                         fi
198                 else
199                         message "Asterisk died with code $EXITSTATUS."
200
201                         PID=`cat ${ASTPIDFILE}`
202                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
203                         if test -f "${RUNDIR}/core.${PID}"; then
204                                 mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
205                         elif test -f "${RUNDIR}/core"; then
206                                 mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
207                         fi
208                 fi
209                 message "Automatically restarting Asterisk."
210                 sleep $SLEEPSECS
211                 if test "0$KILLALLMPG123" -gt 0; then
212                         pkill -9 mpg123
213                 fi
214         done
215 }
216
217 if test -n "$ASTSAFE_FOREGROUND"; then
218         run_asterisk
219 else
220         run_asterisk &
221 fi