diff mbox series

[v2,04/19] osdep.h: Introduce a QEMU file descriptor type

Message ID 20221111042225.1115931-5-bin.meng@windriver.com (mailing list archive)
State New, archived
Headers show
Series At present there is no Windows support for 9p file system. | expand

Commit Message

Bin Meng Nov. 11, 2022, 4:22 a.m. UTC
Introduce a new QemuFd_t type to represent a file descriptor for
different platforms. On POSIX platforms, this is a file descriptor
On Windows, this is a file handle.

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

---

Changes in v2:
- Change to introduce QemuFd_t in osdep.h

 include/qemu/osdep.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Comments

Daniel P. Berrangé Nov. 11, 2022, 9:07 a.m. UTC | #1
On Fri, Nov 11, 2022 at 12:22:10PM +0800, Bin Meng wrote:
> Introduce a new QemuFd_t type to represent a file descriptor for
> different platforms. On POSIX platforms, this is a file descriptor
> On Windows, this is a file handle.

Can we not use  _open_osfhandle() to obtain a C runtime
file descriptor from the Windows HANDLE.  We do this in
QEMU's socket code, so we don't have to work wit different
types and APIs on Windows, and I think that's much nicer
in general.

> 
> Changes in v2:
> - Change to introduce QemuFd_t in osdep.h
> 
>  include/qemu/osdep.h | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index b9c4307779..45fc8bb5d9 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -705,6 +705,32 @@ static inline int platform_does_not_support_system(const char *command)
>  }
>  #endif /* !HAVE_SYSTEM_FUNCTION */
>  
> +/*
> + * QEMU file descriptor type
> + *
> + * On POSIX platforms, this is a file descriptor (int).
> + * On Windows, this is a file handle (HANDLE).
> + */
> +#ifndef _WIN32
> +typedef int QemuFd_t;
> +#define QEMU_FD_INVALID -1
> +#else
> +typedef HANDLE QemuFd_t;
> +#define QEMU_FD_INVALID INVALID_HANDLE_VALUE
> +#endif
> +
> +/**
> + * qemu_fd_invalid - determine if a given QEMU file descriptor is invalid
> + *
> + * @fd: the value of a QEMU file descriptor
> + *
> + * Returns true if a given QEMU file descriptor is invalid, otherwise false.
> + */
> +static inline bool qemu_fd_invalid(QemuFd_t fd)
> +{
> +    return (fd == QEMU_FD_INVALID);
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
> -- 
> 2.25.1
> 

With regards,
Daniel
Bin Meng Nov. 11, 2022, 9:23 a.m. UTC | #2
Hi Daniel,

On Fri, Nov 11, 2022 at 5:08 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> On Fri, Nov 11, 2022 at 12:22:10PM +0800, Bin Meng wrote:
> > Introduce a new QemuFd_t type to represent a file descriptor for
> > different platforms. On POSIX platforms, this is a file descriptor
> > On Windows, this is a file handle.
>
> Can we not use  _open_osfhandle() to obtain a C runtime
> file descriptor from the Windows HANDLE.  We do this in
> QEMU's socket code, so we don't have to work wit different
> types and APIs on Windows, and I think that's much nicer
> in general.
>

I am sorry I don't understand your suggestion. I checked
qemu-sockets.c and did not see how sockets connect to this change.

This change is required to make 9pfs Windows support much easier.

Regards,
Bin
Daniel P. Berrangé Nov. 11, 2022, 9:29 a.m. UTC | #3
On Fri, Nov 11, 2022 at 05:23:58PM +0800, Bin Meng wrote:
> Hi Daniel,
> 
> On Fri, Nov 11, 2022 at 5:08 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
> >
> > On Fri, Nov 11, 2022 at 12:22:10PM +0800, Bin Meng wrote:
> > > Introduce a new QemuFd_t type to represent a file descriptor for
> > > different platforms. On POSIX platforms, this is a file descriptor
> > > On Windows, this is a file handle.
> >
> > Can we not use  _open_osfhandle() to obtain a C runtime
> > file descriptor from the Windows HANDLE.  We do this in
> > QEMU's socket code, so we don't have to work wit different
> > types and APIs on Windows, and I think that's much nicer
> > in general.
> >
> 
> I am sorry I don't understand your suggestion. I checked
> qemu-sockets.c and did not see how sockets connect to this change.
> 
> This change is required to make 9pfs Windows support much easier.

I'm just using it as an example to show that we can continue to
use a plain 'int fd' everywhere, and not invent a QemuFd_t type
abstraction

With regards,
Daniel
diff mbox series

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b9c4307779..45fc8bb5d9 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -705,6 +705,32 @@  static inline int platform_does_not_support_system(const char *command)
 }
 #endif /* !HAVE_SYSTEM_FUNCTION */
 
+/*
+ * QEMU file descriptor type
+ *
+ * On POSIX platforms, this is a file descriptor (int).
+ * On Windows, this is a file handle (HANDLE).
+ */
+#ifndef _WIN32
+typedef int QemuFd_t;
+#define QEMU_FD_INVALID -1
+#else
+typedef HANDLE QemuFd_t;
+#define QEMU_FD_INVALID INVALID_HANDLE_VALUE
+#endif
+
+/**
+ * qemu_fd_invalid - determine if a given QEMU file descriptor is invalid
+ *
+ * @fd: the value of a QEMU file descriptor
+ *
+ * Returns true if a given QEMU file descriptor is invalid, otherwise false.
+ */
+static inline bool qemu_fd_invalid(QemuFd_t fd)
+{
+    return (fd == QEMU_FD_INVALID);
+}
+
 #ifdef __cplusplus
 }
 #endif