ngircd.init 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/bin/sh
  2. # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
  3. #
  4. # This is free software; you may redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as
  6. # published by the Free Software Foundation; either version 2,
  7. # or (at your option) any later version.
  8. #
  9. # This is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License with
  15. # the Debian operating system, in /usr/share/common-licenses/GPL; if
  16. # not, write to the Free Software Foundation, Inc., 59 Temple Place,
  17. # Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. ### BEGIN INIT INFO
  20. # Provides: ngircd
  21. # Required-Start: $network $local_fs
  22. # Required-Stop:
  23. # Should-Start: $named
  24. # Should-Stop:
  25. # Default-Start: 2 3 4 5
  26. # Default-Stop: 0 1 6
  27. # Short-Description: Next generation IRC daemon
  28. # Description: Simple IRC daemon written from scratch
  29. ### END INIT INFO
  30. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  31. DAEMON=/usr/sbin/ngircd
  32. NAME=ngircd
  33. DESC="Next generation IRC daemon"
  34. LOGDIR=/var/log/
  35. PIDFILE=/var/run/ngircd/$NAME.pid
  36. test -x $DAEMON || exit 0
  37. . /lib/lsb/init-functions
  38. DAEMON_OPTS="" # Additional options given to the server
  39. DIETIME=10 # Time to wait for the server to die, in seconds
  40. LOGFILE=$LOGDIR/$NAME.log # Server logfile
  41. DAEMONUSER="irc"
  42. if [ -n "$DAEMONUSER" ] ; then
  43. if getent passwd | grep -q "^$DAEMONUSER:"; then
  44. # Obtain the uid and gid
  45. DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
  46. DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
  47. else
  48. log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
  49. exit 1
  50. fi
  51. fi
  52. set -e
  53. running_pid() {
  54. # Check if a given process pid's cmdline matches a given name
  55. pid=$1
  56. name=$2
  57. [ -z "$pid" ] && return 1
  58. [ ! -d /proc/$pid ] && return 1
  59. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  60. # Is this the expected server
  61. [ "$cmd" != "$name" ] && return 1
  62. return 0
  63. }
  64. running() {
  65. # Check if the process is running looking at /proc
  66. # (works for all users)
  67. # No pidfile, probably no daemon present
  68. [ ! -f "$PIDFILE" ] && return 1
  69. pid=`cat $PIDFILE`
  70. running_pid $pid $DAEMON || return 1
  71. return 0
  72. }
  73. start_server() {
  74. # Start the process using the wrapper
  75. if [ -z "$DAEMONUSER" ] ; then
  76. start_daemon -p $PIDFILE $DAEMON -- $DAEMON_OPTS
  77. errcode=$?
  78. else
  79. # if we are using a daemonuser then change the user id
  80. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  81. --chuid $DAEMONUSER \
  82. --exec $DAEMON -- $DAEMON_OPTS
  83. errcode=$?
  84. fi
  85. return $errcode
  86. }
  87. stop_server() {
  88. # Stop the process using the wrapper
  89. if [ -z "$DAEMONUSER" ] ; then
  90. killproc -p $PIDFILE $DAEMON
  91. errcode=$?
  92. else
  93. # if we are using a daemonuser then look for process that match
  94. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  95. --user $DAEMONUSER \
  96. --exec $DAEMON
  97. errcode=$?
  98. fi
  99. return $errcode
  100. }
  101. reload_server() {
  102. [ ! -f "$PIDFILE" ] && return 1
  103. pid=pidofproc $PIDFILE # This is the daemon's pid
  104. # Send a SIGHUP
  105. kill -1 $pid
  106. return $?
  107. }
  108. force_stop() {
  109. # Force the process to die killing it manually
  110. [ ! -e "$PIDFILE" ] && return
  111. if running ; then
  112. kill -15 $pid
  113. # Is it really dead?
  114. sleep "$DIETIME"s
  115. if running ; then
  116. kill -9 $pid
  117. sleep "$DIETIME"s
  118. if running ; then
  119. echo "Cannot kill $NAME (pid=$pid)!"
  120. exit 1
  121. fi
  122. fi
  123. fi
  124. rm -f $PIDFILE
  125. }
  126. case "$1" in
  127. start)
  128. log_daemon_msg "Starting $DESC " "$NAME"
  129. # Check if it's running first
  130. if running ; then
  131. log_progress_msg "apparently already running"
  132. log_end_msg 0
  133. exit 0
  134. fi
  135. if start_server ; then
  136. # NOTE: Some servers might die some time after they start,
  137. # this code will detect this issue if STARTTIME is set
  138. # to a reasonable value
  139. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  140. if running ; then
  141. # It's ok, the server started and is running
  142. log_end_msg 0
  143. else
  144. # It is not running after we did start
  145. log_end_msg 1
  146. fi
  147. else
  148. # Either we could not start it
  149. log_end_msg 1
  150. fi
  151. ;;
  152. stop)
  153. log_daemon_msg "Stopping $DESC" "$NAME"
  154. if running ; then
  155. # Only stop the server if we see it running
  156. errcode=0
  157. stop_server || errcode=$?
  158. log_end_msg $errcode
  159. else
  160. # If it's not running don't do anything
  161. log_progress_msg "apparently not running"
  162. log_end_msg 0
  163. exit 0
  164. fi
  165. ;;
  166. force-stop)
  167. # First try to stop gracefully the program
  168. $0 stop
  169. if running; then
  170. # If it's still running try to kill it more forcefully
  171. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  172. errcode=0
  173. force_stop || errcode=$?
  174. log_end_msg $errcode
  175. fi
  176. ;;
  177. restart|force-reload)
  178. log_daemon_msg "Restarting $DESC" "$NAME"
  179. errcode=0
  180. stop_server || errcode=$?
  181. # Wait some sensible amount, some server need this
  182. [ -n "$DIETIME" ] && sleep $DIETIME
  183. start_server || errcode=$?
  184. [ -n "$STARTTIME" ] && sleep $STARTTIME
  185. running || errcode=$?
  186. log_end_msg $errcode
  187. ;;
  188. status)
  189. log_daemon_msg "Checking status of $DESC" "$NAME"
  190. if running ; then
  191. log_progress_msg "running"
  192. log_end_msg 0
  193. else
  194. log_progress_msg "apparently not running"
  195. log_end_msg 1
  196. exit 1
  197. fi
  198. ;;
  199. # Use this if the daemon cannot reload
  200. reload)
  201. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  202. log_warning_msg "cannot re-read the config file (use restart)."
  203. ;;
  204. # And this if it cann
  205. #reload)
  206. #
  207. # If the daemon can reload its config files on the fly
  208. # for example by sending it SIGHUP, do it here.
  209. #
  210. # If the daemon responds to changes in its config file
  211. # directly anyway, make this a do-nothing entry.
  212. #
  213. # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  214. # if running ; then
  215. # reload_server
  216. # if ! running ; then
  217. # Process died after we tried to reload
  218. # log_progress_msg "died on reload"
  219. # log_end_msg 1
  220. # exit 1
  221. # fi
  222. # else
  223. # log_progress_msg "server is not running"
  224. # log_end_msg 1
  225. # exit 1
  226. # fi
  227. #;;
  228. *)
  229. N=/etc/init.d/$NAME
  230. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  231. exit 1
  232. ;;
  233. esac
  234. exit 0