From: Markus Armbruster <armbru@redhat.com>
To: marcandre.lureau@redhat.com
Cc: qemu-devel@nongnu.org, "Thomas Huth" <thuth@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Michael Roth" <michael.roth@amd.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Beraldo Leal" <bleal@redhat.com>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
"Stefan Weil" <sw@weilnetz.de>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: Re: [PATCH v4 08/11] qmp: add 'get-win32-socket'
Date: Mon, 06 Mar 2023 16:47:51 +0100 [thread overview]
Message-ID: <871qm1j2zc.fsf@pond.sub.org> (raw)
In-Reply-To: <20230306122751.2355515-9-marcandre.lureau@redhat.com> (marcandre lureau's message of "Mon, 6 Mar 2023 16:27:48 +0400")
marcandre.lureau@redhat.com writes:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> A process with enough capabilities can duplicate a socket to QEMU. Add a
> QMP command to import it and add it to the monitor fd list, so it can be
> later used by other commands.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> qapi/misc.json | 30 ++++++++++++++++++++
> monitor/fds.c | 76 +++++++++++++++++++++++++++++++++++++++-----------
> 2 files changed, 90 insertions(+), 16 deletions(-)
>
> diff --git a/qapi/misc.json b/qapi/misc.json
> index f0217cfba0..031c94050c 100644
> --- a/qapi/misc.json
> +++ b/qapi/misc.json
> @@ -275,6 +275,36 @@
> ##
> { 'command': 'getfd', 'data': {'fdname': 'str'} }
>
> +##
> +# @get-win32-socket:
> +#
> +# Add a socket that was duplicated to QEMU process with WSADuplicateSocketW()
> +# via WSASocket() & WSAPROTOCOL_INFOW structure and assign it a name (the SOCKET
> +# is associated with a CRT file descriptor)
Long line.
> +#
> +# @info: the WSAPROTOCOL_INFOW structure (encoded in base64)
> +#
> +# @fdname: file descriptor name
> +#
> +# Returns: Nothing on success
> +#
> +# Since: 8.0
> +#
> +# Notes: If @fdname already exists, the file descriptor assigned to
> +# it will be closed and replaced by the received file
> +# descriptor.
> +#
> +# The 'closefd' command can be used to explicitly close the
> +# file descriptor when it is no longer needed.
> +#
> +# Example:
> +#
> +# -> { "execute": "get-win32-socket", "arguments": { "info": "abcd123..", fdname": "skclient" } }
> +# <- { "return": {} }
> +#
> +##
> +{ 'command': 'get-win32-socket', 'data': {'info': 'str', 'fdname': 'str'}, 'if': 'CONFIG_WIN32' }
> +
> ##
> # @closefd:
> #
> diff --git a/monitor/fds.c b/monitor/fds.c
> index 7daf1064e1..9ed4197358 100644
> --- a/monitor/fds.c
> +++ b/monitor/fds.c
> @@ -61,46 +61,55 @@ struct MonFdset {
> static QemuMutex mon_fdsets_lock;
> static QLIST_HEAD(, MonFdset) mon_fdsets;
>
> -void qmp_getfd(const char *fdname, Error **errp)
> +static bool monitor_add_fd(Monitor *mon, int fd, const char *fdname, Error **errp)
> {
> - Monitor *cur_mon = monitor_cur();
> mon_fd_t *monfd;
> - int fd, tmp_fd;
> -
> - fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
> - if (fd == -1) {
> - error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
> - return;
> - }
>
> if (qemu_isdigit(fdname[0])) {
> close(fd);
> error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
> "a name not starting with a digit");
> - return;
> + return false;
> }
>
> /* See close() call below. */
> - qemu_mutex_lock(&cur_mon->mon_lock);
> - QLIST_FOREACH(monfd, &cur_mon->fds, next) {
> + qemu_mutex_lock(&mon->mon_lock);
> + QLIST_FOREACH(monfd, &mon->fds, next) {
> + int tmp_fd;
> +
> if (strcmp(monfd->name, fdname) != 0) {
> continue;
> }
>
> tmp_fd = monfd->fd;
> monfd->fd = fd;
> - qemu_mutex_unlock(&cur_mon->mon_lock);
> + qemu_mutex_unlock(&mon->mon_lock);
> /* Make sure close() is outside critical section */
> close(tmp_fd);
> - return;
> + return true;
> }
Have you considered factoring out the loop searching ->fds?
>
> monfd = g_new0(mon_fd_t, 1);
> monfd->name = g_strdup(fdname);
> monfd->fd = fd;
>
> - QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
> - qemu_mutex_unlock(&cur_mon->mon_lock);
> + QLIST_INSERT_HEAD(&mon->fds, monfd, next);
> + qemu_mutex_unlock(&mon->mon_lock);
> + return true;
> +}
> +
> +void qmp_getfd(const char *fdname, Error **errp)
> +{
> + Monitor *cur_mon = monitor_cur();
> + int fd;
> +
> + fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
> + if (fd == -1) {
> + error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
> + return;
> + }
> +
> + monitor_add_fd(cur_mon, fd, fdname, errp);
> }
>
> void qmp_closefd(const char *fdname, Error **errp)
> @@ -214,6 +223,41 @@ error:
> return NULL;
> }
With the doc comment tidied up:
Acked-by: Markus Armbruster <armbru@redhat.com>
The remainder is greek to me :)
>
> +#ifdef WIN32
> +void qmp_get_win32_socket(const char *infos, const char *fdname, Error **errp)
> +{
> + g_autofree WSAPROTOCOL_INFOW *info = NULL;
> + gsize len;
> + SOCKET sk;
> + int fd;
> +
> + info = (void *)g_base64_decode(infos, &len);
> + if (len != sizeof(*info)) {
> + error_setg(errp, "Invalid WSAPROTOCOL_INFOW value");
> + return;
> + }
> +
> + sk = WSASocketW(FROM_PROTOCOL_INFO,
> + FROM_PROTOCOL_INFO,
> + FROM_PROTOCOL_INFO,
> + info, 0, 0);
> + if (sk == INVALID_SOCKET) {
> + error_setg_win32(errp, WSAGetLastError(), "Couldn't import socket");
> + return;
> + }
> +
> + fd = _open_osfhandle(sk, _O_BINARY);
> + if (fd < 0) {
> + error_setg_errno(errp, errno, "Failed to associate a FD with the SOCKET");
> + closesocket(sk);
> + return;
> + }
> +
> + monitor_add_fd(monitor_cur(), fd, fdname, errp);
> +}
> +#endif
> +
> +
> void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
> {
> MonFdset *mon_fdset;
next prev parent reply other threads:[~2023-03-06 15:48 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 12:27 [PATCH v4 00/11] QMP command to import win32 sockets marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 01/11] tests: fix path separator, use g_build_filename() marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 02/11] char: do not double-close fd when failing to add client marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 03/11] tests/docker: fix a win32 error due to portability marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 04/11] osdep: implement qemu_socketpair() for win32 marcandre.lureau
2023-03-07 14:50 ` Daniel P. Berrangé
2023-03-08 6:53 ` Marc-André Lureau
2023-03-08 9:27 ` Daniel P. Berrangé
2023-03-06 12:27 ` [PATCH v4 05/11] qmp: 'add_client' actually expects sockets marcandre.lureau
2023-03-06 15:02 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 06/11] monitor: release the lock before calling close() marcandre.lureau
2023-03-06 15:29 ` Markus Armbruster
2023-03-06 15:44 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 07/11] qapi/gen: run C code through clang-format, if possible marcandre.lureau
2023-03-06 16:02 ` Markus Armbruster
2023-03-06 18:26 ` Marc-André Lureau
2023-03-06 16:05 ` Peter Maydell
2023-03-06 18:29 ` Marc-André Lureau
2023-03-06 18:39 ` Peter Maydell
2023-03-07 8:51 ` Markus Armbruster
2023-03-06 12:27 ` [PATCH v4 08/11] qmp: add 'get-win32-socket' marcandre.lureau
2023-03-06 15:47 ` Markus Armbruster [this message]
2023-03-07 12:31 ` Marc-André Lureau
2023-03-06 12:27 ` [PATCH v4 09/11] libqtest: make qtest_qmp_add_client work on win32 marcandre.lureau
2023-03-07 14:54 ` Daniel P. Berrangé
2023-03-06 12:27 ` [PATCH v4 10/11] qtest: enable vnc-display test " marcandre.lureau
2023-03-06 12:27 ` [PATCH v4 11/11] QMP/HMP: only actually implement getfd on CONFIG_POSIX marcandre.lureau
2023-03-06 16:01 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871qm1j2zc.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=bleal@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=kraxel@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.