summaryrefslogtreecommitdiff
path: root/net/freeswitch-stable/files/freeswitch.init
blob: c17768c1aa74ec4c844465d7a59eada1624dff4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh /etc/rc.common
# Copyright (C) 2017 OpenWrt.org

START=90

USE_PROCD=1

#PROCD_DEBUG=1

FS=freeswitch
DEFAULT=/etc/default/$FS
LOGGER="/usr/bin/logger -p user.err -s -t $FS"
OPTIONS=
PROG=/usr/bin/$FS
TIMEOUT=30

[ -f $DEFAULT ] && . $DEFAULT

fs_user="${FS_USER:-$FS}"
fs_group="${FS_GROUP:-$FS}"

fs_dir_etc="/etc/$FS"
fs_dir_localstate="/var/lib/$FS"
fs_dir_run="/var/run/$FS"

fs_dir_cache="${FS_DIR_CACHE:-/tmp/$FS/cache}"
fs_dir_db="${FS_DIR_DB:-/tmp/$FS/db}"
fs_dir_log="${FS_DIR_LOG:-/tmp/$FS/log}"
fs_dir_recordings="${FS_DIR_RECORDINGS:-/tmp/$FS/recordings}"
fs_dir_storage="${FS_DIR_STORAGE:-/tmp/$FS/storage}"
fs_dir_temp="${FS_DIR_TEMP:-/tmp/$FS/temp}"

start_service() {
  local dir=

  if [ "$ENABLE_FREESWITCH" != yes ]; then
    $LOGGER User configuration incomplete - not starting $FS
    $LOGGER Check ENABLE_FREESWITCH in $DEFAULT
    exit 1
  fi

  for dir in "$fs_dir_cache" "$fs_dir_db" "$fs_dir_localstate" \
    "$fs_dir_log" "$fs_dir_recordings" "$fs_dir_run" "$fs_dir_storage" \
    "$fs_dir_temp"
  do
    [ -n "$dir" ] && {
      mkdir -p "$dir"
      chown "$fs_user":"$fs_group" "$dir"
      chmod 750 "$dir"
    }
  done

  #[ -d "$fs_dir_etc" ] && {
  #  find "$fs_dir_etc" -type f -exec chown root:"$fs_group" {} \;
  #  find "$fs_dir_etc" -type f -exec chmod 640 {} \;
  #}

  procd_open_instance
  # starting with full path seems cleaner judging by 'ps' output
  procd_set_param command $PROG
  # need to specify all or none of -conf, -log, and -db
  procd_append_param command \
    -cache "$fs_dir_cache" \
    -conf "$fs_dir_etc" \
    -db "$fs_dir_db" \
    -g "$fs_group" \
    -log "$fs_dir_log" \
    -recordings "$fs_dir_recordings" \
    -run "$fs_dir_run" \
    -storage "$fs_dir_storage" \
    -temp "$fs_dir_temp" \
    -u "$fs_user" \
    $OPTIONS \
    -nc \
    -nf
  # forward stderr to logd
  procd_set_param stderr 1
  procd_close_instance
}

stop_service() {
  local retval=
  local mypid=
  local timeout=$TIMEOUT

  pgrep $FS &> /dev/null
  [ $? -ne 0 ] && exit 0

  [ -f "$fs_dir_run"/${FS}.pid ]
  retval=$?

  # init script could find itself in a scenario where FS was started
  # very recently, so make it wait a while for a pid file to appear
  while [ $retval -ne 0 -a $timeout -gt 0 ]; do
    sleep 1
    [ -f "$fs_dir_run"/${FS}.pid ]
    retval=$?
    timeout=$(($timeout-1))
  done

  [ $retval -eq 0 ] || {
    $LOGGER PID file does not exist
    exit 1
  }

  mypid=$(cat "$fs_dir_run"/${FS}.pid)

  [ "$mypid" -gt 1 ] 2> /dev/null || {
    $LOGGER PID file contains garbage
    exit 1
  }

  timeout=$TIMEOUT
  kill $mypid 2>/dev/null
  pgrep $FS | grep -w $mypid &>/dev/null
  retval=$?

  while [ $retval -eq 0 -a $timeout -gt 0 ]; do
    sleep 10
    pgrep $FS | grep -w $mypid &>/dev/null
    retval=$?
    [ $retval -eq 0 ] && kill $mypid 2>/dev/null
    timeout=$(($timeout-10))
  done

  [ $retval -ne 1 ] && {
    $LOGGER Failed to stop $FS
    exit 1
  }
}