OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / bin / ipcclean
1 #!/bin/sh
2 #
3 # $Header: /cvsroot/pgsql-server/src/bin/ipcclean/ipcclean.sh,v 1.14 2003/07/23 08:47:23 petere Exp $
4 #
5
6 CMDNAME=`basename $0`
7
8 if [ "$1" = '-?' -o "$1" = "--help" ]; then
9     echo "$CMDNAME cleans up shared memory and semaphores from aborted PostgreSQL"
10     echo "backends."
11     echo
12     echo "Usage:"
13     echo "  $CMDNAME"
14     echo
15     echo "Note: Since the utilities underlying this script are very different"
16     echo "from platform to platform, chances are that it might not work on"
17     echo "yours. If that is the case, please write to <pgsql-bugs@postgresql.org>"
18     echo "so that your platform can be supported in the future."
19     exit 0
20 fi
21
22 if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
23 then
24   (
25     echo "$CMDNAME: cannot be run as root" 1>&2
26     echo "Please log in (using, e.g., \"su\") as the (unprivileged) user that" 1>&2
27     echo "owned the server process." 1>&2
28   ) 1>&2
29     exit 1
30 fi
31
32 EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`
33
34 #-----------------------------------
35 # List of platform-specific hacks
36 # Feel free to add yours here.
37 #-----------------------------------
38 #
39 # This is QNX 4.25
40 #
41 if [ `uname` = 'QNX' ]; then
42     if ps -eA  | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then
43         echo "$CMDNAME: a postmaster is still running" 1>&2
44         exit 1
45     fi
46     rm -f /dev/shmem/PgS*
47     exit $?
48 fi
49 #
50 # This is based on RedHat 5.2.
51 #
52 if [ `uname` = 'Linux' ]; then
53     did_anything=
54
55     if ps x | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then
56         echo "$CMDNAME: a postmaster is still running" 1>&2
57         exit 1
58     fi
59
60     # shared memory
61     for val in `ipcs -m -p | grep '^[0-9]' | awk '{printf "%s:%s:%s\n", $1, $3, $4}'`
62     do
63         save_IFS=$IFS
64         IFS=:
65         set X $val
66         shift
67         IFS=$save_IFS
68         ipcs_shmid=$1
69         ipcs_cpid=$2
70         ipcs_lpid=$3
71
72         # Note: We can do -n here, because we know the platform.
73         echo -n "Shared memory $ipcs_shmid ... "
74
75         # Don't do anything if process still running.
76         # (This check is conceptually phony, but it's
77         # useful anyway in practice.)
78         ps hj $ipcs_cpid $ipcs_lpid >/dev/null 2>&1
79         if [ "$?" -eq 0 ]; then
80             echo "skipped; process still exists (pid $ipcs_cpid or $ipcs_lpid)."
81             continue
82         fi
83
84         # try remove
85         ipcrm shm $ipcs_shmid
86         if [ "$?" -eq 0 ]; then
87             did_anything=t
88         else
89             exit
90         fi
91     done
92
93     # semaphores
94     for val in `ipcs -s -c | grep '^[0-9]' | awk '{printf "%s\n", $1}'`; do
95         echo -n "Semaphore $val ... "
96         # try remove
97         ipcrm sem $val
98         if [ "$?" -eq 0 ]; then
99             did_anything=t
100         else
101             exit
102         fi
103     done
104
105     [ -z "$did_anything" ] && echo "$CMDNAME: nothing removed" && exit 1
106     exit 0
107 fi # end Linux
108
109
110 # This is the original implementation. It seems to work
111 # on FreeBSD, SunOS/Solaris, HP-UX, IRIX, and probably
112 # some others.
113
114 ipcs | egrep '^m .*|^s .*' | egrep "$EffectiveUser" | \
115 awk '{printf "ipcrm -%s %s\n", $1, $2}' '-' | sh