Set up umask as a possible configuration option.
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
1 #!/bin/sh
2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
3
4 CLIARGS="$*"                            # Grab any args passed to safe_asterisk
5 TTY=9                                           # TTY (if you want one) for Asterisk to run on
6 CONSOLE=yes                                     # Whether or not you want a console
7 #NOTIFY=ben@alkaloid.net        # Who to notify about crashes
8 #EXEC=/path/to/somescript       # Run this command if Asterisk crashes
9 #LOGFILE=/path/to/logfile       # Where to place the normal logfile (disabled if blank)
10 #SYSLOG=local0                          # Which syslog facility to use (disabled if blank)
11 MACHINE=`hostname`                      # To specify which machine has crashed when getting the mail
12 DUMPDROP=/tmp
13 SLEEPSECS=4
14 ASTSBINDIR=__ASTERISK_SBIN_DIR__
15 ASTPIDFILE=__ASTERISK_VARRUN_DIR__/asterisk.pid
16
17 # comment this line out to have this script _not_ kill all mpg123 processes when
18 # asterisk exits
19 KILLALLMPG123=1
20
21 # run asterisk with this priority
22 PRIORITY=0
23
24 # set system filemax on supported OSes if this variable is set
25 # SYSMAXFILES=262144
26
27 # Asterisk allows full permissions by default, so set a umask, if you want
28 # restricted permissions.
29 #UMASK=022
30
31 # set max files open with ulimit. On linux systems, this will be automatically
32 # set to the system's maximum files open devided by two, if not set here.
33 # MAXFILES=32768
34
35 message() {
36         echo "$1" >&2
37         if [ "$SYSLOG" != "" ]; then
38             logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
39         fi
40         if [ "$LOGFILE" != "" ]; then
41             echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
42         fi
43 }
44
45 # Check if Asterisk is already running.  If it is, then bug out, because
46 # starting safe_asterisk when Asterisk is running is very bad.
47 VERSION=`${ASTSBINDIR}/asterisk -rx 'core show version'`
48 if [ "`echo $VERSION | cut -c 1-8`" = "Asterisk" ]; then
49         message "Asterisk is already running.  $0 will exit now."
50         exit 1
51 fi
52
53 # since we're going to change priority and open files limits, we need to be
54 # root. if running asterisk as other users, pass that to asterisk on the command
55 # line.
56 # if we're not root, fall back to standard everything.
57 if [ `id -u` != 0 ]
58 then
59         echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
60         echo "This is NOT suitable for large systems." >&2
61         PRIORITY=0
62         message "safe_asterisk was started by `id -n` (uid `id -u`)."
63 else
64         if `echo $OSTYPE | grep linux 2>&1 > /dev/null `
65         then
66                 # maximum number of open files is set to the system maximum divided by two if
67                 # MAXFILES is not set.
68                 if [ "$MAXFILES" = "" ]
69                 then
70                         # just check if file-max is readable
71                         if [ -r /proc/sys/fs/file-max ]
72                         then
73                                 MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 ))
74                         fi
75                 fi
76                 SYSCTL_MAXFILES="fs.file-max"
77         elif `echo $OSTYPE | grep darwin 2>&1 > /dev/null `
78         then
79                 SYSCTL_MAXFILES="kern.maxfiles"
80         fi
81
82
83         if [ "$SYSMAXFILES" != "" ]
84         then
85                 if [ "$SYSCTL_MAXFILES" != "" ]
86                 then
87                         sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
88                 fi
89         fi
90
91         # set the process's filemax to whatever set above
92         ulimit -n $MAXFILES
93
94 fi
95
96 if test "x$UMASK" != "x"; then
97         umask $UMASK
98 fi
99
100 #
101 # Let Asterisk dump core
102 #
103 ulimit -c unlimited
104
105 #
106 # Don't fork when running "safely"
107 #
108 ASTARGS=""
109 if [ "$TTY" != "" ]; then
110         if [ -c /dev/tty${TTY} ]; then
111                 TTY=tty${TTY}
112         elif [ -c /dev/vc/${TTY} ]; then
113                 TTY=vc/${TTY}
114         else
115                 message "Cannot find specified TTY (${TTY})"
116                 exit 1
117         fi
118         ASTARGS="${ASTARGS} -vvvg"
119         if [ "$CONSOLE" != "no" ]; then
120                 ASTARGS="${ASTARGS} -c"
121         fi
122 fi
123 if [ ! -w ${DUMPDROP} ]; then   
124         message "Cannot write to ${DUMPDROP}"
125         exit 1
126 fi
127
128 #
129 # Don't die if stdout/stderr can't be written to
130 #
131 trap '' PIPE
132
133 #
134 # Run scripts to set any environment variables or do any other system-specific setup needed
135 #
136
137 if [ -d /etc/asterisk/startup.d ]; then
138         for script in /etc/asterisk/startup.d/*.sh; do
139                 if [ -x ${script} ]; then
140                         source ${script}
141                 fi
142         done
143 fi
144
145 run_asterisk()
146 {
147         while :; do 
148
149                 if [ "$TTY" != "" ]; then
150                         cd /tmp
151                         stty sane < /dev/${TTY}
152                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
153                 else
154                         cd /tmp
155                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
156                 fi
157                 EXITSTATUS=$?
158                 message "Asterisk ended with exit status $EXITSTATUS"
159                 if [ "$EXITSTATUS" = "0" ]; then
160                         # Properly shutdown....
161                         message "Asterisk shutdown normally."
162                         exit 0
163                 elif [ $EXITSTATUS -gt 128 ]; then
164                         let EXITSIGNAL=EXITSTATUS-128
165                         echo "Asterisk exited on signal $EXITSIGNAL."
166                         if [ "$NOTIFY" != "" ]; then
167                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
168                                 mail -s "Asterisk Died" $NOTIFY
169                                 message "Exited on signal $EXITSIGNAL"
170                         fi
171                         if [ "$EXEC" != "" ]; then
172                                 $EXEC
173                         fi
174
175                         PID=`cat ${ASTPIDFILE}`
176                         if [ -f /tmp/core.${PID} ]; then
177                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
178                         elif [ -f /tmp/core ]; then
179                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
180                         fi
181                 else
182                         message "Asterisk died with code $EXITSTATUS."
183
184                         PID=`cat ${ASTPIDFILE}`
185                         if [ -f /tmp/core.${PID} ]; then
186                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
187                         elif [ -f /tmp/core ]; then
188                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
189                         fi
190                 fi
191                 message "Automatically restarting Asterisk."
192                 sleep $SLEEPSECS
193                 if [ $KILLALLMPG123 ]
194                 then
195                         pkill -9 mpg123
196                 fi
197         done
198 }
199
200 run_asterisk &