1 | #!/bin/sh |
---|
2 | # |
---|
3 | # Copyright (c) 2004-2007 - Consultas, PKG.fr |
---|
4 | # |
---|
5 | # This file is part of A2P. |
---|
6 | # |
---|
7 | # A2P is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # A2P is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with A2P; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | # |
---|
21 | # $Id: a2p.sh 3 2007-10-18 16:20:19Z guillaume $ |
---|
22 | # |
---|
23 | # a2p: This is the init script for starting up a2p.pl |
---|
24 | # |
---|
25 | # chkconfig: 345 99 10 |
---|
26 | # description: Starts and stops afp2print backend daemon |
---|
27 | # that handles threads to process AFPSPOOL |
---|
28 | # processname: a2p |
---|
29 | # pidfile: /var/run/a2p.pid |
---|
30 | |
---|
31 | # Source function library. |
---|
32 | . /etc/init.d/functions |
---|
33 | |
---|
34 | # A2P_SYS_CONF must be a full pathname, daemon need at least this variable |
---|
35 | # to know which file to read when reloading conf |
---|
36 | A2P_SYS_CONF="/etc/afp2print/afpds2tex.conf" |
---|
37 | |
---|
38 | # See how we were called. |
---|
39 | |
---|
40 | # Strip service name from Sxx/Kxx if started during boot or during shutdown |
---|
41 | prog="`basename $0|sed -re 's/^S[0-9]{2}//' -e 's/^K[0-9]{2}//'`" |
---|
42 | pid="`pidof $prog`" |
---|
43 | |
---|
44 | # Delete PID file if service is not running |
---|
45 | [ -z "$pid" -a -s /var/run/$prog.pid ] && rm -f /var/run/$prog.pid |
---|
46 | |
---|
47 | # System configuration integration if not integrated in environment |
---|
48 | if [ -e "$A2P_SYS_CONF" -a -z "$AFPSPOOL" ]; then |
---|
49 | . "$A2P_SYS_CONF" |
---|
50 | fi |
---|
51 | |
---|
52 | # Optional conf integration |
---|
53 | if [ -n "$prog" -a -e "/etc/afp2print/${prog}.conf" ]; then |
---|
54 | . "/etc/afp2print/${prog}.conf" |
---|
55 | fi |
---|
56 | |
---|
57 | [ -z "$LOCKID" ] && LOCKID="`hostname -s|tr 'a-z' 'A-Z'`" |
---|
58 | export LOCKID |
---|
59 | |
---|
60 | # Perl library path options to help service to found its needed modules |
---|
61 | PERLOPT="" |
---|
62 | [ -d "$AFP2PRINT_PATH/A2P" ] && PERLOPT="$PERLOPT -I$AFP2PRINT_PATH" |
---|
63 | [ -d "$AFP2PRINT_PATH/printlib/perllib" ] && PERLOPT="$PERLOPT -I$AFP2PRINT_PATH/printlib" |
---|
64 | [ -d "$PRINTLIB_PATH/perllib" ] && PERLOPT="$PERLOPT -I$PRINTLIB_PATH" |
---|
65 | |
---|
66 | # Prepare Perl binary environment |
---|
67 | # Also unset JAVA_HOME in case it is in our ENV as it should exist only during |
---|
68 | # launch by Eclipse IDE |
---|
69 | PERLENV="unset JAVA_HOME ; LANG=C LOCKID=\"$LOCKID\" A2P_SYS_CONF=\"$A2P_SYS_CONF\"" |
---|
70 | |
---|
71 | # These defaults can be overided in confs |
---|
72 | : ${AFP2PRINT_USER:=afp2print} |
---|
73 | : ${SERVICE_PROG:=$AFP2PRINT_PATH/a2p.pl} |
---|
74 | : ${SERVICE_DEBUG:=0} |
---|
75 | : ${PERL:=/usr/bin/perl} |
---|
76 | : ${NICE:=nice --5} |
---|
77 | : ${LOGFILENAME:=/var/log/a2p.debug} |
---|
78 | |
---|
79 | start() { |
---|
80 | # For SELinux we need to use 'runuser' not 'su' |
---|
81 | SU=su |
---|
82 | if [ -s /etc/redhat-release ]; then |
---|
83 | if ! fgrep -q "Red Hat Enterprise Linux ES release 3" /etc/redhat-release; then |
---|
84 | [ -x /sbin/runuser ] && SU=runuser |
---|
85 | fi |
---|
86 | fi |
---|
87 | |
---|
88 | echo -n "Starting $prog: " |
---|
89 | if [ -e /var/lock/subsys/$prog ]; then |
---|
90 | if [ -s /var/run/$prog.pid ] && [ -e /proc/`< /var/run/$prog.pid` ]; then |
---|
91 | echo -n "cannot start $prog: $prog is already running."; |
---|
92 | failure "cannot start $prog: $prog already running."; |
---|
93 | echo |
---|
94 | return 1 |
---|
95 | fi |
---|
96 | fi |
---|
97 | |
---|
98 | # Prepare pid file |
---|
99 | touch /var/run/${prog}.pid |
---|
100 | chown $AFP2PRINT_USER /var/run/${prog}.pid |
---|
101 | chmod 664 /var/run/${prog}.pid |
---|
102 | |
---|
103 | # Check user will be able to output in LOGFILENAME |
---|
104 | if (( DEBUG_IN_FILE > 0 || LOGFILE_VS_SYSLOG > 0 )); then |
---|
105 | touch $LOGFILENAME |
---|
106 | chown $AFP2PRINT_USER $LOGFILENAME |
---|
107 | chmod 664 $LOGFILENAME |
---|
108 | fi |
---|
109 | |
---|
110 | # Touch the service debug file in case we need to keep some information |
---|
111 | # Usefull when debugging Perl compilation problems |
---|
112 | if (( SERVICE_DEBUG > 0 )); then |
---|
113 | touch /var/run/${prog}.debug |
---|
114 | chown $AFP2PRINT_USER /var/run/${prog}.debug |
---|
115 | if [ "$1" == "debug" ]; then |
---|
116 | PERLOPT="$PERLOPT -dt" |
---|
117 | echo -n "" >/var/run/${prog}.trace |
---|
118 | chown $AFP2PRINT_USER /var/run/${prog}.trace |
---|
119 | chmod 664 /var/run/${prog}.trace |
---|
120 | PERLENV="$PERLENV PERLDB_OPTS=\"NonStop=1 LineInfo=/var/run/${prog}.trace frame=4 dieLevel=2\"" |
---|
121 | fi |
---|
122 | else |
---|
123 | rm -f /var/run/$prog.debug |
---|
124 | fi |
---|
125 | |
---|
126 | # Start service |
---|
127 | $NICE $SU - $AFP2PRINT_USER -c "$PERLENV $PERL $PERLOPT $SERVICE_PROG /var/run/$prog.pid" |
---|
128 | RETVAL=$? |
---|
129 | [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog && success $prog; |
---|
130 | echo |
---|
131 | while [ ! -s /var/run/$prog.pid -a $RETVAL -eq 0 ] |
---|
132 | do |
---|
133 | usleep 100000 |
---|
134 | done |
---|
135 | if [ -n "$1" -a -x "/usr/bin/$1" -a -s /var/run/$prog.pid ]; then |
---|
136 | pid=`</var/run/$prog.pid` |
---|
137 | if [ "$1" == "strace" ]; then |
---|
138 | strace -T -a 120 -s 64 -tt -p $pid -f -q -o /var/log/a2p.strace& |
---|
139 | elif [ "$1" == "ltrace" ]; then |
---|
140 | ltrace -C -f -T -s 64 -tt -p $pid -o /var/log/a2p.ltrace& |
---|
141 | fi |
---|
142 | fi |
---|
143 | return $RETVAL |
---|
144 | } |
---|
145 | |
---|
146 | stop() { |
---|
147 | echo -n "Stopping $prog: " |
---|
148 | if [ ! -e /var/lock/subsys/$prog ]; then |
---|
149 | echo -n "cannot stop $prog: $prog is not running." |
---|
150 | failure "cannot stop $prog: $prog is not running." |
---|
151 | echo |
---|
152 | return 1; |
---|
153 | fi |
---|
154 | |
---|
155 | let RETVAL=0 TOTALRETVAL=0 |
---|
156 | pid="`pidof $prog`" |
---|
157 | |
---|
158 | for p in $pid |
---|
159 | do |
---|
160 | # Send signal to process group |
---|
161 | kill -TERM $p |
---|
162 | RETVAL=$? |
---|
163 | let TOTALRETVAL+=RETVAL |
---|
164 | done |
---|
165 | |
---|
166 | [ $TOTALRETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && echo_success; |
---|
167 | # Wait max 3 minutes for the service to update its pid to 0 when really quitting |
---|
168 | let TIMEOUT=1800 |
---|
169 | while [ -s /var/run/$prog.pid -a $RETVAL -eq 0 ] |
---|
170 | do |
---|
171 | [ "`< /var/run/$prog.pid`" == "0" ] && break |
---|
172 | usleep 100000 |
---|
173 | (( --TIMEOUT == 0 )) && break |
---|
174 | done |
---|
175 | [ -s /var/run/$prog.pid ] && [ "`< /var/run/$prog.pid`" -gt 0 -a $RETVAL -eq 0 ] && RETVAL=1 |
---|
176 | echo |
---|
177 | return $RETVAL |
---|
178 | } |
---|
179 | |
---|
180 | abort() { |
---|
181 | pid="`pidof $prog`" |
---|
182 | echo -n "Aborting any $prog process: $pid" |
---|
183 | |
---|
184 | for p in $pid |
---|
185 | do |
---|
186 | # Send signal to process group |
---|
187 | kill -ABRT -$p |
---|
188 | done |
---|
189 | |
---|
190 | rm -f /var/lock/subsys/$prog |
---|
191 | echo_success |
---|
192 | echo |
---|
193 | return 0 |
---|
194 | } |
---|
195 | |
---|
196 | a2pkill() { |
---|
197 | pid="`pidof $prog`" |
---|
198 | echo -n "Killing any $prog process: $pid" |
---|
199 | |
---|
200 | for p in $pid |
---|
201 | do |
---|
202 | # Send signal to process group |
---|
203 | kill -KILL -$p |
---|
204 | done |
---|
205 | |
---|
206 | rm -f /var/lock/subsys/$prog |
---|
207 | echo_success |
---|
208 | echo |
---|
209 | return 0 |
---|
210 | } |
---|
211 | |
---|
212 | check() { |
---|
213 | pid="`pidof $prog`" |
---|
214 | echo -n "Debugging any $prog process: $pid" |
---|
215 | |
---|
216 | for p in $pid |
---|
217 | do |
---|
218 | # Send signal to process group |
---|
219 | kill -USR1 -$p |
---|
220 | done |
---|
221 | |
---|
222 | sleep 1 |
---|
223 | |
---|
224 | echo_success |
---|
225 | echo |
---|
226 | for p in $pid |
---|
227 | do |
---|
228 | echo "Check for $p:" |
---|
229 | for file in /tmp/$prog-*$pid-self$1.log |
---|
230 | do |
---|
231 | if [ -s $file ]; then |
---|
232 | name=${file/*\/} |
---|
233 | name=${name/-$pid-self$1.log} |
---|
234 | echo "====================$name====================" |
---|
235 | cat $file |
---|
236 | fi |
---|
237 | done |
---|
238 | done |
---|
239 | rm -f /tmp/$prog-*$pid-self*.log |
---|
240 | return 0 |
---|
241 | } |
---|
242 | |
---|
243 | rhstatus() { |
---|
244 | status $prog |
---|
245 | } |
---|
246 | |
---|
247 | restart() { |
---|
248 | stop |
---|
249 | sleep 1 |
---|
250 | start |
---|
251 | } |
---|
252 | |
---|
253 | reload() { |
---|
254 | echo -n "Reloading $prog daemon configuration: " |
---|
255 | killproc $prog -HUP |
---|
256 | RETVAL=$? |
---|
257 | echo |
---|
258 | return $RETVAL |
---|
259 | } |
---|
260 | |
---|
261 | case "$1" in |
---|
262 | start) |
---|
263 | start $2 |
---|
264 | ;; |
---|
265 | stop) |
---|
266 | stop |
---|
267 | ;; |
---|
268 | abort) |
---|
269 | abort |
---|
270 | ;; |
---|
271 | kill) |
---|
272 | a2pkill |
---|
273 | ;; |
---|
274 | restart) |
---|
275 | restart |
---|
276 | ;; |
---|
277 | reload) |
---|
278 | reload |
---|
279 | ;; |
---|
280 | status) |
---|
281 | rhstatus |
---|
282 | ;; |
---|
283 | test) |
---|
284 | check "test" |
---|
285 | ;; |
---|
286 | debug) |
---|
287 | check "debug" |
---|
288 | ;; |
---|
289 | check) |
---|
290 | check $1 |
---|
291 | ;; |
---|
292 | ask) |
---|
293 | shift |
---|
294 | exec $PERL $PERLOPT -we ' |
---|
295 | BEGIN { use A2P::Globals; our $Progname = $ARGV[0] } |
---|
296 | use A2P::Client; |
---|
297 | print "\nTesting: " . join(", ",@ARGV) . "\n" ; |
---|
298 | &DoJob( $ARGV[1] => $ARGV[2] )' "$prog-ask" $prog "$1" |
---|
299 | ;; |
---|
300 | condrestart) |
---|
301 | [ -f /var/lock/subsys/$prog ] && restart || : |
---|
302 | ;; |
---|
303 | *) |
---|
304 | echo "Usage: ${prog} {start|stop|abort|ask|kill|status|reload|restart|condrestart|test|debug}" |
---|
305 | exit 1 |
---|
306 | esac |
---|
307 | |
---|
308 | # Check debug output |
---|
309 | if [ -s /var/run/${prog}.debug ]; then |
---|
310 | echo "Debug output for $prog service:" |
---|
311 | cat /var/run/${prog}.debug |
---|
312 | [ "$1" == "stop" -o "$1" == "abort" -o "$1" == "kill" ] && rm -f /var/run/${prog}.debug |
---|
313 | fi |
---|