blob: 804b41ac3440c1de26a5e2218ed820d7d2afe16a [file] [log] [blame]
mikaelpeltier4d1fcc42015-04-07 11:15:06 +02001#!/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 Lamarche43a5b492015-07-21 10:53:25 +020017# Version: 1.1-mr2
Yohann Roussel1b697cb2015-04-15 15:01:33 +020018#
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020019set -o nounset
20umask 077
21
22#
23# Settings
24#
25LOCAL_SETTING="$HOME/.jack"
26TMPDIR=${TMPDIR:=/tmp}
27SERVER_DIR=$TMPDIR/jack-$USER
28
29#
30# Load local settings
31#
32source "$LOCAL_SETTING" 2>/dev/null
33
34#
35# Create or update local settings if needed
36#
37if [[ ! -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
51EOT
52 ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
53 rm "$LOCAL_SETTING.$$"
54 source "$LOCAL_SETTING"
55fi
56
57#
58# If not in server mode, exec jack
59#
60if [ "$SERVER" != "true" ]; then
Yohann Roussel30b5f312015-04-16 18:19:35 +020061 exec $JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.Main "$@"
Yohann Roussel9ebf1c82015-06-01 09:12:40 +020062 echo "ERROR: Cannot succeed to launch Jack without Jack server" >&2
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020063 exit 255
64fi
65
66#
67# Static setting
68#
69SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
70
71#
72# Prepare compilation
73#
74JACK_DIR="$SERVER_DIR/jack-task-$$/"
75JACK_OUT="$JACK_DIR/out"
76JACK_ERR="$JACK_DIR/err"
77JACK_CLI="$JACK_DIR/cli"
78JACK_EXIT="$JACK_DIR/exit"
79JACK_PWD="$PWD"
80
Yohann Roussel1b697cb2015-04-15 15:01:33 +020081mkdir "$SERVER_DIR" 2>/dev/null
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020082
83# Cleanup
Benoit Lamarche4b541e72015-04-21 22:02:56 +020084trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>>$SERVER_LOG; rmdir "$JACK_DIR" 2>>$SERVER_LOG' EXIT
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020085
86set -o errexit
87
88# Create fifos and files for a task
Yohann Roussel2f3d79d2015-07-09 23:54:54 +020089rm -rf "$JACK_DIR"
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020090mkdir "$JACK_DIR"
91mkfifo "$JACK_OUT"
92mkfifo "$JACK_ERR"
93touch "$JACK_CLI" "$JACK_EXIT"
94
95# Try to cleanup if interrupted
Jean-Marie Henaff56112a02015-04-23 10:00:16 +020096abort () { echo $(uptime) >>$SERVER_LOG; kill -9 $PID_OUT $PID_ERR 2>>$SERVER_LOG; wait $PID_OUT $PID_ERR 2>>$SERVER_LOG; exit 255; }
Yohann Roussel1b697cb2015-04-15 15:01:33 +020097trap 'abort' SIGHUP SIGINT SIGQUIT SIGTERM ERR
98
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020099# Redirect output and error
100cat <"$JACK_OUT" >&1 &
101PID_OUT=$!
102cat <"$JACK_ERR" >&2 &
103PID_ERR=$!
104
105# Prepare the working directory and command line
106echo -n \"$PWD\" "" >"$JACK_CLI"
107for i in "$@"; do
108 echo -n \"$i\" "" >>"$JACK_CLI"
109done
110echo >>"$JACK_CLI"
111
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200112#
113# Launch the compilation
114#
115
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200116set +o errexit
117trap ERR
118
Benoit Lamarche48533412015-04-22 18:04:22 +0200119RETRY_LAUNCH=1
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200120RETRY_SESSION=3
Benoit Lamarche48533412015-04-22 18:04:22 +0200121DELAY_CONNECT=30
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200122
123
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200124# Launch compilation
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200125DATE_CONNECT=$(date +%s)
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200126while true; do
Benoit Lamarche48533412015-04-22 18:04:22 +0200127 CURL_TIME=$(date +%H:%M:%S)
AUDITYA BHATTARAM94b51e52015-08-10 14:25:50 +0530128 HTTP_CODE=$(curl --fail --silent --data @- --output "$JACK_EXIT" --write-out %{http_code} --connect-timeout 10 --max-time 600 --no-proxy 127.0.0.1:$SERVER_PORT_SERVICE http://127.0.0.1:$SERVER_PORT_SERVICE/jack <<< "+ $JACK_OUT $JACK_ERR $JACK_CLI")
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200129 CURL_CODE=$?
130 JACK_CODE=$(cat "$JACK_EXIT")
Benoit Lamarche48533412015-04-22 18:04:22 +0200131 echo "CURL: $$ - $CURL_TIME - $CURL_CODE - $HTTP_CODE - $JACK_CODE" >>$SERVER_LOG
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200132 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 Roussel1b697cb2015-04-15 15:01:33 +0200137 if [ $(date +%s) -ge $DATE_CONNECT ]; then
138 if [ $RETRY_LAUNCH -eq 0 ]; then
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200139 echo "ERROR: Cannot launch Jack server" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200140 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
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200148 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200149 sleep 0.2 2>/dev/null
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200150 fi
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200151 # Trying with a new connection, let's retry session 3 times max
152 RETRY_SESSION=3
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200153 elif [ $CURL_CODE -eq 22 ]; then
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200154 # Http code not OK, let's decode and abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200155 if [ $HTTP_CODE -eq 401 ]; then
156 # 401: Unauthorized
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200157 echo "ERROR: Security problem, see Jack server log ($SERVER_LOG)" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200158 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200159 elif [ $HTTP_CODE -eq 400 ]; then
160 # 400: Bad request
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200161 echo "ERROR: Bad request, see Jack server log ($SERVER_LOG)" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200162 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200163 else
164 # Other
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200165 echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see Jack server log ($SERVER_LOG)" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200166 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200167 fi
168 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200169 # In case of partial, timeout, empty respond, network error, let's retry
170 if [ $RETRY_SESSION -eq 0 ]; then
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200171 echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200172 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200173 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200174 let RETRY_SESSION=RETRY_SESSION-1
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200175 fi
176 fi
177done
178
179# Wait for termination
180wait $PID_OUT
181wait $PID_ERR
182
183# Exit
184exit $JACK_CODE