packages/smtptrapd: use new service functions
[openwrt/svn-archive/archive.git] / net / smtptrapd / files / smtptrapd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 OpenWrt.org
3
4 SERVICE_USE_PID=1
5
6 START=90
7 STOP=10
8
9 EXTRA_COMMANDS="list status exconf"
10 EXTRA_HELP=" list Lists available configurations
11 status Prints status of the service
12 exconf Shows an example config file
13
14 The actions start, stop, restart, reload, status operate
15 globally on all configurations unless the particular
16 configuration has been provided as the next parameter.
17 "
18
19 check_section() {
20 echo "$1" | grep -vq '^cfg[[:xdigit:]]\{6\}$'
21 }
22
23 start_instance() {
24 local section="$1"
25 local instance="$2"
26
27 check_section "$section" || return 1
28 [ -n "$instance" -a "$section" != "$instance" ] && return 0
29
30 local args listen_ip banner_host username num_threads listen_port accept_queue_len
31
32 config_get listen_ip "$section" 'listen_ip'
33 [ -n "$listen_ip" ] && append args "-l $listen_ip"
34
35 config_get listen_port "$section" 'listen_port'
36 [ -n "$listen_port" ] && append args "-p $listen_port"
37
38 config_get username "$section" 'username'
39 [ -n "$username" ] && append args "-u $username"
40
41 config_get banner_host "$section" 'banner_host'
42 [ -n "$banner_host" ] && append args "-b '$banner_host'"
43
44 config_get num_threads "$section" 'num_threads'
45 [ -n "$num_threads" ] && append args "-t $num_threads"
46
47 config_get accept_queue_len "$section" 'accept_queue_len'
48 [ -n "$accept_queue_len" ] && append args "-m $accept_queue_len"
49
50 SERVICE_PID_FILE="/var/run/smtptrapd-${section}.pid
51 service_start /usr/sbin/smtptrapd -f $SERVICE_PID_FILE $args
52 }
53
54 stop_instance() {
55 local section="$1"
56 local instance="$2"
57
58 check_section "$section" || return 1
59 [ -n "$instance" -a "$section" != "$instance" ] && return 0
60
61 SERVICE_PID_FILE="/var/run/smtptrapd-${section}.pid"
62 service_stop /usr/sbin/smtptrapd
63 }
64
65 check_instance() {
66 local section="$1"
67 local instance="$2"
68
69 check_section "$section" || return 1
70 [ -n "$instance" -a "$section" != "$instance" ] && return 0
71
72 SERVICE_PID_FILE="/var/run/smtptrapd-${section}.pid"
73 service_check /usr/sbin/smtptrapd && {
74 echo "${initscript}: configuration instance '$section' is running"
75 } || {
76 echo "${initscript}: configuration instance '$section' is not running"
77 }
78 }
79
80 list_instance() {
81 local section="$1"
82 check_section "$section" || return 1
83 echo " $section"
84 }
85
86 start() {
87 config_load 'smtptrapd'
88 config_foreach start_instance 'smtptrapd' "$@"
89 }
90
91 stop() {
92 config_load 'smtptrapd'
93 config_foreach stop_instance 'smtptrapd' "$@"
94 }
95
96 status() {
97 config_load 'smtptrapd'
98 config_foreach check_instance 'smtptrapd' "$@"
99 }
100
101 list() {
102 config_load 'smtptrapd'
103 echo "${initscript}: available configuration instances:"
104 config_foreach list_instance 'smtptrapd'
105 }
106
107 exconf() {
108 cat <<EOF
109 # /etc/config/smtptrapd - example configuration
110
111 package 'smtptrapd'
112
113 config 'smtptrapd' 'myfailhost'
114 option 'num_threads' '1'
115
116 # The init script operates only with named sections
117 # All options (default values)
118 # option 'banner_host' '<hostname>'
119 # option 'username' 'nobody'
120 # option 'listen_ip' '<all addresses>'
121 # option 'listen_port' '25'
122 # option 'num_threads' '10'
123 # option 'accept_queue_len' '100'
124 EOF
125 }