1#
2# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.
8#
9# This code is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12# version 2 for more details (a copy is included in the LICENSE file that
13# accompanied this code).
14#
15# You should have received a copy of the GNU General Public License version
16# 2 along with this work; if not, write to the Free Software Foundation,
17# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20# or visit www.oracle.com if you need additional information or have any
21# questions.
22#
23
24#
25
26setup() {
27    # Verify directory context variables are set
28    if [ "${TESTJAVA}" = "" ] ; then
29        echo "TESTJAVA not set. Test cannot execute.  Failed."
30        exit 1
31    fi
32
33    if [ "${TESTCLASSES}" = "" ] ; then
34        TESTCLASSES="."
35    fi
36
37    if [ "${TESTSRC}" = "" ] ; then
38        TESTSRC="."
39    fi
40
41    OS=`uname -s`
42    case ${OS} in
43    Windows_* | CYGWIN*)
44        PS=";"
45        FS="\\"
46        ;;
47    *)
48        PS=":"
49        FS="/"
50        ;;
51    esac
52}
53
54verify_os() {
55    OS=`uname -s`
56    case ${OS} in
57    Windows_95 | Windows_98 | Windows_ME | CYGWIN* )
58        echo "Test bypassed: jvmstat feature not supported on ${OS}"
59        exit 0
60        ;;
61    Windows_*)
62        # verify that the tmp directory supports persistent ACLs, which
63        # are required for jvmstat to enable its shared memory feature.
64        # NOTE: FAT type files systems do not support ACLs, but NTFS files
65        # systems do.
66        #
67        echo "temp directory is in: `windir -t`"
68        TMPDRIVE=`windir -t | cut -d: -f1`
69        if [ "${TMPDRIVE}" = "" ] ; then
70            echo "Could not get temp directory drive letter"
71            exit 1
72        fi
73
74        echo "temp file system characteristics:"
75        sysinf drives -a | grep "^${TMPDRIVE}"
76        sysinf drives -a | grep "^${TMPDRIVE}" | grep PERSISTENT_ACLS > /dev/null
77        case $? in
78        0)
79            ;;
80        1)
81            echo "Test bypassed: jvmstat feature disabled because the temp"
82            echo "directory doesn't support persistent ACLs"
83            exit 0
84            ;;
85        2)
86            echo "Could not get temp directory file system features"
87            exit 1
88            ;;
89        *)
90            echo "Unexpected return code from grep - $?"
91            exit 1
92            ;;
93        esac
94        ;;
95    esac
96}
97
98# function to kill the process with the given process id
99kill_proc() {
100    kill_pid=$1
101
102    if [ "${kill_pid}" = "" ]
103    then
104        echo "kill_proc(): null pid: ignored"
105        return
106    fi
107
108    if [ ${kill_pid} -le 0 ]
109    then
110        echo "kill_proc(): invalid pid: ${kill_pid}: ignored"
111        return
112    fi
113
114    OS=`uname -s`
115    case $OS in
116    Windows_*)
117        case ${SHELL_VERSION} in
118        [1234567].* | 8.[12345].*)
119            # when starting processes in the background, mks creates an
120            # intervening between the parent process and the background
121            # process. The pid acquired for background process as acquired
122            # by the $! shell variable is actually the pid of the invervening
123            # shell and not that of the background process. Sending a specific
124            # signal to the intervening shell will only effects the intervening 
125            # shell and not the background process, thus leaving the background
126            # process running. The following code assumes that the pid passed
127            # into this function as ${kill_pid}, was acquired by $!. Therefore,
128            # in order to kill the background process, we must first find the
129            # children of ${kill_pid} (should be only one child) and kill them.
130        
131            ps -o pid,ppid | grep "${kill_pid}$"
132            children=`mks_children ${kill_pid}`
133            echo "killing children of ${kill_pid}: ${children}"
134            for child in ${children} ; do
135                kill_proc_common ${child}
136            done
137            ;;
138        *)
139            # in mks 8.6 and later, the pid returned in $! is now the pid
140            # of the background process and not that of the intervening shell.
141            kill_proc_common ${kill_pid}
142            ;;
143        esac
144        ;;
145    *)
146        kill_proc_common ${kill_pid}
147        ;;
148    esac
149}
150
151mks_children() {
152    ppid=$1
153    ps -o pid,ppid | grep "${ppid}$" | awk '
154BEGIN	{ pids="" }
155	{ pids = pids $1 " " }
156END	{ print pids }'
157}
158
159kill_proc_common() {
160   kpid=$1
161
162    # verify that the process exists and we can signal it
163    kill -0 ${kpid} 2>/dev/null
164    if [ $? -ne 0 ]
165    then
166        echo "Could not signal >${kpid}<"
167        echo "user id = `id`"
168        echo "process information :"
169        ps -l -p ${kpid}
170        return
171    fi
172
173    kill -TERM ${kpid}		# hit it easy first
174    if [ $? -eq 0 ]
175    then
176        sleep 2
177        kill -0 ${kpid} 2>/dev/null
178        # check if it's still hanging around
179        if [ $? -eq 0 ]
180        then
181            # it's still lingering, now it it hard
182            kill -KILL ${kpid} 2>/dev/null
183            if [ $? -ne 0 ]
184            then
185                echo "Could not kill ${kpid}!"
186            fi
187        fi
188    else
189        echo "Error sending term signal to ${kpid}!"
190    fi
191}
192
193# check to see if a port is free
194checkPort() # port
195{
196    inuse=`netstat -a | egrep "\.$1"`
197    if [ "${inuse}" = "" ] ; then
198      echo "free"
199    else
200      echo "inuse"
201    fi
202}
203
204# Get a free port, where port+1 is also free, return 0 when giving up
205freePort()
206{
207  start=3000
208  while [ ${start} -lt 3030 ] ; do
209    port1=`expr ${start} '+' $$ '%' 1000`
210    port2=`expr ${port1} '+' 1`
211    if [ "`checkPort ${port1}`" = "inuse" \
212         -o "`checkPort ${port2}`" = "inuse" ] ; then
213      start=`expr ${start} '+' 1`
214    else
215      break
216    fi
217  done
218  if [ "`checkPort ${port1}`" = "inuse" \
219       -o "`checkPort ${port2}`" = "inuse" ] ; then
220    port1="0"
221  fi
222  echo "${port1}"
223}
224