Subject: bittorrent: Should operate as a background system service
Package: bittorrent
Version: 3.4.2-11.3
Severity: normal
Tags: patch

*** Please type your report below this line ***
Dear BitTorrent maintainers,

It's not currently possible to run a BitTorrent tracker as a system
service, like Apache, SSH, or other services.  It would be awesome if
that was possible, because you could configure a host to automatically
serve files as a tracker whenever it was restarted.

So, I wrote the attached patch to allow BitTorrent to function as a
system service.  The "bittorrent-tracker" file is the script that
starts and stops the tracker service and also allows you to create
torrents by placing the files in an /incoming/ directory.  The
"bittorrent-tracker.conf" file contains all the configuration options
required by the service.

Don't know if this is too Debian-specific to be upstreamable, but I'd
appreciate your help either way.

Thanks,
Nick

#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          bittorrent
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop BitTorrent tracker
### END INIT INFO
#

# originally derived from: http://www.idealog.us/2005/04/howto_install_a.html

#
# startup code
#

CONFIGFILE="/etc/bittorrent-tracker.conf"

set -e
set -u

. /lib/lsb/init-functions

if [ -e $CONFIGFILE ]
then
    . $CONFIGFILE
fi

# handle partial uninstallation
test -f $TRACK || exit 0

if [ $BTENABLED != 1 ]
then
    log_failure_msg "Starting ${TRACKDESC}" "$TRACKNAME"
    log_progress_msg "${TRACKDESC} is disabled."
    log_progress_msg "\n Edit ${CONFIGFILE} to enable"
    log_end_msg 0
    exit 1
fi

#
# utility functions
#

while_sleep () {
    # usage: while_sleep (time)
    I=0
    while [ $I -lt $1 ]
    do
        sleep 1
        I=$(($I + 1))
        log_progress_msg "."
    done
}

start() {
    # Start the Tracker as well as a seed for all the .torrents

    log_daemon_msg "Starting ${TRACKDESC}" "$(basename $TRACKNAME)"
    start-stop-daemon --oknodo -S -b -c $USER -d $FILESDIR -x $TRACK -- $OPTIONS
    log_end_msg 0

    start_seeding
}

stop() {
    # Stop the Tracker and the seeder

    log_daemon_msg "Stopping ${TRACKDESC}" "$(basename $TRACKNAME)"
    start-stop-daemon --oknodo -K -q -R 30 -n $TRACKNAME
    log_end_msg 0

    stop_seeding
}

start_seeding() {
    # Start the seeder.

    log_daemon_msg "Starting ${SEEDDESC} in ${SLEEP} seconds" \
        "$(basename $SEEDNAME)"
    (sleep $SLEEP && start-stop-daemon --oknodo -S -b -c $USER -d $FILESDIR \
        -x $SEED -- $TORRENTSDIR --ip `$MYIP`)&
    log_end_msg 0
}

stop_seeding() {
    # Stop the seeder.

    log_daemon_msg "Stopping ${SEEDDESC}" "$(basename $SEEDNAME)"
    start-stop-daemon --oknodo -K -q -R 30 -n "$SEEDNAME"
    log_end_msg 0
}

restart_seeding() {
    # Restart the seeder.

    stop_seeding
    start_seeding
}

make() {
    # Create torrent, move to TORRENTSDIR. 
    # Move file from FILESINCOMING to FILESDIR.
    # usage: (filename)

    basename="$(basename $1)"
    torrent="$1.torrent"

    cd "$FILESINCOMING"

    if [ ! -e "${FILESINCOMING}/$basename" ]
    then
        log_failure_msg "$1 must be in ${FILESINCOMING}"
        exit 1
    fi

    start-stop-daemon --oknodo -S -b -c $USER -d "$FILESINCOMING" -x 
$MAKETORRENT -- "$1" "${SERVER}/announce"

    # sleep until we produce a torrent.
    while [ ! -e "$torrent" ]
    do
        sleep 1
        log_progress_msg "."
    done

    mv "$1" "${FILESDIR}/"
    mv "$torrent" "${TORRENTSDIR}/"
}

#
# normal (procedural) script execution below.
#

mkdir -p "$FILESDIR" "$FILESINCOMING" "$TORRENTSDIR"
chown $USER "$FILEROOT" "$FILESDIR" "$FILESINCOMING" "$TORRENTSDIR"

case "${1}" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        start
        ;;

    restart-seeding)
        restart_seeding
        ;;

    make-all)
        # Move each file from FILESINCOMING to FILESDIR. Create torrent.
        # Move torrent to TORRENTSDIR.
        log_daemon_msg "Making torrents for files in $FILESINCOMING" \
            "$(basename $MAKETORRENT)"

        for file in "$FILESINCOMING"/*
        do
            if [ `basename "$file"` = "." ]; then
                continue;
            fi

            make "$file"
        done
        log_end_msg 0

        if [ `$SEEDING` ]
        then
            restart_seeding
        fi
        ;;

    make)
        log_daemon_msg "Making torrent file for $2" "$(basename $MAKETORRENT)"
        make "$2"
        log_end_msg 0

        if [ `$SEEDING` ]
        then
            restart_seeding
        fi
        ;;

    *)
        # Echo usage information
        echo "Usage: $0 {start|stop|restart|restart-seeding|make-all|" \
            "make [filename]}" >&2
        exit 1
        ;;

# All is good with the world.  Exit kindly.
esac

exit 0
# This file contains bittorrent-tracker configuration.

# 0 = disabled
# 1 = enabled
BTENABLED=1

#
# server config
#

PORT=6969
SERVER="http://tracker.example.com:${PORT}";

#
# important file locations
#

# the sharing root
FILEROOT="/var/bittorrent-tracker"

# the directory where BitTorrent distributes files from
FILESDIR="${FILEROOT}/files"

# the directory where incoming files are made into torrents, then
# moved to the share directory above
FILESINCOMING="${FILEROOT}/incoming"

# the directory where users can get the torrents via http from their
# browser.  Serve this over HTTP!
TORRENTSDIR="${FILEROOT}/torrents"

# the database's state file
DFILE="${FILEROOT}/dstate"

# logfiles!
LOGFILE=/var/log/bittorrent-tracker.log

# You probably shouldn't touch options below here.
# You probably shouldn't touch options below here.
# You probably shouldn't touch options below here.

TRACKDESC="BitTorrent Tracker"
SEEDDESC="BitTorrent Seed"
USER="bittorrent-tracker"

#
# executables
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
# optional, can be commented out.
PATH=$PATH:/usr/local/bin

TRACK=/usr/bin/bttrack
TRACKNAME=$(basename $TRACK)
MAKETORRENT=/usr/bin/btmakemetafile
SEED=/usr/bin/btlaunchmany
SEEDNAME=$(basename $SEED)
SEEDING="pgrep -U $USER $SEEDNAME"

# Sleep for X seconds to make sure the tracker starts before the seed
# does.
SLEEP=5

OPTIONS="--dfile ${DFILE} --port ${PORT} --logfile ${LOGFILE} --allowed_dir 
${TORRENTSDIR}"
MYIP="/usr/local/bin/ip-external"
-- System Information:
Debian Release: 6.0.2
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable')
Architecture: i386 (i686)

Kernel: Linux 2.6.32-5-686 (SMP w/1 CPU core)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages bittorrent depends on:
ii  lsb-base                3.2-23.2squeeze1 Linux Standard Base 3.2 init scrip
ii  python                  2.6.6-3+squeeze6 interactive high-level object-orie
ii  python-support          1.0.10           automated rebuilding support for P

Versions of packages bittorrent recommends:
ii  mime-support                  3.48-1     MIME files 'mime.types' & 'mailcap

Versions of packages bittorrent suggests:
pn  bittorrent-gui                <none>     (no description available)

-- no debconf information

-- 
GPG: 0x4C682009 | 084E D805 31D8 5391 1D27  0DE1 9780 FD4D 4C68 2009

Reply via email to