kamailio: use return in init script
[feed/telephony.git] / net / kamailio / files / kamailio.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2014 - 2018 OpenWrt.org
3
4 START=99
5
6 NAME=kamailio
7 COMMAND=/usr/sbin/$NAME
8
9 RUNDIR=/var/run/$NAME
10 PIDFILE=$RUNDIR/$NAME.pid
11
12 LOG_ERR="/usr/bin/logger -p user.err -s -t $NAME"
13
14 USE_PROCD=1
15
16 #PROCD_DEBUG=1
17
18 check_listen() {
19 local value="$1"
20 local type="$2"
21
22 local address
23 local has_proto=0
24 local one two three
25 local tmp
26
27 [ -z "$value" ] && {
28 $LOG_ERR empty $type entry
29 return 1
30 }
31
32 # IPv6 addresses need to be enclosed in square brackets. If there are
33 # square brackets in the listen entry, just copy it.
34 echo "$value" | grep "\[[0-9:A-Fa-f]*\]" &> /dev/null && {
35 options=$options" -l $value"
36 return
37 }
38
39 # Bail if more than 2 colons.
40 [ $(echo "$value" | awk -F ":" '{print NF-1}') -gt 2 ] && {
41 $LOG_ERR init script does not understand $type entry \""$value"\"
42 return 1
43 }
44
45 IFS=":" read one two three << EOF
46 $value
47 EOF
48
49 case "$one" in
50 udp|tcp|tls|sctp)
51 tmp="$two"
52 has_proto=1
53 ;;
54 *)
55 tmp="$one"
56 ;;
57 esac
58
59 if [ "$type" = "listen" ]; then
60 network_get_ipaddr address "$tmp" || address="$tmp"
61 else
62 network_get_ipaddr6 address "$tmp" && address="[$address]" || \
63 address="$tmp"
64 fi
65
66 if [ -n "$three" ]; then
67 tmp="$one:$address:$three"
68 elif [ -n "$two" ]; then
69 if [ $has_proto = 1 ]; then
70 tmp="$one:$address"
71 else
72 tmp="$address:$two"
73 fi
74 else
75 tmp="$address"
76 fi
77
78 options=$options" -l $tmp"
79 }
80
81 start_service() {
82 local enabled
83 local user
84 local group
85 local shm_memory
86 local pkg_memory
87 local cfg_file
88 local options
89
90 config_load $NAME
91
92 config_get_bool enabled general enabled 0
93
94 if [ $enabled -eq 0 ]; then
95 $LOG_ERR service not enabled in /etc/config/$NAME
96 return 1
97 fi
98
99 config_get user general user $NAME
100 config_get group general group $NAME
101 config_get shm_memory general shm_memory 8
102 config_get pkg_memory general pkg_memory 2
103 config_get cfg_file general cfg_file /etc/$NAME/$NAME.cfg
104 config_get options general options
105
106 . /lib/functions/network.sh
107
108 config_list_foreach general listen check_listen listen
109 config_list_foreach general listen6 check_listen listen6
110
111 if [ ! -d $RUNDIR ]; then
112 mkdir -p $RUNDIR
113 chown "$user":"$group" $RUNDIR
114 fi
115
116 procd_open_instance
117 procd_set_param command $COMMAND
118 procd_append_param command \
119 -P $PIDFILE \
120 -f "$cfg_file" \
121 -m "$shm_memory" \
122 -M "$pkg_memory" \
123 $options \
124 -u "$user" \
125 -g "$group" \
126 -DD -E
127 # forward stderr to logd
128 procd_set_param stderr 1
129 procd_close_instance
130 }
131