-
Notifications
You must be signed in to change notification settings - Fork 6
/
hostexec
executable file
·235 lines (220 loc) · 8.17 KB
/
hostexec
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env bash
RED='\033[1;91m'
BLUE='\033[1;94m'
GREEN='\033[1;92m'
YELLOW='\033[1;33m'
RESETCOLOR='\033[1;00m'
PATH="$PATH:$RUNSTATIC"
JOBNUMFL="$EXECFLDIR/job"
error_msg() {
echo -e "${RED}[ ERROR ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR"
if [ "$NOT_TERM" == 1 ]
then
notify-send -a 'RunImage Error' "$@" 2>/dev/null &
fi
}
get_child_pids() {
local child_pids="$(ps --forest -o pid= -g $(ps -o sid= -p $1 2>/dev/null) 2>/dev/null)"
echo -e "$1\n$(ps -o pid=,cmd= -p $child_pids 2>/dev/null|sort -n|\
sed "0,/$1/d" 2>/dev/null|grep -Pv '\d+ sleep \d+'|awk '{print$1}')"|sort -nu
}
try_kill() {
ret=1
if [ -n "$1" ]
then
for pid in $1
do
trykillnum=1
while [[ -n "$pid" && -d "/proc/$pid" ]]
do
if [[ "$trykillnum" -le 3 ]]
then
kill -2 $pid 2>/dev/null
ret=$?
sleep 0.05
else
kill -9 $pid 2>/dev/null
ret=$?
wait $pid &>/dev/null
wait_pid "$pid"
fi
trykillnum="$(( $trykillnum + 1 ))"
done
done
fi
return $ret
}
wait_pid() {
if [ -n "$1" ]
then
if [ "$UNLIM_WAIT" == 1 ]
then
while [ -d "/proc/$1" ]; do sleep 0.1; done
else
[ -n "$2" ] && \
timeout="$2"||
timeout="100"
waittime=1
while [[ -d "/proc/$1" && "$waittime" -le "$timeout" ]]
do
sleep 0.01
waittime="$(( $waittime + 1 ))"
done
fi
fi
}
killjobpids() { try_kill "$(get_child_pids "$execjobpid")" ; }
set_terminal() {
if hostexec which konsole &>/dev/null
then args+=("konsole" "-e")
elif hostexec which gnome-terminal &>/dev/null
then args+=("gnome-terminal" "--wait" "--")
elif hostexec which xfce4-terminal &>/dev/null
then args+=("xfce4-terminal" "--disable-server" "-x")
elif hostexec which qterminal &>/dev/null
then args+=("qterminal" "-e")
elif hostexec which kitty &>/dev/null
then args+=("kitty" "-e")
elif hostexec which deepin-terminal &>/dev/null
then args+=("deepin-terminal" "-e")
elif hostexec which lxterminal &>/dev/null
then args+=("lxterminal" "-e")
elif hostexec which roxterm &>/dev/null
then args+=("roxterm" "-e")
elif hostexec which alacritty &>/dev/null
then args+=("alacritty" "-e")
elif hostexec which tilix &>/dev/null
then args+=("tilix" "-e")
elif hostexec which st &>/dev/null
then args+=("st" "-e")
elif hostexec which cool-retro-term &>/dev/null
then args+=("cool-retro-term" "-e")
elif hostexec which xterm &>/dev/null
then args+=("xterm" "-bg" "black" "-fg" "white" "-e")
elif hostexec which sakura &>/dev/null # next terminals don't recognize spaces
then args+=("sakura" "-e")
elif hostexec which terminology &>/dev/null
then args+=("terminology" "-e")
elif hostexec which terminator &>/dev/null
then args+=("terminator" "-e") # need
elif hostexec which tilda &>/dev/null
then args+=("tilda" "-c")
else
error_msg "The terminal application cannot be detected!"
return 1
fi
}
set_supassapp() {
if hostexec which pkexec &>/dev/null
then args+=("pkexec")
elif hostexec which kdesu &>/dev/null
then args+=("kdesu" "--noignorebutton" "-t")
elif hostexec which gksudo &>/dev/null
then args+=("gksudo")
elif hostexec which gksu &>/dev/null
then args+=("gksu")
elif hostexec which sudo &>/dev/null
then
set_terminal && \
args+=("sudo") || \
return 1
else
error_msg "The app for requesting the superuser pass not found!"
return 1
fi
}
print_help() {
RUNHOSTNAME="$(uname -a|awk '{print$2}')"
echo -e "
${RED}Usage:
$RED┌──[$GREEN$RUNUSER$YELLOW@$BLUE${RUNHOSTNAME}$RED]─[$GREEN$PWD$RED] - pass command as args
$RED└──╼ \$$GREEN ${0}${BLUE} {args} $GREEN{executable} $YELLOW{executable args}
$RED┌──[$GREEN$RUNUSER$YELLOW@$BLUE${RUNHOSTNAME}$RED]─[$GREEN$PWD$RED] - pass command to stdin
$RED└──╼ \$${GREEN} echo ${BLUE}\"$GREEN{executable} $YELLOW{executable args}${BLUE}\"${RED}|$GREEN${0}${BLUE} {args}
${BLUE}--help ${RED}|${BLUE}-h$GREEN Show this usage info
${BLUE}--shell ${RED}|${BLUE}-s$GREEN $YELLOW{args}$GREEN Launch host shell (socat + ptyspawn)
${BLUE}--superuser ${RED}|${BLUE}-su$GREEN $YELLOW{args}$GREEN Execute command as superuser
${BLUE}--terminal ${RED}|${BLUE}-t$GREEN $YELLOW{args}$GREEN Execute command in host terminal
"
}
host_shell() {
wait_shell() {
wait_time=100
while [[ ! -S "$SHELL_SOCKET" && "$wait_time" -gt 0 ]]; \
do
sleep 0.01
wait_time=$(( $wait_time - 1 ))
done
}
HOST_SHELL="$(hostexec echo '$SHELL')"
if [ -n "$HOST_SHELL" ]
then
SHELL_SOCKET="/tmp/.shell.$RUNPID"
if [ -n "$1" ]
then
hostexec socat UNIX-LISTEN:"$SHELL_SOCKET" EXEC:"$HOST_SHELL",pty,stderr,setsid,sigint,sane &>/dev/null &
wait_shell
check_args_spaces "$@"
echo "${args[@]}"|socat STDIO UNIX-CONNECT:"$SHELL_SOCKET"
EXEC_STATUS=$?
else
hostexec socat UNIX-LISTEN:"$SHELL_SOCKET" SYSTEM:"ptyspawn '$HOST_SHELL'" &>/dev/null &
wait_shell
socat $(tty),raw,echo=0 UNIX-CONNECT:"$SHELL_SOCKET"
EXEC_STATUS=$?
[ "$EXEC_STATUS" != 0 ] && \
kill $(pgrep -fa socat|grep "$SHELL_SOCKET"|awk '{print$1}')
fi
else
error_msg "Could not determine host shell!"
return 1
fi
return $EXEC_STATUS
}
check_args_spaces() {
for arg in "$@"
do
echo "$arg"|grep -Po '\s' &>/dev/null && \
arg="$(echo "$arg"|sed 's|.*|"&"|' 2>/dev/null)"
args+=("$arg")
done
}
cleanup() { [[ -d "$execjobdir" && -n "$jobnum" ]] && rm -rf "$execjobdir" 2>/dev/null ; }
if [[ -d "$EXECFLDIR" && -e "$JOBNUMFL" ]]
then
trap 'killjobpids;cleanup' SIGINT SIGTERM
trap 'cleanup' EXIT
unset args
case $1 in
-h |--help) print_help ; exit 0 ;;
-s |--shell) shift ; host_shell "$@" ; exit $? ;;
-t |--terminal) shift ; set_terminal || exit 1 ;;
-su|--superuser) shift ; set_supassapp || exit 1 ;;
esac
jobnum="$(cat "$JOBNUMFL" 2>/dev/null)"
if [ -n "$jobnum" ]
then
execjobdir="$EXECFLDIR/$jobnum"
execjobfl="$execjobdir/exec"
execjoboutfl="$execjobdir/out"
execjobstatfl="$execjobdir/stat"
if [ -n "$1" ]
then
check_args_spaces "$@"
echo "${args[@]}" > "$execjobfl"
else
if [ -n "$args" ]
then (echo -n "${args[@]} ";cat) > "$execjobfl" 2>/dev/null
else cat > "$execjobfl" 2>/dev/null
fi
fi
execjobpid="$(cat "$execjobstatfl" 2>/dev/null)"
cat "$execjoboutfl" 2>/dev/null
execstat="$(cat "$execjobstatfl" 2>/dev/null)"
fi
[ -n "$execstat" ]||execstat=1
exit $execstat
else
error_msg "EXECFLDIR is not configured!"
exit 1
fi