Debian Codegraph @

util/android-commands.sh

raw
1 #!/usr/bin/env bash
2 # spell-checker:ignore termux keyevent sdcard binutils unmatch adb's dumpsys logcat pkill nextest logfile
3 # spell-checker:ignore screencap reinit PIPESTATUS keygen sourceslist
4
5 # There are four shells: the host's, adb, termux and termux via ssh.
6 # But only termux and termux via ssh provides a GNU environment on the
7 # emulated device (to e.g. run cargo).
8 # Initially, only adb lets us run commands directly on the emulated device.
9 # Thus we first establish a ssh connection which then can be used to access
10 # the termux shell directly, getting output and return code as usual.
11 # So we use adb to launch termux, then to send keystrokes to it while it's running.
12 # This way we install sshd and a public key from the host. After that we can
13 # use ssh to directly run commands in termux environment.
14
15 # Before ssh, we need to consider some inconvenient, limiting specialties:
16 # The commands sent to termux via adb keystrokes are first parsed as arguments in
17 # this shell, then as arguments in the adb shell, before finally being used as
18 # text inputs to the app. Hence, the "'wrapping'" on those commands.
19 # Using this approach there's no way to get any direct feedback from termux,
20 # so every time we run a command on it, we make sure it creates a unique *.probe file
21 # which is polled every 30 seconds together with the current output of the
22 # command in a *.log file. The contents of the probe file are used as a return code:
23 # 0 on success, some other number for errors (an empty file is basically the same as 0).
24 # Note that the return codes are text, not raw bytes.
25
26 # Additionally, we can use adb screenshot functionality to investigate issues
27 # when there is no feedback arriving from the android device.
28
29 this_repo="$(dirname "$(dirname -- "$(readlink -- "${0}")")")"
30 cache_dir_name="__rust_cache__"
31 dev_probe_dir=/sdcard
32 dev_home_dir=/data/data/com.termux/files/home
33
34 # This is a list of termux package mirrors approved to be used.
35 # The default mirror list contains entries that do not function properly anymore.
36 # To avoid failures due to broken mirrors, we use our own list.
37 # Choose only reliable mirrors here:
38 repo_url_list=(
39 "deb https://packages-cf.termux.org/apt/termux-main/ stable main"
40 "deb https://packages-cf.termux.dev/apt/termux-main/ stable main"
41 # "deb https://grimler.se/termux/termux-main stable main" # slow
42 "deb https://ftp.fau.de/termux/termux-main stable main"
43 )
44 number_repo_urls=${#repo_url_list[@]}
45 repo_url_round_robin=$RANDOM
46
47 move_to_next_repo_url() {
48 repo_url_round_robin=$(((repo_url_round_robin + 1) % number_repo_urls))
49 echo "next round robin repo_url: $repo_url_round_robin"
50 }
51 move_to_next_repo_url # first call needed for modulo
52
53 get_current_repo_url() {
54 echo "${repo_url_list[$repo_url_round_robin]}"
55 }
56
57 # dump some information about the runners system for debugging purposes:
58 echo "====== runner information ======"
59 echo "hostname: $(hostname)"
60 echo "uname -a: $(uname -a)"
61 echo "pwd: $(pwd)"
62 echo "\$*: $*"
63 echo "\$0: $0"
64 # shellcheck disable=SC2140
65 echo "\$(readlink -- "\$\{0\}"): $(readlink -- "${0}")"
66 echo "\$this_repo: $this_repo"
67 echo "readlink -f \$this_repo: $(readlink -f "$this_repo")"
68 this_repo=$(readlink -f "$this_repo")
69 echo "====== runner info end ========="
70
71 help() {
72 echo \
73 "Usage: $0 COMMAND [ARG]
74
75 where COMMAND is one of:
76 init download termux and initialize the emulator image
77 snapshot APK install APK and dependencies on an emulator to prep a snapshot
78 (you can, but probably don't want to, run this for physical
79 devices -- just set up termux and the dependencies yourself)
80 sync_host [REPO]
81 push the repo at REPO to the device, deleting and restoring all symlinks (locally)
82 in the process; The cached rust directories are restored, too; by default, REPO is:
83 $this_repo
84 sync_image [REPO]
85 copy the repo/target and the HOME/.cargo directories from the device back to the
86 host; by default, REPO is: $this_repo
87 build run \`cargo build --features feat_os_unix_android\` on the
88 device
89 tests run \`cargo test --features feat_os_unix_android\` on the
90 device
91
92 If you have multiple devices, use the ANDROID_SERIAL environment variable to
93 specify which to connect to."
94 }
95
96 hit_enter() {
97 adb shell input keyevent 66
98 }
99
100 exit_termux() {
101 adb shell input text \"exit\" && hit_enter && hit_enter
102 }
103
104 timestamp() {
105 date +"%H%M%S%Z"
106 }
107
108 add_timestamp_to_lines() {
109 while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
110 }
111
112 # takes a screenshot with given name from the android device. Filename is prefixed with timestamp.
113 # screenshots are collected at the end of the github workflow and provided as download link.
114 take_screen_shot() {
115 filename_prefix="$1"
116 filename="$this_repo/output/$(timestamp)_${filename_prefix}_screen.png"
117 echo "take screenshot: $filename"
118 mkdir -p "$this_repo/output"
119 adb exec-out screencap -p > "$filename"
120 }
121
122 get_app_user() {
123 app="$1"
124 app_user="$(adb shell dumpsys package $app | grep 'userId=' | cut -d= -f2 | sort -u)"
125 if [[ -z "$app_user" ]]; then
126 echo "Couldn't find user for app: $app">&2
127 exit 1
128 fi
129 echo "$app_user"
130 }
131
132 termux_user() {
133 if [[ -z "$TERMUX_USER" ]]; then
134 TERMUX_USER="$(get_app_user com.termux)"
135 fi
136 echo "$TERMUX_USER"
137 }
138
139 launch_termux() {
140 echo "launching termux"
141 take_screen_shot "launch_termux_enter"
142
143 adb shell input tap 120 380 # close potential dialog "System UI isn't responding" with "wait".
144 # should not cause side effects when dialog is not there as there are
145 # no relevant GUI elements at this position otherwise.
146
147 if ! adb shell 'am start -n com.termux/.HomeActivity'; then
148 echo "failed to launch termux"
149 exit 1
150 fi
151
152 take_screen_shot "launch_termux_after_start_activity"
153
154 # the emulator can sometimes be a little slow to launch the app
155 loop_count=0
156 while ! adb shell "dumpsys window windows" | \
157 grep -E "imeInputTarget in display# 0 Window{[^}]+com.termux\/com\.termux\.HomeActivity}"
158 do
159 sleep 1
160 loop_count=$((loop_count + 1))
161 if [[ loop_count -ge 20 ]]; then
162 break
163 fi
164 done
165
166 take_screen_shot "launch_termux_after_wait_activity"
167
168 touch_cmd() {
169 adb shell input text "\"touch $dev_probe_dir/launch.probe\"" && hit_enter
170 sleep 1
171 }
172
173 local timeout_start=120
174 local timeout=$timeout_start
175 touch_cmd
176 while ! adb shell "ls $dev_probe_dir/launch.probe" 2>/dev/null
177 do
178 echo "waiting for launch.probe - ($timeout / $timeout_start seconds)"
179 take_screen_shot "launch_termux_touch_probe"
180 sleep 4
181 touch_cmd
182
183 timeout=$((timeout - 4))
184 if [[ timeout -le 0 ]]; then
185 take_screen_shot "error_launch_termux"
186 echo "timeout waiting for termux to start up"
187 return 1
188 fi
189
190 done
191 echo "found launch.probe"
192 take_screen_shot "launch_termux_found_probe"
193 adb shell "rm $dev_probe_dir/launch.probe" && echo "removed launch.probe"
194 }
195
196 # Usage: run_termux_command
197 #
198 # Runs the command specified in $1 in a termux shell, polling for the probe specified in $2 (and the
199 # current output). If polling the probe succeeded the command is considered to have finished. This
200 # method prints the current stdout and stderr of the command every SLEEP_INTERVAL seconds and
201 # finishes a command run with a summary. It returns with the exit code of the probe if specified as
202 # file content of the probe.
203 #
204 # Positional arguments
205 # $1 The command to execute in the termux shell
206 # $2 The path to the probe. The file name must end with `.probe`
207 #
208 # It's possible to overwrite settings by specifying the setting the variable before calling this
209 # method (Default in parentheses):
210 # keep_log 0|1 Keeps the logs after running the command if set to 1. The log file name is
211 # derived from the probe file name (the last component of the path) and
212 # `.probe` replaced with `.log. (0)
213 # debug 0|1 Adds additional debugging output to the log file if set to 1. (1)
214 # timeout SECONDS The timeout in full SECONDS for the command to complete before giving up. (3600)
215 # retries RETRIES The number of retries for trying to fix possible issues when we're not receiving
216 # any progress from the emulator. (3)
217 # sleep_interval
218 # SECONDS The time interval in full SECONDS between polls for the probe and the current
219 # output. (5)
220 run_termux_command() {
221 # shellcheck disable=SC2155
222 local command="$(echo "$1" | sed -E "s/^['](.*)[']$/\1/")" # text of the escaped command, including creating the probe!
223 local probe="$2" # unique file that indicates the command is complete
224 local keep_log=${keep_log:-0}
225 local debug=${debug:-1}
226
227 log_name="$(basename -s .probe "${probe}").log" # probe name must have suffix .probe
228 log_file="$dev_probe_dir/${log_name}"
229 log_read="${log_name}.read"
230 echo 0 >"${log_read}"
231 if [[ $debug -eq 1 ]]; then
232 shell_command="'set -x; { ${command}; } &> ${log_file}; set +x'"
233 else
234 shell_command="'{ ${command}; } &> ${log_file}'"
235 fi
236
237 launch_termux || return
238
239 take_screen_shot "run_termux_command_before_input_of_shell_command"
240
241 # remove artificial quoting
242 shell_command="${shell_command%\'}"
243 shell_command="${shell_command#\'}"
244
245 echo "Running command: ${command}"
246 echo "Running shell-command: ${shell_command}"
247 start=$(date +%s)
248 adb_input_text_long "$shell_command" && sleep 1 && hit_enter
249 # just for safety wait a little bit before polling for the probe and the log file
250 sleep 1
251
252 take_screen_shot "run_termux_command_after_input_of_shell_command"
253
254 local timeout=${timeout:-3600}
255 local retries=${retries:-10}
256 local sleep_interval=${sleep_interval:-10}
257 try_fix=3
258 echo "run_termux_command with timeout=$timeout / retries=$retries / sleep_interval=$sleep_interval"
259 while ! adb shell "ls $probe" 2>/dev/null; do
260 echo -n "Waiting for $probe: "
261
262 if [[ -e "$log_name" ]]; then
263 rm "$log_name"
264 fi
265
266 adb pull "$log_file" . || try_fix=$((try_fix - 1))
267 if [[ -e "$log_name" ]]; then
268 tail -n +"$(<"$log_read")" "$log_name"
269 echo
270 wc -l <"${log_name}" | tr -d "[:space:]" >"$log_read"
271 fi
272
273 if [[ retries -le 0 ]]; then
274 echo "Maximum retries reached running command. Aborting ..."
275 take_screen_shot "run_termux_command_maximum_tries_reached"
276 return 1
277 elif [[ try_fix -le 0 ]]; then
278 retries=$((retries - 1))
279 try_fix=3
280 # Since there is no output, there is no way to know what is happening inside. See if
281 # hitting the enter key solves the issue, sometimes the github runner is just a little
282 # bit slow.
283 echo "No output received. Trying to fix the issue ... (${retries} retries left)"
284 take_screen_shot "run_termux_command_before_trying_to_fix"
285 hit_enter
286 sleep 1
287 take_screen_shot "run_termux_command_after_trying_to_fix"
288 fi
289
290 sleep "$sleep_interval"
291 timeout=$((timeout - sleep_interval))
292
293 if [[ $timeout -le 0 ]]; then
294 echo "Timeout reached running command. Aborting ..."
295 take_screen_shot "run_termux_command_timeout_reached"
296 return 1
297 fi
298 done
299 end=$(date +%s)
300
301 return_code=$(adb shell "cat $probe") || return_code=0
302 adb shell "rm ${probe}"
303
304 adb shell "cat $log_file" > "$log_name"
305 echo "==================================== SUMMARY ==================================="
306 echo "Command: ${command}"
307 echo "Finished in $((end - start)) seconds."
308 echo "Output was:"
309 cat "$log_name"
310 echo "Return code: $return_code"
311 echo "================================================================================"
312
313 adb shell "rm ${log_file}"
314 [[ $keep_log -ne 1 ]] && rm -f "$log_name"
315 rm -f "$log_read" "$probe"
316
317 take_screen_shot "run_termux_command_finished_normally"
318
319 # shellcheck disable=SC2086
320 return $return_code
321 }
322
323 init() {
324 arch="$1"
325 # shellcheck disable=SC2034
326 api_level="$2"
327 termux="$3"
328
329 snapshot_name="${AVD_CACHE_KEY}"
330
331 # shellcheck disable=SC2015
332 curl -sLO "https://github.com/termux/termux-app/releases/download/${termux}/termux-app_${termux}+github-debug_${arch}.apk" &&
333 snapshot "termux-app_${termux}+github-debug_${arch}.apk" &&
334 hash_rustc &&
335 exit_termux &&
336 adb -s emulator-5554 emu avd snapshot save "$snapshot_name" &&
337 echo "Emulator image created. Name: $snapshot_name" || {
338 pkill -9 qemu-system-x86_64
339 return 1
340 }
341 pkill -9 qemu-system-x86_64 || true
342 }
343
344 reinit_ssh_connection() {
345 setup_ssh_forwarding
346 test_ssh_connection && return
347
348 start_sshd_via_adb_shell && (
349 test_ssh_connection && return
350 generate_and_install_public_key && test_ssh_connection && return
351 ) || (
352 install_packages_via_adb_shell openssh openssl
353 generate_and_install_public_key
354 start_sshd_via_adb_shell
355 test_ssh_connection && return
356 ) || (
357 echo "failed to setup ssh connection"
358 return 1
359 )
360 }
361
362 start_sshd_via_adb_shell() {
363 echo "start sshd via adb shell"
364 probe="$dev_probe_dir/sshd.probe"
365 command="'sshd; echo \$? > $probe'"
366 run_termux_command "$command" "$probe"
367 }
368
369 setup_ssh_forwarding() {
370 echo "setup ssh forwarding"
371 adb forward tcp:9022 tcp:8022
372 }
373
374 copy_file_or_dir_to_device_via_ssh() {
375 scp -r "$1" "scp://$(termux_user)@127.0.0.1:9022/$2"
376 }
377
378 copy_file_or_dir_from_device_via_ssh() {
379 scp -r "scp://$(termux_user)@127.0.0.1:9022/$1" "$2"
380 }
381
382 # runs the in args provided command on android side via ssh. forwards return code.
383 # adds a timestamp to every line to be able to see where delays are
384 run_command_via_ssh() {
385 ssh -p 9022 "$(termux_user)@127.0.0.1" -o StrictHostKeyChecking=accept-new "$@" 2>&1 | add_timestamp_to_lines
386 return "${PIPESTATUS[0]}"
387 }
388
389 test_ssh_connection() {
390 run_command_via_ssh echo ssh connection is working
391 }
392
393 # takes a local (on runner side) script file and runs it via ssh on the virtual android device. forwards return code.
394 # adds a timestamp to every line to be able to see where delays are
395 run_script_file_via_ssh() {
396 ssh -p 9022 "$(termux_user)@127.0.0.1" -o StrictHostKeyChecking=accept-new "bash -s" < "$1" 2>&1 | add_timestamp_to_lines
397 return "${PIPESTATUS[0]}"
398 }
399
400 # Experiments showed that the adb shell input text functionality has a limitation for the input length.
401 # If input length is too big, the input is not fully provided to the android device.
402 # To avoid this, we divide large inputs into smaller chunks and put them one-by-one.
403 adb_input_text_long() {
404 string=$1
405 length=${#string}
406 step=20
407 p=0
408 for ((i = 0; i < length-step; i = i + step)); do
409 chunk="${string:i:$step}"
410 adb shell input text "'$chunk'"
411 p=$((i+step))
412 done
413
414 remaining="${string:p}"
415 adb shell input text "'$remaining'"
416 }
417
418 generate_rsa_key_local() {
419 yes "" | ssh-keygen -t rsa -b 4096 -C "Github Action" -N ""
420 }
421
422 install_rsa_pub() {
423
424 run_command_via_ssh "echo hello" && return # if this works, we are already fine. Skipping
425
426 # remove old host identity:
427 ssh-keygen -f ~/.ssh/known_hosts -R "[127.0.0.1]:9022"
428
429 rsa_pub_key=$(cat ~/.ssh/id_rsa.pub)
430 echo "====================================="
431 echo "$rsa_pub_key"
432 echo "====================================="
433
434 adb shell input text \"echo \"
435
436 adb_input_text_long "$rsa_pub_key"
437
438 adb shell input text "\" >> ~/.ssh/authorized_keys\"" && hit_enter
439 sleep 1
440 }
441
442 install_packages_via_adb_shell() {
443 install_package_list="$*"
444
445 install_packages_via_adb_shell_using_apt "$install_package_list"
446 if [[ $? -ne 0 ]]; then
447 echo "apt failed. Now try install with pkg as fallback."
448 probe="$dev_probe_dir/pkg.probe"
449 command="'mkdir -vp ~/.cargo/bin; yes | pkg install $install_package_list -y; echo \$? > $probe'"
450 run_termux_command "$command" "$probe" || return 1
451 fi
452
453 return 0
454 }
455
456 # We use apt to install the packages as pkg doesn't respect any pre-defined mirror list.
457 # Its important to have a defined mirror list to avoid issues with broken mirrors.
458 install_packages_via_adb_shell_using_apt() {
459 install_package_list="$*"
460
461 repo_url=$(get_current_repo_url)
462 move_to_next_repo_url
463 echo "set apt repository url: $repo_url"
464 probe="$dev_probe_dir/sourceslist.probe"
465 command="'echo $repo_url | dd of=\$PREFIX/etc/apt/sources.list; echo \$? > $probe'"
466 run_termux_command "$command" "$probe"
467
468 probe="$dev_probe_dir/adb_install.probe"
469 command="'mkdir -vp ~/.cargo/bin; apt update; yes | apt install $install_package_list -y; echo \$? > $probe'"
470 run_termux_command "$command" "$probe"
471 }
472
473 install_packages_via_ssh_using_apt() {
474 install_package_list="$*"
475
476 repo_url=$(get_current_repo_url)
477 move_to_next_repo_url
478 echo "set apt repository url: $repo_url"
479 run_command_via_ssh "echo $repo_url | dd of=\$PREFIX/etc/apt/sources.list"
480
481 run_command_via_ssh "apt update; yes | apt install $install_package_list -y"
482 }
483
484 apt_upgrade_all_packages() {
485 repo_url=$(get_current_repo_url)
486 move_to_next_repo_url
487 echo "set apt repository url: $repo_url"
488 run_command_via_ssh "echo $repo_url | dd of=\$PREFIX/etc/apt/sources.list"
489
490 run_command_via_ssh "apt update; yes | apt upgrade -y"
491 }
492
493 generate_and_install_public_key() {
494 echo "generate local public private key pair"
495 generate_rsa_key_local
496 echo "install public key via 'adb shell input'"
497 install_rsa_pub
498 echo "installed ssh public key on device"
499 }
500
501 run_with_retry() {
502 tries=$1
503 shift 1
504
505 for i in $(seq 1 $tries); do
506 echo "Try #$i of $tries: run $*"
507 "$@" && echo "Done in try#$i" && return 0
508 done
509
510 exit_code=$?
511
512 echo "Still failing after $tries. Code: $exit_code"
513
514 return $exit_code
515 }
516
517 snapshot() {
518 apk="$1"
519 echo "Running snapshot"
520 adb install -g "$apk"
521
522 echo "Prepare and install system packages"
523
524 reinit_ssh_connection || return 1
525
526 apt_upgrade_all_packages
527
528 install_packages_via_ssh_using_apt "rust binutils openssl tar mount-utils"
529
530 echo "Read /proc/cpuinfo"
531 run_command_via_ssh "cat /proc/cpuinfo"
532
533 echo "Installing cargo-nextest"
534 # We need to install nextest via cargo currently, since there is no pre-built binary for android x86
535 # explicitly set CARGO_TARGET_DIR as otherwise a random generated tmp directory is used,
536 # which prevents incremental build for the retries.
537 command="export CARGO_TERM_COLOR=always && export CARGO_TARGET_DIR=\"cargo_install_target_dir\" && cargo install cargo-nextest --locked"
538
539 run_with_retry 3 run_command_via_ssh "$command"
540 return_code=$?
541
542 echo "Info about cargo and rust - via SSH Script"
543 run_script_file_via_ssh "$this_repo/util/android-scripts/collect-info.sh"
544
545 echo "Snapshot complete"
546 # shellcheck disable=SC2086
547 return $return_code
548 }
549
550 sync_host() {
551 repo="$1"
552 cache_home="${HOME}/${cache_dir_name}"
553 cache_dest="$dev_home_dir/${cache_dir_name}"
554
555 reinit_ssh_connection
556
557 echo "Running sync host -> image: ${repo}"
558
559 # run_command_via_ssh "mkdir $dev_home_dir/coreutils"
560
561 copy_file_or_dir_to_device_via_ssh "$repo" "$dev_home_dir"
562 [[ -e "$cache_home" ]] && copy_file_or_dir_to_device_via_ssh "$cache_home" "$cache_dest"
563
564 echo "Finished sync host -> image: ${repo}"
565 }
566
567 sync_image() {
568 repo="$1"
569 cache_home="${HOME}/${cache_dir_name}"
570 cache_dest="$dev_probe_dir/${cache_dir_name}"
571
572 reinit_ssh_connection
573
574 echo "Running sync image -> host: ${repo}"
575
576 command="rm -rf $dev_probe_dir/coreutils ${cache_dest}; \
577 mkdir -p ${cache_dest}; \
578 cd ${cache_dest}; \
579 tar czf cargo.tgz -C ~/ .cargo; \
580 tar czf target.tgz -C ~/coreutils target; \
581 ls -la ${cache_dest}"
582 run_command_via_ssh "$command" || return
583
584 rm -rf "$cache_home"
585 copy_file_or_dir_from_device_via_ssh "$cache_dest" "$cache_home" || return
586
587 echo "Finished sync image -> host: ${repo}"
588 }
589
590 build() {
591 echo "Running build"
592
593 reinit_ssh_connection
594
595 run_script_file_via_ssh "$this_repo/util/android-scripts/collect-info.sh"
596
597 command="export CARGO_TERM_COLOR=always;
598 export CARGO_INCREMENTAL=0; \
599 cd ~/coreutils && cargo build --features feat_os_unix_android"
600 run_with_retry 3 run_command_via_ssh "$command" || return
601
602 echo "Finished build"
603 }
604
605 tests() {
606 echo "Running tests"
607
608 reinit_ssh_connection
609
610 run_script_file_via_ssh "$this_repo/util/android-scripts/collect-info.sh"
611
612 run_script_file_via_ssh "$this_repo/util/android-scripts/run-tests.sh" || return
613
614 echo "Finished tests"
615 }
616
617 hash_rustc() {
618 tmp_hash="__rustc_hash__.tmp"
619 hash="__rustc_hash__"
620
621 reinit_ssh_connection
622
623 echo "Hashing rustc version: ${HOME}/${hash}"
624
625 run_command_via_ssh "rustc -Vv" > rustc.log || return
626 rm -f "$tmp_hash"
627 mv "rustc.log" "$tmp_hash" || return
628 # sha256sum is not available. shasum is the macos native program.
629 shasum -a 256 "$tmp_hash" | cut -f 1 -d ' ' | tr -d '[:space:]' >"${HOME}/${hash}" || return
630
631 rm -f "$tmp_hash"
632
633 echo "Finished hashing rustc version: ${HOME}/${hash}"
634 }
635
636 #adb logcat &
637 exit_code=0
638
639 if [ $# -eq 1 ]; then
640 case "$1" in
641 sync_host)
642 sync_host "$this_repo"
643 exit_code=$?
644 ;;
645 sync_image)
646 sync_image "$this_repo"
647 exit_code=$?
648 ;;
649 build)
650 build
651 exit_code=$?
652 ;;
653 tests)
654 tests
655 exit_code=$?
656 ;;
657 *) help ;;
658 esac
659 elif [ $# -eq 2 ]; then
660 case "$1" in
661 snapshot)
662 snapshot "$2"
663 exit_code=$?
664 ;;
665 sync_host)
666 sync_host "$2"
667 exit_code=$?
668 ;;
669 sync_image)
670 sync_image "$2"
671 exit_code=$?
672 ;;
673 *)
674 help
675 exit 1
676 ;;
677 esac
678 elif [ $# -eq 4 ]; then
679 case "$1" in
680 init)
681 shift
682 init "$@"
683 exit_code=$?
684 ;;
685 *)
686 help
687 exit 1
688 ;;
689 esac
690 else
691 help
692 exit_code=1
693 fi
694
695 #pkill adb
696 exit $exit_code