-
Notifications
You must be signed in to change notification settings - Fork 13
/
tri-client
executable file
·123 lines (95 loc) · 2.37 KB
/
tri-client
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
#!/bin/sh
dotfilebase="/tmp/.tricera"
me=`whoami`
portfile="$dotfilebase"_"$me"
if [ $(uname) = "Linux" ]; then
pathCmd="readlink -f"
elif [ $(uname) = "Darwin" ]; then
pathCmd="stat -f %N"
else
pathCmd="realpath"
fi
################################################################################
startDaemon() {
lockfile="$dotfilebase"_lock_"$me"
while [ -f "$lockfile" ] && \
[ $(( `date +%s` - `date -r "$lockfile" +%s` )) -le 10 ]; do
# apparently a concurrent script is starting up the daemon
# already
echo "waiting ..."
sleep 1
done
if [ ! -f "$portfile" ]; then
echo >"$lockfile"
BASEDIR=`dirname $($pathCmd $0)`
. $BASEDIR/triEnv
tempportfile=`mktemp`
echo >"$tempportfile"
chmod go-rwx "$tempportfile"
$LAZABS_CMD tricera.ServerMain >"$tempportfile" &
lazabsId=$!
sleep 1
while [ `wc -l "$tempportfile" | gawk '{ printf $1 }'` -lt 2 ]; do
if ps -p $lazabsId >/dev/null; then
sleep 1
else
echo "Could not start server"
exit 1
fi
done
mv "$tempportfile" "$portfile"
rm "$lockfile"
fi
}
################################################################################
stdintoexitstatus() {
read exitstatus
return $exitstatus
}
if [ ! -f "$portfile" ]; then
startDaemon
fi
mainProcess=$$
outputlogfile=`mktemp`
success=1
until [ $success -eq 0 ]; do
port=`head -n1 "$portfile"`
(
# send the ticket
tail -n1 "$portfile"
# command line arguments
for var; do
case "$var" in
-hints:*)
echo -hints:`$pathCmd "${var#-hints:}"`
;;
-*|+*)
echo "$var"
;;
*)
echo `$pathCmd "$var"`
;;
esac
done
echo "PROVE_AND_EXIT"
# ping the daemon every second, to show that we are still
# alive
{
sleep 1
while ps -p $mainProcess >/dev/null; do
echo "PING"
sleep 1
done
} &
) | (((( nc localhost $port; echo $? >&3 ) | \
tee $outputlogfile >&4) 3>&1) | stdintoexitstatus) 4>&1
success=$?
if [ $success -ne 0 ]; then
rm "$portfile"
startDaemon
else if grep -q '^ERROR:' $outputlogfile; then
rm $outputlogfile
exit 1
fi; fi
done
rm $outputlogfile