[libvirt] [PATCH v3 11/21] tpm: Check whether previously found executables were updated

Stefan Berger stefanb at linux.vnet.ibm.com
Wed Jul 10 21:08:49 UTC 2019


Check whether previously found executables were updated and if
so look for them again. This helps to use updated features of
swtpm and its tools upon updating them.

Signed-off-by: Stefan Berger <stefanb at linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau at redhat.com>
---
 src/qemu/qemu_tpm.c |  1 +
 src/tpm/virtpm.c    | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 61b4f72320..2afa8db448 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -20,6 +20,7 @@
 
 #include <config.h>
 
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
diff --git a/src/tpm/virtpm.c b/src/tpm/virtpm.c
index 0179b1e8be..e4735f9c4d 100644
--- a/src/tpm/virtpm.c
+++ b/src/tpm/virtpm.c
@@ -77,8 +77,13 @@ virTPMCreateCancelPath(const char *devpath)
  * executables for the swtpm; to be found on the host
  */
 static char *swtpm_path;
+static struct stat swtpm_stat;
+
 static char *swtpm_setup;
+static struct stat swtpm_setup_stat;
+
 static char *swtpm_ioctl;
+static struct stat swtpm_ioctl_stat;
 
 const char *
 virTPMGetSwtpm(void)
@@ -116,18 +121,22 @@ virTPMEmulatorInit(void)
     static const struct {
         const char *name;
         char **path;
+        struct stat *stat;
     } prgs[] = {
         {
             .name = "swtpm",
             .path = &swtpm_path,
+            .stat = &swtpm_stat,
         },
         {
             .name = "swtpm_setup",
             .path = &swtpm_setup,
+            .stat = &swtpm_setup_stat,
         },
         {
             .name = "swtpm_ioctl",
             .path = &swtpm_ioctl,
+            .stat = &swtpm_ioctl_stat,
         }
     };
     size_t i;
@@ -135,6 +144,23 @@ virTPMEmulatorInit(void)
     for (i = 0; i < ARRAY_CARDINALITY(prgs); i++) {
         char *path;
         bool findit = *prgs[i].path == NULL;
+        struct stat statbuf;
+        char *tmp;
+
+        if (!findit) {
+            /* has executables changed? */
+            if (stat(*prgs[i].path, &statbuf) < 0) {
+                virReportSystemError(errno,
+                                     _("Could not stat %s"), path);
+                findit = true;
+            }
+            if (!findit &&
+                memcmp(&statbuf.st_mtim,
+                       &prgs[i].stat->st_mtime,
+                       sizeof(statbuf.st_mtim))) {
+                findit = true;
+            }
+        }
 
         if (findit) {
             path = virFindFileInPath(prgs[i].name);
@@ -151,7 +177,15 @@ virTPMEmulatorInit(void)
                 VIR_FREE(path);
                 return -1;
             }
+            if (stat(path, prgs[i].stat) < 0) {
+                virReportSystemError(errno,
+                                     _("Could not stat %s"), path);
+                VIR_FREE(path);
+                return -1;
+            }
+            tmp = *prgs[i].path;
             *prgs[i].path = path;
+            VIR_FREE(tmp);
         }
     }
 
-- 
2.20.1




More information about the libvir-list mailing list