Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 122598b846ec443f6f650fd5b2ebf876 > files > 6

389-admin-1.1.23-1.fc14.x86_64.rpm

#!/bin/sh
#
# dirsrv-admin    This starts and stops dirsrv-admin
#
# chkconfig:   - 21 79
# description: dirsrv-admin Admin Server
# processname: /usr/sbin/httpd
# piddir:      /var/run/dirsrv
#

# Source function library.
if [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
fi
# Source networking configuration.
if [ -f /etc/sysconfig/network ] ; then
. /etc/sysconfig/network
fi

# Check that networking is up.
if [ "${NETWORKING}" = "no" ]
then
    echo "Networking is down"
    exit 0
fi

# figure out which echo we're using
ECHO_N=`echo -n`

# some shells echo cannot use -n - linux echo by default cannot use \c
echo_n()
{
    if [ "$ECHO_N" = '-n' ] ; then
        echo "$*\c"
    else
        echo -n "$*"
    fi
}

# failure and success are not defined on some platforms
type failure > /dev/null 2>&1 || {
failure()
{
    echo_n " FAILED"
}
}

type success > /dev/null 2>&1 || {
success()
{
    echo_n " SUCCESS"
}
}

# On Solaris /var/run is in tmpfs and gets wiped out upon reboot
# we have to recreate the /var/run/dirsrv-admin directory
# We also have to make sure that the directory is writable
# by the directory server process
# the argument to this function is the server instance directory,
# which must have a dse.ldif file in it
fix_pid_dir_ownership()
{
    if [ -d $piddir ] ; then
        owner=`grep \^sysuser /etc/dirsrv/admin-serv/adm.conf | awk '{print $2}'`
        dirowner=`ls -ld $piddir | awk '{print $3}'`
        dirgrp=`ls -ld $piddir | awk '{print $4}'`
        if [ "$owner" != "$dirowner" ]; then
            groups $owner | grep $dirgrp > /dev/null 2>&1
            rc=$?
            if [ $rc -eq 0 ]; then
                chmod 770 $piddir
            else
                echo_n "$piddir is not writable for $owner"
                failure; echo
                exit 1
            fi
        fi
    else
        mkdir -p $piddir
        owner=`grep \^nsslapd-localuser $1/dse.ldif | awk '{print $2}'`
        if [ -n "$owner" ] ; then
            chown $owner $piddir
            chmod 700 $piddir
        fi
    fi
}

start_script=/usr/sbin/start-ds-admin
restart_script=/usr/sbin/restart-ds-admin
stop_script=/usr/sbin/stop-ds-admin
exec=`grep "^HTTPD=" $start_script | awk -F= '{print $2}'`
prog="dirsrv-admin"
# PID directory
piddir="/var/run/dirsrv"
# PID file
pidfile=$piddir/admin-serv.pid
lockfile=/var/lock/subsys/dirsrv-admin

[ -f $exec ] || exit 0

umask 077

RETVAL=0

# since we use the start script to start admin, we source the
# init config file there, not here
# if we ever get rid of the start script, we'll have to uncomment
# the following line
#[ -f /etc/sysconfig/dirsrv-admin ] && . /etc/sysconfig/dirsrv-admin

start() {
    if [ ! -f $start_script ]; then
        echo_n "*** Error: $start_script does not exist"
        failure; echo
        exit 1
    fi
    echo  "Starting $prog: "

    # the server creates pidfile and writes the pid to it when it is fully
    # started and available to serve clients
    server_running=0
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        if kill -0 $pid > /dev/null 2>&1 ; then
            echo_n " already running"
            success; echo
            server_running=1
        else
            echo_n " not running, but pid file exists - attempt to start anyway..."
            rm -f $pidfile
        fi
    fi
    server_started=0
    if [ $server_running -eq 0 ] ; then
        rm -f $pidfile
        fix_pid_dir_ownership 
        $start_script
        if [ $? -eq 0 ]; then
            server_started=1 # well, perhaps not running, but started ok
        else
            failure; echo
            RETVAL=1
        fi
    fi
    if [ $server_started -eq 1 ] ; then
        loop_counter=1
		# wait for 10 minutes (600 times 1 seconds)
        max_count=600
        while test $loop_counter -le $max_count ; do
            loop_counter=`expr $loop_counter + 1`
            if test ! -f $pidfile ; then
                sleep 1
            else
                pid=`cat $pidfile`
                break
            fi
        done
        if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
            success; echo
        else
            echo_n "*** Error: $prog failed to start"
            failure; echo
            RETVAL=1
        fi
    fi
    [ $RETVAL -eq 0 -a -d /var/lock/subsys ] && touch $lockfile
}

stop() {
    if [ ! -f $stop_script ]; then
       echo_n "$stop_script does not exist"
       failure; echo
       exit 1
    fi
    echo "Shutting down $prog: "
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        server_stopped=0
        if kill -0 $pid > /dev/null 2>&1 ; then
            kill $pid
            if [ $? -eq 0 ]; then
                server_stopped=1
            else
                failure; echo
                RETVAL=1
            fi
        fi
        if [ $server_stopped -eq 1 ] ; then
            loop_counter=1
			# wait for 10 minutes (600 times 1 second)
            max_count=600
            while test $loop_counter -le $max_count; do
                loop_counter=`expr $loop_counter + 1`
                if kill -0 $pid > /dev/null 2>&1 ; then
                    sleep 1
                else
                    if test -f $pidfile ; then
                        rm -f $pidfile
                    fi
                    break
                fi
            done
            if test -f $pidfile ; then
                echo_n "*** Error: $prog failed to stop"
                failure; echo
                RETVAL=1
            else
                success; echo
                rm -f $pidfile
            fi
        fi
    fi
    [ $RETVAL -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}


status() {
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        if kill -0 $pid > /dev/null 2>&1 ; then
            echo "$prog (pid $pid) is running..."
        else
            echo "$prog dead but pid file exists"
        fi
    else
        echo "$prog is stopped"
    fi
}


case "$1" in
    start|stop|restart|reload|status)
        $1
        ;;
    condrestart)
        [ ! -f $lockfile ] || restart
        ;;
    *)
        echo Unknown command $1
        echo "Usage: $0 {start|stop|status|restart|condrestart}"
        exit 2
esac