Blob Blame History Raw
#!/bin/sh
#
# tor    The Onion Router
#
# Startup/shutdown script for tor.
#
# chkconfig: - 90 10
# description: Onion Router - A low-latency anonymous proxy
#

. /etc/rc.d/init.d/functions

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/tor
NAME=tor
PIDFILE=/var/run/tor/tor.pid
STOP_TIMEOUT=60
DEFAULT_ARGS="--defaults-torrc /usr/share/tor/defaults-torrc -f /etc/tor/torrc"
VERIFY_ARGS="--verify-config $DEFAULT_ARGS"
ARGS=""

lockfile=/var/lock/subsys/tor

# Raise ulimit based on number of file descriptors available (thanks, Debian)
if [ -r /proc/sys/fs/file-max ]; then
	system_max=`cat /proc/sys/fs/file-max`
	if [ "$system_max" -gt "80000" ] ; then
		MAX_FILEDESCRIPTORS=32768
	elif [ "$system_max" -gt "40000" ] ; then
		MAX_FILEDESCRIPTORS=16384
	elif [ "$system_max" -gt "10000" ] ; then
		MAX_FILEDESCRIPTORS=8192
	else
		MAX_FILEDESCRIPTORS=1024
		cat << EOF

Warning: Your system has very few filedescriptors available in total.

Maybe you should try raising that by adding 'fs.file-max=100000' to your
/etc/sysctl.conf file.  Feel free to pick any number that you deem appropriate.
Then run 'sysctl -p'.  See /proc/sys/fs/file-max for the current value, and
file-nr in the same directory for how many of those are used at the moment.

EOF
	fi
else
	MAX_FILEDESCRIPTORS=8192
fi

NICE=""

test -x $DAEMON || exit 0

check_config () {
	if ! $DAEMON $VERIFY_ARGS >/dev/null; then
		echo -n $"Checking if $NAME configuration is valid"
		$DAEMON --verify-config >&2
		exit 1
	fi
}

start() {
	if [ -n "$MAX_FILEDESCRIPTORS" ]; then
		echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
		if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
			echo "."
		else
			echo ": FAILED."
		fi
	fi
	check_config
    echo -n $"Starting $NAME: "
    daemon --pidfile=$PIDFILE $DAEMON $DEFAULT_ARGS $ARGS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    echo -n $"Stopping $NAME: "
    killproc -p $PIDFILE -d $STOP_TIMEOUT $DAEMON -INT
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

restart() {
	check_config
    stop
    start
}

reload() {
	check_config
    echo -n $"Reloading $NAME: "
    killproc -p $PIDFILE $DAEMON -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

rh_status() {
	if test ! -r $(dirname $PIDFILE); then
		echo "cannot read PID file $PIDFILE"
		return 4
	fi
	pid=`cat $PIDFILE 2>/dev/null` || true
	if test ! -f $PIDFILE -o -z "$pid"; then
		echo "$NAME is not running"
		return 3
	fi
	if ps "$pid" >/dev/null 2>&1; then
		echo "$NAME is running (pid: $pid)"
		return 0
	else
		echo "$NAME is not running"
		return 1
	fi
}

rh_status_q() {
    rh_status >/dev/null 2>/dev/null
    return $?
}

case $1 in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}"
        exit 2
        ;;
esac

exit $?