]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm: Use sys/mount.h and support block devs only if BLKGETSIZE64 exists
authorStefan Berger <stefanb@linux.ibm.com>
Tue, 5 Oct 2021 15:17:01 +0000 (11:17 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Tue, 5 Oct 2021 17:50:45 +0000 (13:50 -0400)
The header file fs/linux.h only exists on Linux but we can also
use sys/mount.h, which also exists on Cygwin and the BSDs.

Only support  block devices if BLKGETSIZE64 is defined.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
src/swtpm/swtpm_nvstore_linear_file.c
tests/test_tpm2_save_load_state_2_block

index a2e29aa6403a3fd81833d2c9594ff7583ca9c9d7..729907c5fc625fc85357cbf5fdaff46a64c32fe7 100644 (file)
@@ -7,7 +7,7 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
+#include <sys/mount.h>
 #include <fcntl.h>
 
 #include <libtpms/tpm_types.h>
@@ -43,7 +43,6 @@ SWTPM_NVRAM_LinearFile_Mmap(void)
 {
     TPM_RESULT rc = 0;
     struct stat st;
-    uint64_t bd_size;
 
     TPM_DEBUG("SWTPM_NVRAM_LinearFile_Mmap: renewing mmap\n");
 
@@ -82,6 +81,9 @@ SWTPM_NVRAM_LinearFile_Mmap(void)
         mmap_state.size = st.st_size;
         mmap_state.can_truncate = true;
     } else if (S_ISBLK(st.st_mode)) {
+#if defined(BLKGETSIZE64)
+        uint64_t bd_size;
+
         /* valid block device, can't resize, but can use as is */
         if (ioctl(mmap_state.fd, BLKGETSIZE64, &bd_size)) {
             logprintf(STDERR_FILENO,
@@ -101,6 +103,12 @@ SWTPM_NVRAM_LinearFile_Mmap(void)
             rc = TPM_FAIL;
             goto fail;
         }
+#else
+        logprintf(STDERR_FILENO, "SWTPM_NVRAM_LinearFile_Mmap: block devices are"
+                                 " not supported\n");
+        rc = TPM_FAIL;
+        goto fail;
+#endif
     } else {
         logprintf(STDERR_FILENO, "SWTPM_NVRAM_LinearFile_Mmap: invalid stat\n");
         rc = TPM_FAIL;
index 0007a7c1eb0c57c338e943dcf0e2dc80a1ab04ba..07287e9a2e207419df89d26ed7cc2891a4640a82 100755 (executable)
@@ -1,5 +1,10 @@
 #!/bin/bash
 
+if ! [[ "$(uname -s)" =~ Linux ]]; then
+       echo "This test currently only runs on Linux."
+       exit 77
+fi
+
 if [ "$(id -u)" -ne 0 ]; then
        echo "Need to be root to run this test."
        exit 77