siproxd: restructure code and use procd for init

Switch to use procd for init, with support for jails. Clean up code and
restructure callback processing to be more robust and understandable. This
also fixes a bug processing multiple siproxd config sections, as reported
by Michael Kuron.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
(cherry picked from master commit b65b20f6)
This commit is contained in:
guidosarducci 2018-03-24 18:51:47 -07:00
parent e2fee9b6f2
commit 6098d247ec
2 changed files with 89 additions and 84 deletions

View file

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=siproxd PKG_NAME:=siproxd
PKG_VERSION:=0.8.2 PKG_VERSION:=0.8.2
PKG_RELEASE:=3 PKG_RELEASE:=4
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=@SF/siproxd PKG_SOURCE_URL:=@SF/siproxd

View file

@ -4,14 +4,19 @@
START=50 START=50
SERVICE_USE_PID=1 USE_PROCD=1
siproxd_bin="/usr/sbin/siproxd" PROG="/usr/sbin/siproxd"
siproxd_conf_dir="/var/etc/siproxd" CONF_DIR="/var/etc/siproxd"
siproxd_conf_prefix="$siproxd_conf_dir/siproxd-" REG_DIR="/var/lib/siproxd"
siproxd_registration_dir="/var/lib/siproxd" PID_DIR="/var/run/siproxd"
siproxd_registration_prefix="$siproxd_registration_dir/siproxd-" PLUGIN_DIR="/usr/lib/siproxd/"
siproxd_pid_dir="/var/run/siproxd" UID="nobody"
GID="nogroup"
# Some options need special handling or conflict with procd/jail setup.
append CONF_SKIP "interface_inbound interface_outbound chrootjail"
append CONF_SKIP "daemonize user plugindir registration_file pid_file"
# Check if a UCI option is set, or else apply a provided default. # Check if a UCI option is set, or else apply a provided default.
@ -26,7 +31,12 @@ default_conf() {
[ -z "$val" ] || return 0 [ -z "$val" ] || return 0
[ -n "$default" ] || return 0 [ -n "$default" ] || return 0
echo "$opt" = "$default" >> "$siproxd_conf_prefix$sec.conf" config_set "$sec" "$opt" "$default"
append_conf "$opt" = "$default"
}
append_conf() {
echo $* >> "$CONF_DIR/siproxd-$sec.conf"
} }
# Use user-friendly network names (e.g. "wan", "lan") from options # Use user-friendly network names (e.g. "wan", "lan") from options
@ -35,14 +45,13 @@ default_conf() {
setup_networks() { setup_networks() {
local sec="$1" local sec="$1"
local _int_inbound local _int_inbound _int_outbound
local _int_outbound local _dev_inbound _dev_outbound
local _dev_inbound
local _dev_outbound
config_get _int_inbound "$sec" interface_inbound config_get _int_inbound "$sec" interface_inbound
config_get _int_outbound "$sec" interface_outbound config_get _int_outbound "$sec" interface_outbound
. /lib/functions/network.sh
network_get_physdev _dev_inbound $_int_inbound network_get_physdev _dev_inbound $_int_inbound
network_get_physdev _dev_outbound $_int_outbound network_get_physdev _dev_outbound $_int_outbound
@ -56,11 +65,7 @@ apply_defaults() {
local sec="$1" local sec="$1"
default_conf sip_listen_port 5060 default_conf sip_listen_port 5060
default_conf daemonize 1
default_conf user nobody
default_conf registration_file "$siproxd_registration_prefix$sec.reg"
default_conf autosave_registrations 300 default_conf autosave_registrations 300
default_conf pid_file "$siproxd_pid_dir/siproxd-$sec.pid"
default_conf rtp_port_low 7070 default_conf rtp_port_low 7070
default_conf rtp_port_high 7089 default_conf rtp_port_high 7089
default_conf rtp_timeout 300 default_conf rtp_timeout 300
@ -68,7 +73,52 @@ apply_defaults() {
default_conf tcp_timeout 600 default_conf tcp_timeout 600
default_conf tcp_keepalive 20 default_conf tcp_keepalive 20
default_conf default_expires 600 default_conf default_expires 600
default_conf plugindir "/usr/lib/siproxd/" default_conf daemonize 0
default_conf user "$UID"
default_conf registration_file "$REG_DIR/siproxd-$sec.reg"
default_conf plugindir "$PLUGIN_DIR"
}
# Handle activities at start of a new 'siproxd' section.
# Initialize section processing and save section name.
section_start() {
local sec="$1"
rm -f "$CONF_DIR/siproxd-$sec.conf"
append_conf "# config auto-generated from /etc/config/siproxd"
}
# Handle activities at close of a 'siproxd' section.
# Parse OpenWRT interface names (e.g. "wan"), apply defaults and
# set up procd jail.
section_end() {
local sec="$1"
local conf_file="$CONF_DIR/siproxd-$sec.conf"
local pid_file="$PID_DIR/siproxd-$sec.pid"
local reg_file plugin_dir
setup_networks "$sec"
apply_defaults "$sec"
config_get plugin_dir "$sec" plugindir
config_get reg_file "$sec" registration_file
procd_open_instance "$sec"
procd_set_param command "$PROG" --config "$conf_file"
procd_set_param pidfile "$pid_file"
procd_set_param respawn
procd_add_jail siproxd log
procd_add_jail_mount /etc/passwd /etc/group /etc/TZ /dev/null
procd_add_jail_mount "$conf_file"
[ -d "$plugin_dir" ] && procd_add_jail_mount "$plugin_dir"
# Ensure registration file exists for jail
[ -f "$reg_file" ] || touch "$reg_file"
chown "$UID:$GID" "$reg_file"
procd_add_jail_mount_rw "$reg_file"
procd_close_instance
} }
# Setup callbacks for parsing siproxd sections, options, and lists. # Setup callbacks for parsing siproxd sections, options, and lists.
@ -76,90 +126,45 @@ apply_defaults() {
siproxd_cb() { siproxd_cb() {
config_cb() { config_cb() {
local _int_inbound # Section change: close any previous section.
local _int_outbound [ -n "$cur_sec" ] && section_end "$cur_sec"
local _dev_inbound
local _dev_outbound
case "$1" in case "$1" in
# Initialize section processing and save section name. # New 'siproxd' section: begin processing.
"siproxd") "siproxd")
sec="$2" cur_sec="$2"
if [ -f "$siproxd_conf_prefix$sec.conf" ]; then section_start "$cur_sec"
rm "$siproxd_conf_prefix$sec.conf"
fi
echo "# auto-generated config file from /etc/config/siproxd" > \
"$siproxd_conf_prefix$sec.conf"
;; ;;
# Parse OpenWRT interface names (e.g. "wan") and apply defaults, # Config end or unknown section: ignore.
# using saved section name. *)
"") cur_sec=""
local chrootjail
local pid_file
setup_networks "$sec"
apply_defaults "$sec"
config_get chrootjail "$sec" chrootjail
if [ -n "$chrootjail" ]; then
if [ ! -d "$chrootjail" ]; then
mkdir -p "$chrootjail"
chmod 0755 "$chrootjail"
fi
fi
config_get pid_file "$sec" pid_file
SERVICE_PID_FILE="$pid_file" service_start \
$siproxd_bin --config "$siproxd_conf_prefix$sec.conf"
;; ;;
esac esac
return 0
} }
option_cb() { option_cb() {
# These 2 OpenWRT-specific options are handled in post-processing. local sec="$cur_sec"
case "$1" in
"interface_inbound"|"interface_outbound") return 0 ;; [ -z "$sec" ] && return
esac list_contains CONF_SKIP "$1" && return
# Other options match siproxd docs, so write directly to config. [ -n "$2" ] && append_conf "$1" = "$2"
[ -n "$2" ] && echo "$1" = "$2" >> "$siproxd_conf_prefix$sec.conf"
return 0
} }
list_cb() { list_cb() {
# All list items match siproxd docs, so write directly to config. option_cb "$@"
[ -n "$2" ] && echo "$1" = "$2" >> "$siproxd_conf_prefix$sec.conf"
return 0
} }
} }
stop_instance() { service_triggers()
local sec="$1" {
procd_add_reload_trigger "siproxd"
config_get pid_file "$sec" pid_file "$siproxd_pid_dir/siproxd-$sec.pid"
SERVICE_PID_FILE="$pid_file" \
service_stop $siproxd_bin
} }
start() { start_service() {
mkdir -p "$siproxd_conf_dir" mkdir -p "$CONF_DIR" "$REG_DIR" "$PID_DIR"
chmod 755 "$siproxd_conf_dir" chmod 755 "$CONF_DIR" "$REG_DIR" "$PID_DIR"
chown "$UID:$GID" "$REG_DIR"
mkdir -p "$siproxd_registration_dir"
chmod 700 "$siproxd_registration_dir"
chown nobody:nogroup "$siproxd_registration_dir"
mkdir -p "$siproxd_pid_dir"
chmod 700 "$siproxd_pid_dir"
chown nobody:nogroup "$siproxd_pid_dir"
. /lib/functions/network.sh
siproxd_cb siproxd_cb
config_load 'siproxd' config_load 'siproxd'
} }
stop() {
config_load 'siproxd'
config_foreach stop_instance 'siproxd'
}