freeswitch-stable: init script update
[feed/telephony.git] / net / freeswitch-stable / files / freeswitch.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2017 OpenWrt.org
3
4 START=90
5
6 USE_PROCD=1
7
8 #PROCD_DEBUG=1
9
10 FS=freeswitch
11 DEFAULT=/etc/default/$FS
12 LOGGER="/usr/bin/logger -p user.err -s -t $FS"
13 OPTIONS=
14 PROG=/usr/bin/$FS
15 TIMEOUT=30
16
17 [ -f $DEFAULT ] && . $DEFAULT
18
19 fs_user="${FS_USER:-$FS}"
20 fs_group="${FS_GROUP:-$FS}"
21
22 fs_dir_etc="/etc/$FS"
23 fs_dir_localstate="/var/lib/$FS"
24 fs_dir_run="/var/run/$FS"
25
26 fs_dir_cache="${FS_DIR_CACHE:-/tmp/$FS/cache}"
27 fs_dir_db="${FS_DIR_DB:-/tmp/$FS/db}"
28 fs_dir_log="${FS_DIR_LOG:-/tmp/$FS/log}"
29 fs_dir_recordings="${FS_DIR_RECORDINGS:-/tmp/$FS/recordings}"
30 fs_dir_storage="${FS_DIR_STORAGE:-/tmp/$FS/storage}"
31 fs_dir_temp="${FS_DIR_TEMP:-/tmp/$FS/temp}"
32
33 start_service() {
34 local dir=
35
36 if [ -f "/etc/${FS}_disabled" ]; then
37 $LOGGER File \"/etc/${FS}_disabled\" exists
38 $LOGGER Remove it once your configuration is set up
39 exit 1
40 fi
41
42 for dir in "$fs_dir_cache" "$fs_dir_db" "$fs_dir_localstate" \
43 "$fs_dir_log" "$fs_dir_recordings" "$fs_dir_run" "$fs_dir_storage" \
44 "$fs_dir_temp"
45 do
46 [ -n "$dir" ] && {
47 mkdir -p "$dir"
48 chown "$fs_user":"$fs_group" "$dir"
49 chmod 750 "$dir"
50 }
51 done
52
53 #[ -d "$fs_dir_etc" ] && {
54 # find "$fs_dir_etc" -type f -exec chown root:"$fs_group" {} \;
55 # find "$fs_dir_etc" -type f -exec chmod 640 {} \;
56 #}
57
58 procd_open_instance
59 # starting with full path seems cleaner judging by 'ps' output
60 procd_set_param command $PROG
61 # need to specify all or none of -conf, -log, and -db
62 procd_append_param command \
63 -cache "$fs_dir_cache" \
64 -conf "$fs_dir_etc" \
65 -db "$fs_dir_db" \
66 -g "$fs_group" \
67 -log "$fs_dir_log" \
68 -recordings "$fs_dir_recordings" \
69 -run "$fs_dir_run" \
70 -storage "$fs_dir_storage" \
71 -temp "$fs_dir_temp" \
72 -u "$fs_user" \
73 $OPTIONS \
74 -nc \
75 -nf
76 # forward stderr to logd
77 procd_set_param stderr 1
78 procd_close_instance
79 }
80
81 stop_service() {
82 local retval=
83 local mypid=
84 local timeout=$TIMEOUT
85
86 pgrep $FS &> /dev/null
87 [ $? -ne 0 ] && exit 0
88
89 [ -f "$fs_dir_run"/${FS}.pid ]
90 retval=$?
91
92 # init script could find itself in a scenario where FS was started
93 # very recently, so make it wait a while for a pid file to appear
94 while [ $retval -ne 0 -a $timeout -gt 0 ]; do
95 sleep 1
96 [ -f "$fs_dir_run"/${FS}.pid ]
97 retval=$?
98 timeout=$(($timeout-1))
99 done
100
101 [ $retval -eq 0 ] || {
102 $LOGGER PID file does not exist
103 exit 1
104 }
105
106 mypid=$(cat "$fs_dir_run"/${FS}.pid)
107
108 [ "$mypid" -gt 1 ] 2> /dev/null || {
109 $LOGGER PID file contains garbage
110 exit 1
111 }
112
113 timeout=$TIMEOUT
114 kill $mypid 2>/dev/null
115 pgrep $FS | grep -w $mypid &>/dev/null
116 retval=$?
117
118 while [ $retval -eq 0 -a $timeout -gt 0 ]; do
119 sleep 10
120 pgrep $FS | grep -w $mypid &>/dev/null
121 retval=$?
122 [ $retval -eq 0 ] && kill $mypid 2>/dev/null
123 timeout=$(($timeout-10))
124 done
125
126 [ $retval -ne 1 ] && {
127 $LOGGER Failed to stop $FS
128 exit 1
129 }
130 }