Imported Upstream version 2.5.1
[debian/amanda] / server-src / amcleanup.sh.in
1 #!@SHELL@
2 #
3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 University of Maryland at College Park
5 # All Rights Reserved.
6 #
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation, and that the name of U.M. not be used in advertising or
12 # publicity pertaining to distribution of the software without specific,
13 # written prior permission.  U.M. makes no representations about the
14 # suitability of this software for any purpose.  It is provided "as is"
15 # without express or implied warranty.
16 #
17 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #
24 # Author: James da Silva, Systems Design and Analysis Group
25 #                          Computer Science Department
26 #                          University of Maryland at College Park
27 #
28
29 #
30 # amcleanup.sh - clean up and generate a report after a crash.
31
32 # try to hit all the possibilities here
33 prefix=@prefix@
34 exec_prefix=@exec_prefix@
35 libexecdir=@libexecdir@
36 sbindir=@sbindir@
37
38 confdir=@CONFIG_DIR@
39
40 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
41 export PATH
42
43 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
44 if test "$USE_VERSION_SUFFIXES" = yes; then
45         SUF=-@VERSION@
46 else
47         SUF=
48 fi
49
50 if test -h /proc/1/exe ; then
51         if test $# -eq 1 ; then
52                 KILL_ENABLE=0
53                 conf=$1
54                 shift;
55         elif test $# -eq 2 && test "$1" == "-k" ; then
56                 KILL_ENABLE=1
57                 conf=$2
58                 shift
59                 shift
60         else
61                 echo "Usage: amcleanup [-k] conf"
62                 exit 1
63         fi
64 else
65         if test $# -ne 1 ; then
66                 echo "Usage: amcleanup conf"
67                 exit 1
68         else
69                 conf=$1
70                 KILL_ENABLE=0
71         fi
72 fi
73
74 if test ! -d $confdir/$conf ; then
75         echo "amcleanup: could not cd into $confdir/$conf"
76         exit 1
77 fi
78
79 cd $confdir/$conf
80
81 logdir=`amgetconf$SUF $conf logdir "$@"`
82 rc=$?
83 if test $rc -ne 0 ; then
84         echo "amcleanup: 'amgetconf$SUF logdir' exited with status: $rc" 1>&2
85         exit 1
86 fi
87 logfile=$logdir/log
88 errfile=$logdir/amdump
89 erramflush=$logdir/amflush
90 tapecycle=`amgetconf$SUF $conf tapecycle "$@"`
91 rc=$?
92 if test $rc -ne 0 ; then
93         echo "amcleanup: 'amgetconf$SUF tapecycle' exited with status: $rc " 1>&2
94         exit 1
95 fi
96 dumpuser=`amgetconf$SUF $conf dumpuser "$@"`
97 rc=$?
98 if test $rc -ne 0 ; then
99         echo "amcleanup: 'amgetconf$SUF $conf dumpuser' exited with status: $rc" 1>&2
100         exit 1
101 fi
102
103 # Check for running processes which should not be
104 # running right now.
105 if test ${KILL_ENABLE} -eq 1 ; then
106         if test -h /proc/1/exe ; then
107                 USER_PROCESS_NAMES="\
108                         @libexecdir@/amandad \
109                         @libexecdir@/amcleanupdisk \
110                         @libexecdir@/amidxtaped \
111                         @libexecdir@/amindexd \
112                         @libexecdir@/amlogroll \
113                         @libexecdir@/amtrmidx \
114                         @libexecdir@/amtrmlog \
115                         @libexecdir@/chg-chio \
116                         @libexecdir@/chg-chs \
117                         @libexecdir@/chg-disk \
118                         @libexecdir@/chg-iomega \
119                         @libexecdir@/chg-juke \
120                         @libexecdir@/chg-manual \
121                         @libexecdir@/chg-mcutil \
122                         @libexecdir@/chg-mtx \
123                         @libexecdir@/chg-multi \
124                         @libexecdir@/chg-null \
125                         @libexecdir@/chg-rait \
126                         @libexecdir@/chg-rth \
127                         @libexecdir@/chg-scsi \
128                         @libexecdir@/chg-zd-mtx \
129                         @libexecdir@/chunker \
130                         @libexecdir@/driver \
131                         @libexecdir@/generic-dumper \
132                         @libexecdir@/gnutar \
133                         @libexecdir@/noop \
134                         @libexecdir@/patch-system \
135                         @libexecdir@/selfcheck \
136                         @libexecdir@/sendbackup \
137                         @libexecdir@/sendsize \
138                         @libexecdir@/star \
139                         @libexecdir@/taper \
140                         @libexecdir@/versionsuffix \
141                         @sbindir@/amaddclient \
142                         @sbindir@/amadmin \
143                         @sbindir@/amaespipe \
144                         @sbindir@/amcheckdb \
145                         @sbindir@/amcrypt \
146                         @sbindir@/amcryptsimple \
147                         @sbindir@/amdd \
148                         @sbindir@/amdump \
149                         @sbindir@/amfetchdump \
150                         @sbindir@/amflush \
151                         @sbindir@/amgetconf \
152                         @sbindir@/amgpgcrypt \
153                         @sbindir@/amlabel \
154                         @sbindir@/ammt \
155                         @sbindir@/amoverview \
156                         @sbindir@/amplot \
157                         @sbindir@/amrecover \
158                         @sbindir@/amreport \
159                         @sbindir@/amrestore \
160                         @sbindir@/amrmtape \
161                         @sbindir@/amserverconfig \
162                         @sbindir@/amstatus \
163                         @sbindir@/amtape \
164                         @sbindir@/amtapetype \
165                         @sbindir@/amtoc \
166                         @sbindir@/amverify \
167                         @sbindir@/amverifyrun"
168
169                 ROOT_PROCESS_NAMES="\
170                         @libexecdir@/calcsize \
171                         @libexecdir@/killpgrp \
172                         @libexecdir@/rundump \
173                         @libexecdir@/runtar \
174                         @libexecdir@/dumper \
175                         @libexecdir@/planner \
176                         @sbindir@/amcheck"
177
178                 PREVIOUS_DIR="`pwd`"
179                 cd /proc
180                 PIDS_FOUND=0
181                 KEEP_CHECKING=1
182                 while test ${KEEP_CHECKING} -ne 0 ; do
183                         PIDS_THIS_PASS=0
184                         for search_user in ${dumpuser} root ; do
185                                 if test "${search_user}" == "${dumpuser}" ; then
186                                         PROCESS_NAMES=${USER_PROCESS_NAMES}
187                                 elif test "${search_user}" == "root" ; then
188                                         PROCESS_NAMES=${ROOT_PROCESS_NAMES}
189                                 fi
190                                 for search_pid in [0-9]* ; do
191                                         for search_name in ${PROCESS_NAMES} ; do
192                                                 ls -l /proc/${search_pid}/exe 2>/dev/null | grep ${search_name} >/dev/null
193                                                 match_name=$?
194                                                 pid_uid="`cat /proc/${search_pid}/status 2>/dev/null | grep Uid | awk '//{split($_,i); print i[2]}'`"
195                                                 if test ${match_name} -eq 0  && test "${pid_uid}" == "${search_user}" ; then
196                                                         echo "amcleanup: Process ${search_name} found running at pid #${search_pid}."
197                                                         kill_pid=${search_pid}
198                                                         kill_name=${search_name}
199                                                         PIDS_FOUND=`expr ${PIDS} + 1`
200                                                         PIDS_THIS_PASS=1
201                                                         break
202                                                 else
203                                                         kill_pid=""
204                                                         continue
205                                                 fi
206                                         done
207                                         if test ! -z "${kill_pid}" ; then
208                                                 if test -d /proc/${kill_pid} ; then
209                                                         echo "amcleanup: Sending process ${kill_pid} the TERM signal."
210                                                         kill -15 -- ${kill_pid}
211                                                         sleep 5
212                                                         if test -d /proc/${kill_pid} ; then
213                                                                 echo "amcleanup: Sending process  ${kill_pid} the KILL signal."
214                                                                 kill -9 -- ${kill_pid}
215                                                         fi
216                                                         sleep 5
217                                                         if test -d /proc/${kill_pid} ; then
218                                                                 echo "amcleanup: Process ${kill_pid} did not respond to the KILL signal (and may be hung)!" 1>&2
219                                                                 KILL_FAILURES=`expr ${KILL_FAILURES} + 1`
220                                                         fi
221                                                 else
222                                                         echo "amcleanup: Process ${kill_pid} no longer running.  Skipping..."
223                                                 fi
224                                         fi
225                                 done
226                         done
227                         if test ${PIDS_THIS_PASS} -eq 0 ; then
228                                 KEEP_CHECKING=0
229                         else
230                                 KEEP_CHECKING=1
231                         fi
232                 done
233                 if test ${PIDS_FOUND} -gt 0 ; then
234                         echo "amcleanup: ${PIDS_FOUND} Amanda processes were found running."
235                         echo "amcleanup: ${KILL_FAILURES} processes failed to terminate."
236                 else
237                         echo "amcleanup: No Amanda processes were found running."
238                 fi
239                 cd "${PREVIOUS_DIR}"
240         fi
241 fi
242
243 retstatus=0
244 if test -f $logfile ; then
245         echo "amcleanup: processing outstanding log file."
246         exec </dev/null >/dev/null 2>&1
247         amreport$SUF $conf "$@"
248         rc=$?
249         if test $rc -ne 0 ; then
250                 echo "amcleanup: amreport exited with status: $rc" 1>&2
251                 retstatus=`expr $retstatus + 1`
252         fi
253
254         # Roll the log file to its datestamped name.
255         amlogroll$SUF $conf "$@"
256         rc=$?
257         if test $rc -ne 0 ; then
258                 echo "acmleanup: amlogroll exited with status: $rc" 1>&2
259                 retstatus=`expr $retstatus + 2`
260         fi
261
262         # Trim the index file to those for dumps that still exist.
263         amtrmidx$SUF $conf "$@"
264         rc=$?
265         if test $rc -ne 0 ; then
266                 echo "amcleanup: amtrmidx exited with status: $rc" 1>&2
267                 retstatus=`expr $retstatus + 4`
268         fi
269
270 else
271         echo "amcleanup: no unprocessed logfile to clean up."
272 fi
273
274 if test -f $errfile ; then
275     # if log was found, this will have been directed to /dev/null,
276     # which is fine.
277     echo "amcleanup: $errfile exists, renaming it."
278
279     # Keep debug log through the tapecycle plus a couple days
280     maxdays=`expr $tapecycle + 2`
281
282     days=1
283     # First, find out the last existing errfile,
284     # to avoid ``infinite'' loops if tapecycle is infinite
285     while test $days -lt $maxdays  && test -f $errfile.$days ; do
286         days=`expr $days + 1`
287     done
288     # Now, renumber the existing log files
289     while test $days -ge 2 ; do
290         ndays=`expr $days - 1`
291         mv $errfile.$ndays $errfile.$days
292         days=$ndays
293     done
294     mv $errfile $errfile.1
295 fi
296
297 if test -f $erramflush ; then
298     # if log was found, this will have been directed to /dev/null,
299     # which is fine.
300     echo "amcleanup: $erramflush exists, renaming it."
301
302     # Keep debug log through the tapecycle plus a couple days
303     maxdays=`expr $tapecycle + 2`
304
305     days=1
306     # First, find out the last existing erramflush,
307     # to avoid ``infinite'' loops if tapecycle is infinite
308     while test $days -lt $maxdays  && test -f $erramflush.$days ; do
309         days=`expr $days + 1`
310     done
311     # Now, renumber the existing log files
312     while test $days -ge 2 ; do
313         ndays=`expr $days - 1`
314         mv $erramflush.$ndays $erramflush.$days
315         days=$ndays
316     done
317     mv $erramflush $erramflush.1
318 fi
319
320 $libexecdir/amcleanupdisk $conf "$@"
321 rc=$?
322 if test $rc -ne 0 ; then
323         echo "amcleanup: amcleanupdisk exited with status: $rc" 1>&2
324         retstatus=`expr $retstatus + 8`
325 fi
326
327 exit $retstatus