ngircd.init 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/sh
  2. #
  3. # ngIRCd start and stop script for Debian-based systems
  4. # Copyright 2008-2010 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 -x $DAEMON || exit 5
  25. test -h "$0" && me=`readlink $0` || me="$0"
  26. BASENAME=`basename $me`
  27. test -r /etc/default/$BASENAME && . /etc/default/$BASENAME
  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. start-stop-daemon --start \
  76. --quiet --exec $DAEMON -- $PARAMS
  77. sleep $STARTTIME
  78. Do_Status || return 7
  79. return 0
  80. }
  81. Do_Stop() {
  82. if ! Do_Status; then
  83. log_end_msg 0
  84. log_warning_msg "$NAME seems not to be running, nothing to do."
  85. exit 0
  86. fi
  87. Do_ForceStop
  88. return $?
  89. }
  90. Do_ForceStop() {
  91. [ -e $PIDFILE ] \
  92. && pidfile="--pidfile $PIDFILE" \
  93. || pidfile=""
  94. start-stop-daemon --stop \
  95. --quiet --oknodo --exec $DAEMON $pidfile
  96. for i in `seq 1 $DIETIME`; do
  97. Do_Status || return 0
  98. sleep 1
  99. done
  100. return 1
  101. }
  102. Do_Reload() {
  103. start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
  104. return $?
  105. }
  106. Do_Status() {
  107. [ -e $PIDFILE ] \
  108. && pidfile="--pidfile $PIDFILE" \
  109. || pidfile=""
  110. start-stop-daemon --stop \
  111. --quiet --signal 0 --exec $DAEMON $pidfile >/dev/null
  112. return $?
  113. }
  114. case "$1" in
  115. start)
  116. log_daemon_msg "Starting $DESC" "$NAME"
  117. Check_Config
  118. Prepare
  119. Do_Start; r=$?
  120. log_end_msg $r
  121. ;;
  122. stop)
  123. log_daemon_msg "Stopping $DESC" "$NAME"
  124. Do_Stop; r=$?
  125. log_end_msg $r
  126. ;;
  127. reload|force-reload)
  128. log_daemon_msg "Reloading $DESC" "$NAME"
  129. Check_Config
  130. Do_Reload; r=$?
  131. log_end_msg $r
  132. ;;
  133. restart)
  134. log_daemon_msg "Restarting $DESC" "$NAME"
  135. Check_Config
  136. Prepare
  137. Do_ForceStop
  138. Do_Start; r=$?
  139. log_end_msg $r
  140. ;;
  141. status)
  142. log_daemon_msg "Checking for $DESC" "$NAME"
  143. Do_Status; r=$?
  144. log_end_msg $r
  145. ;;
  146. test)
  147. Check_Config
  148. echo "Configuration of $DAEMON seems to be ok."; r=0
  149. ;;
  150. *)
  151. N=/etc/init.d/$NAME; r=2
  152. echo "Usage: $N {start|stop|restart|reload|force-reload|status|test}" >&2
  153. ;;
  154. esac
  155. exit $r
  156. # -eof-