hpcc_setenv.in 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/bin/bash
  2. ################################################################################
  3. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ################################################################################
  17. #
  18. # HPCC Set Environment
  19. # Extract pathing parameters from config file (optional section) and set
  20. # relevant environment varibles if we are being sourced. Otherwise, just
  21. # echo bash-style commands to the caller that can in-turn be sourced.
  22. ###<REPLACE>###
  23. function kill_process () {
  24. local PID=$1
  25. local PROCESS=$2
  26. local TIMEOUT=$3
  27. if [[ $# -eq 4 ]]; then
  28. SENTINEL=$4
  29. [[ -e $SENTINEL ]] && rm -f $SENTINEL
  30. fi
  31. if [[ -e $PID ]]; then
  32. pidwait_fn $PID $PROCESS $TIMEOUT 1
  33. local RC_PIDWAIT=$?
  34. return $RC_PIDWAIT
  35. fi
  36. }
  37. function dali_kill_process () {
  38. local PID=$1
  39. local PROCESS=$2
  40. local TIMEOUT=$3
  41. if [[ $# -eq 4 ]]; then
  42. SENTINEL=$4
  43. [[ -e $SENTINEL ]] && rm -f $SENTINEL
  44. fi
  45. if [[ -e $PID ]]; then
  46. pidwait_fn $PID $PROCESS $TIMEOUT 0
  47. local RC_PIDWAIT=$?
  48. return $RC_PIDWAIT
  49. fi
  50. }
  51. function pidwait_fn () {
  52. local PID=$1
  53. local WATCH_PID=$( cat $PID )
  54. local PROCESS=$2
  55. local TIMEOUT=$(($3*1000))
  56. local FORCE=$4
  57. if ps -p $WATCH_PID -o command= | grep -v "${PROCESS}" &>/dev/null ; then
  58. return 0
  59. fi
  60. kill $WATCH_PID > /dev/null 2>&1
  61. while [[ -d /proc/$WATCH_PID ]] && [[ $TIMEOUT -gt 0 ]]; do
  62. sleep 0.2
  63. local TIMEOUT=$(($TIMEOUT-200))
  64. done
  65. if [[ $TIMEOUT -le 0 ]] && ps -p $WATCH_PID -o command= | grep "${PROCESS}" &>/dev/null ; then
  66. log "Failed to kill ${PROCESS} with SIGTERM"
  67. if [[ $FORCE -eq 1 ]]; then
  68. kill -SIGKILL $WATCH_PID > /dev/null 2>&1
  69. log "killing ${PROCESS} (pid: $WATCH_PID) with SIGKILL"
  70. rm -f $PID > /dev/null 2>&1
  71. return 0
  72. fi
  73. return 1
  74. fi
  75. rm -f $PID > /dev/null 2>&1
  76. return 0
  77. }
  78. platform='unknown'
  79. unamestr=`uname`
  80. if [[ "$unamestr" == 'Linux' ]]; then
  81. platform='linux'
  82. which nproc >/dev/null 2>&1
  83. rc=$?
  84. if [[ $rc -eq 0 ]] ; then
  85. num_cpus=$(nproc)
  86. else
  87. num_cpus=$(grep -c ^processor /proc/cpuinfo 2>/dev/null)
  88. fi
  89. elif [[ "$unamestr" == 'Darwin' ]]; then
  90. platform='mac'
  91. num_cpus=$(sysctl -n hw.ncpu 2>/dev/null)
  92. fi
  93. if [[ -z "$num_cpus" ]] ; then
  94. num_cpus=1
  95. fi
  96. HPCC_CONFIG=${HPCC_CONFIG:-${CONFIG_DIR}/${ENV_CONF_FILE}}
  97. #SECTION=${1:-DEFAULT}
  98. SECTION="DEFAULT"
  99. OUMASK=$(umask)
  100. OIFS="${IFS}"
  101. unset IFS
  102. source /etc/profile
  103. IFS="${OIFS}"
  104. umask ${OUMASK}
  105. # use less heap when threaded
  106. # but if set too low it can affect performance significantly
  107. num_arenas=$num_cpus
  108. if [ -z "$num_arenas" ] ; then
  109. num_arenas=8
  110. elif [ $num_arenas -lt 8 ] ; then
  111. num_arenas=8
  112. elif [ $num_arenas -gt 32 ] ; then
  113. num_arenas=32
  114. fi
  115. export MALLOC_ARENA_MAX=$num_arenas
  116. PATH_PREFIX=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^path *= *" | sed -e 's/^path *= *//'`
  117. export PID=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^pid *= *" | sed -e 's/^pid *= *//'`
  118. export LD_PRELOAD=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^JNI_PATH *= *" | sed -e 's/^JNI_PATH *= *//'`:$LD_PRELOAD
  119. NEW_PATH="${PATH_PREFIX}/bin"
  120. for D in ${PATH//:/ } ; do
  121. if [ "${D}" = "${NEW_PATH}" ]; then
  122. unset NEW_PATH
  123. break
  124. fi
  125. done
  126. if [ -n "${NEW_PATH}" ]; then
  127. NEW_PATH="${NEW_PATH}:${PATH_PREFIX}/sbin:${PATH_PREFIX}/etc/init.d:${PATH_PREFIX}/testing/regress"
  128. fi
  129. if [[ $platform == 'linux' ]]; then
  130. NEW_LIB="${PATH_PREFIX}/lib"
  131. for D in ${LD_LIBRARY_PATH//:/ } ; do
  132. if [ "${D}" = "${NEW_LIB}" ]; then
  133. unset NEW_LIB
  134. break
  135. fi
  136. done
  137. NEW_LIB2="${PATH_PREFIX}/plugins"
  138. for D in ${LD_LIBRARY_PATH//:/ } ; do
  139. if [ "${D}" = "${NEW_LIB2}" ]; then
  140. unset NEW_LIB2
  141. break
  142. fi
  143. done
  144. if [[ -d "/opt/rh/devtoolset-2" ]]; then
  145. export CL_PATH=/opt/rh/devtoolset-2/root/usr
  146. fi
  147. elif [[ $platform == 'mac' ]]; then
  148. NEW_LIB="${PATH_PREFIX}/lib"
  149. for D in ${DYLD_LIBRARY_PATH//:/ } ; do
  150. if [ "${D}" = "${NEW_LIB}" ]; then
  151. unset NEW_LIB
  152. break
  153. fi
  154. done
  155. NEW_LIB2="${PATH_PREFIX}/plugins"
  156. for D in ${DYLD_LIBRARY_PATH//:/ } ; do
  157. if [ "${D}" = "${NEW_LIB2}" ]; then
  158. unset NEW_LIB2
  159. break
  160. fi
  161. done
  162. fi
  163. if [ `basename -- "$0"` = "hpcc_setenv" ]; then
  164. if [ -z ${PATH_PREFIX} ]; then
  165. echo "# Can't find path in ${SECTION} section in ${HPCC_CONFIG}"
  166. exit 1
  167. fi
  168. # Not being sourced directly, so echo out commands to do so to the caller
  169. if [ -n "${NEW_PATH}" ]; then
  170. echo "export PATH=${PATH:+${PATH}:}${NEW_PATH}"
  171. else
  172. echo "# \$PATH already contains ${PATH_PREFIX}/bin"
  173. fi
  174. if [[ $platform == 'linux' ]]; then
  175. if [ -n "${NEW_LIB}" ]; then
  176. echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${NEW_LIB}"
  177. else
  178. echo "# \$LD_LIBRARY_PATH already contains ${PATH_PREFIX}/lib"
  179. fi
  180. if [ -n "${NEW_LIB2}" ]; then
  181. echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${NEW_LIB2}"
  182. else
  183. echo "# \$LD_LIBRARY_PATH already contains ${PATH_PREFIX}/plugins"
  184. fi
  185. elif [[ $platform == 'mac' ]]; then
  186. if [ -n "${NEW_LIB}" ]; then
  187. echo "export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH:+${DYLD_LIBRARY_PATH}:}${NEW_LIB}"
  188. else
  189. echo "# \$DYLD_LIBRARY_PATH already contains ${PATH_PREFIX}/lib"
  190. fi
  191. if [ -n "${NEW_LIB2}" ]; then
  192. echo "export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH:+${DYLD_LIBRARY_PATH}:}${NEW_LIB2}"
  193. else
  194. echo "# \$DYLD_LIBRARY_PATH already contains ${PATH_PREFIX}/plugins"
  195. fi
  196. fi
  197. else
  198. if [ -z ${PATH_PREFIX} ]; then
  199. return
  200. fi
  201. # being sourced - can set environment vars directly
  202. if [ -n "${NEW_PATH}" ]; then
  203. export PATH=${PATH:+${PATH}:}${NEW_PATH}
  204. fi
  205. if [[ $platform == 'linux' ]]; then
  206. if [ -n "${NEW_LIB}" ]; then
  207. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${NEW_LIB}
  208. fi
  209. if [ -n "${NEW_LIB2}" ]; then
  210. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${NEW_LIB2}
  211. fi
  212. elif [[ $platform == 'mac' ]]; then
  213. if [ -n "${NEW_LIB}" ]; then
  214. export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH:+${DYLD_LIBRARY_PATH}:}${NEW_LIB}
  215. fi
  216. if [ -n "${NEW_LIB2}" ]; then
  217. export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH:+${DYLD_LIBRARY_PATH}:}${NEW_LIB2}
  218. fi
  219. fi
  220. fi