[PATCH v4 39/54] tests/qtest: boot-serial-test: Close the serial file before starting QEMU

Bin Meng posted 54 patches 3 years, 4 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>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Greg Kurz <groug@kaod.org>, Christian Schoenebeck <qemu_oss@crudebyte.com>, 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>, Richard Henderson <richard.henderson@linaro.org>, John Snow <jsnow@redhat.com>, "Cédric Le Goater" <clg@kaod.org>, 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>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Markus Armbruster <armbru@redhat.com>, Coiby Xu <Coiby.Xu@gmail.com>, Jason Wang <jasowang@redhat.com>, Fam Zheng <fam@euphon.net>, Michael Roth <michael.roth@amd.com>, Konstantin Kostiuk <kkostiuk@redhat.com>
There is a newer version of this series
[PATCH v4 39/54] tests/qtest: boot-serial-test: Close the serial file before starting QEMU
Posted by Bin Meng 3 years, 4 months ago
From: Bin Meng <bin.meng@windriver.com>

This qtest executable created a serial chardev file to be passed to
the QEMU executable. The serial file was created by g_file_open_tmp(),
which internally opens the file with FILE_SHARE_WRITE security attribute
on Windows. Based on [1], there is only one case that allows the first
call to CreateFile() with GENERIC_READ & FILE_SHARE_WRITE, and second
call to CreateFile() with GENERIC_WRITE & FILE_SHARE_READ. All other
combinations require FILE_SHARE_WRITE in the second call. But there is
no way for the second call (in this case the QEMU executable) to know
what combination was passed to the first call, unless FILE_SHARE_WRITE
is passed to the second call.

Two processes shouldn't share the same file for writing with a chardev.
Let's close the serial file before starting QEMU.

[1] https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files

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

Changes in v4:
- New patch: "tests/qtest: boot-serial-test: Close the serial file before starting QEMU"

 tests/qtest/boot-serial-test.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
index 72310ba30e..b216519b62 100644
--- a/tests/qtest/boot-serial-test.c
+++ b/tests/qtest/boot-serial-test.c
@@ -233,6 +233,7 @@ static void test_machine(const void *data)
 
     ser_fd = g_file_open_tmp("qtest-boot-serial-sXXXXXX", &serialtmp, NULL);
     g_assert(ser_fd != -1);
+    close(ser_fd);
 
     if (test->kernel) {
         code = test->kernel;
@@ -266,6 +267,8 @@ static void test_machine(const void *data)
         unlink(codetmp);
     }
 
+    ser_fd = open(serialtmp, O_RDONLY);
+    g_assert(ser_fd != -1);
     if (!check_guest_output(qts, test, ser_fd)) {
         g_error("Failed to find expected string. Please check '%s'",
                 serialtmp);
-- 
2.34.1
Re: [PATCH v4 39/54] tests/qtest: boot-serial-test: Close the serial file before starting QEMU
Posted by Marc-André Lureau 3 years, 4 months ago
On Tue, Sep 27, 2022 at 3:08 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> From: Bin Meng <bin.meng@windriver.com>
>
> This qtest executable created a serial chardev file to be passed to
> the QEMU executable. The serial file was created by g_file_open_tmp(),
> which internally opens the file with FILE_SHARE_WRITE security attribute
> on Windows. Based on [1], there is only one case that allows the first
> call to CreateFile() with GENERIC_READ & FILE_SHARE_WRITE, and second
> call to CreateFile() with GENERIC_WRITE & FILE_SHARE_READ. All other
> combinations require FILE_SHARE_WRITE in the second call. But there is
> no way for the second call (in this case the QEMU executable) to know
> what combination was passed to the first call, unless FILE_SHARE_WRITE
> is passed to the second call.
>
> Two processes shouldn't share the same file for writing with a chardev.
> Let's close the serial file before starting QEMU.
>
> [1] https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files
>
> Signed-off-by: Bin Meng <bin.meng@windriver.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>



> ---
>
> Changes in v4:
> - New patch: "tests/qtest: boot-serial-test: Close the serial file before starting QEMU"
>
>  tests/qtest/boot-serial-test.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
> index 72310ba30e..b216519b62 100644
> --- a/tests/qtest/boot-serial-test.c
> +++ b/tests/qtest/boot-serial-test.c
> @@ -233,6 +233,7 @@ static void test_machine(const void *data)
>
>      ser_fd = g_file_open_tmp("qtest-boot-serial-sXXXXXX", &serialtmp, NULL);
>      g_assert(ser_fd != -1);
> +    close(ser_fd);
>
>      if (test->kernel) {
>          code = test->kernel;
> @@ -266,6 +267,8 @@ static void test_machine(const void *data)
>          unlink(codetmp);
>      }
>
> +    ser_fd = open(serialtmp, O_RDONLY);
> +    g_assert(ser_fd != -1);
>      if (!check_guest_output(qts, test, ser_fd)) {
>          g_error("Failed to find expected string. Please check '%s'",
>                  serialtmp);
> --
> 2.34.1
>