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