lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

mosquitto (4364B)


      1 #!/bin/sh /etc/rc.common
      2 # Basic init script for mosquitto
      3 # April 2012, OpenWrt.org
      4 # Provides support for the luci-app-mosquitto package, if installed
      5 
      6 START=80
      7 USE_PROCD=1
      8 TCONF=/tmp/mosquitto.generated.conf
      9 
     10 # Usage: append_if cfg uci_name output_name
     11 # add a config line of the form "output_name <value>"
     12 # if the "uci_name" was found.
     13 # output_name defaults to uci_name if not specified.
     14 append_if() {
     15     local cfg="$1"
     16     local uci_name="$2"
     17     local out_name="$3"
     18     if [ -z "$out_name" ]; then
     19         out_name=$uci_name
     20     fi
     21     config_get val $cfg $uci_name
     22     if [ -n "$val" ]; then
     23         echo "$out_name $val" >> $TCONF
     24     fi
     25 }
     26 
     27 # mosquitto uses true/false, uci uses 1/0
     28 # note that this is not shell truthy, but equality with 1!
     29 append_bool() {
     30     if [ $2 -eq 1 ]; then
     31         echo "$1 true" >> $TCONF
     32     else
     33         echo "$1 false" >> $TCONF
     34     fi
     35 }
     36 
     37 # as per append_if, but gets the value as a uci bool, not raw
     38 append_optional_bool() {
     39     local cfg="$1"
     40     local uci_name="$2"
     41     local out_name="$3"
     42     config_get val $cfg $uci_name
     43     if [ -n "$val" ]; then
     44         config_get_bool real $cfg $uci_name
     45         append_bool $out_name $real
     46     fi
     47 }
     48 
     49 
     50 convert_mosq_general() {
     51 	local cfg="$1"
     52 	config_get destinations "$1" log_dest
     53 	for dest in $destinations; do
     54 		echo "log_dest $dest" >> $TCONF
     55 	done
     56 
     57 	config_get_bool no_remote "$1" no_remote_access 0
     58 	if [ "$no_remote" -eq 1 ]; then
     59 		echo "bind_address 127.0.0.1" >> $TCONF
     60 	fi
     61 
     62 	config_get port "$1" port 1883
     63 	echo "port $port" >> $TCONF
     64 	append_if "$1" protocol
     65 	append_if "$1" max_inflight_messages
     66 	append_if "$1" max_queued_messages
     67 }
     68 
     69 convert_persistence() {
     70 	local cfg="$1"
     71 
     72 	append_if "$cfg" client_expiration persistent_client_expiration
     73 	append_if "$cfg" autosave_interval
     74 	append_optional_bool "$cfg" autosave_on_changes autosave_on_changes
     75 	append_optional_bool "$cfg" persistence persistence
     76 	append_if "$cfg" file persistence_file
     77 	config_get loc "$cfg" location
     78 	if [ -n "$loc" ]; then
     79 		[ -d "$loc" ] || {
     80 			mkdir -p "$loc";
     81 			chown mosquitto "$loc";
     82 		}
     83 		echo "persistence_location $loc" >> $TCONF
     84 	fi
     85 }
     86 
     87 add_listener() {
     88     echo "" >> $TCONF
     89     config_get port "$1" port
     90     if [ -z "$port" ]; then
     91         echo "Ignoring listener section without port"
     92         return
     93     fi
     94     config_get_bool no_remote "$1" no_remote_access 0
     95     if [ "$no_remote" -eq 1 ]; then
     96         echo "listener $port 127.0.0.1" >> $TCONF
     97     else
     98         echo "listener $port" >> $TCONF
     99     fi
    100 
    101     append_if "$1" protocol
    102 }
    103 
    104 add_topic() {
    105     echo "topic $1" >> $TCONF
    106 }
    107 
    108 add_bridge() {
    109     config_get conn "$1" connection
    110     config_get addr "$1" address
    111     if [ -z "$conn" -o -z "$addr" ]; then
    112         echo "Ignoring bridge section, misisng connection/address"
    113         return
    114     fi
    115     echo "" >> $TCONF
    116     echo "# Bridge connection from UCI section" >> $TCONF
    117     append_if "$1" connection
    118     append_if "$1" address
    119 
    120     config_list_foreach "$1" topic add_topic
    121     append_optional_bool "$1" cleansession cleansession
    122     append_optional_bool "$1" try_private try_private
    123 
    124     append_if "$1" clientid
    125     append_if "$1" identity bridge_identity
    126     append_if "$1" psk bridge_psk
    127     append_if "$1" tls_version bridge_tls_version
    128 }
    129 
    130 
    131 convert_uci() {
    132 	rm -rf $TCONF
    133 	echo "Generating mosquitto config file in $TCONF"
    134 	echo "# mosquitto.conf file generated from UCI config." >>$TCONF
    135 	# Don't include a timestamp, it makes md5sum compares fail
    136 
    137 	config_load mosquitto
    138 	config_foreach convert_mosq_general "mosquitto"
    139 	config_foreach convert_persistence "persistence"
    140 	config_foreach add_listener "listener"
    141 	config_foreach add_bridge "bridge"
    142 }
    143 
    144 start_service_real() {
    145 	local cfg="$1"
    146 	local use_uci write_pid
    147 	config_get use_uci "$cfg" use_uci
    148 	if [ "$use_uci" -eq 1 ]; then
    149 		CONF=$TCONF
    150 		convert_uci
    151 	else
    152 		CONF=/etc/mosquitto/mosquitto.conf
    153         fi
    154 	config_get write_pid "$cfg" write_pid 0
    155 
    156 	procd_open_instance
    157 	procd_set_param command mosquitto
    158 	procd_append_param command -c $CONF
    159 	# Makes /etc/init.d/mosquitto reload work if you edit the final file.
    160 	procd_set_param file $CONF
    161 	[ "$write_pid" -eq 1 ] && procd_set_param pidfile /var/run/mosquitto.pid
    162 	procd_close_instance
    163 }
    164 
    165 start_service() {
    166 	config_load mosquitto
    167 	config_foreach start_service_real owrt
    168 }
    169 
    170 service_triggers() {
    171 	# Makes "reload_config" work
    172 	procd_add_reload_trigger "mosquitto"
    173 }