Juan Pablo Conde | 64f59be | 2024-01-12 16:56:58 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2024, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
Harrison Mutai | fa12396 | 2025-08-26 15:45:20 +0000 | [diff] [blame^] | 8 | source "$ci_root/utils.sh" |
| 9 | |
Juan Pablo Conde | 64f59be | 2024-01-12 16:56:58 -0600 | [diff] [blame] | 10 | # Launch a program. Have its PID saved in a file with given name with .pid |
| 11 | # suffix. When the program exits, create a file with .success suffix, or one |
| 12 | # with .fail if it fails. This function blocks, so the caller must '&' this if |
| 13 | # they want to continue. Call must wait for $pid_dir/$name.pid to be created |
| 14 | # should it want to read it. |
| 15 | launch() { |
Juan Pablo Conde | 6389ef5 | 2024-01-30 15:54:02 -0600 | [diff] [blame] | 16 | local pid |
Juan Pablo Conde | 64f59be | 2024-01-12 16:56:58 -0600 | [diff] [blame] | 17 | |
Juan Pablo Conde | 6389ef5 | 2024-01-30 15:54:02 -0600 | [diff] [blame] | 18 | "$@" & |
| 19 | pid="$!" |
| 20 | echo "$pid" > "$pid_dir/${name:?}.pid" |
| 21 | |
| 22 | # If the execution is halted, handle the process termination properly, |
| 23 | # so the caller does not keep looping waiting for the result file to be |
| 24 | # generated. |
| 25 | trap "{ touch \"$pid_dir/$name.fail\"; exit 1 }" SIGINT SIGHUP SIGTERM |
| 26 | |
| 27 | if wait "$pid"; then |
| 28 | touch "$pid_dir/$name.success" |
| 29 | else |
| 30 | touch "$pid_dir/$name.fail" |
| 31 | fi |
Juan Pablo Conde | 64f59be | 2024-01-12 16:56:58 -0600 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | # Provide signal as an argument to the trap function. |
| 35 | trap_with_sig() { |
| 36 | local func |
| 37 | |
| 38 | func="$1" ; shift |
| 39 | for sig ; do |
| 40 | trap "$func $sig" "$sig" |
| 41 | done |
| 42 | } |