If `--tty` is not passed to `docker exec` because stdout is not available (`[ ! -t 1 ]`), like due to redirection to file (`&> build.log`) or if stdin is not available (`< /dev/null`), then docker does not forward kill signals to the process started and they remain running.
To fix the issue, the `DOCKER_EXEC_PID_FILE_PATH` env variable with the value `/tmp/docker-exec-pid-<timestamp>` is passed to the process called with `docke exec` and the process started stores its pid in the file path passed. Traps are set in `run-docker.sh` that runs the `docker exec` command to receive any kills signals, and if it does, it runs another `docker exec` command to read the pid of the process previously started from `DOCKER_EXEC_PID_FILE_PATH` and then kills it and all its children.
See Also:
https://github.com/docker/cli/issues/2607https://github.com/moby/moby/issues/9098https://github.com/moby/moby/pull/41548https://stackoverflow.com/questions/41097652/how-to-fix-ctrlc-inside-a-docker-container
Also passing `--init` to `docker run` to "Run an init inside the container that forwards signals and reaps processes", although it does not work for above cases, but may helpful in others. The `--init` flag changes will only engage on new container creation.
https://docs.docker.com/engine/reference/run/#specify-an-init-processhttps://docs.docker.com/engine/reference/commandline/run/
```
./scripts/run-docker.sh ./build-package.sh -f libjpeg-turbo &> build.log
^C
$ ./scripts/run-docker.sh ps -efww
Running container 'termux-package-builder' from image 'termux/package-builder'...
UID PID PPID C STIME TTY TIME CMD
builder 1 0 0 05:48 pts/0 00:00:00 bash
builder 9243 0 0 06:01 pts/1 00:00:00 bash
builder 28127 0 0 06:12 ? 00:00:00 /bin/bash ./build-package.sh -f libjpeg-turbo
builder 28141 28127 0 06:12 ? 00:00:00 /bin/bash ./build-package.sh -f libjpeg-turbo
builder 28449 28141 1 06:12 ? 00:00:00 ninja -w dupbuild=warn -j 8
builder 28656 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28657 28656 79 06:12 ? 00:00:01 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28694 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28695 28694 89 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28728 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28729 28728 0 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28731 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28734 28731 0 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28740 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28741 28740 0 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28744 0 0 06:12 pts/2 00:00:00 ps -efww
builder 28748 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28752 28748 0 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28753 28449 0 06:12 ? 00:00:00 /bin/sh -c /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28754 28753 0 06:12 ? 00:00:00 /home/builder/.termux-build/_cache/android-r23c-api-24-v0/bin/clang
builder 28755 28449 0 06:12 ? 00:00:00 ninja -w dupbuild=warn -j 8
$ ./scripts/run-docker.sh ./build-package.sh -f libjpeg-turbo &> build.log
$ ./scripts/run-docker.sh ./build-package.sh -f libjpeg-turbo
Running container 'termux-package-builder' from image 'termux/package-builder'...
ERROR: Another build is already running within same environment.
```
- quote bash vars
- handle unknown flags in `getopts`
- add `-r` flag in `read` to avoid mangling `\`
- remove `$` in arithmetic which gives it PEMDAS characteristic
- single quote trap to expand when signaled vs. when defined
Partial compatibility for on-device builds.
There is no guarantee that it will be possible to build all available
packages and built packages will have same reliability that cross-compiled
but should solve "self-hosting" problems as much as possible.
Use scripts/buildorder.py with a new -i flag to get all dependencies
(including subpackages). The script now also spits out both package
name and package dir, to make it easier to build packages from another
repo.
- Output deb files to debs/ folder by default.
- Use $HOME/.termux-build for build folder by default (changed
from previous $HOME/termux).
- Make scripts/run-docker.sh re-use a single container instead of
creating new ones for each run.
- Avoid mounting the whole Termux build folder in docker. This
fixes#294, mmap not supported host folders under OS X.
- Add a scripts/update-docker.sh utility for checking for an
updated docker image..
- Make build-package.sh support the [-a ARCH] flag to specify arch.
- Make aarch64 the default arch (from previous arm).
- Try to improve the README a bit.
Enhanced build-all.sh to:
- Print the messages logged to ALL.out, to keep track of progress
- Print an error message when it fails, with a hint to what failed
- Keep track of packaged that build successfully, and if build-all failed:
- Skip packages that were build successfully in the previous run.