[PATCH] qemu: Do not keep swtmp pidfile around after stopping

Just like the socket, remove the pidfile when TPM emulator is being stopped. In order to make this a bit cleaner, try to remove it even if swtpm_ioctl does not exist. Signed-off-by: Martin Kletzander <mkletzan@redhat.com> --- src/qemu/qemu_tpm.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 584c787b700b..50e8c19f3a0b 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -795,28 +795,25 @@ qemuTPMEmulatorStop(const char *swtpmStateDir, g_autofree char *pathname = NULL; g_autofree char *errbuf = NULL; g_autofree char *swtpm_ioctl = virTPMGetSwtpmIoctl(); + g_autofree char *pidfile = qemuTPMEmulatorPidFileBuildPath(swtpmStateDir, + shortName); + if (swtpm_ioctl && + (pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName)) && + virFileExists(pathname) && + (cmd = virCommandNew(swtpm_ioctl))) { - if (!swtpm_ioctl) - return; - - if (!(pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName))) - return; - - if (!virFileExists(pathname)) - return; - - cmd = virCommandNew(swtpm_ioctl); - if (!cmd) - return; + virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL); - virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL); + virCommandSetErrorBuffer(cmd, &errbuf); - virCommandSetErrorBuffer(cmd, &errbuf); + ignore_value(virCommandRun(cmd, NULL)); - ignore_value(virCommandRun(cmd, NULL)); + /* clean up the socket */ + unlink(pathname); + } - /* clean up the socket */ - unlink(pathname); + if (pidfile) + virPidFileDeletePath(pidfile); } -- 2.37.2

On Thu, Aug 18, 2022 at 04:20:56PM +0200, Martin Kletzander wrote:
Just like the socket, remove the pidfile when TPM emulator is being stopped. In order to make this a bit cleaner, try to remove it even if swtpm_ioctl does not exist.
Forgot to mention the BZ: https://bugzilla.redhat.com/show_bug.cgi?id=2111301
Signed-off-by: Martin Kletzander <mkletzan@redhat.com> --- src/qemu/qemu_tpm.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 584c787b700b..50e8c19f3a0b 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -795,28 +795,25 @@ qemuTPMEmulatorStop(const char *swtpmStateDir, g_autofree char *pathname = NULL; g_autofree char *errbuf = NULL; g_autofree char *swtpm_ioctl = virTPMGetSwtpmIoctl(); + g_autofree char *pidfile = qemuTPMEmulatorPidFileBuildPath(swtpmStateDir, + shortName); + if (swtpm_ioctl && + (pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName)) && + virFileExists(pathname) && + (cmd = virCommandNew(swtpm_ioctl))) {
- if (!swtpm_ioctl) - return; - - if (!(pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName))) - return; - - if (!virFileExists(pathname)) - return; - - cmd = virCommandNew(swtpm_ioctl); - if (!cmd) - return; + virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL);
- virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL); + virCommandSetErrorBuffer(cmd, &errbuf);
- virCommandSetErrorBuffer(cmd, &errbuf); + ignore_value(virCommandRun(cmd, NULL));
- ignore_value(virCommandRun(cmd, NULL)); + /* clean up the socket */ + unlink(pathname); + }
- /* clean up the socket */ - unlink(pathname); + if (pidfile) + virPidFileDeletePath(pidfile); }
-- 2.37.2

On 8/18/22 16:20, Martin Kletzander wrote:
Just like the socket, remove the pidfile when TPM emulator is being stopped. In order to make this a bit cleaner, try to remove it even if swtpm_ioctl does not exist.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com> --- src/qemu/qemu_tpm.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 584c787b700b..50e8c19f3a0b 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -795,28 +795,25 @@ qemuTPMEmulatorStop(const char *swtpmStateDir, g_autofree char *pathname = NULL; g_autofree char *errbuf = NULL; g_autofree char *swtpm_ioctl = virTPMGetSwtpmIoctl(); + g_autofree char *pidfile = qemuTPMEmulatorPidFileBuildPath(swtpmStateDir, + shortName); + if (swtpm_ioctl && + (pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName)) && + virFileExists(pathname) && + (cmd = virCommandNew(swtpm_ioctl))) {
virCommandNew() succeeds, always.
- if (!swtpm_ioctl) - return; - - if (!(pathname = qemuTPMEmulatorSocketBuildPath(swtpmStateDir, shortName))) - return; - - if (!virFileExists(pathname)) - return; - - cmd = virCommandNew(swtpm_ioctl); - if (!cmd) - return; + virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL);
- virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL); + virCommandSetErrorBuffer(cmd, &errbuf);
- virCommandSetErrorBuffer(cmd, &errbuf); + ignore_value(virCommandRun(cmd, NULL));
- ignore_value(virCommandRun(cmd, NULL)); + /* clean up the socket */ + unlink(pathname); + }
- /* clean up the socket */ - unlink(pathname); + if (pidfile) + virPidFileDeletePath(pidfile);
Here, we can be more aggressive: virPidFileForceCleanupPath(). Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal

On 8/23/22 16:19, Michal Prívozník wrote:
On 8/18/22 16:20, Martin Kletzander wrote:
Just like the socket, remove the pidfile when TPM emulator is being stopped. In order to make this a bit cleaner, try to remove it even if swtpm_ioctl does not exist.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com> --- src/qemu/qemu_tpm.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
What I forgot to mention is that qemuTPMEmulatorStop() which is called from qemuTPMEmulatorStart() can be removed, because if you accept my suggestion and go with virPidFileForceCleanupPath() there's no chance for the swtpm process to run at either the end of qemuTPMEmulatorStop() or at the time of qemuTPMEmulatorStart(). But that can be done in a follow up patch. Michal

On 8/24/22 13:19, Michal Prívozník wrote:
On 8/23/22 16:19, Michal Prívozník wrote:
On 8/18/22 16:20, Martin Kletzander wrote:
Just like the socket, remove the pidfile when TPM emulator is being stopped. In order to make this a bit cleaner, try to remove it even if swtpm_ioctl does not exist.
Signed-off-by: Martin Kletzander <mkletzan@redhat.com> --- src/qemu/qemu_tpm.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
What I forgot to mention is that qemuTPMEmulatorStop() which is called from qemuTPMEmulatorStart() can be removed, because if you accept my suggestion and go with virPidFileForceCleanupPath() there's no chance for the swtpm process to run at either the end of qemuTPMEmulatorStop() or at the time of qemuTPMEmulatorStart(). But that can be done in a follow up patch.
In fact, it is completely independent. Because the pid file is in form of: ${swtpmStateDir}/${shortName}-swtpm.pid where ${shortName} is the result of virDomainDefGetShortName() thus it contains domain ID and therefore, it's never ever the same across two 'virsh start's of a domain. I'm sorry I haven't realized this during review earlier. Michal
participants (2)
-
Martin Kletzander
-
Michal Prívozník