From 9b90b9ea78c3e0b90a7456171faecc8c9fff97f8 Mon Sep 17 00:00:00 2001 From: micmac1 Date: Thu, 25 Jul 2019 09:33:06 +0200 Subject: [PATCH] asterisk-16.x: update init to procd (#421) Converts the init script to procd. The user "asterisk" is added to the system. This user will be used by default. The init script no longer extracts its configuration from "/etc/default/asterisk". Instead the uci configuration "/etc/config/asterisk" is introduced. Signed-off-by: Sebastian Kemper --- net/asterisk-16.x/Makefile | 9 +- net/asterisk-16.x/files/asterisk.config | 26 ++++ net/asterisk-16.x/files/asterisk.default | 4 - net/asterisk-16.x/files/asterisk.init | 150 +++++++++++++++++++---- 4 files changed, 155 insertions(+), 34 deletions(-) create mode 100644 net/asterisk-16.x/files/asterisk.config delete mode 100644 net/asterisk-16.x/files/asterisk.default diff --git a/net/asterisk-16.x/Makefile b/net/asterisk-16.x/Makefile index 24a4d6d..f7ecbc1 100644 --- a/net/asterisk-16.x/Makefile +++ b/net/asterisk-16.x/Makefile @@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk AST_MAJOR_VERSION:=16 PKG_NAME:=asterisk$(AST_MAJOR_VERSION) PKG_VERSION:=$(AST_MAJOR_VERSION).3.0 -PKG_RELEASE:=5 +PKG_RELEASE:=6 PKG_SOURCE:=asterisk-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=https://downloads.asterisk.org/pub/telephony/asterisk/releases @@ -442,6 +442,7 @@ $(call Package/$(PKG_NAME)/Default) TITLE:=Complete open source PBX, v$(PKG_VERSION) MENU:=1 DEPENDS:=$(CXX_DEPENDS) +jansson +libcap +libedit +libopenssl +libsqlite3 +libuuid +libxml2 +zlib + USERID:=asterisk=385:asterisk=385 endef define Package/$(PKG_NAME)/description @@ -470,7 +471,7 @@ define Package/$(PKG_NAME)/conffiles /etc/asterisk/stasis.conf /etc/asterisk/udptl.conf /etc/asterisk/users.conf -/etc/default/asterisk +/etc/config/asterisk /etc/init.d/asterisk endef @@ -494,10 +495,10 @@ $(call Package/$(PKG_NAME)/install/sbin,$(1),astgenkey) $(foreach m,$(AST_CFG_FILES),$(call Package/$(PKG_NAME)/install/conffile,$(1),$(m));) $(foreach m,$(AST_EMB_MODULES),$(call Package/$(PKG_NAME)/install/module,$(1),$(m));) $(INSTALL_DIR) $(1)/usr/share/asterisk/sounds/ - $(INSTALL_DIR) $(1)/etc/default - $(INSTALL_DATA) ./files/asterisk.default $(1)/etc/default/asterisk + $(INSTALL_DIR) $(1)/etc/config $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/asterisk.init $(1)/etc/init.d/asterisk + $(INSTALL_CONF) ./files/asterisk.config $(1)/etc/config/asterisk endef define Package/$(PKG_NAME)-sounds diff --git a/net/asterisk-16.x/files/asterisk.config b/net/asterisk-16.x/files/asterisk.config new file mode 100644 index 0000000..3c05e0a --- /dev/null +++ b/net/asterisk-16.x/files/asterisk.config @@ -0,0 +1,26 @@ + +config asterisk 'general' + option enabled '0' + # If you have problems running Asterisk as user "asterisk" we'd + # like to hear from you. Please raise an issue at: + # https://github.com/openwrt/telephony/issues + option user 'asterisk' + option group 'asterisk' + option log_stderr '1' + option log_stdout '1' + option options '' + +config asterisk 'directories' + # The init script will only create below directories and update + # their permissions if they don't exist. + # Note: To change the default paths you need to update your + # "asterisk.conf" file. + option agidir '/usr/share/asterisk/agi-bin' + option datadir '/usr/share/asterisk' + option dbdir '/var/lib/asterisk/astdb' + option keydir '/usr/share/asterisk/keys' + option logdir '/var/log/asterisk' + option rundir '/var/run/asterisk' + option spooldir '/var/spool/asterisk' + option varlibdir '/var/lib/asterisk' + diff --git a/net/asterisk-16.x/files/asterisk.default b/net/asterisk-16.x/files/asterisk.default deleted file mode 100644 index 9d046c4..0000000 --- a/net/asterisk-16.x/files/asterisk.default +++ /dev/null @@ -1,4 +0,0 @@ -## startup options for /etc/init.d/asterisk - -ENABLE_ASTERISK="yes" -OPTIONS="" diff --git a/net/asterisk-16.x/files/asterisk.init b/net/asterisk-16.x/files/asterisk.init index aa413fc..fd83948 100644 --- a/net/asterisk-16.x/files/asterisk.init +++ b/net/asterisk-16.x/files/asterisk.init @@ -2,32 +2,130 @@ # Copyright (C) 2014 OpenWrt.org START=99 -APP=asterisk -OPTIONS= -DEST= -DEFAULT=$DEST/etc/default/$APP -BIN_FILE=$DEST/usr/sbin/$APP -PID_FILE=$DEST/var/run/$APP/$APP.pid - -start() { - [ -f $DEFAULT ] && . $DEFAULT - [ -d $DEST/var/run/asterisk ] || mkdir -p $DEST/var/run/asterisk - [ -d $DEST/var/log/asterisk ] || mkdir -p $DEST/var/log/asterisk - [ -d $DEST/var/spool/asterisk ] || mkdir -p $DEST/var/spool/asterisk - [ -d $DEST/var/lib/asterisk ] || mkdir -p $DEST/var/lib/asterisk - [ -d $DEST/var/lib/asterisk/keys ] || mkdir -p $DEST/var/lib/asterisk/keys - [ -d $DEST/var/log/asterisk/cdr-csv ] || mkdir -p $DEST/var/log/asterisk/cdr-csv - - SERVICE_PID_FILE="/var/run/asterisk/asterisk.pid" \ - service_start $BIN_FILE $OPTIONS -} -stop() { - SERVICE_PID_FILE="/var/run/asterisk/asterisk.pid" \ - service_stop $BIN_FILE -} +USE_PROCD=1 + +#PROCD_DEBUG=1 + +NAME=asterisk +COMMAND=/usr/sbin/$NAME + +LOGGER="/usr/bin/logger -p user.err -s -t $NAME --" + +start_service() { + local enabled + + local user + local group + + local log_stderr + local log_stdout + + local agidir + local cdrcsvdir + local datadir + local dbdir + local keydir + local logdir + local rundir + local spooldir + local varlibdir + + local options + + config_load $NAME -reload() { - SERVICE_PID_FILE="/var/run/asterisk/asterisk.pid" \ - service_reload $BIN_FILE + config_get_bool enabled general enabled 0 + if [ $enabled -eq 0 ]; then + $LOGGER service not enabled in /etc/config/$NAME + exit 1 + fi + + config_get user general user $NAME + config_get group general group $NAME + + user_exists "$user" || { + $LOGGER user \""$user"\" does not exist + exit 1 + } + group_exists "$group" || { + $LOGGER group \""$group"\" does not exist + exit 1 + } + + if [ "$user" = $NAME ]; then + if ! id -nG $NAME | grep -qwF dialout; then + group_exists dialout && group_add_user dialout $NAME + fi + fi + + config_get_bool log_stderr general log_stderr 1 + config_get_bool log_stdout general log_stdout 1 + + config_get agidir directories agidir /usr/share/$NAME/agi-bin + config_get datadir directories datadir /usr/share/$NAME + config_get dbdir directories dbdir /var/lib/$NAME/astdb + config_get keydir directories keydir /usr/share/$NAME/keys + config_get logdir directories logdir /var/log/$NAME + config_get rundir directories rundir /var/run/$NAME + config_get spooldir directories spooldir /var/spool/$NAME + config_get varlibdir directories varlibdir /var/lib/$NAME + + config_get options general options + + cdrcsvdir="${logdir}/cdr-csv" + + # do not touch directories that already exist + # posix shell does not support arrays, hence using awk + awk \ + -v user="$user" \ + -v group="$group" \ + -v a="$agidir" \ + -v b="$cdrcsvdir" \ + -v c="$datadir" \ + -v d="$dbdir" \ + -v e="$keydir" \ + -v f="$logdir" \ + -v g="$rundir" \ + -v h="$spooldir" \ + -v i="$varlibdir" \ + ' + BEGIN { + dir[0]=a + dir[1]=b + dir[2]=c + dir[3]=d + dir[4]=e + dir[5]=f + dir[6]=g + dir[7]=h + dir[8]=i + for (x in dir) { + if (system("test ! -e \"" dir[x] "\"" )) { + delete dir[x] + } + } + for (x in dir) { + system("mkdir -p \"" dir[x] "\"" ) + system("chmod 750 \"" dir[x] "\"" ) + system("chown \"" user "\":\"" group "\" \"" dir[x] "\"" ) + } + } + ' + + chown -R "$user":"$group" /etc/$NAME + + procd_open_instance + procd_set_param command $COMMAND + procd_append_param command \ + -G "$group" \ + -U "$user" \ + $options \ + -f + # forward stderr to logd + procd_set_param stderr $log_stderr + # same for stdout + procd_set_param stdout $log_stdout + procd_close_instance } + -- 2.30.2