dd932bd7fb0580a74e27d19f9f1633dc6abf659c
[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 # Use user-friendly network names (e.g. "wan", "lan") from options
43 # 'interface_inbound' and 'interface_outbound', but use standard siproxd
44 # parameters 'if_inbound' and 'if_outbound' if explicitly set.
45
46 setup_networks() {
47 local sec="$1"
48 local _int_inbound _int_outbound
49 local _dev_inbound _dev_outbound
50
51 config_get _int_inbound "$sec" interface_inbound
52 config_get _int_outbound "$sec" interface_outbound
53
54 . /lib/functions/network.sh
55 network_get_physdev _dev_inbound $_int_inbound
56 network_get_physdev _dev_outbound $_int_outbound
57
58 default_conf if_inbound $_dev_inbound
59 default_conf if_outbound $_dev_outbound
60 }
61
62 # Apply default values to key options if unset in user's UCI config.
63
64 apply_defaults() {
65 local sec="$1"
66
67 default_conf sip_listen_port 5060
68 default_conf autosave_registrations 300
69 default_conf rtp_port_low 7070
70 default_conf rtp_port_high 7089
71 default_conf rtp_timeout 300
72 default_conf rtp_dscp 46
73 default_conf tcp_timeout 600
74 default_conf tcp_keepalive 20
75 default_conf default_expires 600
76 default_conf daemonize 0
77 default_conf user "$SIPROXD_UID"
78 default_conf registration_file "$REG_DIR/siproxd-$sec.reg"
79 default_conf plugindir "$PLUGIN_DIR"
80 }
81
82 # Handle activities at start of a new 'siproxd' section.
83 # Initialize section processing and save section name.
84
85 section_start() {
86 local sec="$1"
87
88 rm -f "$CONF_DIR/siproxd-$sec.conf"
89 append_conf "# config auto-generated from /etc/config/siproxd"
90 }
91
92 # Handle activities at close of a 'siproxd' section.
93 # Parse OpenWRT interface names (e.g. "wan"), apply defaults and
94 # set up procd jail.
95
96 section_end() {
97 local sec="$1"
98
99 local conf_file="$CONF_DIR/siproxd-$sec.conf"
100 local pid_file="$PID_DIR/siproxd-$sec.pid"
101 local reg_file plugin_dir
102
103 setup_networks "$sec"
104 apply_defaults "$sec"
105
106 config_get plugin_dir "$sec" plugindir
107 config_get reg_file "$sec" registration_file
108
109 procd_open_instance "$sec"
110 procd_set_param command "$PROG" --config "$conf_file"
111 procd_set_param pidfile "$pid_file"
112 procd_set_param respawn
113 procd_add_jail siproxd log
114 procd_add_jail_mount /etc/passwd /etc/group /etc/TZ /dev/null
115 procd_add_jail_mount "$conf_file"
116 [ -d "$plugin_dir" ] && procd_add_jail_mount "$plugin_dir"
117 # Ensure registration file exists for jail
118 [ -f "$reg_file" ] || touch "$reg_file"
119 chown "$SIPROXD_UID:$SIPROXD_GID" "$reg_file"
120 procd_add_jail_mount_rw "$reg_file"
121 procd_close_instance
122 }
123
124 # Setup callbacks for parsing siproxd sections, options, and lists.
125 # This avoids hardcoding all supported siproxd configuration parameters.
126
127 siproxd_cb() {
128 config_cb() {
129 # Section change: close any previous section.
130 [ -n "$cur_sec" ] && section_end "$cur_sec"
131
132 case "$1" in
133 # New 'siproxd' section: begin processing.
134 "siproxd")
135 cur_sec="$2"
136 section_start "$cur_sec"
137 ;;
138 # Config end or unknown section: ignore.
139 *)
140 cur_sec=""
141 ;;
142 esac
143 }
144
145 option_cb() {
146 local sec="$cur_sec"
147
148 [ -z "$sec" ] && return
149 list_contains CONF_SKIP "$1" && return
150 [ -n "$2" ] && append_conf "$1" = "$2"
151 }
152
153 list_cb() {
154 option_cb "$@"
155 }
156 }
157
158 service_triggers()
159 {
160 procd_add_reload_trigger "siproxd"
161 }
162
163 start_service() {
164 mkdir -p "$CONF_DIR" "$REG_DIR" "$PID_DIR"
165 chmod 755 "$CONF_DIR" "$REG_DIR" "$PID_DIR"
166 chown "$SIPROXD_UID:$SIPROXD_GID" "$REG_DIR"
167
168 siproxd_cb
169 config_load 'siproxd'
170 }