home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / init.d / tor < prev    next >
Encoding:
Text File  |  2013-01-10  |  5.6 KB  |  236 lines

  1. #! /bin/bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          tor
  5. # Required-Start:    $local_fs $remote_fs $network $named $time
  6. # Required-Stop:     $local_fs $remote_fs $network $named $time
  7. # Should-Start:      $syslog
  8. # Should-Stop:       $syslog
  9. # Default-Start:
  10. # Default-Stop:      0 1 6 2 3 4 5
  11. # Short-Description: Starts The Onion Router daemon processes
  12. # Description:       Start The Onion Router, a TCP overlay
  13. #                    network client that provides anonymous
  14. #                    transport.
  15. ### END INIT INFO
  16.  
  17. # Load the VERBOSE setting and other rcS variables
  18. . /lib/init/vars.sh
  19.  
  20. # Define LSB log_* functions.
  21. . /lib/lsb/init-functions
  22.  
  23. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  24. DAEMON=/usr/sbin/tor
  25. NAME=tor
  26. DESC="tor daemon"
  27. TORPIDDIR=/var/run/tor
  28. TORPID=$TORPIDDIR/tor.pid
  29. DEFAULTSFILE=/etc/default/$NAME
  30. WAITFORDAEMON=60
  31. DEFAULT_ARGS="--defaults-torrc /usr/share/tor/tor-service-defaults-torrc"
  32. VERIFY_ARGS="--verify-config $DEFAULT_ARGS"
  33. USE_AA_EXEC="yes"
  34. ARGS=""
  35. if [ "${VERBOSE:-}" != "yes" ]; then
  36.     ARGS="$ARGS --hush"
  37. fi
  38.  
  39. # Let's try to figure our some sane defaults:
  40. if [ -r /proc/sys/fs/file-max ]; then
  41.     system_max=`cat /proc/sys/fs/file-max`
  42.     if [ "$system_max" -gt "80000" ] ; then
  43.         MAX_FILEDESCRIPTORS=32768
  44.     elif [ "$system_max" -gt "40000" ] ; then
  45.         MAX_FILEDESCRIPTORS=16384
  46.     elif [ "$system_max" -gt "10000" ] ; then
  47.         MAX_FILEDESCRIPTORS=8192
  48.     else
  49.         MAX_FILEDESCRIPTORS=1024
  50.         cat << EOF
  51.  
  52. Warning: Your system has very few filedescriptors available in total.
  53.  
  54. Maybe you should try raising that by adding 'fs.file-max=100000' to your
  55. /etc/sysctl.conf file.  Feel free to pick any number that you deem appropriate.
  56. Then run 'sysctl -p'.  See /proc/sys/fs/file-max for the current value, and
  57. file-nr in the same directory for how many of those are used at the moment.
  58.  
  59. EOF
  60.     fi
  61. else
  62.     MAX_FILEDESCRIPTORS=8192
  63. fi
  64.  
  65. NICE=""
  66.  
  67. test -x $DAEMON || exit 0
  68.  
  69. # Include tor defaults if available
  70. if [ -f $DEFAULTSFILE ] ; then
  71.     . $DEFAULTSFILE
  72. fi
  73.  
  74. wait_for_deaddaemon () {
  75.     pid=$1
  76.     sleep 1
  77.     if test -n "$pid"
  78.     then
  79.         if kill -0 $pid 2>/dev/null
  80.         then
  81.             cnt=0
  82.             while kill -0 $pid 2>/dev/null
  83.             do
  84.                 cnt=`expr $cnt + 1`
  85.                 if [ $cnt -gt $WAITFORDAEMON ]
  86.                 then
  87.                     log_action_end_msg 1 "still running"
  88.                 fi
  89.                 sleep 1
  90.                 [ "`expr $cnt % 3`" != 2 ] || log_action_cont_msg ""
  91.             done
  92.         fi
  93.     fi
  94.     log_action_end_msg 0
  95. }
  96.  
  97.  
  98. check_torpiddir () {
  99.     if test ! -d $TORPIDDIR; then
  100.         mkdir -m 02750 "$TORPIDDIR"
  101.         chown debian-tor:debian-tor "$TORPIDDIR"
  102.         ! [ -x /sbin/restorecon ] || /sbin/restorecon "$TORPIDDIR"
  103.     fi
  104.  
  105.     if test ! -x $TORPIDDIR; then
  106.         log_action_end_msg 1 "cannot access $TORPIDDIR directory, are you root?"
  107.         exit 1
  108.     fi
  109. }
  110.  
  111. check_config () {
  112.     if ! $DAEMON $VERIFY_ARGS > /dev/null; then
  113.         log_failure_msg "Checking if $NAME configuration is valid"
  114.         $DAEMON --verify-config >&2
  115.         exit 1
  116.     fi
  117. }
  118.  
  119.  
  120. case "$1" in
  121.   start)
  122.     if [ "$RUN_DAEMON" != "yes" ]; then
  123.         log_action_msg "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  124.         exit 0
  125.     fi
  126.  
  127.     if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  128.         [ "${VERBOSE:-}" != "yes" ] || log_action_begin_msg "Raising maximum number of filedescriptors (ulimit -n) for tor to $MAX_FILEDESCRIPTORS"
  129.         if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  130.             [ "${VERBOSE:-}" != "yes" ] || log_action_end_msg 0
  131.         else
  132.             [ "${VERBOSE:-}" != "yes" ] || log_action_end_msg 1
  133.         fi
  134.     fi
  135.  
  136.     check_torpiddir
  137.     check_config
  138.  
  139.     log_action_begin_msg "Starting $DESC"
  140.  
  141.     if start-stop-daemon --stop --signal 0 --quiet --pidfile $TORPID --exec $DAEMON; then
  142.         log_action_end_msg 0 "already running"
  143.     else
  144.         if [ "$USE_AA_EXEC" = "yes" ] &&
  145.            [ -x /usr/sbin/aa-status ] && \
  146.            [ -x /usr/sbin/aa-exec ] && \
  147.            [ -e /etc/apparmor.d/system_tor ] && \
  148.            /usr/sbin/aa-status --enabled ; then
  149.             AA_EXEC="--startas /usr/sbin/aa-exec"
  150.             AA_EXEC_ARGS="--profile=system_tor -- $DAEMON"
  151.         else
  152.             AA_EXEC=""
  153.             AA_EXEC_ARGS=""
  154.         fi
  155.         if start-stop-daemon --start --quiet \
  156.             --pidfile $TORPID \
  157.             $NICE \
  158.             $AA_EXEC \
  159.             --exec $DAEMON -- $AA_EXEC_ARGS $DEFAULT_ARGS $ARGS
  160.         then
  161.             log_action_end_msg 0
  162.         else
  163.             log_action_end_msg 1
  164.         fi
  165.     fi
  166.     ;;
  167.   stop)
  168.     log_action_begin_msg "Stopping $DESC"
  169.     pid=`cat $TORPID 2>/dev/null` || true
  170.  
  171.     if test ! -f $TORPID -o -z "$pid"; then
  172.         log_action_end_msg 0 "not running - there is no $TORPID"
  173.         exit 0
  174.     fi
  175.  
  176.     if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  177.         wait_for_deaddaemon $pid
  178.     elif kill -0 $pid 2>/dev/null; then
  179.         log_action_end_msg 1 "Is $pid not $NAME?  Is $DAEMON a different binary now?"
  180.     else
  181.         log_action_end_msg 1 "$DAEMON died: process $pid not running; or permission denied"
  182.     fi
  183.     ;;
  184.   reload|force-reload)
  185.     check_config
  186.  
  187.     log_action_begin_msg "Reloading $DESC configuration"
  188.     pid=`cat $TORPID 2>/dev/null` || true
  189.  
  190.     if test ! -f $TORPID -o -z "$pid"; then
  191.         log_action_end_msg 1 "not running - there is no $TORPID"
  192.         exit 0
  193.     fi
  194.  
  195.     if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  196.     then
  197.         log_action_end_msg 0
  198.     elif kill -0 $pid 2>/dev/null; then
  199.         log_action_end_msg 1 "Is $pid not $NAME?  Is $DAEMON a different binary now?"
  200.     else
  201.         log_action_end_msg 1 "$DAEMON died: process $pid not running; or permission denied"
  202.     fi
  203.     ;;
  204.   restart)
  205.     check_config
  206.  
  207.     $0 stop
  208.     sleep 1
  209.     $0 start
  210.     ;;
  211.   status)
  212.     if test ! -r $(dirname $TORPID); then
  213.         log_failure_msg "cannot read PID file $TORPID"
  214.         exit 4
  215.     fi
  216.     pid=`cat $TORPID 2>/dev/null` || true
  217.     if test ! -f $TORPID -o -z "$pid"; then
  218.         log_failure_msg "$NAME is not running"
  219.         exit 3
  220.     fi
  221.     if ps "$pid" >/dev/null 2>&1; then
  222.         log_success_msg "$NAME is running"
  223.         exit 0
  224.     else
  225.         log_failure_msg "$NAME is not running"
  226.         exit 1
  227.     fi
  228.     ;;
  229.   *)
  230.     log_action_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
  231.     exit 1
  232.     ;;
  233. esac
  234.  
  235. exit 0
  236.