Skip to contentSkip to conversation

tests/boot-sector: Use mkstemp() to create a unique file name

PATCH In review Thomas Huth 4 participants 7 messages Oct 11, 2016 – Oct 12, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/boot-sector: Use mkstemp() to create a unique file name

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +7 -2
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }
     if (!f) {
         fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
         return 1;
tests/boot-sector.h +2 -2
index f64b477..35d61c7 100644
@@ -14,8 +14,8 @@
 #ifndef TEST_BOOT_SECTOR_H
 #define TEST_BOOT_SECTOR_H
 
-/* Create boot disk file.  */
-int boot_sector_init(const char *fname);
+/* Create boot disk file. fname must be a suitable string for mkstemp() */
+int boot_sector_init(char *fname);
 
 /* Loop until signature in memory is OK.  */
 void boot_sector_test(void);
tests/pxe-test.c +1 -1
index 5d3ddbe..34282d3 100644
@@ -19,7 +19,7 @@
 
 #define NETNAME "net0"
 
-static const char *disk = "tests/pxe-test-disk.raw";
+static char disk[] = "tests/pxe-test-disk-XXXXXX";
 
 static void test_pxe_one(const char *params, bool ipv6)
 {
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 39 lines · Thomas Huth

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +0 -1
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)

tests/boot-sector.c static uint8_t boot_sector[0x7e000] = { · line 80

Quoted 10 lines
tests/boot-sector.c +7 -1
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }

Given that we're only writing a pile of bytes, we could just use write() and close() in this function rather than doing an fdopen() to use fwrite() and fclose(). But I can see the attraction of not doing that in this patch for review purposes.

Quoted 23 lines
tests/boot-sector.c +0 -0
     if (!f) {
         fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
         return 1;
tests/boot-sector.h +2 -2
index f64b477..35d61c7 100644
@@ -14,8 +14,8 @@
 #ifndef TEST_BOOT_SECTOR_H
 #define TEST_BOOT_SECTOR_H
 
-/* Create boot disk file.  */
-int boot_sector_init(const char *fname);
+/* Create boot disk file. fname must be a suitable string for mkstemp() */
+int boot_sector_init(char *fname);
 
 /* Loop until signature in memory is OK.  */
 void boot_sector_test(void);
tests/pxe-test.c +0 -0
index 5d3ddbe..34282d3 100644

tests/pxe-test.c line 25

Quoted 8 lines
tests/pxe-test.c +1 -1
@@ -19,7 +19,7 @@
 
 #define NETNAME "net0"
 
-static const char *disk = "tests/pxe-test-disk.raw";
+static char disk[] = "tests/pxe-test-disk-XXXXXX";
 
 static void test_pxe_one(const char *params, bool ipv6)
 {

thanks
-- PMM

#2 lore raw
Quoted 46 lines · Peter Maydell
Quoted 45 lines · Thomas Huth

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +3 -2
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
Quoted 8 lines

tests/boot-sector.c static uint8_t boot_sector[0x7e000] = { · line 80

Quoted 4 lines
tests/boot-sector.c +4 -0
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }

Given that we're only writing a pile of bytes, we could just use write() and close() in this function rather than doing an fdopen() to use fwrite() and fclose().

You're right, that sounds nicer, so please ignore this version, I'll send an update.

I think we might also need to increase the default timeout in boot_sector_test() a little bit, since the pxe test is really rather slow on ppc64 ... by increasing the timeout, we can make sure that it also still passes on machines with high CPU load, so I'll add a patch to do that, too.

Thomas

#3 lore raw
In reply to Thomas Huth
Quoted 39 lines · On Tue, 11 Oct 2016 15:32:02 +0200 Thomas Huth <[email protected]> wrote:

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +0 -1
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)

tests/boot-sector.c static uint8_t boot_sector[0x7e000] = { · line 80

Quoted 10 lines
tests/boot-sector.c +7 -1
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }

Unless we really want to control how the temporary file is named (maybe for debugging purpose?), I have the impression that using tmpfile(3) would result in a simpler patch.

Cheers.

--
Greg

Quoted 31 lines
tests/boot-sector.c +0 -0
     if (!f) {
         fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
         return 1;
tests/boot-sector.h +2 -2
index f64b477..35d61c7 100644
@@ -14,8 +14,8 @@
 #ifndef TEST_BOOT_SECTOR_H
 #define TEST_BOOT_SECTOR_H
 
-/* Create boot disk file.  */
-int boot_sector_init(const char *fname);
+/* Create boot disk file. fname must be a suitable string for mkstemp() */
+int boot_sector_init(char *fname);
 
 /* Loop until signature in memory is OK.  */
 void boot_sector_test(void);
tests/pxe-test.c +1 -1
index 5d3ddbe..34282d3 100644
@@ -19,7 +19,7 @@
 
 #define NETNAME "net0"
 
-static const char *disk = "tests/pxe-test-disk.raw";
+static char disk[] = "tests/pxe-test-disk-XXXXXX";
 
 static void test_pxe_one(const char *params, bool ipv6)
 {
#4 lore raw
Quoted 48 lines · Greg Kurz
Quoted 45 lines · On Tue, 11 Oct 2016 15:32:02 +0200 Thomas Huth <[email protected]> wrote:

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +3 -2
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
Quoted 8 lines

tests/boot-sector.c static uint8_t boot_sector[0x7e000] = { · line 80

Quoted 4 lines
tests/boot-sector.c +4 -0
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }

Unless we really want to control how the temporary file is named (maybe for debugging purpose?), I have the impression that using tmpfile(3) would result in a simpler patch.

That unfortunately does not work here - the file name is needed for starting the QEMU process later (see test_acpi_one() or test_pxe_one() for details).

Thomas

#5 lore raw
Quoted 58 lines · On Tue, 11 Oct 2016 16:06:55 +0200 Thomas Huth <[email protected]> wrote:
Quoted 48 lines · Greg Kurz
Quoted 45 lines · On Tue, 11 Oct 2016 15:32:02 +0200 Thomas Huth <[email protected]> wrote:

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +3 -2
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
Quoted 8 lines

tests/boot-sector.c static uint8_t boot_sector[0x7e000] = { · line 80

Quoted 4 lines
tests/boot-sector.c +4 -0
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }  

Unless we really want to control how the temporary file is named (maybe for debugging purpose?), I have the impression that using tmpfile(3) would result in a simpler patch.

Quoted 4 lines

That unfortunately does not work here - the file name is needed for starting the QEMU process later (see test_acpi_one() or test_pxe_one() for details).

Oops you're right *of course*.

Then I guess Peter's remark on fwrite()/fclose() should also be addressed, but I see you already sent an update for that :)

Quoted 2 lines

Thomas

Cheers.

--
Greg

Tested-by on v1 1/1
#6 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

The pxe-test is run for three different targets now (x86_64, i386 and ppc64), and the bios-tables-test is run for two targets (x86_64 and i386). But each of the tests is using an invariant name for the disk image with the boot sector code - so if the tests are running in parallel, there is a race condition that they destroy the disk image of a parallel test program. Let's use mkstemp() to create unique temporary files here instead.

Nice, this does help fix the failure that patchew hits with "make docker-test-clang@ubuntu J=8".

Quoted 74 lines
---
 tests/bios-tables-test.c | 2 +-
 tests/boot-sector.c      | 9 +++++++--
 tests/boot-sector.h      | 4 ++--
 tests/pxe-test.c         | 2 +-
 4 files changed, 11 insertions(+), 6 deletions(-)
tests/bios-tables-test.c +1 -1
index 6ea2b6d..812f830 100644
@@ -112,7 +112,7 @@ typedef struct {
     g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
 } while (0)
 
-static const char *disk = "tests/acpi-test-disk.raw";
+static char disk[] = "tests/acpi-test-disk-XXXXXX";
 static const char *data_dir = "tests/acpi-test-data";
 #ifdef CONFIG_IASL
 static const char *iasl = stringify(CONFIG_IASL);
tests/boot-sector.c +7 -2
index e3193c0..e75572f 100644
@@ -69,10 +69,15 @@ static uint8_t boot_sector[0x7e000] = {
 };
 
 /* Create boot disk file.  */
-int boot_sector_init(const char *fname)
+int boot_sector_init(char *fname)
 {
-    FILE *f = fopen(fname, "w");
+    FILE *f = NULL;
+    int fd;
 
+    fd = mkstemp(fname);
+    if (fd != -1) {
+        f = fdopen(fd, "w");
+    }
     if (!f) {
         fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
         return 1;
tests/boot-sector.h +2 -2
index f64b477..35d61c7 100644
@@ -14,8 +14,8 @@
 #ifndef TEST_BOOT_SECTOR_H
 #define TEST_BOOT_SECTOR_H
 
-/* Create boot disk file.  */
-int boot_sector_init(const char *fname);
+/* Create boot disk file. fname must be a suitable string for mkstemp() */
+int boot_sector_init(char *fname);
 
 /* Loop until signature in memory is OK.  */
 void boot_sector_test(void);
tests/pxe-test.c +1 -1
index 5d3ddbe..34282d3 100644
@@ -19,7 +19,7 @@
 
 #define NETNAME "net0"
 
-static const char *disk = "tests/pxe-test-disk.raw";
+static char disk[] = "tests/pxe-test-disk-XXXXXX";
 
 static void test_pxe_one(const char *params, bool ipv6)
 {
Signature · 1 line
1.8.3.1