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