siproxd: improve network device resolution
[feed/telephony.git] / net / siproxd / files / siproxd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 Alina Friedrichsen
3 # Copyright (C) 2011 OpenWrt.org
4
5 START=50
6
7 USE_PROCD=1
8
9 PROG="/usr/sbin/siproxd"
10 CONF_DIR="/var/etc/siproxd"
11 REG_DIR="/var/lib/siproxd"
12 PID_DIR="/var/run/siproxd"
13 PLUGIN_DIR="/usr/lib/siproxd/"
14 SIPROXD_UID="nobody"
15 SIPROXD_GID="nogroup"
16
17 # Some options need special handling or conflict with procd/jail setup.
18 append CONF_SKIP "interface_inbound interface_outbound chrootjail"
19 append CONF_SKIP "daemonize user plugindir registration_file pid_file"
20
21
22 # Check if a UCI option is set, or else apply a provided default.
23
24 default_conf() {
25 local opt="$1"
26 local default="$2"
27 local val
28
29 config_get "$opt" "$sec" "$opt"
30 eval "val=\"\${$opt}\""
31
32 [ -z "$val" ] || return 0
33 [ -n "$default" ] || return 0
34 config_set "$sec" "$opt" "$default"
35 append_conf "$opt" = "$default"
36 }
37
38 append_conf() {
39 echo $* >> "$CONF_DIR/siproxd-$sec.conf"
40 }
41
42 # Resolve network device by layer 3 first, then layer 2
43
44 siproxd_get_device() {
45 network_get_device $1 $2 || network_get_physdev $1 $2
46 }
47
48 # Use user-friendly network names (e.g. "wan", "lan") from options
49 # 'interface_inbound' and 'interface_outbound', but use standard siproxd
50 # parameters 'if_inbound' and 'if_outbound' if explicitly set.
51
52 setup_networks() {
53 local sec="$1"
54 local _int_inbound _int_outbound
55 local _dev_inbound _dev_outbound
56
57 config_get _int_inbound "$sec" interface_inbound
58 config_get _int_outbound "$sec" interface_outbound
59
60 siproxd_get_device _dev_inbound $_int_inbound
61 siproxd_get_device _dev_outbound $_int_outbound
62
63 default_conf if_inbound $_dev_inbound
64 default_conf if_outbound $_dev_outbound
65 }
66
67 # Apply default values to key options if unset in user's UCI config.
68
69 apply_defaults() {
70 local sec="$1"
71
72 default_conf sip_listen_port 5060
73 default_conf autosave_registrations 300
74 default_conf rtp_port_low 7070
75 default_conf rtp_port_high 7089
76 default_conf rtp_timeout 300
77 default_conf rtp_dscp 46
78 default_conf tcp_timeout 600
79 default_conf tcp_keepalive 20
80 default_conf default_expires 600
81 default_conf daemonize 0
82 default_conf user "$SIPROXD_UID"
83 default_conf registration_file "$REG_DIR/siproxd-$sec.reg"
84 default_conf plugindir "$PLUGIN_DIR"
85 }
86
87 # Handle activities at start of a new 'siproxd' section.
88 # Initialize section processing and save section name.
89
90 section_start() {
91 local sec="$1"
92
93 rm -f "$CONF_DIR/siproxd-$sec.conf"
94 append_conf "# config auto-generated from /etc/config/siproxd"
95 }
96
97 # Handle activities at close of a 'siproxd' section.
98 # Parse OpenWRT interface names (e.g. "wan"), apply defaults and
99 # set up procd jail.
100
101 section_end() {
102 local sec="$1"
103
104 local conf_file="$CONF_DIR/siproxd-$sec.conf"
105 local pid_file="$PID_DIR/siproxd-$sec.pid"
106 local reg_file plugin_dir
107
108 setup_networks "$sec"
109 apply_defaults "$sec"
110
111 config_get plugin_dir "$sec" plugindir
112 config_get reg_file "$sec" registration_file
113
114 procd_open_instance "$sec"
115 procd_set_param command "$PROG" --config "$conf_file"
116 procd_set_param pidfile "$pid_file"
117 procd_set_param respawn
118 procd_add_jail siproxd log
119 procd_add_jail_mount /etc/passwd /etc/group /etc/TZ /dev/null
120 procd_add_jail_mount "$conf_file"
121 [ -d "$plugin_dir" ] && procd_add_jail_mount "$plugin_dir"
122 # Ensure registration file exists for jail
123 [ -f "$reg_file" ] || touch "$reg_file"
124 chown "$SIPROXD_UID:$SIPROXD_GID" "$reg_file"
125 procd_add_jail_mount_rw "$reg_file"
126 procd_close_instance
127 }
128
129 # Setup callbacks for parsing siproxd sections, options, and lists.
130 # This avoids hardcoding all supported siproxd configuration parameters.
131
132 siproxd_cb() {
133 config_cb() {
134 # Section change: close any previous section.
135 [ -n "$cur_sec" ] && section_end "$cur_sec"
136
137 case "$1" in
138 # New 'siproxd' section: begin processing.
139 "siproxd")
140 cur_sec="$2"
141 section_start "$cur_sec"
142 ;;
143 # Config end or unknown section: ignore.
144 *)
145 cur_sec=""
146 ;;
147 esac
148 }
149
150 option_cb() {
151 local sec="$cur_sec"
152
153 [ -z "$sec" ] && return
154 list_contains CONF_SKIP "$1" && return
155 [ -n "$2" ] && append_conf "$1" = "$2"
156 }
157
158 list_cb() {
159 option_cb "$@"
160 }
161 }
162
163 service_triggers()
164 {
165 procd_add_reload_trigger "siproxd"
166 }
167
168 start_service() {
169 mkdir -p "$CONF_DIR" "$REG_DIR" "$PID_DIR"
170 chmod 755 "$CONF_DIR" "$REG_DIR" "$PID_DIR"
171 chown "$SIPROXD_UID:$SIPROXD_GID" "$REG_DIR"
172
173 . /lib/functions/network.sh
174 siproxd_cb
175 config_load 'siproxd'
176 }