Home | History | Annotate | Download | only in tools
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2016 The Android Open Source Project
      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 # Version: 1.3-a8
     18 #
     19 set -o nounset
     20 
     21 #
     22 # Settings
     23 #
     24 JACK_HOME="${JACK_HOME:=$HOME/.jack-server}"
     25 CLIENT_SETTING="${CLIENT_SETTING:=$HOME/.jack-settings}"
     26 TMPDIR=${TMPDIR:=/tmp}
     27 
     28 LAUNCHER_JAR="$JACK_HOME/launcher.jar"
     29 LAUNCHER_NAME=com.android.jack.launcher.ServerLauncher
     30 CURRENT_CHARSET=$(locale charmap)
     31 if [ -z "$CURRENT_CHARSET" ]; then
     32   CHARSET_ARGUMENT=
     33 else
     34   CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET"
     35 fi
     36 
     37 JACK_LOGS_DIR="$JACK_HOME"/logs
     38 JACK_OUT_ERR="$JACK_LOGS_DIR"/outputs.txt
     39 JACK_CONNECTION_TIMEOUT=300
     40 
     41 #
     42 # Load client settings
     43 #
     44 if [ -f "$CLIENT_SETTING" ]; then
     45   source "$CLIENT_SETTING"
     46 fi
     47 
     48 
     49 usage () {
     50   echo "Usage : $0"
     51 }
     52 
     53 checkCurl() {
     54   CURL_COMMAND=$(which curl)
     55 
     56   if [ $? -ne 0 ] ; then
     57     echo "curl not found in PATH"
     58     return 255
     59   fi
     60 
     61   if ! "$CURL_COMMAND" --version >/dev/null 2>&1 ; then
     62     echo "Failed to execute '$CURL_COMMAND'"
     63     return 255
     64   fi
     65 
     66   if "$CURL_COMMAND" --version | grep -q "SecureTransport" ; then
     67     echo "'$CURL_COMMAND' unsupported, please use a curl not based on SecureTransport"
     68   fi
     69 
     70   if "$CURL_COMMAND" --version | grep -q "OpenSSL/1.0.0" ; then
     71     echo "'$CURL_COMMAND' is using a too old SSL library, please update curl and its dependencies"
     72   elif curl --version | grep -q "OpenSSL/0." ; then
     73     echo "'$CURL_COMMAND' is using a too old SSL library, please update curl and its dependencies"
     74   fi
     75 }
     76 
     77 
     78 checkJava() {
     79   JAVA_COMMAND=$(which java)
     80 
     81   if [ $? -ne 0 ] ; then
     82     echo "java not found in PATH"
     83     return 255
     84   fi
     85 
     86   if ! "$JAVA_COMMAND" -version >/dev/null 2>&1 ; then
     87     echo "Failed to execute '$JAVA_COMMAND'"
     88     return 255
     89   fi
     90 
     91 
     92   JAVA_VERSION=$("$JAVA_COMMAND" -version 2>&1 | head -1 | grep --only-matching -e \"1\\.[0-9]* | cut -c 4-)
     93   if [ -z "$JAVA_VERSION" ] ; then
     94     echo "'$CURL_COMMAND': Failed to parse version, please ensure you're running a supported java"
     95     return 255
     96   fi
     97 
     98   if [ "$JAVA_VERSION" -lt 7 ] ; then
     99     echo "'$JAVA_COMMAND' is too old, please update to 1.7 or newer"
    100   fi
    101 }
    102 
    103 
    104 checkKeytool() {
    105   KEYTOOL_COMMAND=$(which keytool)
    106 
    107   if [ $? -ne 0 ] ; then
    108     echo "keytool not found in PATH"
    109     return 255
    110   fi
    111 
    112   if ! $KEYTOOL_COMMAND -help >/dev/null 2>&1 ; then
    113     echo "failed to execute '$KEYTOOL_COMMAND'"
    114     return 255
    115   fi
    116 
    117 
    118   if ! $KEYTOOL_COMMAND -help 2>&1 | grep -q -e "-genkeypair" ; then
    119     echo "'$KEYTOOL_COMMAND' unsupported, please ensure PATH allows finding keytool from a supported Java Runtime Environment ie 1.7 or newer"
    120   fi
    121 }
    122 
    123 
    124 #
    125 # $1: port number
    126 #
    127 checkport() {
    128   PID_USING_PORT=$(lsof -F p -i TCP:$1 -sTCP:LISTEN | cut -c 2-)
    129   if [ -z "$PID_USING_PORT" ] ; then
    130     # port is free nothing to check
    131     return 0
    132   fi
    133 
    134   PS_OUT=$(ps -p $PID_USING_PORT -o  "pid uid args" | tail -1)
    135   if [ $? -ne 0 ] ; then
    136     # process exited before we could get any info, weird but means the port is now available
    137     return 0
    138   fi
    139 
    140   if ! echo $PS_OUT | grep -q $LAUNCHER_NAME  ; then
    141     echo "Port $1 is used by another process (pid=$(echo $PS_OUT | awk '{print $1}')), please ensure to free the port or change port configuration in '$CLIENT_SETTING' and '$JACK_HOME/config.properties'"
    142     return 255
    143   fi
    144 
    145   if [ "$(echo $PS_OUT | awk '{print $2}')" -ne "$(id -u)" ] ; then
    146     echo "Port $1 is used b a Jack server from another user uid=$(echo $PS_OUT | awk '{print $2}'), please change port configuration in '$CLIENT_SETTING' and '$JACK_HOME/config.properties'"
    147     return 255
    148   fi
    149 }
    150 
    151 checkCurl
    152 
    153 checkJava
    154 
    155 checkKeytool
    156 
    157 checkport $SERVER_PORT_ADMIN
    158 checkport $SERVER_PORT_SERVICE
    159 
    160 # Exit
    161 
    162 exit 0
    163