From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: Michal Privoznik <mprivozn@redhat.com>, qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, sw@weilnetz.de, marcandre.lureau@gmail.com,
vfeenstr@redhat.com, mdroth@linux.vnet.ibm.com
Subject: Re: [PATCH v3 1/2] util: Introduce qemu_get_host_name()
Date: Mon, 22 Jun 2020 20:37:41 +0200 [thread overview]
Message-ID: <151caec3-fbdf-10ff-34c7-39737c7f4d07@redhat.com> (raw)
In-Reply-To: <02ebd01e1ff7b4aba70e58a674f457ec8e4cf20b.1592849834.git.mprivozn@redhat.com>
On 6/22/20 8:19 PM, Michal Privoznik wrote:
> This function offers operating system agnostic way to fetch host
> name. It is implemented for both POSIX-like and Windows systems.
>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Thanks for the updates!
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
> include/qemu/osdep.h | 10 ++++++++++
> util/oslib-posix.c | 35 +++++++++++++++++++++++++++++++++++
> util/oslib-win32.c | 13 +++++++++++++
> 3 files changed, 58 insertions(+)
>
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index ff7c17b857..a795d46b28 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -607,4 +607,14 @@ static inline void qemu_reset_optind(void)
> #endif
> }
>
> +/**
> + * qemu_get_host_name:
> + * @errp: Error object
> + *
> + * Operating system agnostic way of querying host name.
> + *
> + * Returns allocated hostname (caller should free), NULL on failure.
> + */
> +char *qemu_get_host_name(Error **errp);
> +
> #endif
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 916f1be224..3997ee0442 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -761,3 +761,38 @@ void sigaction_invoke(struct sigaction *action,
> }
> action->sa_sigaction(info->ssi_signo, &si, NULL);
> }
> +
> +#ifndef HOST_NAME_MAX
> +# ifdef _POSIX_HOST_NAME_MAX
> +# define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
> +# else
> +# define HOST_NAME_MAX 255
> +# endif
> +#endif
> +
> +char *qemu_get_host_name(Error **errp)
> +{
> + long len = -1;
> + g_autofree char *hostname = NULL;
> +
> +#ifdef _SC_HOST_NAME_MAX
> + len = sysconf(_SC_HOST_NAME_MAX);
> +#endif /* _SC_HOST_NAME_MAX */
> +
> + if (len < 0) {
> + len = HOST_NAME_MAX;
> + }
> +
> + /* Unfortunately, gethostname() below does not guarantee a
> + * NULL terminated string. Therefore, allocate one byte more
> + * to be sure. */
> + hostname = g_new0(char, len + 1);
> +
> + if (gethostname(hostname, len) < 0) {
> + error_setg_errno(errp, errno,
> + "cannot get hostname");
> + return NULL;
> + }
> +
> + return g_steal_pointer(&hostname);
> +}
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index e9b14ab178..3b49d27297 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -808,3 +808,16 @@ bool qemu_write_pidfile(const char *filename, Error **errp)
> }
> return true;
> }
> +
> +char *qemu_get_host_name(Error **errp)
> +{
> + wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
> + DWORD size = G_N_ELEMENTS(tmp);
> +
> + if (GetComputerNameW(tmp, &size) == 0) {
> + error_setg_win32(errp, GetLastError(), "failed close handle");
> + return NULL;
> + }
> +
> + return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
> +}
>
next prev parent reply other threads:[~2020-06-22 18:39 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-22 18:19 [PATCH v3 0/2] qga: Ditch g_get_host_name() Michal Privoznik
2020-06-22 18:19 ` [PATCH v3 1/2] util: Introduce qemu_get_host_name() Michal Privoznik
2020-06-22 18:37 ` Philippe Mathieu-Daudé [this message]
2020-06-23 8:50 ` Daniel P. Berrangé
2020-06-22 18:19 ` [PATCH v3 2/2] qga: Use qemu_get_host_name() instead of g_get_host_name() Michal Privoznik
2020-06-23 8:50 ` Daniel P. Berrangé
2020-07-10 9:43 ` [PATCH v3 0/2] qga: Ditch g_get_host_name() Michal Privoznik
2020-07-13 10:57 ` Philippe Mathieu-Daudé
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=151caec3-fbdf-10ff-34c7-39737c7f4d07@redhat.com \
--to=philmd@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=mprivozn@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=vfeenstr@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).