PageRenderTime 137ms CodeModel.GetById 125ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/rc4.d/S20console-log

http://github.com/brinkman83/bashrc
#! | 316 lines | 292 code | 24 blank | 0 comment | 0 complexity | aff9f5838a2e05d0f973e0fa62231ae6 MD5 | raw file
  1#! /bin/bash
  2#
  3# console-log   init script for console-log
  4
  5### BEGIN INIT INFO
  6# Provides:          console-log
  7# Required-Start:    $local_fs $remote_fs $syslog
  8# Required-Stop:     $local_fs $remote_fs $syslog
  9# Should-Start:
 10# Should-Stop:
 11# Default-Start:     2 3 4 5
 12# Default-Stop:      0 1 6
 13# Short-Description: Puts a logfile pager on virtual consoles
 14# Description:       console-log keeps logfile pagers open on virtual consoles.
 15### END INIT INFO
 16
 17
 18PATH=/sbin:/bin:/usr/sbin:/usr/bin
 19DESC="console-log"
 20DEFAULTPAGER="less"
 21LOGPAGER="/usr/share/console-log/logpager"
 22PIDFILEDEFDIR="/var/run/console-log"
 23CONFIGFILE="/etc/console-log.conf"
 24USERNAME="Debian-console-log"
 25MAXFILESIZE="7000000"
 26
 27set -e
 28
 29if [ -r "/lib/lsb/init-functions" ]; then
 30  . /lib/lsb/init-functions
 31else
 32  echo "E: /lib/lsb/init-functions not found, lsb-base (>= 3.0-6) needed"
 33  exit 1
 34fi
 35
 36if [ -n "$CONLoGDEBUG" ]; then
 37  echo "now debugging $0 $@"
 38  set -x
 39fi
 40
 41
 42mkdir -p $PIDFILEDEFDIR
 43
 44# this is from madduck on IRC, 2006-07-06
 45# There should be a better possibility to give daemon error messages
 46# and/or to log things
 47log()
 48{
 49  case "$1" in
 50    [[:digit:]]*) success=$1; shift;;
 51    *) :;;
 52  esac
 53  log_action_begin_msg "$1"; shift
 54  log_action_end_msg ${success:-0} "$*"
 55}
 56
 57
 58# WARNING! The pager might be run as root. /usr/share/console-log/logpager
 59# is a wrapper for the actual pager that is supposed to configure the pager
 60# in a secure way to not allow shell escapes. If you have extended the
 61# pager wrapper to support other pagers, please submit your patches via
 62# the BTS.
 63
 64start_pager()
 65{
 66TTY="$1"
 67CHVT="$2"
 68FILE="$3"
 69USER="$4"
 70GROUP="$5"
 71MAXFILESIZE="$6"
 72PAGER="$7"
 73LOCSCRIPT="$8"
 74
 75if echo $TTY | grep "[[:digit:]]\+" >/dev/null; then
 76  PIDFILEDIR="$PIDFILEDEFDIR"
 77  DAEMONUSER=""
 78  if [ -n "$USER" ]; then
 79    DAEMONUSER="--user $USER"
 80    mkdir -p $PIDFILEDEFDIR/$USER
 81    chown $USER $PIDFILEDEFDIR/$USER
 82    PIDFILEDIR="$PIDFILEDEFDIR/$USER"
 83    if [ -n "$GROUP" ]; then
 84      DAEMONUSER="$DAEMONUSER.$GROUP"
 85    fi
 86  fi
 87  unset found
 88  for file in $FILE; do
 89    if [ -f "$file" ] || [ -L "$file" ]; then
 90      # check if file is readable by the given user
 91      if su --shell=$SHELL --command="head -n 1 $file" $USER > /dev/null 2>&1; then
 92        FILENAME="$TTY-${file//\//_-_}"
 93	if [ -f "$PIDFILEDIR/$FILENAME" ]; then
 94	  log_progress_msg "$file (already running)"
 95	else
 96          if [ -x "$LOCSCRIPT" ]; then
 97            . $LOCSCRIPT $file
 98          fi
 99          if [ -x "$LOGPAGER" ]; then
100	    RET=0
101	    (ulimit -S -v $(( $MAXFILESIZE / 1000 * 2 + 10000 ))
102            openvt -f -c $TTY -- \
103                 daemon --foreground --respawn --attempts=20 --delay=10 \
104                         --name=$FILENAME --pidfile=$PIDFILEDIR/$FILENAME \
105                         $DAEMONUSER $LOGPAGER -- $PAGER $file $MAXFILESIZE) || RET=$?
106	    if [ "$RET" = 2 ]; then
107	      log 1 "E: openvt failed. headless system?"
108	      exit 1
109	    fi
110            if [ -f /etc/console.noblank ]; then
111              setterm -blank 0 > /dev/tty$TTY
112            fi
113            [ "$CHVT" == "yes" ] && chvt $TTY
114            log_progress_msg "$file"
115          else
116            log 1 "W: $LOGPAGER is not executeable."
117          fi
118	fi
119      else
120        log 1 "W: $file not readable by $USER"
121      fi
122      found="1"
123      break
124    fi
125    if [ -z "found" ]; then
126      log 1 "W: no files in $FILE do exist"
127    fi
128  done
129else
130  log 1 "E: illegal tty $TTY."
131  exit 1
132fi
133}
134
135check_pager()
136{
137  USER="$1"
138  shift
139  TTY="$1"
140  shift
141  FILELIST="$@"
142  cd $PIDFILEDEFDIR
143
144  PIDFILEDIR="$PIDFILEDEFDIR"
145  if [ -n "$USER" ]; then
146    PIDFILEDIR="$PIDFILEDEFDIR/$USER"
147  fi
148
149  unset found
150  CHECKRET=0
151  for FILE in $FILELIST; do
152    if [ -f "$FILE" ] || [ -L "$FILE" ]; then
153      log_progress_msg "$FILE"
154      FILENAME="$TTY-${FILE//\//_-_}"
155      if daemon --running $USER --name=$FILENAME --pidfile=$PIDFILEDIR/$FILENAME; then
156        log_progress_msg "(running)"
157      else
158        log_progress_msg "(not running)"
159        CHECKRET=3
160      fi
161      found="1"
162      break
163    fi
164  done
165  if [ -z "found" ]; then
166    log 1 "W: no files in $FILE do exist"
167  fi
168  return $CHECKRET
169}
170
171
172do_from_config()
173{
174mkdir -p $PIDFILEDEFDIR
175cd $PIDFILEDEFDIR
176ACTION="$1"
177(
178while true; do
179  unset tty
180  chvt="no"
181  unset file
182  user="$USERNAME"
183  unset group
184  group="$USERNAME"
185  unset locscript
186  pager="$DEFAULTPAGER"
187  unset maxfilesize
188  maxfilesize="$MAXFILESIZE"
189  COUNTER=""
190  ELINE=0
191  while read KEY VALUE; do
192    case "$KEY" in
193      "#" | \#* )
194	continue
195        ;; # comment
196      "" )
197        ELINE=1
198        break
199        ;;
200      tty|chvt|file|user|group|pager|locscript|maxfilesize)
201        eval $KEY=\"$VALUE\"
202        COUNTER=".$COUNTER"
203        ;;
204      *)
205        log 1 "ERR: illegal key $KEY"
206	exit 1
207	;;
208    esac
209  done
210  # do things only if configuration was read
211  RET=0
212  if [ -n "$COUNTER" ]; then
213    case "$ACTION" in
214      start) start_pager "$tty" "$chvt" "$file" "$user" "$group" "$maxfilesize" "$pager" "$locscript"
215             ;;
216      check) check_pager "$user" "$tty" "$file"
217      	     RET=$?
218             ;;
219      *) log 1 "E: illegal action to do_from_config"
220         ;;
221    esac
222  fi
223  # break out of loop if eof
224  # if we get here without eof, then ELINE==1
225  [ "$ELINE" != "1" ] && break
226done
227) < $CONFIGFILE
228return $RET
229}
230
231do_from_running()
232{
233  ACTION="$1"
234  cd $PIDFILEDEFDIR
235  if [ "$ACTION" = "check" ]; then
236    log_action_begin_msg "checking console-log"
237  fi
238  CHECKRET=0
239  for PIDPATH in $(find . -maxdepth 2 -type f); do
240    FILENAME=$(echo $PIDPATH | sed -n 's/.*\/\(.*\)/\1/p')
241    PIDFILEDIR=$(echo $PIDPATH | sed -n 's/^.*\/\(.*\)\/.*/\1/p')
242    if [ -z "$PIDFILEDIR" ]; then
243      USER=""
244      PIDFILEDIR="$PWD"
245    else
246      USER="--user $PIDFILEDIR"
247      PIDFILEDIR="$PWD/$PIDFILEDIR"
248    fi
249    OUTPUT="${FILENAME#*-}"
250    OUTPUT="${OUTPUT//_-_//}"
251    TTY=${FILENAME%%-*}
252    RUNNING="no"
253    if daemon --running $USER --name=$FILENAME --pidfile=$PIDFILEDIR/$FILENAME; then
254      if [ "$ACTION" = "stop" ]; then
255        daemon --stop $USER --name=$FILENAME --pidfile=$PIDFILEDIR/$FILENAME
256        TERM=vt100 tput clear > /dev/tty$TTY
257      fi
258      RUNNING="yes"
259    else
260      CHECKRET=3
261    fi
262    if [ -d $PIDFILEDIR ]; then
263      rmdir --ignore-fail-on-non-empty $PIDFILEDIR
264    fi
265	  
266    # BUGS: This creates weird output if the log file name contains
267    # the string "_-_". Go figure.
268	  
269    log_progress_msg "$OUTPUT"
270    if [ "$ACTION" = "check" ]; then
271      if [ "$RUNNING" = "no" ]; then
272        log_progress_msg "(not running)"
273      else
274        log_progress_msg "(running)"
275      fi
276    fi
277  done
278  if [ "$ACTION" = "check" ]; then
279    log_end_msg 0
280    return $CHECKRET
281  fi
282}
283
284
285case "$1" in
286  start)
287	log_daemon_msg "Starting $DESC"
288        do_from_config start
289	log_end_msg 0
290	;;
291  stop)
292	log_daemon_msg "Stopping $DESC"
293  	do_from_running stop
294	log_end_msg 0
295	;;
296  reload|force-reload|restart)
297        log_daemon_msg "Stopping $DESC for restart"
298        do_from_running stop
299	log_end_msg 0
300	log_daemon_msg "Restarting $DESC"
301	do_from_config start
302	log_end_msg 0
303	;;
304  status)
305        log_daemon_msg "Checking $DESC processes"
306  	do_from_config check
307	log_end_msg 0
308	;;
309  *)
310	N=/etc/init.d/$NAME
311	echo >&2 "Usage: $N {start|stop|restart|reload|force-reload|status}"
312	exit 1
313	;;
314esac
315
316exit 0