FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

build_library/qemu_template.sh: use POSIX kill -0 for swtpm cleanup watcher for macOS+linux portability by shipitdev · Pull Request #4232 · flatcar/scripts · GitHub

build_library/qemu_template.sh: use POSIX kill -0 for swtpm cleanup watcher for macOS+linux portability - #4232

Open
shipitdev wants to merge 1 commit into
flatcar:mainfrom
shipitdev:fix/qemu-template-tpm-proc-cleanup
Open

build_library/qemu_template.sh: use POSIX kill -0 for swtpm cleanup watcher for macOS+linux portability#4232
shipitdev wants to merge 1 commit into
flatcar:mainfrom
shipitdev:fix/qemu-template-tpm-proc-cleanup

Conversation

Copy link
Copy Markdown

Description

(reopening (#4231) since i deleted the head of the last fork)
Use POSIX kill -0 for swtpm cleanup watcher to ensure cross-platform compatibility
In build_library/qemu_template.sh, the background monitor responsible for cleaning up the swtpm daemon process checks for parent termination by probing the /proc filesystem:

(while [ -e "/proc/${PARENT}" ]; do sleep 1; done; kill "${SWTPM_PROC}" 2>/dev/null; exit 0) &

On macOS (Darwin), the /proc filesystem does not exist. Because of this, [ -e "/proc/${PARENT}" ] immediately returns false in 0 seconds, which causes the watcher to kill swtpm before QEMU can connect to the socket. Running with software TPM (-T) on macOS immediately crashes with Failed to connect to TPM socket: Connection refused.

Solution

Replace /proc/${PARENT} with the POSIX standard kill -0 "${PARENT}".

kill -0 checks if the parent process ID is alive without sending a terminating signal:

While the launcher script is running ➡️ kill -0 returns 0 (true) and keeps swtpm alive.
When the launcher script exits ➡️ kill -0 returns non-zero (false) and cleanly kills swtpm to avoid orphaned processes.
This makes the cleanup logic fully portable across Linux, macOS, and BSD.

How to use

Run the QEMU launcher with a TPM directory (-T):

./flatcar_production_qemu_uefi.sh -T /tmp/swtpm-state ...

Testing done

  1. macOS: Verified that kill -0 "${PARENT}" keeps the daemon process alive while the parent script runs, and cleanly terminates it within 1 second after the parent exits.
# 1. Start mock background daemon
$ sleep 30 & DAEMON_PID=$!

# 2. Run parent script with the POSIX kill -0 watcher
$ (
    PARENT=$$
    (while kill -0 "${PARENT}" 2>/dev/null; do sleep 1; done; kill "${DAEMON_PID}" 2>/dev/null) &
    echo "Parent running (PID $PARENT)... Daemon alive? $(kill -0 $DAEMON_PID 2>/dev/null && echo YES || echo NO)"
    sleep 2
    echo "Parent exiting..."
  )

Parent running (PID 8524)... Daemon alive? YES
Parent exiting...

# 3. Verify daemon was cleanly terminated by watcher after parent exit
$ sleep 1
$ kill -0 $DAEMON_PID 2>/dev/null && echo "Daemon alive" || echo "Daemon killed successfully"
Daemon killed successfully
  1. Linux / POSIX: Verified that kill -0 is POSIX compliant (kill(2)) and functions identically across Linux distros.
  • Changelog entries added in the respective changelog/ directory (user-facing change, bug fix, security fix, update)
  • Inspected CI output for image differences: /boot and /usr size, packages, list files for any missing binaries, kernel modules, config files, kernel modules, etc.

Signed-off-by: shipitdev <harshsinghrajpoot07@gmail.com>
shipitdev requested a review from a team as a code owner August 24, 2026 00:27
shipitdev changed the title build_library/qemu_template: use POSIX kill -0 for swtpm cleanup watcher build_library/qemu_template.sh: use POSIX kill -0 for swtpm cleanup watcher for macOS+linux portability Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL