/[polypkg]/trunk/pp.back.deb.svc
ViewVC logotype

Contents of /trunk/pp.back.deb.svc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show annotations)
Tue Feb 27 02:42:45 2007 UTC (2 years, 8 months ago) by dpovey
File size: 7183 byte(s)
- Add support for services. 
- Refactor some stuff so that we build with dpkg-deb rather than doing ar hackery
1 #@ pp_backend_deb_init_svc_vars(): initialises deb backend variables
2 pp_backend_deb_init_svc_vars () {
3 pp_deb_default_start_runlevels= # should be "3 4 5"??
4 pp_deb_default_svc_description="No description"
5 }
6
7 #@ pp_backend_deb_init_svc_vars(): initialise deb-specific service variables
8 # pp-provided vars are cmd, pidfile, stop_signal and user
9 pp_backend_deb_init_svc_vars () {
10
11 reload_signal=
12 start_runlevels=${pp_deb_default_start_runlevels} # == lsb default-start
13 stop_runlevels="0 1 2 6" # == lsb default-stop
14 svc_description="${pp_deb_default_svc_description}" # == lsb short descr
15 svc_process=
16
17 lsb_required_start='$local_fs $network'
18 lsb_should_start=
19 lsb_required_stop=
20 lsb_description=
21
22 start_priority=50
23 stop_priority=50 #-- stop_priority = 100 - start_priority
24 }
25
26 #@ pp_deb_service_make_init_script(svc): generates a service init script
27 # Assumes %service variables have been set before invocation
28 # Does nothing if an /etc/init.d/$svc script exists already.
29 # Otherwise creates an /etc/init.d/$svc script
30 pp_deb_service_make_init_script () {
31 typeset svc=$1
32 typeset script=/etc/init.d/$svc
33 typeset out=$pp_destdir$script
34 typeset _process _cmd
35
36 pp_add_file_if_missing $script run 755 || return 0
37
38 #-- start out as an empty shell script
39 cat <<-'.' >$out
40 #!/bin/sh
41 .
42
43 #-- determine the process name from $cmd unless $svc_process is given
44 set -- $cmd
45 #_process=${svc_process:-"$1"} --? WTF
46
47 #-- construct a start command that builds a pid file if needed
48 _cmd="$cmd";
49 _cmd_path=`echo $cmd | cut -d" " -f1`
50 _cmd_name=`basename $_cmd_path`
51 _cmd_args=`echo $cmd | cut -d" " -f2-`
52 test x"$_cmd_path" != x"$_cmd_args" || _cmd_args=
53
54 #-- generate the LSB init info
55 cat <<-. >>$out
56 ### BEGIN INIT INFO
57 # Provides: ${svc}
58 # Required-Start: ${lsb_required_start}
59 # Should-Start: ${lsb_should_start}
60 # Required-Stop: ${lsb_required_stop}
61 # Default-Start: ${start_runlevels}
62 # Default-Stop: ${stop_runlevels}
63 # Short-Description: ${svc_description}
64 ### END INIT INFO
65 # Generated by PolyPackage ${pp_version}
66 # ${copyright}
67
68 .
69
70 if test x"${svc_description}" = x"${pp_deb_default_svc_description}"; then
71 svc_description=
72 fi
73
74 #-- write service-specific definitions
75 cat <<. >>$out
76 #-- definitions specific to service ${svc}
77 NAME="${_cmd_name}"
78 DESC="${svc_description:-$svc service}"
79 USER="${user}"
80 GROUP="${group}"
81 PIDFILE="${pidfile}"
82 STOP_SIGNAL="${stop_signal}"
83 RELOAD_SIGNAL="${reload_signal}"
84 CMD="${_cmd}"
85 DAEMON="${_cmd_path}"
86 DAEMON_ARGS="${_cmd_args}"
87 SCRIPTNAME=${script}
88 .
89
90 #-- write the generic part of the init script
91 cat <<'.' >>$out
92
93 # Exit if the package is not installed
94 [ -x "$DAEMON" ] || exit 0
95
96 # Read configuration variable file if it is present
97 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
98
99 # Load the VERBOSE setting and other rcS variables
100 [ -f /etc/default/rcS ] && . /etc/default/rcS
101
102 # Define LSB log_* functions.
103 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
104 . /lib/lsb/init-functions
105
106 #
107 # Function that starts the daemon/service
108 #
109 do_start()
110 {
111 # Return
112 # 0 if daemon has been started
113 # 1 if daemon was already running
114 # 2 if daemon could not be started
115 if [ -n "$PIDFILE" ]
116 then
117 pidfile_opt="--pidfile $PIDFILE"
118 else
119 pidfile_opt="--make-pidfile --background --pidfile /var/run/$NAME.pid"
120 fi
121 if [ -n "$USER" ]
122 then
123 user_opt="--user $USER"
124 fi
125 if [ -n "$GROUP" ]
126 then
127 group_opt="--group $GROUP"
128 fi
129 if [ "$VERBOSE" = no ]
130 then
131 quiet_opt="--quiet"
132 else
133 quiet_opt="--verbose"
134 fi
135
136 start-stop-daemon --start $quiet_opt $pidfile_opt $user_opt --exec $DAEMON --test > /dev/null \
137 || return 1
138
139 # Note: there seems to be no way to tell whether the daemon will fork itself or not, so pass
140 # --background for now
141 start-stop-daemon --start $quiet_opt $pidfile_opt $user_opt --exec $DAEMON -- \
142 $DAEMON_ARGS \
143 || return 2
144 }
145
146 #
147 # Function that stops the daemon/service
148 #
149 do_stop()
150 {
151 # Return
152 # 0 if daemon has been stopped
153 # 1 if daemon was already stopped
154 # 2 if daemon could not be stopped
155 # other if a failure occurred
156 if [ -n "$PIDFILE" ]
157 then
158 pidfile_opt="--pidfile $PIDFILE"
159 else
160 pidfile_opt="--pidfile /var/run/$NAME.pid"
161 fi
162 if [ -n "$USER" ]
163 then
164 user_opt="--user $USER"
165 fi
166 if [ -n $STOP_SIGNAL ]
167 then
168 signal_opt="--signal $STOP_SIGNAL"
169 fi
170 if [ "$VERBOSE" = "no" ]
171 then
172 quiet_opt="--quiet"
173 else
174 quiet_opt="--verbose"
175 fi
176 start-stop-daemon --stop $quiet_opt $signal_opt --retry=TERM/30/KILL/5 $pidfile_opt --name $NAME
177 RETVAL="$?"
178 [ "$RETVAL" = 2 ] && return 2
179 # Wait for children to finish too if this is a daemon that forks
180 # and if the daemon is only ever run from this initscript.
181 # If the above conditions are not satisfied then add some other code
182 # that waits for the process to drop all resources that could be
183 # needed by services started subsequently. A last resort is to
184 # sleep for some time.
185 start-stop-daemon --stop $quiet_opt --oknodo --retry=0/30/KILL/5 --exec $DAEMON
186 [ "$?" = 2 ] && return 2
187 # Many daemons don't delete their pidfiles when they exit.
188 test -z $PIDFILE || rm -f $PIDFILE
189 return "$RETVAL"
190 }
191
192 #
193 # Function that sends a restart signal to the daemon/service
194 #
195 do_reload() {
196 #
197 # If the daemon can reload its configuration without
198 # restarting (for example, when it is sent a SIGHUP),
199 # then implement that here.
200 #
201 if [ -n "$PIDFILE" ]
202 then
203 pidfile_opt="--pidfile $PIDFILE"
204 else
205 pidfile_opt="--pidfile /var/run/$NAME.pid"
206 fi
207 if [ -n "$RELOAD_SIGNAL" ]
208 then
209 start-stop-daemon --stop --signal $RELOAD_SIGNAL $quiet_opt $pidfile_opt --name $NAME
210 fi
211 return 0
212 }
213
214 case "$1" in
215 start)
216 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
217 do_start
218 case "$?" in
219 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
220 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
221 esac
222 ;;
223 stop)
224 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
225 do_stop
226 case "$?" in
227 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
228 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
229 esac
230 ;;
231 reload|force-reload)
232 if [ -n "$RELOAD_SIGNAL" ]
233 then
234 log_daemon_msg "Reloading $DESC" "$NAME"
235 do_reload
236 log_end_msg $?
237 else
238 # Do a restart instead
239 "$0" restart
240 fi
241 ;;
242 restart)
243 #
244 # If the "reload" option is implemented then remove the
245 # 'force-reload' alias
246 #
247 log_daemon_msg "Restarting $DESC" "$NAME"
248 do_stop
249 case "$?" in
250 0|1)
251 do_start
252 case "$?" in
253 0) log_end_msg 0 ;;
254 1) log_end_msg 1 ;; # Old process is still running
255 *) log_end_msg 1 ;; # Failed to start
256 esac
257 ;;
258 *)
259 # Failed to stop
260 log_end_msg 1
261 ;;
262 esac
263 ;;
264 *)
265 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
266 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
267 exit 3
268 ;;
269 esac
270
271 :
272 .
273 chmod 755 $out
274 }

Ted.Percival@quest.com
ViewVC Help
Powered by ViewVC 1.1.2