fwknop: init script improvements
[feed/packages.git] / net / darkstat / files / darkstat.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2018 Jean-Michel Lacroix
3
4 USE_PROCD=1
5
6 START=60
7
8 APP=darkstat
9 RUN_D=/var/empty
10 PID_F=$RUN_D/$APP.pid
11 CONFIGNAME=darkstat
12 USER=nobody
13 GROUP=nogroup
14
15 CONFIGSTR=""
16 FILTERSTR=""
17
18 export_bool () {
19 local option="$1"
20 local section="$2"
21 local _keystr="$3"
22 local _loctmp
23 config_get_bool _loctmp "$section" "$option"
24 if [ -n "$_loctmp" ]; then
25 if [ 1 -eq "$_loctmp" ]; then
26 CONFIGSTR="$CONFIGSTR${_keystr} "
27 fi
28 fi
29 }
30
31 set_config_string(){
32 mkdir -p $RUN_D
33 chown $USER:$GROUP $RUN_D
34 . /lib/functions/network.sh
35 config_load $CONFIGNAME
36 config_foreach build_config_string darkstat
37 }
38
39 build_config_string() {
40 local cfg="$1"
41 config_get interface $cfg interface
42 network_get_device ifname "$interface"
43 CONFIGSTR=" -i $ifname "
44 export_bool syslog $cfg "--syslog"
45 export_bool verbose $cfg "--verbose"
46 # We need the --no-daemon parameter as with PROCD the process has to run in the background
47 CONFIGSTR="$CONFIGSTR--no-daemon "
48 export_bool no_promisc $cfg "--no-promisc"
49 export_bool no_dns $cfg "--no-dns"
50 export_bool no_macs $cfg "--no-macs"
51 export_bool no_lastseen $cfg "--no-lastseen"
52 config_get httpaddr $cfg httpaddr
53 CONFIGSTR="$CONFIGSTR${httpaddr:+-b "$httpaddr"} "
54 config_get httpport $cfg httpport
55 CONFIGSTR="$CONFIGSTR${httpport:+-p "$httpport"} "
56 config_get network_netmask $cfg network_netmask
57 CONFIGSTR="$CONFIGSTR${network_netmask:+-l "$network_netmask"} "
58 export_bool local_only $cfg "--local-only"
59 config_get hosts_max $cfg hosts_max
60 CONFIGSTR="$CONFIGSTR${hosts_max:+--hosts-max "$hosts_max"} "
61 config_get hosts_keep $cfg hosts_keep
62 CONFIGSTR="$CONFIGSTR${ports_keep:+--ports-keep "$ports_keep"} "
63 config_get highest_port $cfg highest_port
64 CONFIGSTR="$CONFIGSTR${highest_port:+--highest-port "$highest_port"} "
65 config_get export_file $cfg export_file
66 CONFIGSTR="$CONFIGSTR${export_file:+--export "$export_file"} "
67 config_get import_file $cfg import_file
68 CONFIGSTR="$CONFIGSTR${import_file:+--import "$import_file"} "
69 config_get daylog_file $cfg daylog_file
70 CONFIGSTR="$CONFIGSTR${daylog_file:+--daylog "$daylog_file"} "
71 CONFIGSTR="$CONFIGSTR--chroot $RUN_D --pidfile $PID_F"
72 # Now that we have the configuration string (CONFIGSTR), let us get the filter (FILTERSTR)
73 config_get network_filter $cfg network_filter
74 FILTERSTR="${network_filter:+$network_filter}"
75 }
76
77 service_triggers() {
78 procd_add_reload_trigger $CONFIGNAME
79 }
80
81 start_service() {
82 set_config_string
83 procd_open_instance
84 procd_set_param command /usr/sbin/$APP
85 procd_append_param command $CONFIGSTR
86 # Let us check if we have a filter string and apply it if we do
87 if [ "$FILTERSTR" != "" ]; then
88 procd_append_param command "-f" "$FILTERSTR"
89 fi
90 procd_close_instance
91 }
92
93 stop_service() {
94 rm -f $PID_F
95 }
96
97 reload_service() {
98 stop
99 start
100 }