| 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # Container entrypoint that waits for all spawned processes. |
| 5 | # |
| 6 | |
| 7 | set -e -u |
| 8 | |
| 9 | # Create a FIFO and start reading from its read end. |
| 10 | tempdir=$(mktemp -d "/tmp/done.XXXXXXXXXX") |
| 11 | trap 'rm -r "$tempdir"' EXIT |
| 12 | done="$tempdir/pipe" |
| 13 | mkfifo "$done" |
| 14 | cat "$done" & waiter=$! |
| 15 | |
| 16 | # Start the workload. Its descendants will inherit the FIFO's write end. |
| 17 | status=0 |
| 18 | if [ "$#" -eq 0 ]; then |
| 19 | bash 9>"$done" || status=$? |
| 20 | else |
| 21 | "$@" 9>"$done" || status=$? |
| 22 | fi |
| 23 | |
| 24 | # When the workload and all of its descendants exit, the FIFO's write end will |
| 25 | # be closed and `cat "$done"` will exit. Wait until it happens. This is needed |
| 26 | # in order to handle SelfUpdater, which the workload may start in background |
| 27 | # before exiting. |
| 28 | wait "$waiter" |
| 29 | |
| 30 | exit "$status" |