mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +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 | # |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 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 | # If not in server mode, exec jack |
| 59 | # |
| 60 | if [ "$SERVER" != "true" ]; then |
Yohann Roussel | 30b5f31 | 2015-04-16 18:19:35 +0200 | [diff] [blame] | 61 | exec $JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.Main "$@" |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 62 | echo "ERROR: Cannot succeed to launch Jack without Jack server" >&2 |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 63 | exit 255 |
| 64 | fi |
| 65 | |
| 66 | # |
| 67 | # Static setting |
| 68 | # |
| 69 | SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer" |
| 70 | |
| 71 | # |
| 72 | # Prepare compilation |
| 73 | # |
| 74 | JACK_DIR="$SERVER_DIR/jack-task-$$/" |
| 75 | JACK_OUT="$JACK_DIR/out" |
| 76 | JACK_ERR="$JACK_DIR/err" |
| 77 | JACK_CLI="$JACK_DIR/cli" |
| 78 | JACK_EXIT="$JACK_DIR/exit" |
| 79 | JACK_PWD="$PWD" |
| 80 | |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 81 | mkdir "$SERVER_DIR" 2>/dev/null |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 82 | |
| 83 | # Cleanup |
Benoit Lamarche | 4b541e7 | 2015-04-21 22:02:56 +0200 | [diff] [blame] | 84 | trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>>$SERVER_LOG; rmdir "$JACK_DIR" 2>>$SERVER_LOG' EXIT |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 85 | |
| 86 | set -o errexit |
| 87 | |
| 88 | # Create fifos and files for a task |
Yohann Roussel | 2f3d79d | 2015-07-09 23:54:54 +0200 | [diff] [blame] | 89 | rm -rf "$JACK_DIR" |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 90 | mkdir "$JACK_DIR" |
| 91 | mkfifo "$JACK_OUT" |
| 92 | mkfifo "$JACK_ERR" |
| 93 | touch "$JACK_CLI" "$JACK_EXIT" |
| 94 | |
| 95 | # Try to cleanup if interrupted |
Jean-Marie Henaff | 56112a0 | 2015-04-23 10:00:16 +0200 | [diff] [blame] | 96 | abort () { echo $(uptime) >>$SERVER_LOG; kill -9 $PID_OUT $PID_ERR 2>>$SERVER_LOG; wait $PID_OUT $PID_ERR 2>>$SERVER_LOG; exit 255; } |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 97 | trap 'abort' SIGHUP SIGINT SIGQUIT SIGTERM ERR |
| 98 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 99 | # Redirect output and error |
| 100 | cat <"$JACK_OUT" >&1 & |
| 101 | PID_OUT=$! |
| 102 | cat <"$JACK_ERR" >&2 & |
| 103 | PID_ERR=$! |
| 104 | |
| 105 | # Prepare the working directory and command line |
| 106 | echo -n \"$PWD\" "" >"$JACK_CLI" |
| 107 | for i in "$@"; do |
| 108 | echo -n \"$i\" "" >>"$JACK_CLI" |
| 109 | done |
| 110 | echo >>"$JACK_CLI" |
| 111 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 112 | # |
| 113 | # Launch the compilation |
| 114 | # |
| 115 | |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 116 | set +o errexit |
| 117 | trap ERR |
| 118 | |
Benoit Lamarche | 4853341 | 2015-04-22 18:04:22 +0200 | [diff] [blame] | 119 | RETRY_LAUNCH=1 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 120 | RETRY_SESSION=3 |
Benoit Lamarche | 4853341 | 2015-04-22 18:04:22 +0200 | [diff] [blame] | 121 | DELAY_CONNECT=30 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 122 | |
| 123 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 124 | # Launch compilation |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 125 | DATE_CONNECT=$(date +%s) |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 126 | while true; do |
Benoit Lamarche | 4853341 | 2015-04-22 18:04:22 +0200 | [diff] [blame] | 127 | CURL_TIME=$(date +%H:%M:%S) |
Jean-Marie Henaff | 8bfc194 | 2015-04-17 16:32:26 +0200 | [diff] [blame] | 128 | HTTP_CODE=$(curl --fail --silent --data @- --output "$JACK_EXIT" --write-out %{http_code} --connect-timeout 10 --no-proxy 127.0.0.1:$SERVER_PORT_SERVICE http://127.0.0.1:$SERVER_PORT_SERVICE/jack <<< "+ $JACK_OUT $JACK_ERR $JACK_CLI") |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 129 | CURL_CODE=$? |
| 130 | JACK_CODE=$(cat "$JACK_EXIT") |
Benoit Lamarche | 4853341 | 2015-04-22 18:04:22 +0200 | [diff] [blame] | 131 | echo "CURL: $$ - $CURL_TIME - $CURL_CODE - $HTTP_CODE - $JACK_CODE" >>$SERVER_LOG |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 132 | if [ $CURL_CODE -eq 0 ]; then |
| 133 | # No problem, let's go |
| 134 | break; |
| 135 | elif [ $CURL_CODE -eq 7 ]; then |
| 136 | # Failed to connect |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 137 | if [ $(date +%s) -ge $DATE_CONNECT ]; then |
| 138 | if [ $RETRY_LAUNCH -eq 0 ]; then |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 139 | echo "ERROR: Cannot launch Jack server" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 140 | abort |
| 141 | else |
| 142 | let RETRY_LAUNCH=RETRY_LAUNCH-1 |
| 143 | echo "Launching background server" $SERVER_PRG |
| 144 | $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 & |
| 145 | # New server, let's try a bit to connect |
| 146 | let DATE_CONNECT=$(date +%s)+$DELAY_CONNECT; |
| 147 | fi |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 148 | else |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 149 | sleep 0.2 2>/dev/null |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 150 | fi |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 151 | # Trying with a new connection, let's retry session 3 times max |
| 152 | RETRY_SESSION=3 |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 153 | elif [ $CURL_CODE -eq 22 ]; then |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 154 | # Http code not OK, let's decode and abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 155 | if [ $HTTP_CODE -eq 401 ]; then |
| 156 | # 401: Unauthorized |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 157 | echo "ERROR: Security problem, see Jack server log ($SERVER_LOG)" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 158 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 159 | elif [ $HTTP_CODE -eq 400 ]; then |
| 160 | # 400: Bad request |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 161 | echo "ERROR: Bad request, see Jack server log ($SERVER_LOG)" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 162 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 163 | else |
| 164 | # Other |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 165 | echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see Jack server log ($SERVER_LOG)" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 166 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 167 | fi |
| 168 | else |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 169 | # In case of partial, timeout, empty respond, network error, let's retry |
| 170 | if [ $RETRY_SESSION -eq 0 ]; then |
Yohann Roussel | 9ebf1c8 | 2015-06-01 09:12:40 +0200 | [diff] [blame] | 171 | echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2 |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 172 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 173 | else |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 174 | let RETRY_SESSION=RETRY_SESSION-1 |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 175 | fi |
| 176 | fi |
| 177 | done |
| 178 | |
| 179 | # Wait for termination |
| 180 | wait $PID_OUT |
| 181 | wait $PID_ERR |
| 182 | |
| 183 | # Exit |
| 184 | exit $JACK_CODE |