Imported Upstream version 2.5.1p3
[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 $# -ge 2 && test "$1" == "-k" ; then
52                 KILL_ENABLE=1
53                 conf=$2
54                 shift
55                 shift
56         elif test $# -ge 1 ; then
57                 KILL_ENABLE=0
58                 conf=$1
59                 shift;
60         else
61                 echo "Usage: amcleanup [-k] conf"
62                 exit 1
63         fi
64 else
65         if test $# -lt 1 ; then
66                 echo "Usage: amcleanup conf"
67                 exit 1
68         else
69                 conf=$1
70                 shift
71                 KILL_ENABLE=0
72         fi
73 fi
74
75 if test ! -d $confdir/$conf ; then
76         echo "amcleanup: could not cd into $confdir/$conf"
77         exit 1
78 fi
79
80 cd $confdir/$conf
81
82 logdir=`amgetconf$SUF $conf logdir "$@"`
83 rc=$?
84 if test $rc -ne 0 ; then
85         echo "amcleanup: 'amgetconf$SUF logdir' exited with status: $rc" 1>&2
86         exit 1
87 fi
88 logfile=$logdir/log
89 errfile=$logdir/amdump
90 erramflush=$logdir/amflush
91 tapecycle=`amgetconf$SUF $conf tapecycle "$@"`
92 rc=$?
93 if test $rc -ne 0 ; then
94         echo "amcleanup: 'amgetconf$SUF tapecycle' exited with status: $rc " 1>&2
95         exit 1
96 fi
97 dumpuser=`amgetconf$SUF $conf dumpuser "$@"`
98 rc=$?
99 if test $rc -ne 0 ; then
100         echo "amcleanup: 'amgetconf$SUF $conf dumpuser' exited with status: $rc" 1>&2
101         exit 1
102 fi
103
104 # Check for running processes which should not be
105 # running right now.
106 if test ${KILL_ENABLE} -eq 1 ; then
107         if test -h /proc/1/exe ; then
108                 USER_PROCESS_NAMES="\
109                         @libexecdir@/amandad \
110                         @libexecdir@/amcleanupdisk \
111                         @libexecdir@/amidxtaped \
112                         @libexecdir@/amindexd \
113                         @libexecdir@/amlogroll \
114                         @libexecdir@/amtrmidx \
115                         @libexecdir@/amtrmlog \
116                         @libexecdir@/chg-chio \
117                         @libexecdir@/chg-chs \
118                         @libexecdir@/chg-disk \
119                         @libexecdir@/chg-iomega \
120                         @libexecdir@/chg-juke \
121                         @libexecdir@/chg-manual \
122                         @libexecdir@/chg-mcutil \
123                         @libexecdir@/chg-mtx \
124                         @libexecdir@/chg-multi \
125                         @libexecdir@/chg-null \
126                         @libexecdir@/chg-rait \
127                         @libexecdir@/chg-rth \
128                         @libexecdir@/chg-scsi \
129                         @libexecdir@/chg-zd-mtx \
130                         @libexecdir@/chunker \
131                         @libexecdir@/driver \
132                         @libexecdir@/generic-dumper \
133                         @libexecdir@/gnutar \
134                         @libexecdir@/noop \
135                         @libexecdir@/patch-system \
136                         @libexecdir@/selfcheck \
137                         @libexecdir@/sendbackup \
138                         @libexecdir@/sendsize \
139                         @libexecdir@/star \
140                         @libexecdir@/taper \
141                         @libexecdir@/versionsuffix \
142                         @sbindir@/amaddclient \
143                         @sbindir@/amadmin \
144                         @sbindir@/amaespipe \
145                         @sbindir@/amcheckdb \
146                         @sbindir@/amcrypt \
147                         @sbindir@/amcryptsimple \
148                         @sbindir@/amdd \
149                         @sbindir@/amdump \
150                         @sbindir@/amfetchdump \
151                         @sbindir@/amflush \
152                         @sbindir@/amgetconf \
153                         @sbindir@/amgpgcrypt \
154                         @sbindir@/amlabel \
155                         @sbindir@/ammt \
156                         @sbindir@/amoverview \
157                         @sbindir@/amplot \
158                         @sbindir@/amrecover \
159                         @sbindir@/amreport \
160                         @sbindir@/amrestore \
161                         @sbindir@/amrmtape \
162                         @sbindir@/amserverconfig \
163                         @sbindir@/amstatus \
164                         @sbindir@/amtape \
165                         @sbindir@/amtapetype \
166                         @sbindir@/amtoc \
167                         @sbindir@/amverify \
168                         @sbindir@/amverifyrun"
169
170                 ROOT_PROCESS_NAMES="\
171                         @libexecdir@/calcsize \
172                         @libexecdir@/killpgrp \
173                         @libexecdir@/rundump \
174                         @libexecdir@/runtar \
175                         @libexecdir@/dumper \
176                         @libexecdir@/planner \
177                         @sbindir@/amcheck"
178
179                 PREVIOUS_DIR="`pwd`"
180                 cd /proc
181                 PIDS_FOUND=0
182                 KEEP_CHECKING=1
183                 while test ${KEEP_CHECKING} -ne 0 ; do
184                         PIDS_THIS_PASS=0
185                         for search_user in ${dumpuser} root ; do
186                                 if test "${search_user}" == "${dumpuser}" ; then
187                                         PROCESS_NAMES=${USER_PROCESS_NAMES}
188                                 elif test "${search_user}" == "root" ; then
189                                         PROCESS_NAMES=${ROOT_PROCESS_NAMES}
190                                 fi
191                                 for search_pid in [0-9]* ; do
192                                         for search_name in ${PROCESS_NAMES} ; do
193                                                 ls -l /proc/${search_pid}/exe 2>/dev/null | grep ${search_name} >/dev/null
194                                                 match_name=$?
195                                                 pid_uid="`cat /proc/${search_pid}/status 2>/dev/null | grep Uid | awk '//{split($_,i); print i[2]}'`"
196                                                 if test ${match_name} -eq 0  && test "${pid_uid}" == "${search_user}" ; then
197                                                         echo "amcleanup: Process ${search_name} found running at pid #${search_pid}."
198                                                         kill_pid=${search_pid}
199                                                         kill_name=${search_name}
200                                                         PIDS_FOUND=`expr ${PIDS} + 1`
201                                                         PIDS_THIS_PASS=1
202                                                         break
203                                                 else
204                                                         kill_pid=""
205                                                         continue
206                                                 fi
207                                         done
208                                         if test ! -z "${kill_pid}" ; then
209                                                 if test -d /proc/${kill_pid} ; then
210                                                         echo "amcleanup: Sending process ${kill_pid} the TERM signal."
211                                                         kill -15 -- ${kill_pid}
212                                                         sleep 5
213                                                         if test -d /proc/${kill_pid} ; then
214                                                                 echo "amcleanup: Sending process  ${kill_pid} the KILL signal."
215                                                                 kill -9 -- ${kill_pid}
216                                                         fi
217                                                         sleep 5
218                                                         if test -d /proc/${kill_pid} ; then
219                                                                 echo "amcleanup: Process ${kill_pid} did not respond to the KILL signal (and may be hung)!" 1>&2
220                                                                 KILL_FAILURES=`expr ${KILL_FAILURES} + 1`
221                                                         fi
222                                                 else
223                                                         echo "amcleanup: Process ${kill_pid} no longer running.  Skipping..."
224                                                 fi
225                                         fi
226                                 done
227                         done
228                         if test ${PIDS_THIS_PASS} -eq 0 ; then
229                                 KEEP_CHECKING=0
230                         else
231                                 KEEP_CHECKING=1
232                         fi
233                 done
234                 if test ${PIDS_FOUND} -gt 0 ; then
235                         echo "amcleanup: ${PIDS_FOUND} Amanda processes were found running."
236                         echo "amcleanup: ${KILL_FAILURES} processes failed to terminate."
237                 else
238                         echo "amcleanup: No Amanda processes were found running."
239                 fi
240                 cd "${PREVIOUS_DIR}"
241         fi
242 fi
243
244 retstatus=0
245 if test -f $logfile ; then
246         echo "amcleanup: processing outstanding log file."
247         exec </dev/null >/dev/null 2>&1
248         amreport$SUF $conf "$@"
249         rc=$?
250         if test $rc -ne 0 ; then
251                 echo "amcleanup: amreport exited with status: $rc" 1>&2
252                 retstatus=`expr $retstatus + 1`
253         fi
254
255         # Roll the log file to its datestamped name.
256         amlogroll$SUF $conf "$@"
257         rc=$?
258         if test $rc -ne 0 ; then
259                 echo "acmleanup: amlogroll exited with status: $rc" 1>&2
260                 retstatus=`expr $retstatus + 2`
261         fi
262
263         # Trim the index file to those for dumps that still exist.
264         amtrmidx$SUF $conf "$@"
265         rc=$?
266         if test $rc -ne 0 ; then
267                 echo "amcleanup: amtrmidx exited with status: $rc" 1>&2
268                 retstatus=`expr $retstatus + 4`
269         fi
270
271 else
272         echo "amcleanup: no unprocessed logfile to clean up."
273 fi
274
275 if test -f $errfile ; then
276     # if log was found, this will have been directed to /dev/null,
277     # which is fine.
278     echo "amcleanup: $errfile exists, renaming it."
279
280     # Keep debug log through the tapecycle plus a couple days
281     maxdays=`expr $tapecycle + 2`
282
283     days=1
284     # First, find out the last existing errfile,
285     # to avoid ``infinite'' loops if tapecycle is infinite
286     while test $days -lt $maxdays  && test -f $errfile.$days ; do
287         days=`expr $days + 1`
288     done
289     # Now, renumber the existing log files
290     while test $days -ge 2 ; do
291         ndays=`expr $days - 1`
292         mv $errfile.$ndays $errfile.$days
293         days=$ndays
294     done
295     mv $errfile $errfile.1
296 fi
297
298 if test -f $erramflush ; then
299     # if log was found, this will have been directed to /dev/null,
300     # which is fine.
301     echo "amcleanup: $erramflush exists, renaming it."
302
303     # Keep debug log through the tapecycle plus a couple days
304     maxdays=`expr $tapecycle + 2`
305
306     days=1
307     # First, find out the last existing erramflush,
308     # to avoid ``infinite'' loops if tapecycle is infinite
309     while test $days -lt $maxdays  && test -f $erramflush.$days ; do
310         days=`expr $days + 1`
311     done
312     # Now, renumber the existing log files
313     while test $days -ge 2 ; do
314         ndays=`expr $days - 1`
315         mv $erramflush.$ndays $erramflush.$days
316         days=$ndays
317     done
318     mv $erramflush $erramflush.1
319 fi
320
321 $libexecdir/amcleanupdisk $conf "$@"
322 rc=$?
323 if test $rc -ne 0 ; then
324         echo "amcleanup: amcleanupdisk exited with status: $rc" 1>&2
325         retstatus=`expr $retstatus + 8`
326 fi
327
328 exit $retstatus