ipcclean.sh 2.93 KB
Newer Older
1 2
#!/bin/sh
#
3
# $Header: /cvsroot/pgsql/src/bin/ipcclean/Attic/ipcclean.sh,v 1.12 2001/09/30 22:17:50 momjian Exp $
4
#
5 6 7 8 9 10 11 12 13 14 15

CMDNAME=`basename $0`

if [ "$1" = '-?' -o "$1" = "--help" ]; then
    echo "$CMDNAME cleans up shared memory and semaphores from aborted PostgreSQL backends."
    echo
    echo "Usage:"
    echo "  $CMDNAME"
    echo
    echo "Note: Since the utilities underlying this script are very different"
    echo "from platform to platform, chances are that it might not work on"
16
    echo "yours. If that is the case, please write to <pgsql-bugs@postgresql.org>"
17 18 19 20 21 22
    echo "so that your platform can be supported in the future."
    exit 0
fi

if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
then
23
  (
24 25
    echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
    echo "as the (unprivileged) user that owned the server process."
26
  ) 1>&2
27 28 29 30 31 32 33 34 35
    exit 1
fi

EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`

#-----------------------------------
# List of platform-specific hacks
# Feel free to add yours here.
#-----------------------------------
36 37 38 39 40 41 42 43 44 45 46
#
# This is QNX 4.25
#
if [ `uname` = 'QNX' ]; then
    if ps -eA  | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then
        echo "$CMDNAME: You still have a postmaster running." 1>&2
        exit 1
    fi
    rm -f /dev/shmem/PgS*
    exit $?
fi
47 48 49 50 51 52
#
# This is based on RedHat 5.2.
#
if [ `uname` = 'Linux' ]; then
    did_anything=

53
    if ps x | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then
54
        echo "$CMDNAME: You still have a postmaster running." 1>&2
55 56 57 58
        exit 1
    fi

    # shared memory
Peter Eisentraut's avatar
Peter Eisentraut committed
59
    for val in `ipcs -m -p | grep '^[0-9]' | awk '{printf "%s:%s:%s\n", $1, $3, $4}'`
60
    do
Peter Eisentraut's avatar
Peter Eisentraut committed
61 62 63 64 65 66 67 68
	save_IFS=$IFS
	IFS=:
	set X $val
	shift
	IFS=$save_IFS
	ipcs_shmid=$1
	ipcs_cpid=$2
	ipcs_lpid=$3
69

Peter Eisentraut's avatar
Peter Eisentraut committed
70 71
        # Note: We can do -n here, because we know the platform.
        echo -n "Shared memory $ipcs_shmid ... "
72 73 74 75

        # Don't do anything if process still running.
        # (This check is conceptually phony, but it's
        # useful anyway in practice.)
Peter Eisentraut's avatar
Peter Eisentraut committed
76
        ps hj $ipcs_cpid $ipcs_lpid >/dev/null 2>&1
77
        if [ "$?" -eq 0 ]; then
Peter Eisentraut's avatar
Peter Eisentraut committed
78 79 80 81 82 83
            echo "skipped; process still exists (pid $ipcs_cpid or $ipcs_lpid)."
	    continue
	fi

        # try remove
        ipcrm shm $ipcs_shmid
84
        if [ "$?" -eq 0 ]; then
Peter Eisentraut's avatar
Peter Eisentraut committed
85
            did_anything=t
86
        else
Peter Eisentraut's avatar
Peter Eisentraut committed
87
            exit
88 89 90 91 92 93 94 95
        fi
    done

    # semaphores
    for val in `ipcs -s -c | grep '^[0-9]' | awk '{printf "%s\n", $1}'`; do
        echo -n "Semaphore $val ... "
        # try remove
        ipcrm sem $val
96
        if [ "$?" -eq 0 ]; then
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            did_anything=t
        else
            exit
        fi
    done

    [ -z "$did_anything" ] && echo "$CMDNAME: nothing removed" && exit 1
    exit 0
fi # end Linux


# This is the original implementation. It seems to work
# on FreeBSD, SunOS/Solaris, HP-UX, IRIX, and probably
# some others.

ipcs | egrep '^m .*|^s .*' | egrep "$EffectiveUser" | \
113
awk '{printf "ipcrm -%s %s\n", $1, $2}' '-' | sh