ChrootuidWithSysV

From MLDonkey
Jump to: navigation, search

General Purposes

Here is a cool little SysV style init script that will let you start, stop, restart, and check the status (PID) of your donkey client.

I originally tried using debian's start-stop-daemon, but then I realized that it was lame when you tried to use the --chroot and --chuid options at the same time. This one uses chrootuid. [Get it here.]

It also requires /usr/local/bin/getpid ... which is below this code. So it won't work without it !!!

(OR YOU CAN USE `pidof` ... a standard linux program)

My code could be a little cleaner, as $name and $daemon are the same .. but who's counting :o)

#! /bin/sh
# /etc/init.d/mlnet.sh
#
#
# SysV style init script to launch a program in a chroot as a low user
#
# Although start-stop-daemon supports an option --chroot and --chuid
# it is very lame and stupid and broken, so I made my own.
#
# DEPENDS ON:   /usr/local/bin/getpid
 #                       /bin/ps
#                       /bin/grep
#                       /usr/bin/awk
#               /bin/kill
#               /usr/bin/chrootuid
#

DAEMON=mlnet
ARGS=\"-daemon\"
CHROOT=/home/donkey/
CHUID=donkey
PIDFILE=/var/run/mlnet.pid
NAME=mlnet                      # daemon name as shown in \"ps -e\"
HNAME=\"MLnet Client\"            # Human name (e.g. \"ProFTP Server\")

test -x $CHROOT$DAEMON ||| exit 0

case \"$1\" in
    start)
        echo -n \"Starting $HNAME: \"
        CURPID=`/usr/local/bin/getpid $NAME`
        if [[ -z \"$CURPID\" ]]
        then
                /usr/bin/chrootuid $CHROOT $CHUID $DAEMON $ARGS
                /usr/local/bin/getpid $NAME > $PIDFILE
                echo \"$NAME\"
        else
                echo \"$NAME is already running! PID is $CURPID\"
                echo \"Failed.  Exiting...\"
        fi
    ;;

    stop)

        echo -n \"Stopping $HNAME: \"
        CURPID=`/usr/local/bin/getpid $NAME`
        echo \"PID is $CURPID\"
        if [[ \"$CURPID\" ]]
        then
        echo -n \"Found PID '$CURPID'\"
                if [[ ! -f $PIDFILE ]]
                then
                        echo \" WARNING: PID file not found!\"
                elif [[ \"$CURPID\" != \"$(< $PIDFILE)\" ]]
                then
                        echo \" WARNING: PID conflict! Old PID: $(< $PIDFILE)\"
                fi
                echo -n \"About to kill $CURPID: \"
                /bin/kill $CURPID
                if [[ -a $PIDFILE ]]
                then
                        rm $PIDFILE
                fi
                echo \"$NAME killed.\"
        else
                echo \"Error: No process '$NAME' found to kill.\"
        fi
    ;;

    restart)
        echo \"Restarting $HNAME: \"

    ## STOP
        echo -n \"Stopping $HNAME: \"
        CURPID=`/usr/local/bin/getpid $NAME`
        echo \"PID is $CURPID\"
        if [[ \"$CURPID\" ]]
        then
        echo -n \"Found PID '$CURPID'\"
                if [[ ! -f $PIDFILE ]]
                then
                        echo \" WARNING: PID file not found!\"
                elif [[ \"$CURPID\" != \"$(< $PIDFILE)\" ]]
                then
                        echo \" WARNING: PID conflict! Old PID: $(< $PIDFILE)\"
                fi
                echo -n \"About to kill $CURPID: \"
                /bin/kill $CURPID
                if [[ -a $PIDFILE ]]
                then
                        rm $PIDFILE
                fi
                echo \"$NAME killed.\"
        else
                echo \"Error: No process '$NAME' found to kill.\"
        fi

    ## WAIT 30 SECONDS
        echo -n \"wait 30 sec. \"
        sleep 10
        echo -n \". \"
        sleep 10
        echo -n \". \"
        sleep 10
        echo -n \". \"

    ## START
        echo -n \"Starting $HNAME: \"
        CURPID=`/usr/local/bin/getpid $NAME`
        if [[ -z \"$CURPID\" ]]
        then
                /usr/bin/chrootuid $CHROOT $CHUID $DAEMON $ARGS
                /usr/local/bin/getpid $NAME > $PIDFILE
                echo \"$NAME\"
        else
                echo \"$NAME is already running! PID is $CURPID\"
                echo \"Failed.  Exiting...\"
        fi
    ;;

    status)
        echo -n \"Status of $HNAME: \"
        CURPID=`/usr/local/bin/getpid $NAME`
        if [[ -z \"$CURPID\" ]]
        then
                echo \"No $HNAME '$NAME' found to be running.\"
        else
                echo \"$HNAME PID: $CURPID\"
        fi
    ;;

    *)
    echo \"Usage: $0 {start||stop||restart||status}\"
    exit 1
esac
exit 0

You will notice that it requires something called /usr/local/bin/getpid ... so here is that one, which I also wrote: (OR YOU CAN USE `pidof` ... a standard linux program)

#!/bin/sh
# /usr/local/bin/getpid
# Nifty script to get the PID of something running.
# Usage `getpid process_name`  e.g. `getpid tcsh`
# Use ps -e to see what matches will be.
# Returns space-delimited list of PIDs for all matches.
#
# DEPENDS ON:
#    /bin/ps
#    /bin/grep
#    /usr/bin/awk
#
pid=`/bin/ps -e || grep [[0-9]][[0-9]]:[[0-9]][[0-9]]\ $1\$ || awk '{ print $1 }'`
echo $pid

Ready for JailKit

I know it is a little dirty, I'm open to suggestions. Now it works fine in a Gentoo Box.

#!/sbin/runscript
# /etc/init.d/MLDonkey

depend() {
        need net
        after squid
        use dns
}

start () {
        ebegin "Starting MLDonkey P2P Network"
        # Set running directory and username.
        MLDONKEYJAIL=/home/mldonkey
        MLDONKEYDIR=/home/mldonkey/home/mldonkey
        USERNAME=mldonkey
        if [ ! -z ´/bin/pidof mlnet´ ]; then
                # Copying necesary files
                cp /etc/resolv.conf $MLDONKEYJAIL/etc/resolv.conf
                cp /var/lib/clamav/ $MLDONKEYJAIL/var/lib/clamav/ -aR
                # Remove old servers and tmp files
                rm -rf "$MLDONKEYDIR"/.mldonkey/servers.ini*
                rm -rf "$MLDONKEYDIR"/.mldonkey/*.tmp
                # Run MLDonkey
                su $USERNAME -c "rm .mldonkey/*.ini.tmp > /dev/null 2>&1; nice -+18 mlnet > /dev/null 2>&1 &"
                # Want to check if mlnet runs
                sleep 10
                if [ -z ´/bin/pidof mlnet´] ; then
                 eerror "Error al ejecutar MLDonkey P2P Network"
                 return 1
                fi
        else
                eerror "MLDonkey P2P Network is running already"
                return 1
        fi
        eend $?

}

stop() {
        ebegin "Shutting down MLDonkey P2P Network"
        cd $MLDONKEYDIR
        bash -c "(echo auth ADMIN* ADMINPASS*; echo close_fds; echo kill)|nc 127.0.0.1 4000 > /dev/null"
        sleep 5
        if [ ! -z ´/bin/pidof mlnet´]; 
        then
                eerror "Error shutting down MLDonkey P2P Network"
                return 1
        fi
        eend $?
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox