1#!/bin/sh
2###############################################################################
3# Written by Chris Dunlap <cdunlap@llnl.gov>.
4# Copyright (C) 2007-2018 Lawrence Livermore National Security, LLC.
5# Copyright (C) 2001-2007 The Regents of the University of California.
6# UCRL-CODE-2002-009.
7###############################################################################
8# chkconfig:          - 95 5
9# Description:        Start/Stop the ConMan (serial console manager) daemon.
10###############################################################################
11### BEGIN INIT INFO
12# Provides:           conman
13# Required-Start:     $local_fs $named $network
14# Required-Stop:      $local_fs $named $network
15# Should-Start:       $remote_fs $syslog $time
16# Should-Stop:        $remote_fs $syslog $time
17# Default-Start:
18# Default-Stop:
19# Short-Description:  Start/Stop the ConMan (serial console manager) daemon.
20# Description:        Start/Stop the ConMan (serial console manager) daemon.
21### END INIT INFO
22###############################################################################
23
24unset DESC DAEMON CONFIG DAEMON_ARGS PIDFILE NICE USER SIGHUP_RELOAD
25
26prefix="@prefix@"
27exec_prefix="@exec_prefix@"
28sbindir="@sbindir@"
29sysconfdir="@sysconfdir@"
30localstatedir="@localstatedir@"
31
32DESC="ConMan"
33DAEMON="$sbindir/conmand"
34#CONFIG=
35#DAEMON_ARGS=
36#PIDFILE=
37#NICE=
38#USER=
39#SIGHUP_RELOAD=
40
41###############################################################################
42
43service_init ()
44{
45# Determine the system type and initialize the environment.
46#
47# Note that the shell positional parameters must be preserved when calling
48#   this function in order for SuSE to initialize its environment properly.
49##
50  PATH=/sbin:/usr/sbin:/bin:/usr/bin
51  DAEMON_NAME="`basename \"$DAEMON\"`"
52  SCRIPT_NAME="`basename \"$0\" .init | sed 's/^[SK][0-9][0-9]*//'`"
53  SIGTERM_TIMEOUT="3"
54  STATUS=0
55
56  # Read configuration defaults to override variables:
57  #   $CONFIG, $DAEMON_ARGS, $PIDFILE, $USER, $NICE, $SIGHUP_RELOAD
58  ##
59  for dir in "$sysconfdir/default" "$sysconfdir/sysconfig"; do
60    [ -r "$dir/$SCRIPT_NAME" ] && . "$dir/$SCRIPT_NAME"
61  done
62  [ -z "$DAEMON_ARGS" -a -n "$OPTIONS" ] && DAEMON_ARGS="$OPTIONS"
63  [ "`id | sed 's/^uid=\([0-9]*\).*/\1/'`" -ne 0 ] && unset USER
64  expr -- "$NICE" : '[0-9]*$' >/dev/null 2>&1 && NICE="+$NICE"
65  [ -n "$SIGHUP_RELOAD" -a "$SIGHUP_RELOAD" != 0 ] \
66    && RELOAD=1 || unset RELOAD
67
68  if [ -f /etc/debian_version -a -x /sbin/start-stop-daemon ]; then
69    SYSTEM="DEBIAN"
70    [ -x "$DAEMON" ] || exit 0                  # pkg removed but not purged
71    [ -r /etc/default/rcS ] && . /etc/default/rcS
72    [ -r /lib/init/vars.sh ] && . /lib/init/vars.sh
73    [ -r /lib/lsb/init-functions ] && . /lib/lsb/init-functions
74  elif [ -f /etc/redhat-release -a -r /etc/init.d/functions ]; then
75    SYSTEM="REDHAT"
76    . /etc/init.d/functions
77    RH_SUBSYS="/var/lock/subsys/$DAEMON_NAME"
78  elif [ -f /etc/SuSE-release -a -r /etc/rc.status ]; then
79    SYSTEM="SUSE"
80    . /etc/rc.status
81    rc_reset
82  elif [ -r /lib/lsb/init-functions ]; then
83    SYSTEM="LSB"
84    . /lib/lsb/init-functions
85  else
86    SYSTEM="OTHER"
87  fi
88
89  # Exit if the package has been removed.
90  ##
91  [ -x "$DAEMON" ] || exit 5                    # LSB: program not installed
92
93  # Exit if the configuration has been removed.
94  ##
95  [ -z "$CONFIG" -o -r "$CONFIG" ] || exit 6    # LSB: program not configured
96}
97
98service_fini ()
99{
100# Return the exit status.
101##
102  case $SYSTEM in
103    SUSE)
104      rc_exit
105      ;;
106    DEBIAN|REDHAT|LSB|*)
107      exit $STATUS
108      ;;
109  esac
110}
111
112service_start ()
113{
114# Start the service.
115#
116# Required by LSB, where running "start" on a service already running should be
117#   considered successful.
118##
119  log_init "Starting $DESC" "$DAEMON_NAME"
120  case $SYSTEM in
121    DEBIAN)
122      if $0 status >/dev/null 2>&1; then
123        STATUS=0
124      else
125        ERRMSG=`start-stop-daemon --start --quiet \
126          ${NICE:+"--nicelevel"} ${NICE:+"$NICE"} \
127          ${USER:+"--chuid"} ${USER:+"$USER"} \
128          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
129          --exec "$DAEMON" -- $DAEMON_ARGS 2>&1`
130        STATUS=$?
131      fi
132      ;;
133    REDHAT)
134      if $0 status >/dev/null 2>&1; then
135        STATUS=0
136      else
137        daemon ${NICE:+"$NICE"} ${USER:+"--user"} ${USER:+"$USER"} \
138          "$DAEMON" $DAEMON_ARGS
139        STATUS=$?
140      fi
141      [ $STATUS -eq 0 ] && touch "$RH_SUBSYS" >/dev/null 2>&1
142      ;;
143    SUSE)
144      ERRMSG=`startproc ${NICE:+"-n"} ${NICE:+"$NICE"} \
145        ${USER:+"-u"} ${USER:+"$USER"} \
146        ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
147        "$DAEMON" $DAEMON_ARGS 2>&1`
148      rc_status -v
149      STATUS=$?
150      ;;
151    LSB)
152      if [ -n "$USER" ]; then
153        ERRMSG=`su "$USER" -c "/sbin/start_daemon \
154          ${NICE:+\"-n\"} ${NICE:+\"$NICE\"} \
155          ${PIDFILE:+\"-p\"} ${PIDFILE:+\"$PIDFILE\"} \
156          \"$DAEMON\" $DAEMON_ARGS" 2>&1`
157      else
158        ERRMSG=`start_daemon ${NICE:+"-n"} ${NICE:+"$NICE"} \
159          ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" $DAEMON_ARGS 2>&1`
160      fi
161      STATUS=$?
162      ;;
163    *)
164      if $0 status >/dev/null 2>&1; then
165        STATUS=0
166      else
167        [ -n "$NICE" ] && nice="nice -n $NICE"
168        if [ -n "$USER" ]; then
169          ERRMSG=`su "$USER" -c "$nice \"$DAEMON\" $DAEMON_ARGS" 2>&1`
170        else
171          ERRMSG=`$nice "$DAEMON" $DAEMON_ARGS 2>&1`
172        fi
173        STATUS=$?
174      fi
175      ;;
176  esac
177  log_fini "$STATUS" "$ERRMSG"
178}
179
180service_stop ()
181{
182# Stop the service.
183#
184# Required by LSB, where running "stop" on a service already stopped or not
185#   running should be considered successful.
186##
187  log_init "Stopping $DESC" "$DAEMON_NAME"
188  case $SYSTEM in
189    DEBIAN)
190      if ! $0 status >/dev/null 2>&1; then
191        STATUS=0
192      else
193        start-stop-daemon --stop --quiet \
194          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
195          --name "$DAEMON_NAME" ${SIGTERM_TIMEOUT:+"--retry"} \
196          ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} >/dev/null 2>&1
197        STATUS=$?
198      fi
199      ;;
200    REDHAT)
201      if ! $0 status >/dev/null 2>&1; then
202        STATUS=0
203      else
204        killproc "$DAEMON"
205        STATUS=$?
206      fi
207      [ $STATUS -eq 0 ] && rm -f "$RH_SUBSYS" >/dev/null 2>&1
208      ;;
209    SUSE)
210      killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
211        ${SIGTERM_TIMEOUT:+"-t"} ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} \
212        "$DAEMON"
213      rc_status -v
214      ;;
215    LSB)
216      killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
217      STATUS=$?
218      ;;
219    *)
220      signal_process "$DAEMON"
221      rc=$?
222      [ $rc -eq 0 -o $rc -eq 2 ] && STATUS=0 || STATUS=1
223      ;;
224  esac
225  log_fini "$STATUS"
226  [ -f "$PIDFILE" ] && rm -f "$PIDFILE"
227}
228
229service_restart ()
230{
231# Stop and restart the service if it is already running;
232#   otherwise, start the service.
233#
234# Required by LSB, where running "restart" on a service already stopped or not
235#   running should be considered successful.
236##
237  if $0 status >/dev/null 2>&1; then
238    $0 stop && $0 start
239  else
240    $0 start
241  fi
242
243  case $SYSTEM in
244    SUSE)
245      rc_status
246      ;;
247    DEBIAN|REDHAT|LSB|*)
248      STATUS=$?
249      ;;
250  esac
251}
252
253service_try_restart ()
254{
255# Restart the service if it is already running.
256#
257# Optional for LSB, where running "try-restart" on a service already stopped or
258#   not running should be considered successful.
259# Also known as "condrestart" by RedHat.
260##
261  case $SYSTEM in
262    REDHAT)
263      [ -f "$RH_SUBSYS" ] && $0 restart || :
264      STATUS=$?
265      ;;
266    SUSE)
267      $0 status >/dev/null 2>&1 && $0 restart || rc_reset
268      rc_status
269      ;;
270    DEBIAN|LSB|*)
271      $0 status >/dev/null 2>&1 && $0 restart || :
272      STATUS=$?
273      ;;
274  esac
275}
276
277service_reload ()
278{
279# Reload the configuration without stopping and restarting the service.
280#
281# Optional for LSB.
282##
283  [ -z "$RELOAD" ] && STATUS=3          # LSB: unimplemented feature
284
285  log_init "Reloading $DESC" "$DAEMON_NAME"
286  case $SYSTEM in
287    DEBIAN)
288      if [ -n "$RELOAD" ]; then
289        start-stop-daemon --stop --quiet --signal HUP \
290          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
291          --name "$DAEMON_NAME" >/dev/null 2>&1
292        STATUS=$?
293      fi
294      ;;
295    REDHAT)
296      if [ -n "$RELOAD" ]; then
297        killproc "$DAEMON" -HUP
298        STATUS=$?
299      else
300        echo_failure
301      fi
302      ;;
303    SUSE)
304      if [ -n "$RELOAD" ]; then
305        killproc -HUP ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
306      else
307        rc_failed $STATUS
308      fi
309      rc_status -v
310      ;;
311    LSB)
312      if [ -n "$RELOAD" ]; then
313        killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" -HUP
314        STATUS=$?
315      fi
316      ;;
317    *)
318      if [ -n "$RELOAD" ]; then
319        signal_process "$DAEMON" "HUP"
320        STATUS=$?
321      fi
322      ;;
323  esac
324  log_fini "$STATUS"
325}
326
327service_force_reload ()
328{
329# Reload the configuration if the service supports this;
330#   otherwise, restart the service if it is already running.
331#
332# Required by LSB, where running "force-reload" on a service already stopped or
333#   not running should be considered successful.
334##
335  if [ -n "$RELOAD" ]; then
336    $0 reload
337  else
338    $0 try-restart
339  fi
340
341  case $SYSTEM in
342    SUSE)
343      rc_status
344      ;;
345    DEBIAN|REDHAT|LSB|*)
346      STATUS=$?
347      ;;
348  esac
349}
350
351service_status ()
352{
353# Print the current status of the service.
354#
355# Required by LSB.
356##
357  case $SYSTEM in
358    REDHAT)
359      status "$DAEMON"
360      STATUS=$?
361      ;;
362    SUSE)
363      printf "Checking for service $DESC: "
364      checkproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
365      rc_status -v
366      ;;
367    LSB)
368      printf "Checking status of $DESC: "
369      pids=`pidofproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
370        "$DAEMON" 2>/dev/null`
371      STATUS=$?
372      if [ $STATUS -eq 0 -a -n "$pids" ]; then
373        echo "running."
374      elif [ $STATUS -ne 0 -a -s "$PIDFILE" ]; then
375        echo "dead."
376      else
377        echo "stopped."
378      fi
379      ;;
380    DEBIAN|*)
381      printf "Checking status of $DESC: "
382      pids=`query_pids "$DAEMON" "$PIDFILE"`
383      rc=$?
384      if [ $rc -eq 0 -a -n "$pids" ]; then
385        echo "running."
386        STATUS=0                        # LSB: program is running
387      elif [ $rc -ne 0 -a -s "$PIDFILE" ]; then
388        echo "dead."
389        STATUS=1                        # LSB: program is dead & pidfile exists
390      elif [ $rc -ne 0 ]; then
391        echo "stopped."
392        STATUS=3                        # LSB: program is not running
393      else
394        echo "unknown."
395        STATUS=4                        # LSB: program status unknown
396      fi
397      ;;
398  esac
399}
400
401query_pids ()
402{
403# Writes the matching PIDs to stdout.
404# Returns 0 on success (ie, pids found).
405##
406  PROCNAME="$1"
407  PIDFILE="$2"
408
409  if type pgrep >/dev/null 2>&1; then
410    pids=`pgrep -d ' ' -x "\`basename \"$PROCNAME\"\`" 2>/dev/null`
411    rc=$?
412  elif type pidof >/dev/null 2>&1; then
413    pids=`pidof -o $$ -x "$PROCNAME" 2>/dev/null`
414    rc=$?
415  else
416    pids=`(ps awx -o pid -o command || ps -e -f -o pid -o args) 2>/dev/null \
417      | tail +2 | egrep "( |/)$PROCNAME( |$)" | grep -v egrep \
418      | sed 's/ *\([0-9]*\).*/\1/' | sort -n | tr '\012' ' '`
419    [ -n "$pids" ] && rc=0 || rc=1
420  fi
421
422  unset pids_running
423  if [ -n "$pids" -a -r "$PIDFILE" ]; then
424    read pid_line < "$PIDFILE"
425    for pid in $pid_line; do
426      expr -- "$pid" : '[0-9]*$' >/dev/null 2>&1 \
427        && expr -- " $pids " : ".* $pid .*" >/dev/null 2>&1 \
428        && pids_running="$pids_running $pid"
429    done
430    [ -n "$pids_running" ] && pids=$pids_running
431  fi
432
433  echo $pids
434  return $rc
435}
436
437signal_process ()
438{
439# Returns 0 on success, 1 if kill failed, 2 if PROCNAME is not running.
440##
441  PROCNAME="$1"
442  SIGNUM="$2"
443
444  pids=`query_pids "$DAEMON" "$PIDFILE"`
445  [ $? -ne 0 -o -z "$pids" ] && return 2
446
447  kill ${SIGNUM:+"-$SIGNUM"} $pids >/dev/null 2>&1
448  [ $? -ne 0 ] && return 1
449  [ -n "$SIGNUM" ] && return 0
450
451  pids=`query_pids "$DAEMON" "$PIDFILE"`
452  [ $? -ne 0 -o -z "$pids" ] && return 0
453  [ -z "$SIGTERM_TIMEOUT" ] && return 1
454
455  sleep "$SIGTERM_TIMEOUT"
456  kill -KILL $pids >/dev/null 2>&1
457  pids=`query_pids "$DAEMON" "$PIDFILE"`
458  [ $? -ne 0 -o -z "$pids" ] && return 0
459  return 1
460}
461
462log_init ()
463{
464# Output informational message at beginning of action.
465##
466  MESSAGE="$1"
467  PROCNAME="$2"
468
469  case $SYSTEM in
470    DEBIAN)
471      if [ "$VERBOSE" != no ]; then
472        if type log_daemon_msg >/dev/null 2>&1; then
473          log_daemon_msg "$MESSAGE" "$PROCNAME"
474        else
475          printf "$MESSAGE: $PROCNAME"
476        fi
477      fi
478      ;;
479    REDHAT|SUSE|LSB|*)
480      printf "$MESSAGE: $PROCNAME"
481      ;;
482  esac
483}
484
485log_fini ()
486{
487# Output informational/error message at end of action.
488##
489  STATUS="$1"
490  ERRMSG="$2"
491
492  case $SYSTEM in
493    DEBIAN)
494      if [ "$VERBOSE" != no ]; then
495        if ( type log_end_msg && type log_failure_msg ) >/dev/null 2>&1; then
496          log_end_msg "$STATUS"
497          [ $STATUS -eq 0 -o -z "$ERRMSG" ] || log_failure_msg "$ERRMSG"
498        else
499          [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
500          [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
501        fi
502      fi
503      ;;
504    REDHAT)
505      echo
506      ;;
507    SUSE)
508      [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
509      ;;
510    LSB|*)
511      [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
512      [ $STATUS -eq 0 -o -z "$ERRMSG" ] || echo "$ERRMSG" >&2
513      ;;
514  esac
515}
516
517###############################################################################
518
519service_init "$@"
520
521case "$1" in
522  start)
523    service_start
524    ;;
525  stop)
526    service_stop
527    ;;
528  restart)
529    service_restart
530    ;;
531  try-restart|condrestart)
532    service_try_restart
533    ;;
534  reload)
535    service_reload
536    ;;
537  force-reload)
538    service_force_reload
539    ;;
540  status)
541    service_status
542    ;;
543  *)
544    echo "Usage: `basename \"$0\"`" \
545      "(start|stop|restart|try-restart|reload|force-reload|status)" >&2
546    exit 2                              # LSB: invalid or excess argument(s)
547    ;;
548esac
549
550service_fini
551