This file is indexed.

/usr/share/cluster/oracledb.sh is in resource-agents 1:3.9.3+git20121009-3ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#!/bin/bash
#
# Copyright (C) 1997-2003 Sistina Software, Inc.  All rights reserved.
# Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author(s):
#     Hardy Merrill <hmerrill at redhat.com>
#     Lon Hohberger <lhh at redhat.com>
#     Michael Moon <Michael dot Moon at oracle.com>
#
# chkconfig: 345 99 01
# description: Service script for starting/stopping      \
#	       Oracle(R) Database 10g on                 \
#		        Red Hat Enterprise Linux 5
#
# NOTES:
#
# (1) You can comment out the LOCKFILE declaration below.  This will prevent
# the need for this script to access anything outside of the ORACLE_HOME 
# path.
#
# (2) You MUST customize ORACLE_USER, ORACLE_HOME, ORACLE_SID, and
# ORACLE_HOSTNAME to match your installation if not running from within
# rgmanager.
#
# (3) Do NOT place this script in shared storage; place it in ORACLE_USER's
# home directory in non-clustered environments and /usr/share/cluster
# in rgmanager/Red Hat cluster environments.
#
# Oracle is a registered trademark of Oracle Corporation.
# Oracle9i is a trademark of Oracle Corporation.
# Oracle10g is a trademark of Oracle Corporation.
# All other trademarks are property of their respective owners.
#

. /etc/init.d/functions

#
# Source stuff from /etc/sysconfig, but this may be overridden if
# this is being called as a cluster resource agent instead.
#. /etc/sysconfig/oracledb

declare SCRIPT="`basename $0`"
declare SCRIPTDIR="`dirname $0`"

[ -n "$OCF_RESKEY_user" ] && ORACLE_USER=$OCF_RESKEY_user
[ -n "$OCF_RESKEY_home" ] && ORACLE_HOME=$OCF_RESKEY_home
[ -n "$OCF_RESKEY_name" ] && ORACLE_SID=$OCF_RESKEY_name
[ -n "$OCF_RESKEY_listener_name" ] && ORACLE_LISTENER=$OCF_RESKEY_listener_name
[ -n "$OCF_RESKEY_lockfile" ] && LOCKFILE=$OCF_RESKEY_lockfile
[ -n "$OCF_RESKEY_type" ] && ORACLE_TYPE=$OCF_RESKEY_type
[ -n "$OCF_RESKEY_vhost" ] && ORACLE_HOSTNAME=$OCF_RESKEY_vhost

######################################################
# Customize these to match your Oracle installation. #
######################################################
#
# 1. Oracle user.  Must be the same across all cluster members.  In the event
#    that this script is run by the super-user, it will automatically switch
#    to the Oracle user and restart.  Oracle needs to run as the Oracle
#    user, not as root.
#
#[ -n "$ORACLE_USER" ] || ORACLE_USER=oracle

#
# 2. Oracle home.  This is set up during the installation phase of Oracle.
#    From the perspective of the cluster, this is generally the mount point
#    you intend to use as the mount point for your Oracle Infrastructure
#    service.
#
#[ -n "$ORACLE_HOME" ] || ORACLE_HOME=/mnt/oracle/home

#
# 3. This is your SID.  This is set up during oracle installation as well.
#
#[ -n "$ORACLE_SID" ] || ORACLE_SID=orcl

#
# 4. The oracle user probably doesn't have the permission to write to 
# /var/lock/subsys, so use the user's home directory.
#
#[ -n "$LOCKFILE" ] || LOCKFILE="/home/$ORACLE_USER/.oracle-ias.lock"
[ -n "$LOCKFILE" ] || LOCKFILE="$ORACLE_HOME/.oracle-ias.lock"
#[ -n "$LOCKFILE" ] || LOCKFILE="/var/lock/subsys/oracle-ias" # Watch privileges

#
# 5. Type of Oracle Database.  Currently supported: 10g 10g-iAS(untested!)
#
[ -n "$ORACLE_TYPE" ] || ORACLE_TYPE="base-em"

#
# 6. Oracle virtual hostname.  This is the hostname you gave Oracle during
#    installation.
#
#[ -n "$ORACLE_HOSTNAME" ] || ORACLE_HOSTNAME=svc0.foo.test.com



###########################################################################
ORACLE_TYPE=`echo $ORACLE_TYPE | tr A-Z a-z`
export ORACLE_USER ORACLE_HOME ORACLE_SID LOCKFILE ORACLE_TYPE
export ORACLE_HOSTNAME


##########################
# Set up paths we'll use.  Not all are used by all the different types of
# Oracle installations
#
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/opmn/lib
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/opmn/bin:$ORACLE_HOME/dcm/bin:$PATH

declare -i	RESTART_RETRIES=3
declare -r	DB_PROCNAMES="pmon"
#declare -r	DB_PROCNAMES="pmonXX" # testing
#declare -r	DB_PROCNAMES="pmon smon dbw0 lgwr"

declare -r	LSNR_PROCNAME="tnslsnr"
#declare -r	LSNR_PROCNAME="tnslsnrXX" # testing


##########################################################
# (Hopefully) No user-serviceable parts below this line. #
##########################################################
meta_data()
{
	cat <<EOT
<?xml version="1.0" ?>
<resource-agent name="oracledb" version="rgmanager 2.0">
    <version>1.0</version>

    <longdesc lang="en">
	Oracle 10g Failover Instance
    </longdesc>
    <shortdesc lang="en">
	Oracle 10g Failover Instance
    </shortdesc>

    <parameters>
        <parameter name="name" primary="1">
	    <longdesc lang="en">
		Instance name (SID) of oracle instance
	    </longdesc>
            <shortdesc lang="en">
		Oracle SID
            </shortdesc>
	    <content type="string"/>
        </parameter>

        <parameter name="listener_name" unique="1">
	    <longdesc lang="en">
		Oracle Listener Instance Name.  If you have multiple 
		instances of Oracle running, it may be necessary to 
		have multiple listeners on the same machine with
		different names.
	    </longdesc>
            <shortdesc lang="en">
		Oracle Listener Instance Name
            </shortdesc>
	    <content type="string"/>
        </parameter>


        <parameter name="user" required="1">
	    <longdesc lang="en">
		Oracle user name.  This is the user name of the Oracle
		user which the Oracle AS instance runs as.
	    </longdesc>
            <shortdesc lang="en">
		Oracle User Name
            </shortdesc>
	    <content type="string"/>
        </parameter>

        <parameter name="home" required="1">
	    <longdesc lang="en">
		This is the Oracle (application, not user) home directory.
		This is configured when you install Oracle.
	    </longdesc>
            <shortdesc lang="en">
		Oracle Home Directory
            </shortdesc>
	    <content type="string"/>
        </parameter>

        <parameter name="type" required="0">
	    <longdesc lang="en">
		This is the Oracle installation type:
		base - Database Instance and Listener only
		base-em (or 10g) - Database, Listener, Enterprise Manager,
				   and iSQL*Plus
		ias (or 10g-ias) - Internet Application Server (Infrastructure)
	    </longdesc>
            <shortdesc lang="en">
		Oracle Installation Type
            </shortdesc>
	    <content type="string"/>
        </parameter>

        <parameter name="vhost" required="0" unique="1">
	    <longdesc lang="en">
	        Virtual Hostname matching the installation hostname of
		Oracle 10g.  Note that during the start/stop of an oracledb
		resource, your hostname will temporarily be changed to
		this hostname.  As such, it is recommended that oracledb
		resources be instanced as part of an exclusive service only.
	    </longdesc>
            <shortdesc lang="en">
		Virtual Hostname
            </shortdesc>
	    <content type="string"/>
        </parameter>

    </parameters>

    <actions>
        <action name="start" timeout="900"/>
	<action name="stop" timeout="90"/>
        <action name="recover" timeout="990"/>

	<!-- Checks to see if it's mounted in the right place -->
	<action name="status" timeout="10"/>
	<action name="monitor" timeout="10"/>

	<action name="status" depth="10" timeout="30" interval="30"/>
	<action name="monitor" depth="10" timeout="30" interval="30"/>

	<action name="meta-data" timeout="5"/>
	<action name="validate-all" timeout="5"/>
    </actions>

    <special tag="rgmanager">
	<attributes maxinstances="1"/>
    </special>
</resource-agent>
EOT
}


#
# "action"-like macro supporting functions
#
faction()
{
	echo -n "$1"
	shift
	$*
	if [ $? -eq 0 ]; then
		echo_success
		echo
		return 0
	fi

	echo_failure
	echo
	return 1
}


#
# Start Oracle9i (database portion)
#
start_db()
{
	declare tmpfile
	declare logfile
	declare -i rv

	tmpfile="$(mktemp /tmp/$SCRIPT-start.XXXXXX)"
	logfile=/var/log/cluster/$SCRIPT-start.log

	#
	# Set up our sqlplus script.  Basically, we're trying to 
	# capture output in the hopes that it's useful in the case
	# that something doesn't work properly.
	#
	echo "startup" > $tmpfile
	echo "quit" >> $tmpfile

	sqlplus "/ as sysdba" < $tmpfile &> $logfile
	rv=$?

	rm -f $tmpfile

	# Dump logfile to /var/log/messages
	logger -f $logfile

	if [ $rv -ne 0 ]; then
		echo "ORACLE_HOME Incorrectly set?"
		echo "See $logfile for more information."
		return 1
	fi

	# 
	# If we see:
	# ORA-.....: failure, we failed
	#

	rm -f $tmpfile
	grep -q "^ORA-" $logfile
	if [ $? -eq 0 ]; then
		echo "ORACLE_SID Incorrectly set?"
		rm -f $tmpfile
		echo "See $logfile for more information."
		return 1
	fi

	return 0
}


#
# Stop Oracle9i (database portion)
#
stop_db()
{
	declare tmpfile
	declare logfile
	declare -i rv
	declare how_shutdown="$1"

	if [ -z "$1" ]; then
		how_shutdown="immediate"
	fi

	tmpfile="$(mktemp /tmp/$SCRIPT-stop.XXXXXX)"
	logfile=/var/log/cluster/$SCRIPT-stop.log

	# Setup for Stop ...
	echo "shutdown $how_shutdown" > $tmpfile
	echo "quit" >> $tmpfile

	sqlplus "/ as sysdba" < $tmpfile &> $logfile
	rv=$?

	rm -f $tmpfile

	# Dump logfile to /var/log/messages
	logger -f $logfile

	if [ $rv -ne 0 ]; then
		echo "ORACLE_HOME Incorrectly set?"
		echo "See $logfile for more information."
		return 1
	fi

	# 
	# If we see 'failure' in the log, we're done.
	#
	rm -f $tmpfile
	grep -q "^ORA-" $logfile
	if [ $? -eq 0 ]; then
		echo_failure
		echo
		echo "Possible reason: ORACLE_SID Incorrectly set."
		echo "See $logfile for more information."
		return 1
	fi

	return 0
}


#
# Destroy any remaining processes with refs to $ORACLE_HOME
#
force_cleanup()
{
	declare pids
	declare pid

	# Patch from Shane Bradley to fix 471266
	pids=`ps ax | grep $ORACLE_HOME | grep "ora_.*_${ORACLE_SID}" | grep -v grep | awk '{print $1}'`

	logger -t $SCRIPT "<err> Not all Oracle processes exited cleanly, killing"
	
	for pid in $pids; do
		kill -9 $pid
		if [ $? -eq 0 ]; then
			logger -t $SCRIPT "Killed $pid"
		fi
	done

	return 0
}



#
# Wait for oracle processes to exit.  Time out after 60 seconds
#
exit_idle()
{
	declare -i n=0
	while ps ax | grep $ORACLE_HOME | grep -q -v grep; do
		if [ $n -ge 90 ]; then
			force_cleanup
			return 0
		fi
		sleep 1
		((n++))
	done
	return 0
}


#
# Get database background process status.  Restart it if it failed and
# we have seen the lock file.
#
get_db_status()
{
	declare -i subsys_lock=$1
	declare -i i=0
	declare -i rv=0
	declare ora_procname

	for procname in $DB_PROCNAMES ; do

		ora_procname="ora_${procname}_${ORACLE_SID}"
		
		status $ora_procname
		if [ $? -eq 0 ] ; then
			# This one's okay; go to the next one.
			continue
		fi

		#
		# We're not supposed to be running, and we are,
		# in fact, not running...
		# XXX only works when monitoring one db process; consider
		# extending in future.
		#
		if [ $subsys_lock -ne 0 ]; then
			return 3
		fi

		for (( i=$RESTART_RETRIES ; i; i-- )) ; do
			# this db process is down - stop and
			# (re)start all ora_XXXX_$ORACLE_SID processes
			logger -t $SCRIPT "Restarting Oracle Database..."
			stop_db immediate
			if [ $? != 0 ] ; then
				# stop failed - return 1
				return 1
			fi

			start_db
			if [ $? == 0 ] ; then
				# ora_XXXX_$ORACLE_SID processes started
				# successfully, so break out of the
				# stop/start # 'for' loop
				break
			fi
		done

		if [ $i -eq 0 ]; then
			# stop/start's failed - return 1 (failure)
			return 1
		fi
	done
	return 0
}


#
# Get the status of the Oracle listener process
#
get_lsnr_status() 
{
	declare -i subsys_lock=$1
	declare -i rv

	status $LSNR_PROCNAME
	rv=$?
	if [ $rv == 0 ] ; then
		return 0 # Listener is running fine
	fi

	#
	# We're not supposed to be running, and we are,
	# in fact, not running.  Return 3
	#
	if [ $subsys_lock -ne 0 ]; then
		return 3
	fi

	#
	# Listener is NOT running (but should be) - try to restart
	#
	for (( i=$RESTART_RETRIES ; i; i-- )) ; do

		action "Restarting Oracle listener:" lsnrctl start \
					$ORACLE_LISTENER
		lsnrctl status $ORACLE_LISTENER >& /dev/null
		if [ $? == 0 ] ; then
			break # Listener was (re)started and is running fine
		fi
	done

	if [ $i -eq 0 ]; then
		# stop/start's failed - return 1 (failure)
		return 1
	fi

	status $LSNR_PROCNAME
	if [ $? != 0 ] ; then
		return 1 # Problem restarting the Listener
	fi
	return 0 # Success restarting the Listener
}


#
# usage: get_opmn_proc_status <ias-component> [process-type]
#
# Get the status of a specific OPMN-managed process.  If process-type
# is not specified, assume the process-type is the same as the ias-component.
# If the lock-file exists (or no lock file is specified), try to restart
# the given process-type if it is not running.
#
get_opmn_proc_status()
{
	declare comp=$1
	declare opmntype=$2
	declare type_pretty
	declare _pid _status
	
	[ -n "$comp" ] || return 1
	if [ -z "$opmntype" ]; then
		opmntype=$comp
	else
		type_pretty=" [$opmntype]"
	fi

	for (( i=$RESTART_RETRIES ; i; i-- )) ; do

		_status=`opmnctl status | grep "^$comp " | grep " $opmntype " | cut -d '|' -f3,4 | sed -e 's/ //g' -e 's/|/ /g'`

		_pid=`echo $_status | cut -f1 -d' '`
		_status=`echo $_status | cut -f2 -d' '`
		if [ "${_status}" == "Alive" ] || [ "${_status}" == "Init" ]; then
			if [ $i -lt $RESTART_RETRIES ] ; then
				echo "  $comp$type_pretty restarted"
			fi
			echo "  $comp$type_pretty (pid $_pid) is running..."
			break
		else
			echo "  $comp$type_pretty is stopped"

			#
			# Try to restart it, but don't worry if we fail.  OPMN
			# is supposed to handle restarting these anyway.
			#
			# If it's running and you tell OPMN to "start" it,
			# you will get an error.
			#
			# If it's NOT running and you tell OPMN to "restart"
			# it, you will also get an error.
			#
			opmnctl startproc process-type=$opmntype &> /dev/null
		fi
	done

	if [ $i -eq 0 ]; then
		# restarts failed - return 1 (failure)
		return 1
	fi

	return 0
}


#
# Get the status of the OPMN-managed processes.
#
get_opmn_status()
{
	declare -i subsys_lock=$1
	declare -i ct_errors=0

	opmnctl status &> /dev/null
	if [ $? -eq 2 ]; then
		#
		# OPMN not running??
		#
		echo "opmn is stopped"

		if [ $subsys_lock -eq 0 ]; then
			#
			# Don't handle full opmn-restart. XXX
			#
			return 1
		fi

		# That's okay, it's not supposed to be!
		return 3
	fi

	#
	# Print out the PIDs for everyone.
	#
	echo "opmn is running..."
	echo "opmn components:"

	#
	# Check the OPMN-managed processes
	#
	get_opmn_proc_status OID || ((ct_errors++))
	get_opmn_proc_status HTTP_Server || ((ct_errors++))
	get_opmn_proc_status OC4J OC4J_SECURITY || ((ct_errors++))

	#
	# One or more OPMN-managed processes failed and could not be
	# restarted.
	#
	if [ $ct_errors -ne 0 ]; then
		return 1
	fi
	return 0
}


#
# Helps us keep a running status so we know what our ultimate return
# code will be.  Returns 1 if the $1 and $2 are not equivalent, otherwise
# returns $1.  The return code is meant to be the next $1 when this is
# called, so, for example:
#
# update_status 0   <-- returns 0
# update_status $? 0 <-- returns 0
# update_status $? 3 <-- returns 1 (values different - error condition)
# update_status $? 1 <-- returns 1 (same, but happen to be error state!)
#
# update_status 3
# update_status $? 3 <-- returns 3
#
# (and so forth...)
#
update_status()
{
	declare -i old_status=$1
	declare -i new_status=$2

	if [ -z "$2" ]; then
		return $old_status
	fi

	if [ $old_status -ne $new_status ]; then
		return 1
	fi

	return $old_status
}


#
# Print an error message to the user and exit.
#
oops()
{
	echo "Please configure this script ($0) to"
	echo "match your installation."
	echo 
	echo "    $1 failed validation checks."
	exit 1
}


#
# Do some validation on the user-configurable stuff at the beginning of the
# script.
#
validation_checks()
{
	#
	# If the oracle user doesn't exist, we're done.
	#
	[ -n "$ORACLE_USER" ] || oops "ORACLE_USER"
	id -u $ORACLE_USER > /dev/null || oops "ORACLE_USER"
	id -g $ORACLE_USER > /dev/null || oops "ORACLE_USER"

	#
	# If the oracle home isn't a directory, we're done
	#
	[ -n "$ORACLE_HOME" ] || oops ORACLE_HOME
	#[ -d "$ORACLE_HOME" ] || oops ORACLE_HOME

	#
	# If the oracle SID is NULL, we're done
	#
	[ -n "$ORACLE_SID" ] || oops ORACLE_SID

	#
	# If we don't know the type, we're done
	#
	if [ "$ORACLE_TYPE" = "base" ]; then
		# Other names for base
		ORACLE_TYPE="base"
	elif [ "$ORACLE_TYPE" = "10g" ] || [ "$ORACLE_TYPE" = "base-em" ]; then
		ORACLE_TYPE="base-em"
	elif [ "$ORACLE_TYPE" = "10g-ias" ] || [ "$ORACLE_TYPE" = "ias" ]; then
		ORACLE_TYPE="ias"
	else
		oops ORACLE_TYPE
	fi

	#
	# If the hostname is zero-length, fix it
	#
	[ -n "$ORACLE_HOSTNAME" ] || ORACLE_HOSTNAME=`hostname`

	#
	# Super user? Automatically change UID and exec as oracle user.
	# Oracle needs to be run as the Oracle user, not root!
	#
	if [ "`id -u`" = "0" ]; then
		echo "Restarting $0 as $ORACLE_USER."
		#
		# Breaks on RHEL5 
		# exec sudo -u $ORACLE_USER $0 $*
		#
		su $ORACLE_USER -c "$0 $*"
		exit $?
	fi

	#
	# If we're not root and not the Oracle user, we're done.
	#
	[ "`id -u`" = "`id -u $ORACLE_USER`" ] || exit 1
	[ "`id -g`" = "`id -g $ORACLE_USER`" ] || exit 1

	#
	# Go home.
	#
	cd $ORACLE_HOME

	return 0
}


#
# Start Oracle9i Application Server Infrastructure
#
start_oracle()
{
	faction "Starting Oracle Database:" start_db || return 1
	action "Starting Oracle Listener:" lsnrctl start $ORACLE_LISTENER || return 1

	if [ "$ORACLE_TYPE" = "base-em" ]; then
		action "Starting iSQL*Plus:" isqlplusctl start || return 1
		action "Starting Oracle EM DB Console:" emctl start dbconsole || return 1
	elif [ "$ORACLE_TYPE" = "ias" ]; then
		action "Starting Oracle EM:" emctl start em || return 1
		action "Starting iAS Infrastructure:" opmnctl startall || return 1
	fi

	if [ -n "$LOCKFILE" ]; then
		touch $LOCKFILE
	fi
	return 0
}


#
# Stop Oracle9i Application Server Infrastructure
#
stop_oracle()
{
	if ! [ -e "$ORACLE_HOME/bin/lsnrctl" ]; then
		echo "Oracle Listener Control is not available"
		echo "    ($ORACLE_HOME not mounted?)"
		return 0
	fi

	if [ "$ORACLE_TYPE" = "base-em" ]; then
		action "Stopping Oracle EM DB Console:" emctl stop dbconsole || return 1
		action "Stopping iSQL*Plus:" isqlplusctl stop || return 1
	elif [ "$ORACLE_TYPE" = "ias" ]; then
		action "Stopping iAS Infrastructure:" opmnctl stopall || return 1
		action "Stopping Oracle EM:" emctl stop em || return 1
	fi

	faction "Stopping Oracle Database:" stop_db immediate
	if [ $? -ne 0 ]; then
		faction "Stopping Oracle Database (hard):" stop_db abort || return 1
	fi

	action "Stopping Oracle Listener:" lsnrctl stop $ORACLE_LISTENER
	faction "Waiting for all Oracle processes to exit:" exit_idle 

	if [ $? -ne 0 ]; then
		echo "WARNING: Not all Oracle processes exited cleanly"
	fi

	if [ -n "$LOCKFILE" ]; then
		rm -f $LOCKFILE
	fi
	return 0
}


#
# Find and display the status of iAS infrastructure.
#
# This has three parts:
# (1) Oracle database itself
# (2) Oracle listener process
# (3) OPMN and OPMN-managed processes
#
# - If all are (cleanly) down, we return 3.  In order for this to happen,
# $LOCKFILE must not exist.  In this case, we try and restart certain parts
# of the service - as this may be running in a clustered environment.
#
# - If some but not all are running (and, if $LOCKFILE exists, we could not
# restart the failed portions), we return 1 (ERROR)
#
# - If all are running, return 0.  In the "all-running" case, we recreate
# $LOCKFILE if it does not exist.
#
status_oracle()
{
	declare -i subsys_lock=1
	declare -i last 

	#
	# Check for lock file.  Crude and rudimentary, but it works
	#
	if [ -z "$LOCKFILE" ] || [ -f $LOCKFILE ]; then
		subsys_lock=0 
	fi

	# Check database status
	get_db_status $subsys_lock
	update_status $? # Start
	last=$?

	# Check & report listener status
	get_lsnr_status $subsys_lock
	update_status $? $last
	last=$?
	
	if [ "$ORACLE_TYPE" = "base-em" ]; then
		# XXX Add isqlplus status check?!
		emctl status dbconsole 2>&1 | grep "is running"
		update_status $? $last
		last=$?
	elif [ "$ORACLE_TYPE" = "ias" ]; then
		# Check & report opmn / opmn-managed process status
		get_opmn_status $subsys_lock
		update_status $? $last
		last=$?
	fi

	#
	# No lock file, but everything's running.  Put the lock
	# file back. XXX - this kosher?
	#
	if [ $last -eq 0 ] && [ $subsys_lock -ne 0 ]; then
		touch $LOCKFILE
	fi

	return $last
}


########################
# Do some real work... #
########################
if [ "$1" = "meta-data" ]; then
	meta_data
	exit 0
fi

validation_checks $*

case $1 in
	start)
		start_oracle
		exit $?
		;;
	stop)
		stop_oracle
		exit $?
		;;
	status|monitor)
		status_oracle
		exit $?
		;;
	restart)
		$0 stop || exit $?
		$0 start || exit $?
		exit 0
		;;
	*)
		echo "usage: $SCRIPT {start|stop|status|restart|meta-data}"
		exit 1
		;;
esac
exit 0