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