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

Bin Meng posted 51 patches 3 years, 5 months ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Thomas Huth <thuth@redhat.com>, Wainer dos Santos Moschetta <wainersm@redhat.com>, Beraldo Leal <bleal@redhat.com>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Greg Kurz <groug@kaod.org>, Christian Schoenebeck <qemu_oss@crudebyte.com>, "Cédric Le Goater" <clg@kaod.org>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Gerd Hoffmann <kraxel@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Michael Roth <michael.roth@amd.com>, Konstantin Kostiuk <kkostiuk@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, John Snow <jsnow@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, Andrew Jeffery <andrew@aj.id.au>, Joel Stanley <joel@jms.id.au>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <ani@anisinha.ca>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Havard Skinnemoen <hskinnemoen@google.com>, Tyrone Ting <kfting@nuvoton.com>, Markus Armbruster <armbru@redhat.com>, Coiby Xu <Coiby.Xu@gmail.com>, Jason Wang <jasowang@redhat.com>, Fam Zheng <fam@euphon.net>
There is a newer version of this series
[PATCH 32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32
Posted by Bin Meng 3 years, 5 months ago
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(+)

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
Re: [PATCH 32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32
Posted by Marc-André Lureau 3 years, 5 months ago
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
>
>
>

-- 
Marc-André Lureau
Re: [PATCH 32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32
Posted by Bin Meng 3 years, 5 months ago
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
Re: [PATCH 32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32
Posted by Thomas Huth 3 years, 5 months ago
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
Re: [PATCH 32/51] tests/qtest: Fix ERROR_SHARING_VIOLATION for win32
Posted by Bin Meng 3 years, 5 months ago
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