#!/bin/ksh93
#
# $Id$
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at
# trunk/opends/resource/legal-notices/OpenDS.LICENSE
# or https://OpenDS.dev.java.net/OpenDS.LICENSE.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at
# trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
# add the following below this CDDL HEADER, with the fields enclosed
# by brackets "[]" replaced with your own identifying information:
# Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2008-2010 Sun Microsystems, Inc.
# Portions Copyright 2010-2011 ForgeRock AS
# Portions Copyright 2013 Jens Elkner
# Fallback to use on non-SunOS
DEFAULT_INSTANCE_DIR=/var/share/ldap
# Fallback to use on SunOS
DEFAULT_SMF_FMRI=network/ldap/opendj@VERS@:default
#
# Display an error message
#
function display_java_not_found_error {
print -u2 '
Please set OPENDJ_JAVA_HOME to the root of a Java 6 update 10 (or higher)
installation or edit the java.properties file and then run the dsjavaproperties
script to specify the Java version to be used.'
}
# function that tests the JAVA_HOME env variable.
function test_java_home {
[[ -z ${JAVA_HOME} ]] && display_java_not_found_error && exit 999
export OPENDJ_JAVA_BIN="${JAVA_HOME}/bin/java"
[[ ! -x ${OPENDJ_JAVA_BIN} ]] && display_java_not_found_error && exit 999
}
# function that tests the JAVA_BIN env variable.
function test_java_bin {
if [[ -n ${JAVA_BIN} && -x ${JAVA_BIN} ]]; then
export OPENDJ_JAVA_BIN="${JAVA_BIN}"
return
fi
test_java_home
}
# function that tests the java executable in the PATH env variable.
function test_java_path {
export OPENDJ_JAVA_BIN="${ whence java ; }"
[[ -n $OPENDJ_JAVA_BIN && -x ${OPENDJ_JAVA_BIN} ]] && return
test_java_bin
}
# function that tests the OPENDJ_JAVA_HOME env variable.
function test_opendj_java_home {
if [[ -z ${OPENDJ_JAVA_HOME} ]]; then
test_java_path
return
fi
export OPENDJ_JAVA_BIN="${OPENDJ_JAVA_HOME}/bin/java"
[[ -x ${OPENDJ_JAVA_BIN} ]] || test_java_path
}
# function that tests the OPENDJ_JAVA_BIN env variable.
function test_opendj_java_bin {
if [[ -z ${OPENDJ_JAVA_BIN} ]]; then
test_opendj_java_home
elif [[ -x ${OPENDJ_JAVA_BIN} ]]; then
export OPENDJ_JAVA_BIN
else
test_java_path
fi
}
# function that sets the java home
function set_java_home_and_args {
[[ -r ${INSTANCE_ROOT}/lib/set-java-home ]] && \
. "${INSTANCE_ROOT}"/lib/set-java-home
test_opendj_java_bin
}
# Determine whether the detected Java environment is acceptable for use.
function test_java {
# this call just tests the java version compatibility (for now: If the
# JVM vendor is "Sun Microsystems Inc.", than JVM version string must be
# lexicographically greater or equal to 1.6.0_10. Otherwise it is
# assumed, that the JVM is compatible) and known args.
typeset ERRMSG='' LC_ALL= LC_MESSAGES=C
ERRMSG=${ "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} \
-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
org.opends.server.tools.InstallDS --testOnly 2>&1 ; }
integer RC=$?
(( ${RC} == 0 )) && return 0
if (( ${RC} == 8 )); then
# This is a particular error code that means that the Java version
# is not supported. Let InstallDS to display the localized error msg
print -u2 "${ERRMSG}"
exit 999
fi
if (( ${RC} == 2 )); then
# the error message includes the "Usage info" - so strip off the bloat
ERRMSG="${ERRMSG%%$'\n\n'This utility*}"
print -u2 "Argument parse error(s):${ERRMSG##*arguments:}"
print -u2 'To fix this problem, check your OPENDJ_JAVA_ARGS' \
'environment variable (if explicitly set), check your' \
"'${INSTANCE_ROOT}/config/java.properties'" 'file and re-run' \
"'${INSTALL_ROOT}/bin/dsjavaproperties'."
exit 999
fi
if (( ${RC} != 8 || ${RC} != 1 )); then
print -u2 "${ERRMSG}"
exit 999
fi
# Since no other tests are done, this must be a JVM error.
print -u2 'The following Java invocation error occured:\n' "${ERRMSG}"
[[ -n ${OPENDJ_JAVA_ARGS} ]] && \
print -u2 'OPENDJ_JAVA_ARGS=' "${OPENDJ_JAVA_ARGS}"
print -u2 '
To fix the problem:
1. Delete the following file if it exists:
' "${INSTANCE_ROOT}/lib/set-java-home" '
2. Check the Java properties file for correct *.java-home and *.java-args
settings. See:
' "${INSTANCE_ROOT}/config/java.properties" '
3. Run the following command-line tool to persist the changes made:
' "${INSTALL_ROOT}/bin/dsjavaproperties" '
4. When you run an OpenDJ tool, make sure, that OPENDJ_* environment
variables are not already set.'
exit 999
}
# Explicitly set the PATH, LD_LIBRARY_PATH, LD_PRELOAD, and other important
# system environment variables for security and compatibility reasons.
function set_environment_vars {
# first unset everything we definitely do not need
typeset KEEP=' COLUMNS DBUS_SESSION_BUS_ADDRESS DEFAULT_INSTANCE_DIR DEFAULT_SMF_FMRI DISPLAY FNLIST FNTRACE HOME HOST INSTALL_ROOT INSTANCE_ROOT JAVA_BIN JAVA_HOME KSH_VERSION LANG LC_COLLATE LC_CTYPE LC_MESSAGES LC_TIME LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_64 LINENO LINES LOGNAME MISC_ARGS NLSPATH OLD_SCRIPT_NAME OPENDJ_INVOKE_CLASS OPENDJ_JAVA_ARGS OPENDJ_JAVA_BIN OPENDJ_JAVA_HOME OPTIND ORIGINAL_JAVA_ARGS ORIGINAL_JAVA_BIN ORIGINAL_JAVA_HOME PATH PPID PS1 PS2 PS3 PS4 PWD RANDOM REMOTEHOST SCRIPT_ARGS SCRIPT_NAME SCRIPT_NAME_ARG SCRIPT_UTIL_CMD SECONDS SESSION_MANAGER SHELL SHLVL SMF_FMRI SSH_AGENT_PID SSH_AUTH_SOCK TERM TMOUT TMPDIR TZ USER USERNAME XAUTHORITY XDG_DATA_DIRS XDG_SESSION_COOKIE _AST_FEATURES _ VAR KEEP PWD '
set | while read LINE ; do
LINE=${LINE%%=*}
[[ -z ${.sh.match} || ${KEEP} =~ " ${LINE} " ]] && continue
typeset -n VAR=${LINE}
unset VAR
done
typeset -n VAR
for VAR in LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_64 ; do
[[ ${VAR} != 'libumem.so' ]] && unset VAR
done
SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=${SCRIPT_NAME}"
export PATH='/usr/bin:/usr/sbin'
# LD_LIBRARY_PATH='' LD_LIBRARY_PATH_32='' LD_LIBRARY_PATH_64='' \
# LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_64
}
# Configure the appropriate CLASSPATH.
function set_classpath {
typeset JAR
CLASSPATH=''
[[ -d ${INSTANCE_ROOT}/classes ]] && CLASSPATH+=":${INSTANCE_ROOT}"/classes
for JAR in ~(N)"${INSTALL_ROOT}"/lib/*.jar ; do
[[ -n ${JAR} ]] && CLASSPATH+=":${JAR}"
done
if [[ ${INSTALL_ROOT} != ${INSTANCE_ROOT} ]]; then
for JAR in ~(N)"${INSTANCE_ROOT}"/lib/*.jar ; do
[[ -n ${JAR} ]] && CLASSPATH+=":${JAR}"
done
fi
export CLASSPATH=${CLASSPATH:1} # remove leading :
}
function isVersionOrHelp {
typeset opt
for opt in "$@" ; do
case "$opt" in
'-V'|'--version'|'-?'|'-H'|'--help'|'-F'|'--fullversion') return 0;;
esac
done
return 1
}
function getprop {
typeset PROPVAL=''
if [[ ${ uname -s ; } == 'SunOS' && ${ id -un ; } == 'ldapd' ]]; then
if svcprop -q -p "$1" ${SMF_FMRI} ; then
PROPVAL=${ svcprop -p $1 ${SMF_FMRI}; }
[[ $PROPVAL == '""' ]] && PROPVAL=""
# remove space escaping backslashes
PROPVAL="${PROPVAL//\\ / }"
fi
if [[ -z ${PROPVAL} || ${PROPVAL:0:1} != '/' ]]; then
# Actually this should never happen, however, sometimes people are
# really smart ;-)
[[ -z ${INSTANCE_ROOT} ]] && X='set' || X='an absolute path'
print -u2 '
The config/datadir property for the service' "${SMF_FMRI}" 'is not' $X'.
It should point to the OpenDJ data directory for this service instance.
Please set it and try again. E.g.:
svccfg -s '"${SMF_FMRI}"" 'setprop config/datadir = /var/share/opendj'"'
svccfg -s '"${SMF_FMRI}"' refresh
'
exit 999
fi
fi
if [[ -z ${PROPVAL} ]]; then
[[ ${ id -un ; } == 'ldapd' ]] \
&& PROPVAL=${DEFAULT_INSTANCE_ROOT} \
|| PROPVAL=${HOME}/opendj
fi
print -- "${PROPVAL}"
}
# Try to give the user a hint. Actually this should be done by the
# application/utility and return an appropriate error code, but it doesn't :(
function checkInstanceDir {
typeset EXT=''
[[ ${SCRIPT_NAME} != 'dsjavaproperties' ]] && EXT='.startok'
[[ -f "${INSTANCE_ROOT}"/config/hostname || \
-f "${INSTANCE_ROOT}/config/config.ldif${EXT}" ]] && return
# Or if one wants check for populated instances, only.
# if [[ ( -f ${INSTANCE_ROOT}/config/config.ldif ) \
# && -n ${ ls ${INSTANCE_ROOT}/config/archived-configs/*.gz 2>/dev/null; } \
# && -n ${ ls ${INSTANCE_ROOT}/db/* 2>/dev/null ; } ]] \
# then
# return
# fi
if [[ ${ uname -s ; } == 'SunOS' ]]; then
print -u2 '
It seems, that the following service has not yet been initialized:
\t'"${SMF_FMRI}"'
Please run' "${INSTALL_ROOT}/setup" '.\n'
else
print -u2 '
The instance data directory "'${INSTANCE_ROOT}'"
seems to contain no valid data. If you want to use a different instance data
directory, set your INSTANCE_ROOT environment variable to the appropriate
path. Otherwise, to initialize the shown directory shown above, run
'"${INSTALL_ROOT}/setup"' .\n'
fi
exit 999
}
function checkEnv {
integer CHECK_INSTANCE=0
[[ ${1} == '-i' ]] && CHECK_INSTANCE=1 && shift
typeset SCRIPT_UTIL_CMD="$1"
# Capture the location of this script so that we can use them to create
# appropriate paths.
INSTALL_ROOT="${.sh.file%/*/*}"
if [[ ${ uname -s ; } == 'SunOS' && ${ id -un ; } == 'root' ]]; then
print -u2 "\nWARNING: ${SCRIPT_NAME} should not be run as user 'root'!\n"
fi
# If no SMF_FMRI is set (interactive use), we choose the default instance.
[[ -z ${SMF_FMRI} ]] && SMF_FMRI='network/ldap/opendj@VERS@:default'
[[ -z ${INSTANCE_ROOT} ]] && INSTANCE_ROOT="${ getprop config/datadir ; }"
case "${SCRIPT_UTIL_CMD}" in
'set-full-environment-and-test-java')
set_java_home_and_args
set_environment_vars
set_classpath
test_java
;;
'set-full-environment')
set_java_home_and_args
set_environment_vars
set_classpath
;;
'set-java-home-and-args')
set_java_home_and_args
;;
'set-environment-vars')
set_environment_vars
;;
'set-classpath')
set_classpath
;;
'test-java')
test_java
;;
esac
(( ! CHECK_INSTANCE )) && return
# No check for --version or --help option
isVersionOrHelp "$@" && return 0
checkInstanceDir
}
# list all defined script functions known at this point
[[ -n ${FNLIST} ]] && typeset +f
# If set to 'ALL', trace all functions, otherwise trace only the functions whose
# name is in this comma or whitespace separated list
if [[ -n ${FNTRACE} ]]; then
[[ ${FNTRACE} == 'ALL' ]] && typeset -ft ${ typeset +f ; } || \
typeset -ft ${FNTRACE//,/ }
fi