Skip to content

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

Merged
chewi merged 1 commit into
flatcar:mainfrom
shipitdev:fix/qemu-template-tpm-proc-cleanup
Sep 1, 2026
Merged

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

Conversation

@shipitdev

@shipitdev shipitdev commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

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.

@shipitdev
shipitdev requested a review from a team as a code owner August 24, 2026 00:27
@shipitdev 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
Signed-off-by: shipitdev <harshsinghrajpoot07@gmail.com>
@shipitdev
shipitdev force-pushed the fix/qemu-template-tpm-proc-cleanup branch from 888caf8 to df548fe Compare August 26, 2026 15:26

@chewi chewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks fine, although when I try it on a Mac myself, QEMU dies immediately with this:

qemu-system-aarch64: -device tpm-tis-device,tpmdev=tpm0: Error: ret = HV_BAD_ARGUMENT (0xfae94003, at ../accel/hvf/hvf-all.c:123)

I guess you didn't hit that.

@shipitdev

shipitdev commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

The fix looks fine, although when I try it on a Mac myself, QEMU dies immediately with this:

qemu-system-aarch64: -device tpm-tis-device,tpmdev=tpm0: Error: ret = HV_BAD_ARGUMENT (0xfae94003, at ../accel/hvf/hvf-all.c:123)

I guess you didn't hit that.

Ah yeah, I ran into that! With all the different Apple Silicon HVF quirks popping up, I didn't want to flood you with a whole laundry list of issues on day one lol, so I kept the scope of this PR strictly focused on fixing the swtpm daemon watcher and /proc portability.

i'm not sure where to proceed from here though Would you like me to add a quick note to the PR description mentioning the limitation or something ?

Although it looks like another upstream QEMU issue with tpm-tis-device under HVF. i'm gonna have a look at it later today

@chewi chewi self-assigned this Sep 1, 2026
@chewi
chewi merged commit df78685 into flatcar:main Sep 1, 2026
2 checks passed
@chewi

chewi commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

It's fine, we can just take this now, especially if it's just an upstream issue. Most users don't need a TPM anyway.

@shipitdev
shipitdev deleted the fix/qemu-template-tpm-proc-cleanup branch September 1, 2026 14:55
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.

2 participants