ngircd.init 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. PIDDIR=/var/run/ngircd
  36. PIDFILE=$PIDDIR/$NAME.pid
  37. test -x $DAEMON || exit 0
  38. . /lib/lsb/init-functions
  39. DAEMON_OPTS="" # Additional options given to the server
  40. STARTTIME=1
  41. DIETIME=10 # Time to wait for the server to die, in seconds
  42. LOGFILE=$LOGDIR/$NAME.log # Server logfile
  43. DAEMONUSER="irc"
  44. if [ -n "$DAEMONUSER" ] ; then
  45. if getent passwd | grep -q "^$DAEMONUSER:"; then
  46. # Obtain the uid and gid
  47. DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
  48. DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
  49. else
  50. log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
  51. exit 1
  52. fi
  53. fi
  54. set -e
  55. running_pid() {
  56. # Check if a given process pid's cmdline matches a given name
  57. pid=$1
  58. name=$2
  59. [ -z "$pid" ] && return 1
  60. [ ! -d /proc/$pid ] && return 1
  61. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  62. # Is this the expected server
  63. [ "$cmd" != "$name" ] && return 1
  64. return 0
  65. }
  66. running() {
  67. # Check if the process is running looking at /proc
  68. # (works for all users)
  69. # No pidfile, probably no daemon present
  70. [ ! -f "$PIDFILE" ] && return 1
  71. pid=`cat $PIDFILE`
  72. running_pid $pid $DAEMON || return 1
  73. return 0
  74. }
  75. start_server() {
  76. # Start the process using the wrapper
  77. if [ -z "$DAEMONUSER" ] ; then
  78. start_daemon -p $PIDFILE $DAEMON -- $DAEMON_OPTS
  79. errcode=$?
  80. else
  81. # if we are using a daemonuser then change the user id
  82. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  83. --chuid $DAEMONUSER \
  84. --exec $DAEMON -- $DAEMON_OPTS
  85. errcode=$?
  86. fi
  87. return $errcode
  88. }
  89. stop_server() {
  90. # Stop the process using the wrapper
  91. if [ -z "$DAEMONUSER" ] ; then
  92. killproc -p $PIDFILE $DAEMON
  93. errcode=$?
  94. else
  95. # if we are using a daemonuser then look for process that match
  96. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  97. --user $DAEMONUSER \
  98. --exec $DAEMON
  99. errcode=$?
  100. fi
  101. return $errcode
  102. }
  103. reload_server() {
  104. [ ! -f "$PIDFILE" ] && return 1
  105. pid=pidofproc $PIDFILE # This is the daemon's pid
  106. # Send a SIGHUP
  107. kill -1 $pid
  108. return $?
  109. }
  110. force_stop() {
  111. # Force the process to die killing it manually
  112. [ ! -e "$PIDFILE" ] && return
  113. if running ; then
  114. kill -15 $pid
  115. # Is it really dead?
  116. sleep "$DIETIME"s
  117. if running ; then
  118. kill -9 $pid
  119. sleep "$DIETIME"s
  120. if running ; then
  121. echo "Cannot kill $NAME (pid=$pid)!"
  122. exit 1
  123. fi
  124. fi
  125. fi
  126. rm -f $PIDFILE
  127. }
  128. case "$1" in
  129. start)
  130. log_daemon_msg "Starting $DESC " "$NAME"
  131. # Check if it's running first
  132. if running ; then
  133. log_progress_msg "apparently already running"
  134. log_end_msg 0
  135. exit 0
  136. fi
  137. # If $PIDDIR doesn't exist, create it and give it proper permissions
  138. # Useful if $PIDDIR is in a tmpfs
  139. if [ ! -d "$PIDDIR" ]; then
  140. mkdir $PIDDIR
  141. chown irc:irc $PIDDIR
  142. chmod 770 $PIDDIR
  143. fi
  144. if start_server ; then
  145. # NOTE: Some servers might die some time after they start,
  146. # this code will detect this issue if STARTTIME is set
  147. # to a reasonable value
  148. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  149. if running ; then
  150. # It's ok, the server started and is running
  151. log_end_msg 0
  152. else
  153. # It is not running after we did start
  154. log_end_msg 1
  155. fi
  156. else
  157. # Either we could not start it
  158. log_end_msg 1
  159. fi
  160. ;;
  161. stop)
  162. log_daemon_msg "Stopping $DESC" "$NAME"
  163. if running ; then
  164. # Only stop the server if we see it running
  165. errcode=0
  166. stop_server || errcode=$?
  167. log_end_msg $errcode
  168. else
  169. # If it's not running don't do anything
  170. log_progress_msg "apparently not running"
  171. log_end_msg 0
  172. exit 0
  173. fi
  174. ;;
  175. force-stop)
  176. # First try to stop gracefully the program
  177. $0 stop
  178. if running; then
  179. # If it's still running try to kill it more forcefully
  180. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  181. errcode=0
  182. force_stop || errcode=$?
  183. log_end_msg $errcode
  184. fi
  185. ;;
  186. restart|force-reload)
  187. log_daemon_msg "Restarting $DESC" "$NAME"
  188. errcode=0
  189. stop_server || errcode=$?
  190. # Wait some sensible amount, some server need this
  191. [ -n "$DIETIME" ] && sleep $DIETIME
  192. start_server || errcode=$?
  193. [ -n "$STARTTIME" ] && sleep $STARTTIME
  194. running || errcode=$?
  195. log_end_msg $errcode
  196. ;;
  197. status)
  198. log_daemon_msg "Checking status of $DESC" "$NAME"
  199. if running ; then
  200. log_progress_msg "running"
  201. log_end_msg 0
  202. else
  203. log_progress_msg "apparently not running"
  204. log_end_msg 1
  205. exit 1
  206. fi
  207. ;;
  208. reload)
  209. log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  210. if running ; then
  211. reload_server
  212. if ! running ; then
  213. # Process died after we tried to reload
  214. log_progress_msg "died on reload"
  215. log_end_msg 1
  216. exit 1
  217. fi
  218. else
  219. log_progress_msg "server is not running"
  220. log_end_msg 1
  221. exit 1
  222. fi
  223. ;; #;;
  224. *)
  225. N=/etc/init.d/$NAME
  226. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  227. exit 1
  228. ;;
  229. esac
  230. exit 0