debian experimental exim-daemon-heavy config
[exim.git] / src / src / exicyclog.src
index 4762f21f796e43905472f53c973be91e611ac231..754c9edb8a9b498f2e87db560edc58b388a1e26b 100644 (file)
@@ -1,9 +1,12 @@
 #! /bin/sh
-# $Cambridge: exim/src/src/exicyclog.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $
 
-# Copyright (c) 2004 University of Cambridge.
+# Copyright (c) University of Cambridge, 1995 - 2015
 # See the file NOTICE for conditions of use and distribution.
 
+# This script takes the following command line arguments:
+# -l dir       Log file directory
+# -k days      Number of days to keep the log files
+
 # Except when they appear in comments, the following placeholders in this
 # source are replaced when it is turned into a runnable script:
 #
@@ -16,6 +19,8 @@
 # COMPRESS_SUFFIX
 # CHOWN_COMMAND
 # CHGRP_COMMAND
+# CHMOD_COMMAND
+# TOUCH_COMMAND
 # MV_COMMAND
 # RM_COMMAND
 
 # This is a shell script for cycling exim main and reject log files. Each time
 # it is run, the files get "shuffled down" by one, the current one (e.g.
 # mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02,
-# and so on, up to the limit configured here. The same happens to the reject
-# logs. All those with numbers greater than 1 are compressed.
+# and so on, up to the limit configured here. When the number to keep is
+# greater than 99 (not common, but some people do it), three digits are used
+# (e.g. mainlog.001). The same shuffling happens to the reject logs. All
+# renamed files with numbers greater than 1 are compressed.
 
 # This script should be called regularly (e.g. daily) by a root crontab
 # entry of the form
 # this source is built into a script, but you can subsequently edit them
 # without rebuilding things, as long are you are careful not to overwrite
 # the script in the next Exim rebuild/install. "Keep" is the number of old log
-# files that are required to be kept. "Compress" and "suffix" define your
-# chosen compression method. The others are provided because the location
-# of certain commands varies from OS to OS. Sigh.
+# files that are required to be kept. Its value can be overridden by the -k
+# command line option. "Compress" and "suffix" define your chosen compression
+# method. The others are provided because the location of certain commands
+# varies from OS to OS. Sigh.
 
 keep=EXICYCLOG_MAX
 compress=COMPRESS_COMMAND
 suffix=COMPRESS_SUFFIX
 
-chown=CHOWN_COMMAND
 chgrp=CHGRP_COMMAND
+chmod=CHMOD_COMMAND
+chown=CHOWN_COMMAND
 mv=MV_COMMAND
 rm=RM_COMMAND
+touch=TOUCH_COMMAND
 
 # End of editable lines
 #########################################################################
 
+# Sort out command line options.
+
+while [ $# -gt 0 ] ; do
+  case "$1" in
+  -l) log_file_path=$2
+      shift
+      ;;
+  -k) keep=$2
+      shift
+      ;;
+   --version)
+      echo "`basename $0`: $0"
+      echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
+      exit 0
+      ;;
+   *) echo "** exicyclog: unknown option $1"
+      exit 1
+      ;;
+   esac
+   shift
+done
+
 # Some operating systems have different versions in which the commands live
 # in different places. We have a fudge that will search the usual suspects if
 # requested.
 
-for cmd in chown chgrp mv rm ; do
+for cmd in chgrp chmod chown mv rm touch; do
   eval "oldcmd=\$$cmd"
   if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
   newcmd=$cmd
@@ -110,16 +142,20 @@ done
 # Determine if the log file path is set, and where the spool directory is.
 # Search for an exim_path setting in the configure file; otherwise use the bin
 # directory. Call that version of Exim to find the spool directory and log file
-# path. BEWARE: a tab character is needed in the command below. It has had a
-# nasty tendency to get lost in the past. Use a variable to hold a space and a
-# tab to keep the tab in one place.
+# path, unless log_file_path was set above by a command line option. BEWARE: a
+# tab character is needed in the command below. It has had a nasty tendency to
+# get lost in the past. Use a variable to hold a space and a tab to keep the
+# tab in one place.
 
 st='    '
 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
-if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
+if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim4; fi
 
 spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
-log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
+
+if [ "$log_file_path" = "" ] ; then
+  log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
+fi
 
 # If log_file_path contains only "syslog" then no Exim log files are in use.
 # We can't cycle anything. Complain and give up.
@@ -137,25 +173,36 @@ fi
 log_file_path=`echo "$log_file_path" | \
   sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
 
-# If log_file_path is empty, then the logs we are interested in are called
-# "mainlog" and "rejectlog" in the directory called "log" in the spool
-# directory. Otherwise we fish out the directory from the given path, and
-# also the names of the logs.
+# If log_file_path is empty, try and get the compiled in default by using
+# /dev/null as the configuration file.
+
+if [ "$log_file_path" = "" ]; then
+  log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[  ]*//'`
+  log_file_path=`echo "$log_file_path" | \
+    sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
+fi
+
+# If log_file_path is still empty, the logs we are interested in are probably
+# called "mainlog" and "rejectlog" in the directory called "log" in the spool
+# directory. Otherwise we fish out the directory from the given path, and also
+# the names of the logs.
 
 if [ "$log_file_path" = "" ]; then
   logdir=$spool_directory/log
   mainlog=mainlog
   rejectlog=rejectlog
+  paniclog=paniclog
 else
   logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
   logbase=`echo $log_file_path | sed 's?^.*/??'`
   mainlog=`echo $logbase | sed 's/%s/main/'`
   rejectlog=`echo $logbase | sed 's/%s/reject/'`
+  paniclog=`echo $logbase | sed 's/%s/panic/'`
 fi
 
 # Get into the log directory to do the business.
 
-cd $logdir
+cd $logdir || exit 1
 
 # If there is no main log file, do nothing.
 
@@ -190,23 +237,33 @@ $b
 # Now do the job. First remove the files that have "fallen off the bottom".
 # Look for both the compressed and uncompressed forms.
 
-if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
+if [ $keep -lt 10 ]; then rotation=0$keep; else rotation=$keep; fi;
+
+if [ -f $mainlog.$rotation ]; then $rm $mainlog.$rotation; fi;
+if [ -f $mainlog.$rotation.$suffix ]; then $rm $mainlog.$rotation.$suffix; fi;
 
-if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
-if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
+if [ -f $rejectlog.$rotation ]; then $rm $rejectlog.$rotation; fi;
+if [ -f $rejectlog.$rotation.$suffix ]; then $rm $rejectlog.$rotation.$suffix; fi;
 
-if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
-if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
+if [ -f $paniclog.$rotation ]; then $rm $paniclog.$rotation; fi;
+if [ -f $paniclog.$rotation.$suffix ]; then $rm $paniclog.$rotation.$suffix; fi;
 
 # Now rename all the previous old files by increasing their numbers by 1.
 # When the number is less than 10, insert a leading zero.
 
 count=$keep
-if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi;
+if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
 
 while [ $count -gt 1 ]; do
-  old=`expr $count - 1`
-  if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
+  old=`expr -- $count - 1`
+  if [ $keep -gt 99 ]; then
+    if   [ $old -lt 10 ]; then oldt=00$old
+    elif [ $old -lt 100 ]; then oldt=0$old
+    else oldt=$old
+    fi
+  else
+    if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
+  fi
   if [ -f $mainlog.$oldt ]; then
     $mv $mainlog.$oldt $mainlog.$countt
   elif [ -f $mainlog.$oldt.$suffix ]; then
@@ -217,29 +274,63 @@ while [ $count -gt 1 ]; do
   elif [ -f $rejectlog.$oldt.$suffix ]; then
     $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
   fi
+  if [ -f $paniclog.$oldt ]; then
+    $mv $paniclog.$oldt $paniclog.$countt
+  elif [ -f $paniclog.$oldt.$suffix ]; then
+    $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix
+  fi
   count=$old
   countt=$oldt
 done
 
-# Now rename the current files as 01
+# Now rename the current files as 01 or 001 if keeping more than 99
+
+if [ $keep -gt 99 ]; then first=001; else first=01; fi
+
+# Grab our pid ro avoid race in file creation
+ourpid=$$
 
 if [ -f $mainlog ]; then
-  $mv $mainlog $mainlog.01
-  $chown $user:$group $mainlog.01
+  $mv $mainlog $mainlog.$first
+  $chown $user:$group $mainlog.$first
+  $touch $mainlog.$ourpid
+  $chown $user:$group $mainlog.$ourpid
+  $chmod 640 $mainlog.$ourpid
+  $mv $mainlog.$ourpid $mainlog
 fi
 
 if [ -f $rejectlog ]; then
-  $mv $rejectlog $rejectlog.01
-  $chown $user:$group $rejectlog.01
+  $mv $rejectlog $rejectlog.$first
+  $chown $user:$group $rejectlog.$first
+  $touch $rejectlog.$ourpid
+  $chown $user:$group $rejectlog.$ourpid
+  $chmod 640 $rejectlog.$ourpid
+  $mv $rejectlog.$ourpid $rejectlog
 fi
 
-# Now scan the 02 and later files, compressing where necessary, and
+if [ -f $paniclog ]; then
+  $mv $paniclog $paniclog.$first
+  $chown $user:$group $paniclog.$first
+  $touch $paniclog.$ourpid
+  $chown $user:$group $paniclog.$ourpid
+  $chmod 640 $paniclog.$ourpid
+  $mv $paniclog.$ourpid $paniclog
+fi
+
+# Now scan the (0)02 and later files, compressing where necessary, and
 # ensuring that their owners and groups are correct.
 
 count=2;
 
 while [ $count -le $keep ]; do
-  if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
+  if [ $keep -gt 99 ]; then
+    if   [ $count -lt 10 ]; then countt=00$count
+    elif [ $count -lt 100 ]; then countt=0$count
+    else countt=$count
+    fi
+  else
+    if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
+  fi
   if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
   if [ -f $mainlog.$countt.$suffix ]; then
     $chown $user:$group $mainlog.$countt.$suffix
@@ -248,7 +339,12 @@ while [ $count -le $keep ]; do
   if [ -f $rejectlog.$countt.$suffix ]; then
     $chown $user:$group $rejectlog.$countt.$suffix
   fi
-  count=`expr $count + 1`
+  if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi
+  if [ -f $paniclog.$countt.$suffix ]; then
+    $chown $user:$group $paniclog.$countt.$suffix
+  fi
+
+  count=`expr -- $count + 1`
 done
 
 # End of exicyclog