ngircd.init 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/sh
  2. #
  3. # ngIRCd start and stop script for Debian-based systems
  4. # Copyright 2008-2015 Alexander Barton <alex@barton.de>
  5. #
  6. ### BEGIN INIT INFO
  7. # Provides: ngircd
  8. # Required-Start: $network $remote_fs
  9. # Required-Stop: $network $remote_fs
  10. # Should-Start: $syslog $named
  11. # Should-Stop: $syslog
  12. # Default-Start: 2 3 4 5
  13. # Default-Stop: 0 1 6
  14. # Short-Description: Next Generation IRC Server
  15. # Description: IRC daemon written from scratch
  16. ### END INIT INFO
  17. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  18. DAEMON=/usr/sbin/ngircd
  19. NAME=ngIRCd
  20. DESC="IRC daemon"
  21. PARAMS=""
  22. STARTTIME=1
  23. DIETIME=10
  24. test -h "$0" && me=`readlink $0` || me="$0"
  25. BASENAME=`basename $me`
  26. test -r /etc/default/$BASENAME && . /etc/default/$BASENAME
  27. test -x $DAEMON || exit 5
  28. # LSB compatibility functions that become used if there is no local
  29. # include file available.
  30. log_daemon_msg() {
  31. echo -n "$*"
  32. }
  33. log_end_msg() {
  34. [ "$1" = "0" ] && echo "." || echo " failed!"
  35. }
  36. log_failure_msg() {
  37. echo "$*"
  38. }
  39. log_warning_msg() {
  40. log_failure_msg "$*"
  41. }
  42. # Include LSB functions, if available:
  43. test -r /lib/lsb/init-functions && . /lib/lsb/init-functions
  44. PIDFILE=`$DAEMON $PARAMS -t | tr -d ' ' | grep "^PidFile=" | cut -d'=' -f2`
  45. [ -n "$PIDFILE" ] || PIDFILE="/var/run/ircd/ngircd.pid"
  46. r=3
  47. Check_Config()
  48. {
  49. # Make sure that the configuration of ngIRCd is valid:
  50. $DAEMON $PARAMS --configtest >/dev/null 2>&1
  51. [ $? -eq 0 ] && return 0
  52. log_end_msg 1
  53. log_failure_msg "Configuration of $NAME is not valid, won't (re)start!"
  54. log_failure_msg "Run \"$DAEMON --configtest\" and fix it up ..."
  55. exit 6
  56. }
  57. Prepare() {
  58. # Make sure the PID file directory exists and is writable:
  59. user=`$DAEMON $PARAMS -t|tr -d ' '|grep "^ServerUID="|cut -d'=' -f2`
  60. group=`$DAEMON $PARAMS -t|tr -d ' '|grep "^ServerGID="|cut -d'=' -f2`
  61. piddir=`dirname "$PIDFILE"`
  62. [ -d "$piddir" ] || mkdir -p "$piddir" 2>/dev/null
  63. chown "$user:$group" "$piddir" 2>/dev/null
  64. [ $? -eq 0 ] && return 0
  65. log_end_msg 1
  66. log_failure_msg "Failed to prepare '$piddir' for user '$user'!"
  67. exit 1
  68. }
  69. Do_Start() {
  70. if Do_Status; then
  71. log_end_msg 0
  72. log_warning_msg "$NAME seems to be already running, nothing to do."
  73. exit 0
  74. fi
  75. rm -f "$PIDFILE"
  76. start-stop-daemon --start \
  77. --quiet --exec $DAEMON -- $PARAMS
  78. sleep $STARTTIME
  79. Do_Status || return 7
  80. return 0
  81. }
  82. Do_Stop() {
  83. if ! Do_Status; then
  84. log_end_msg 0
  85. log_warning_msg "$NAME seems not to be running, nothing to do."
  86. exit 0
  87. fi
  88. Do_ForceStop
  89. return $?
  90. }
  91. Do_ForceStop() {
  92. [ -e $PIDFILE ] \
  93. && pidfile="--pidfile $PIDFILE" \
  94. || pidfile=""
  95. start-stop-daemon --stop \
  96. --quiet --oknodo --exec $DAEMON $pidfile
  97. for i in `seq 1 $DIETIME`; do
  98. Do_Status || return 0
  99. sleep 1
  100. done
  101. return 1
  102. }
  103. Do_Reload() {
  104. start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
  105. return $?
  106. }
  107. Do_Status() {
  108. [ -e $PIDFILE ] \
  109. && pidfile="--pidfile $PIDFILE" \
  110. || pidfile=""
  111. start-stop-daemon --stop \
  112. --quiet --signal 0 --exec $DAEMON $pidfile >/dev/null
  113. return $?
  114. }
  115. case "$1" in
  116. start)
  117. log_daemon_msg "Starting $DESC" "$NAME"
  118. Check_Config
  119. Prepare
  120. Do_Start; r=$?
  121. log_end_msg $r
  122. ;;
  123. stop)
  124. log_daemon_msg "Stopping $DESC" "$NAME"
  125. Do_Stop; r=$?
  126. log_end_msg $r
  127. ;;
  128. reload|force-reload)
  129. log_daemon_msg "Reloading $DESC" "$NAME"
  130. Check_Config
  131. Do_Reload; r=$?
  132. log_end_msg $r
  133. ;;
  134. restart)
  135. log_daemon_msg "Restarting $DESC" "$NAME"
  136. Check_Config
  137. Prepare
  138. Do_ForceStop
  139. Do_Start; r=$?
  140. log_end_msg $r
  141. ;;
  142. status)
  143. log_daemon_msg "Checking for $DESC" "$NAME"
  144. Do_Status; r=$?
  145. log_end_msg $r
  146. ;;
  147. test)
  148. Check_Config
  149. echo "Configuration of $DAEMON seems to be ok."; r=0
  150. ;;
  151. *)
  152. N=/etc/init.d/$NAME; r=2
  153. echo "Usage: $N {start|stop|restart|reload|force-reload|status|test}" >&2
  154. ;;
  155. esac
  156. exit $r
  157. # -eof-