Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2015 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 | # |
Benoit Lamarche | 43a5b49 | 2015-07-21 10:53:25 +0200 | [diff] [blame] | 17 | # Version: 1.1-mr2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 18 | # |
| 19 | set -o nounset |
| 20 | umask 077 |
| 21 | |
| 22 | # |
| 23 | # Settings |
| 24 | # |
| 25 | LOCAL_SETTING="$HOME/.jack" |
| 26 | TMPDIR=${TMPDIR:=/tmp} |
| 27 | SERVER_DIR=$TMPDIR/jack-$USER |
| 28 | |
| 29 | # |
| 30 | # Load local settings |
| 31 | # |
| 32 | source "$LOCAL_SETTING" 2>/dev/null |
| 33 | |
| 34 | # |
| 35 | # Create or update local settings if needed |
| 36 | # |
| 37 | if [[ ! -f "$LOCAL_SETTING" || $SETTING_VERSION -lt 2 ]]; then |
| 38 | echo "Writing local settings in" $LOCAL_SETTING |
| 39 | cat >"$LOCAL_SETTING.$$" <<-EOT |
| 40 | # Server settings |
| 41 | SERVER=${SERVER:=true} |
| 42 | SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8072} |
| 43 | SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8073} |
| 44 | SERVER_COUNT=${SERVER_COUNT:=1} |
| 45 | SERVER_NB_COMPILE=${SERVER_NB_COMPILE:=4} |
| 46 | SERVER_TIMEOUT=${SERVER_TIMEOUT:=60} |
| 47 | SERVER_LOG=\${SERVER_LOG:=\$SERVER_DIR/jack-\$SERVER_PORT_SERVICE.log} |
| 48 | JACK_VM_COMMAND=\${JACK_VM_COMMAND:=java} |
| 49 | # Internal, do not touch |
| 50 | SETTING_VERSION=2 |
| 51 | EOT |
| 52 | ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING" |
| 53 | rm "$LOCAL_SETTING.$$" |
| 54 | source "$LOCAL_SETTING" |
| 55 | fi |
| 56 | |
| 57 | # |
| 58 | # Static setting |
| 59 | # |
| 60 | mkdir "$SERVER_DIR" 2>/dev/null |
| 61 | |
| 62 | # |
| 63 | # Decoding argument |
| 64 | # |
| 65 | |
| 66 | if [ $# -ne 1 ] |
| 67 | then |
| 68 | echo "Usage : $0 [ start-server | stop-server | kill-server | list-server | server-log | server-stat ]" |
| 69 | exit 255 |
| 70 | fi |
| 71 | |
| 72 | case $1 in |
| 73 | stop-server) |
| 74 | echo "Stopping background server" |
| 75 | CMD="stop" ;; |
| 76 | server-stat) |
| 77 | echo "Getting statistic from background server" |
| 78 | CMD="stat" ;; |
| 79 | server-log) |
| 80 | echo "Log location of background server:" |
| 81 | echo $SERVER_LOG |
| 82 | exit 0 ;; |
| 83 | kill-server) |
| 84 | echo "Killing background server" |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 85 | kill $(ps aux | grep JackSimpleServer | grep -v grep | awk '{print $2}') 2>/dev/null |
| 86 | if [ $? -ne 0 ]; then |
| 87 | echo "ERROR: No Jack server to kill" >&2 |
| 88 | exit 2 |
| 89 | else |
| 90 | exit 0 |
| 91 | fi ;; |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 92 | list-server) |
| 93 | ps aux | grep JackSimpleServer | grep -v grep |
| 94 | exit $? ;; |
| 95 | start-server) |
| 96 | SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer" |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 97 | echo "Launching Jack server" $SERVER_PRG |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 98 | $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 & |
| 99 | exit 0 ;; |
| 100 | *) |
| 101 | echo "Usage : $0 [ start-server | stop-server | kill-server | list-server | server-log | server-stat ]" |
| 102 | exit 255 ;; |
| 103 | esac |
| 104 | |
| 105 | # |
| 106 | # Send command |
| 107 | # |
| 108 | |
| 109 | set +o errexit |
| 110 | |
| 111 | # Try to cleanup if interrupted |
| 112 | # Cleanup |
| 113 | JACK_EXIT="$SERVER_DIR/admin-$$" |
| 114 | trap 'rm -f "$JACK_EXIT" 2>/dev/null;' EXIT |
| 115 | |
| 116 | RETRY_SESSION=3 |
| 117 | DELAY_CONNECT=3 |
| 118 | |
| 119 | |
| 120 | # Send command to Jack server |
| 121 | DATE_CONNECT=$(date +%s) |
| 122 | while true; do |
| 123 | HTTP_CODE=$(curl --fail --silent --output $JACK_EXIT --write-out %{http_code} --connect-timeout 5 --no-proxy 127.0.0.1:$SERVER_PORT_ADMIN http://127.0.0.1:$SERVER_PORT_ADMIN/$CMD) |
| 124 | CURL_CODE=$? |
| 125 | if [ $CURL_CODE -eq 0 ]; then |
| 126 | # No problem, let's go |
| 127 | break; |
| 128 | elif [ $CURL_CODE -eq 7 ]; then |
| 129 | # Failed to connect |
| 130 | if [ $(date +%s) -ge $DATE_CONNECT ]; then |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 131 | echo "ERROR: No Jack server running" |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 132 | exit 255 |
| 133 | else |
| 134 | sleep 0.2 2>/dev/null |
| 135 | fi |
| 136 | # Trying with a new connection, let's retry session 3 times max |
| 137 | RETRY_SESSION=3 |
| 138 | elif [ $CURL_CODE -eq 22 ]; then |
| 139 | # Http code not OK, let's decode and abort |
| 140 | if [ $HTTP_CODE -eq 401 ]; then |
| 141 | # 401: Unauthorized |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 142 | echo "ERROR: Security problem, see Jack server log" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 143 | exit 255 |
| 144 | elif [ $HTTP_CODE -eq 400 ]; then |
| 145 | # 400: Bad request |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 146 | echo "ERROR: Bad request, see Jack server log" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 147 | exit 255 |
| 148 | else |
| 149 | # Other |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 150 | echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see Jack server log" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 151 | exit 255 |
| 152 | fi |
| 153 | else |
| 154 | # In case of partial, timeout, empty respond, network error, let's retry |
| 155 | if [ $RETRY_SESSION -eq 0 ]; then |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 156 | echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 157 | exit 255 |
| 158 | else |
| 159 | let RETRY_SESSION=RETRY_SESSION-1 |
| 160 | fi |
| 161 | fi |
| 162 | done |
| 163 | |
| 164 | # Exit |
| 165 | if [ -r $JACK_EXIT ]; then |
| 166 | cat <$JACK_EXIT 2>/dev/null |
| 167 | fi |
| 168 | |
| 169 | exit 0 |