summaryrefslogtreecommitdiffstats
path: root/net/kamailio/files/kamailio.init
blob: 0c8c722c50488c8b06c089f5d09b106756c5c69c (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh /etc/rc.common
# Copyright (C) 2014 - 2018 OpenWrt.org

START=99

NAME=kamailio
COMMAND=/usr/sbin/$NAME

RUNDIR=/var/run/$NAME
PIDFILE=$RUNDIR/$NAME.pid

LOG_ERR="/usr/bin/logger -p user.err -s -t $NAME"

USE_PROCD=1

#PROCD_DEBUG=1

check_listen() {
	local value="$1"
	local type="$2"

	local proto host port address result

	[ -z "$value" ] && {
		$LOG_ERR empty $type entry
		return 1
	}

	# [IPv6] - pass through as-is
	case "$value" in
		*\[*\]*)
			options="$options -l $value"
			return
			;;
	esac

	# Count colons. More than 2 means malformed
	# Format: proto:host:port
	if [ "$(echo "$value" | awk -F: '{print NF-1}')" -gt 2 ]; then
		$LOG_ERR init script does not understand $type entry \""$value"\"
		return 1
	fi

	# Parse proto (if present)
	case "$value" in
		udp:*|tcp:*|tls:*|sctp:*)
			proto="${value%%:*}"
			value="${value#*:}"
			;;
	esac

	# Parse port (if present)
	case "$value" in
		*:* )
			host="${value%%:*}"
			port="${value#*:}"
			;;
		*)
			host="$value"
			port=""
			;;
	esac

	# Resolve host to IP or interface
	if [ "$type" = "listen" ]; then
		network_get_ipaddr address "$host" || address="$host"
	else
		network_get_ipaddr6 address "$host" && address="[$address]" || address="$host"
	fi

	# Reconstruct result
	if [ -n "$proto" ]; then
		if [ -n "$port" ]; then
			result="$proto:$address:$port"
		else
			result="$proto:$address"
		fi
	else
		if [ -n "$port" ]; then
			result="$address:$port"
		else
			result="$address"
		fi
	fi

	options="$options -l $result"
}

start_service() {
	local enabled
	local user
	local group
	local shm_memory
	local pkg_memory
	local cfg_file
	local options

	config_load $NAME

	config_get_bool enabled general enabled 0

	if [ $enabled -eq 0 ]; then
		$LOG_ERR service not enabled in /etc/config/$NAME
		return 1
	fi

	config_get user        general user       $NAME
	config_get group       general group      $NAME
	config_get shm_memory  general shm_memory 8
	config_get pkg_memory  general pkg_memory 3
	config_get cfg_file    general cfg_file   /etc/$NAME/$NAME.cfg
	config_get options     general options
	config_get_bool stderr general stderr 0
	config_get debug_level general debug_level 0

	. /lib/functions/network.sh

	config_list_foreach general listen  check_listen listen
	config_list_foreach general listen6 check_listen listen6

	if [ ! -d "$RUNDIR" ]; then
		mkdir -p "$RUNDIR"
		chown "$user":"$group" "$RUNDIR"
	fi

	if [ -d "/etc/kamailio" ]; then
		chown "$user":"$group" /etc/kamailio/ -R
	fi

	procd_open_instance
	procd_set_param command $COMMAND
	procd_append_param command \
		-P $PIDFILE \
		-f "$cfg_file" \
		-m "$shm_memory" \
		-M "$pkg_memory" \
		$options \
		-u "$user" \
		-g "$group" \
		-DDD
	# If log_stderror=no (default) global parameter and -E is not provided, then it writes to syslog daemon
	[ "$stderr" -eq '1' ] && procd_append_param command -E
	procd_append_param command --debug $debug_level

	# silence stderr (useful only for debug)
	procd_set_param stderr 0
	# forward stdout to logd
	procd_set_param stdout 1
	procd_close_instance
}