diff mbox series

[32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32

Message ID 20220824094029.1634519-33-bmeng.cn@gmail.com (mailing list archive)
State New, archived
Headers show
Series tests/qtest: Enable running qtest on Windows | expand

Commit Message

Bin Meng Aug. 24, 2022, 9:40 a.m. UTC
From: Bin Meng <bin.meng@windriver.com>

On Windows, the MinGW provided mkstemp() API opens the file with
exclusive access, denying other processes to read/write the file.
Such behavior prevents the QEMU executable from opening the file,
(e.g.: CreateFile returns ERROR_SHARING_VIOLATION).

This can be fixed by closing the file and reopening it.

Signed-off-by: Bin Meng <bin.meng@windriver.com>
---

 tests/qtest/ahci-test.c        | 14 ++++++++++++++
 tests/qtest/boot-serial-test.c | 13 +++++++++++++
 2 files changed, 27 insertions(+)

Comments

Thomas Huth Aug. 25, 2022, 12:06 p.m. UTC | #1
On 24/08/2022 11.40, Bin Meng wrote:
> From: Bin Meng <bin.meng@windriver.com>
> 
> On Windows, the MinGW provided mkstemp() API opens the file with
> exclusive access, denying other processes to read/write the file.
> Such behavior prevents the QEMU executable from opening the file,
> (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
> 
> This can be fixed by closing the file and reopening it.

Would it work to use the glib functions instead (like g_file_open_tmp() ?)

  Thomas
Marc-André Lureau Sept. 1, 2022, 8:42 a.m. UTC | #2
Hi

On Wed, Aug 24, 2022 at 2:03 PM Bin Meng <bmeng.cn@gmail.com> wrote:

> From: Bin Meng <bin.meng@windriver.com>
>
> On Windows, the MinGW provided mkstemp() API opens the file with
> exclusive access, denying other processes to read/write the file.
> Such behavior prevents the QEMU executable from opening the file,
> (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
>

g_mkstemp() doesn't have this behaviour (after running a quick test). Use
it?


>
> This can be fixed by closing the file and reopening it.
>
> Signed-off-by: Bin Meng <bin.meng@windriver.com>
> ---
>
>  tests/qtest/ahci-test.c        | 14 ++++++++++++++
>  tests/qtest/boot-serial-test.c | 13 +++++++++++++
>  2 files changed, 27 insertions(+)
>
> diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
> index f26cd6f86f..0e88cd0eef 100644
> --- a/tests/qtest/ahci-test.c
> +++ b/tests/qtest/ahci-test.c
> @@ -1443,6 +1443,20 @@ static int prepare_iso(size_t size, unsigned char
> **buf, char **name)
>      int fd = mkstemp(cdrom_path);
>
>      g_assert(fd != -1);
> +#ifdef _WIN32
> +    /*
> +     * On Windows, the MinGW provided mkstemp() API opens the file with
> +     * exclusive access, denying other processes to read/write the file.
> +     * Such behavior prevents the QEMU executable from opening the file,
> +     * (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
> +     *
> +     * Close the file and reopen it.
> +     */
> +    close(fd);
> +    fd = open(cdrom_path, O_WRONLY);
> +    g_assert(fd != -1);
> +#endif
> +
>      g_assert(buf);
>      g_assert(name);
>      patt = g_malloc(size);
> diff --git a/tests/qtest/boot-serial-test.c
> b/tests/qtest/boot-serial-test.c
> index 404adcfa20..fb6c81bf35 100644
> --- a/tests/qtest/boot-serial-test.c
> +++ b/tests/qtest/boot-serial-test.c
> @@ -235,6 +235,19 @@ static void test_machine(const void *data)
>
>      ser_fd = mkstemp(serialtmp);
>      g_assert(ser_fd != -1);
> +#ifdef _WIN32
> +    /*
> +     * On Windows, the MinGW provided mkstemp() API opens the file with
> +     * exclusive access, denying other processes to read/write the file.
> +     * Such behavior prevents the QEMU executable from opening the file,
> +     * (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
> +     *
> +     * Close the file and reopen it.
> +     */
> +    close(ser_fd);
> +    ser_fd = open(serialtmp, O_RDONLY);
> +    g_assert(ser_fd != -1);
> +#endif
>
>      if (test->kernel) {
>          code = test->kernel;
> --
> 2.34.1
>
>
>
Bin Meng Sept. 2, 2022, 10:59 a.m. UTC | #3
On Thu, Aug 25, 2022 at 8:06 PM Thomas Huth <thuth@redhat.com> wrote:
>
> On 24/08/2022 11.40, Bin Meng wrote:
> > From: Bin Meng <bin.meng@windriver.com>
> >
> > On Windows, the MinGW provided mkstemp() API opens the file with
> > exclusive access, denying other processes to read/write the file.
> > Such behavior prevents the QEMU executable from opening the file,
> > (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
> >
> > This can be fixed by closing the file and reopening it.
>
> Would it work to use the glib functions instead (like g_file_open_tmp() ?)
>

Yep, I've switched to using g_file_open_tmp() in patch #7 "tests:
Avoid using hardcoded /tmp in test cases", and testing shows that it
does not have such an issue.

So this patch can be dropped.

Regards,
Bin
Bin Meng Sept. 2, 2022, 11:02 a.m. UTC | #4
On Thu, Sep 1, 2022 at 4:42 PM Marc-André Lureau
<marcandre.lureau@gmail.com> wrote:
>
> Hi
>
> On Wed, Aug 24, 2022 at 2:03 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>>
>> From: Bin Meng <bin.meng@windriver.com>
>>
>> On Windows, the MinGW provided mkstemp() API opens the file with
>> exclusive access, denying other processes to read/write the file.
>> Such behavior prevents the QEMU executable from opening the file,
>> (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
>
>
> g_mkstemp() doesn't have this behaviour (after running a quick test). Use it?
>

Thanks for the suggestion!

I've switched to using g_file_open_tmp() in patch #7 "tests: Avoid
using hardcoded /tmp in test cases", and testing shows that it does
not have such an issue.

I checked glib sources and see both g_mkstemp() and g_file_open_tmp()
call g_open() which allows shared read/write on Windows.

So this patch can be dropped.

Regards,
Bin
diff mbox series

Patch

diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index f26cd6f86f..0e88cd0eef 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1443,6 +1443,20 @@  static int prepare_iso(size_t size, unsigned char **buf, char **name)
     int fd = mkstemp(cdrom_path);
 
     g_assert(fd != -1);
+#ifdef _WIN32
+    /*
+     * On Windows, the MinGW provided mkstemp() API opens the file with
+     * exclusive access, denying other processes to read/write the file.
+     * Such behavior prevents the QEMU executable from opening the file,
+     * (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
+     *
+     * Close the file and reopen it.
+     */
+    close(fd);
+    fd = open(cdrom_path, O_WRONLY);
+    g_assert(fd != -1);
+#endif
+
     g_assert(buf);
     g_assert(name);
     patt = g_malloc(size);
diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
index 404adcfa20..fb6c81bf35 100644
--- a/tests/qtest/boot-serial-test.c
+++ b/tests/qtest/boot-serial-test.c
@@ -235,6 +235,19 @@  static void test_machine(const void *data)
 
     ser_fd = mkstemp(serialtmp);
     g_assert(ser_fd != -1);
+#ifdef _WIN32
+    /*
+     * On Windows, the MinGW provided mkstemp() API opens the file with
+     * exclusive access, denying other processes to read/write the file.
+     * Such behavior prevents the QEMU executable from opening the file,
+     * (e.g.: CreateFile returns ERROR_SHARING_VIOLATION).
+     *
+     * Close the file and reopen it.
+     */
+    close(ser_fd);
+    ser_fd = open(serialtmp, O_RDONLY);
+    g_assert(ser_fd != -1);
+#endif
 
     if (test->kernel) {
         code = test->kernel;