diff mbox series

[46/51] tests/qtest: libqtest: Replace the call to close a socket with closesocket()

Message ID 20220824094029.1634519-47-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>

close() is a *nix function. It works on any file descriptor, and
sockets in *nix are an example of a file descriptor.

closesocket() is a Windows-specific function, which works only
specifically with sockets. Sockets on Windows do not use *nix-style
file descriptors, and socket() returns a handle to a kernel object
instead, so it must be closed with closesocket().

In QEMU there is already a logic to handle such platform difference
in os-posix.h and os-win32.h, that:

  * closesocket maps to close on POSIX
  * closesocket maps to a wrapper that calls the real closesocket()
    on Windows

Replace the call to close a socket with closesocket() instead.

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

 tests/qtest/libqtest.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Marc-André Lureau Sept. 1, 2022, 11:46 a.m. UTC | #1
On Wed, Aug 24, 2022 at 3:09 PM Bin Meng <bmeng.cn@gmail.com> wrote:

> From: Bin Meng <bin.meng@windriver.com>
>
> close() is a *nix function. It works on any file descriptor, and
> sockets in *nix are an example of a file descriptor.
>
> closesocket() is a Windows-specific function, which works only
> specifically with sockets. Sockets on Windows do not use *nix-style
> file descriptors, and socket() returns a handle to a kernel object
> instead, so it must be closed with closesocket().
>
> In QEMU there is already a logic to handle such platform difference
> in os-posix.h and os-win32.h, that:
>
>   * closesocket maps to close on POSIX
>   * closesocket maps to a wrapper that calls the real closesocket()
>     on Windows
>
> Replace the call to close a socket with closesocket() instead.
>
> Signed-off-by: Bin Meng <bin.meng@windriver.com>
>

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


> ---
>
>  tests/qtest/libqtest.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
> index 99e52ff571..918f4657ed 100644
> --- a/tests/qtest/libqtest.c
> +++ b/tests/qtest/libqtest.c
> @@ -115,7 +115,7 @@ static int socket_accept(int sock)
>                     (void *)&timeout, sizeof(timeout))) {
>          fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
>                  __func__, strerror(errno));
> -        close(sock);
> +        closesocket(sock);
>          return -1;
>      }
>
> @@ -126,7 +126,7 @@ static int socket_accept(int sock)
>      if (ret == -1) {
>          fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
>      }
> -    close(sock);
> +    closesocket(sock);
>
>      return ret;
>  }
> @@ -512,8 +512,8 @@ void qtest_quit(QTestState *s)
>      qtest_remove_abrt_handler(s);
>
>      qtest_kill_qemu(s);
> -    close(s->fd);
> -    close(s->qmp_fd);
> +    closesocket(s->fd);
> +    closesocket(s->qmp_fd);
>      g_string_free(s->rx, true);
>
>      for (GList *it = s->pending_events; it != NULL; it = it->next) {
> --
> 2.34.1
>
>
>
diff mbox series

Patch

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 99e52ff571..918f4657ed 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -115,7 +115,7 @@  static int socket_accept(int sock)
                    (void *)&timeout, sizeof(timeout))) {
         fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
                 __func__, strerror(errno));
-        close(sock);
+        closesocket(sock);
         return -1;
     }
 
@@ -126,7 +126,7 @@  static int socket_accept(int sock)
     if (ret == -1) {
         fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
     }
-    close(sock);
+    closesocket(sock);
 
     return ret;
 }
@@ -512,8 +512,8 @@  void qtest_quit(QTestState *s)
     qtest_remove_abrt_handler(s);
 
     qtest_kill_qemu(s);
-    close(s->fd);
-    close(s->qmp_fd);
+    closesocket(s->fd);
+    closesocket(s->qmp_fd);
     g_string_free(s->rx, true);
 
     for (GList *it = s->pending_events; it != NULL; it = it->next) {