tuntox.debian-init 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: tuntox
  4. # Required-start: $network $syslog
  5. # Required-stop: $network $syslog
  6. # Default-start: 2 3 4 5
  7. # Default-stop: 0 1 6
  8. # Short-description: This script takes care of starting and stopping the Tutnox daemon.
  9. # Description: tuntox is a daemon for tunnelling TCP connections over the Tox protocol
  10. # Notes
  11. # Modeled after init script for NSCA written by Ethan Galstad (nagios@nagios.org)
  12. ### END INIT INFO
  13. #
  14. # If you want to use tuntox as a system service, do the following:
  15. # cp tuntox /usr/bin/
  16. # cp scripts/tuntox.debian-init /etc/init.d/tuntox
  17. # update-rc.d tuntox defaults
  18. #
  19. # Edit the User ID below (it needs to be a number in static builds)
  20. #
  21. # User ID to which the program will drop
  22. USERID=13
  23. DESC="TCP-over-Tox daemon"
  24. NAME=tuntox
  25. DAEMON=/usr/bin/$NAME
  26. DAEMON_CFG_DIR=/etc/tuntox/
  27. PIDFILE=/var/run/$NAME/$NAME.pid
  28. DAEMON_ARGS="-C $DAEMON_CFG_DIR -D -F $PIDFILE -U $USERID -d"
  29. SCRIPTNAME=/etc/init.d/$NAME
  30. # Exit if packages not installed
  31. [ -x "$DAEMON" ] || exit 5
  32. # Read configuration variable file if it is present
  33. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  34. # Load the VERBOSE setting and other rcS variables
  35. [ -x /lib/init/vars.sh ] && . /lib/init/vars.sh
  36. # Load the VERBOSE setting and other rcS variables
  37. . /lib/init/vars.sh
  38. # Define LSB log_* functions.
  39. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  40. . /lib/lsb/init-functions
  41. mkdir -p /var/run/$NAME
  42. chown $USERID /var/run/$NAME
  43. #
  44. # Function that starts the daemon/service
  45. #
  46. do_start()
  47. {
  48. # Return value
  49. # 0 if daemon has been started
  50. # 1 if daemon was already started
  51. # 2 if daemon could not be started
  52. # Test if daemon already exist
  53. start-stop-daemon --test --start --quiet --pidfile $PIDFILE \
  54. --exec $DAEMON > /dev/null \
  55. || return 1
  56. # Start it if not
  57. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  58. --exec $DAEMON -- $DAEMON_ARGS \
  59. || return 2
  60. # Add code here, if the process need to be ready to handle
  61. # request form service that depend on it. At last resort,
  62. # sleep some time.
  63. sleep 1s
  64. }
  65. #
  66. # Function that stop the dameon
  67. #
  68. do_stop() {
  69. # Return value
  70. # 0 if daemon has been stopped
  71. # 1 if daemon was already stopped
  72. # 2 if daemon could not be stop
  73. # other if a failure occured
  74. ###
  75. # When nsca exits, clean lock file.
  76. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  77. RETVAL="$?"
  78. # If process was already stop. Nothing to do. Exit with 1
  79. [ "$RETVAL" = 1 ] && return 1
  80. # If process could not be stop. Exit with 2
  81. [ "$RETVAL" = 2 ] && return 2
  82. # If the daemon is only run from initscript
  83. # and forks, wait for children to finish.
  84. # If this two condition are not satisfied the add code
  85. # that waits for the process to drop all resources that
  86. # could be needed by services started subsequently.
  87. # At a last resort, sleep for some time.
  88. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON
  89. [ "$?" = 2 ] && return 2
  90. # Many daemon do not delete their pidfiles when they exit
  91. rm -f $PIDFILE
  92. return "$RETVAL"
  93. }
  94. #
  95. # Function that sends a SIGHUP to the daemon
  96. #
  97. do_reload()
  98. {
  99. #
  100. # A basic reload function. Not used.
  101. start-stop-daemon --stop --quiet --signal 1 --pidfile $PIDFILE --name $NAME
  102. return 0
  103. }
  104. case "$1" in
  105. start)
  106. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  107. do_start
  108. case "$?" in
  109. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  110. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  111. *) log_end_msg 1 ;; # Failed to start. Fatal
  112. esac
  113. ;;
  114. stop)
  115. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  116. do_stop
  117. case "$?" in
  118. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  119. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  120. *) log_end_msg 1 ;; # Failed to stop. Fatal
  121. esac
  122. ;;
  123. #reload|force-reload)
  124. # #
  125. # # If do_reload() is not implemented then leave this comment out
  126. # # and leave "force-reload" as an alias for "restart".
  127. # #
  128. # log_daemon_msg "Reloading $DESC" "$NAME"
  129. # do_reload
  130. # # Note : the basic do_reload always return 0
  131. # log_end_msg "$?"
  132. # ;;
  133. restart|force-reload)
  134. #
  135. # If the "reload" option is implemented then remove
  136. # the "force-reload" alias.
  137. log_daemon_msg "Restarting $DESC" "$NAME"
  138. do_stop
  139. case "$?" in
  140. 0|1)
  141. do_start
  142. case "$?" in
  143. 0) log_end_msg 0 ;;
  144. 1) log_end_msg 1 ;; # Old process still running
  145. *) log_end_msg 1 ;; # Failed to start
  146. esac
  147. ;;
  148. *)
  149. # Failed to stop
  150. log_end_msg 1
  151. ;;
  152. esac
  153. ;;
  154. status)
  155. log_daemon_msg "Checking status of $DESC" "$NAME"
  156. status_of_proc -p $PIDFILE "$DAEMON" "$NAME"
  157. log_end_msg "$?"
  158. exit $?
  159. ;;
  160. *)
  161. # echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
  162. echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
  163. exit 3
  164. ;;
  165. esac
  166. # ":" = "exit 0"
  167. :