summaryrefslogtreecommitdiffstats
path: root/jack/etc/jack
blob: 4c07d14553b29ee2dc3a58f62c0ab5c511f82a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Default setting
#

if [ -z "$TMPDIR" ]; then
  TMPDIR="/tmp"
fi

if [ -z "$SERVER_NB_COMPILE" ]; then
  SERVER_NB_COMPILE=6
fi

if [ -z "$SERVER_TIMEOUT" ]; then
  SERVER_TIMEOUT=60
fi

#
# Static setting
#

set -o nounset

# Release code (3 digits) followed by the sub-release code (3 digits)
JACK_ID=002009

SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.Server"
SERVER_DIR="$TMPDIR/jack-$USER"
SERVER_FIFO="$SERVER_DIR/jack-$JACK_ID.cmd"
SERVER_LOCK="$SERVER_DIR/jack-$JACK_ID.lock"
SERVER_LOG="$SERVER_DIR/jack-$JACK_ID.log"

JACK_DIR="$SERVER_DIR/jack-task-$$/"
JACK_OUT="$JACK_DIR/out"
JACK_ERR="$JACK_DIR/err"
JACK_EXIT="$JACK_DIR/exit"
JACK_CLI="$JACK_DIR/cli"
JACK_PWD="$PWD"

#
# Prepare server
#

mkdir  -m 700 "$SERVER_DIR"  2>/dev/null
mkfifo -m 600 "$SERVER_FIFO" 2>/dev/null

#
# Launch the server if needed
#

SERVER_TMP_LOCK=$SERVER_LOCK.$$
trap 'rm -f $SERVER_TMP_LOCK 2>/dev/null;' EXIT

while true; do
  echo $$ > "$SERVER_TMP_LOCK" 2>/dev/null
  if ln "$SERVER_TMP_LOCK" "$SERVER_LOCK" 2>/dev/null; then
    rm "$SERVER_TMP_LOCK"
    echo "Launching background server" $SERVER_PRG
    $SERVER_PRG $SERVER_NB_COMPILE $SERVER_TIMEOUT "$SERVER_FIFO" "$SERVER_LOCK" >$SERVER_LOG 2>&1 &
    echo $! > "$SERVER_LOCK"
    break
  else
    CANDIDATE_PID=`cat $SERVER_LOCK`
    if [ $? -eq 0 ]; then
      if kill -0 $CANDIDATE_PID 2>/dev/null; then
        # Server seems to be alive, go ahead
        rm "$SERVER_TMP_LOCK" 2>/dev/null
        break
      else
        # Stale lock, clean and retry again
        # Unfortunately, there is a race condition here leading to launching several
        # servers. We are living with that.
        echo "Stale lock detected, cleaning"
        rm "$SERVER_LOCK" "$SERVER_TMP_LOCK" 2>/dev/null
      fi
    else
      # Someone is removing or echoing inside the lock, try again
      rm "$SERVER_TMP_LOCK" 2>/dev/null
    fi
    # Calm down the retry
    sleep 1
  fi
done

#
# Prepare compilation
#

# Cleanup
trap 'rm -f $JACK_OUT $JACK_ERR $JACK_EXIT $JACK_CLI 2>/dev/null; rmdir $JACK_DIR 2>/dev/null' EXIT

# Create fifo for a task
mkdir  -m 700 "$JACK_DIR"
mkfifo -m 600 "$JACK_OUT"
mkfifo -m 600 "$JACK_ERR"
mkfifo -m 600 "$JACK_EXIT"

# Try to cleanup if interrupted
trap 'rm $JACK_EXIT; kill -9 $PID_OUT $PID_ERR; wait $PID_OUT; wait $PID_ERR; exit -1' SIGHUP SIGINT SIGQUIT SIGTERM

# Redirect output and error
cat <"$JACK_OUT" >&1 &
PID_OUT=$!
cat <"$JACK_ERR" >&2 &
PID_ERR=$!

# Prepare the working directory and command line
echo -n \"$PWD\" "" >"$JACK_CLI"
for i in "$@"; do
  echo -n \"$i\" "" >>"$JACK_CLI"
done
echo >>"$JACK_CLI"

#
# Launch the compilation
#

# Launch compilation
echo "+ $JACK_OUT $JACK_ERR $JACK_EXIT $JACK_CLI" >>"$SERVER_FIFO"
EXIT_CODE=$(cat "$JACK_EXIT")
if [ -z "$EXIT_CODE" ]; then
  EXIT_CODE=-1;
fi

# Wait for termination
wait $PID_OUT
wait $PID_ERR

# Exit
exit $EXIT_CODE