[libvirt] [PATCH 4/5] util: Introduce virhostuptime

Jiri Denemark jdenemar at redhat.com
Wed Aug 21 15:11:32 UTC 2019


On Wed, Aug 14, 2019 at 16:33:22 +0200, Michal Privoznik wrote:
> This module contains function to get host boot time.
> 
> Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
> ---
>  src/libvirt_private.syms |  4 +++
>  src/util/Makefile.inc.am |  2 ++
>  src/util/virhostuptime.c | 61 ++++++++++++++++++++++++++++++++++++++++
>  src/util/virhostuptime.h | 27 ++++++++++++++++++
>  4 files changed, 94 insertions(+)
>  create mode 100644 src/util/virhostuptime.c
>  create mode 100644 src/util/virhostuptime.h
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 7a3feb8efa..bed00c3cb8 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -2135,6 +2135,10 @@ virHostMemGetStats;
>  virHostMemSetParameters;
>  
>  
> +# util/virhostuptime.h
> +virHostGetBootTime;
> +
> +
>  # util/viridentity.h
>  virIdentityGetAttr;
>  virIdentityGetCurrent;
> diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
> index a47f333a98..46866cf213 100644
> --- a/src/util/Makefile.inc.am
> +++ b/src/util/Makefile.inc.am
> @@ -91,6 +91,8 @@ UTIL_SOURCES = \
>  	util/virhostdev.h \
>  	util/virhostmem.c \
>  	util/virhostmem.h \
> +	util/virhostuptime.c \
> +	util/virhostuptime.h \
>  	util/viridentity.c \
>  	util/viridentity.h \
>  	util/virinitctl.c \
> diff --git a/src/util/virhostuptime.c b/src/util/virhostuptime.c
> new file mode 100644
> index 0000000000..f48de672e6
> --- /dev/null
> +++ b/src/util/virhostuptime.c
> @@ -0,0 +1,61 @@
> +/*
> + * virhostuptime.c: helper APIs for host uptime
> + *
> + * Copyright (C) 2019 Red Hat, Inc.
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library.  If not, see
> + * <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <config.h>
> +
> +#if defined(__linux__) || defined(__FreeBSD__)
> +# include <utmpx.h>
> +#endif
> +
> +#include "virhostuptime.h"
> +
> +
> +/**
> + * virHostGetBootTime:
> + * @when: UNIX timestamp of boot time
> + *
> + * Get a UNIX timestamp of host boot time and store it at @when.
> + * Note that this function is not reentrant.
> + *
> + * Return: 0 on success,
> + *        -1 otherwise.
> + */
> +int
> +virHostGetBootTime(unsigned long long *when ATTRIBUTE_UNUSED)
> +{
> +#if defined(__linux__) || defined(__FreeBSD__)

I believe we usually use a bit different approach:

    #if ...

    int
    virHostGetBootTime(unsigned long long *when)
    {
        ...
    }

    #else

    int
    virHostGetBootTime(unsigned long long *when ATTRIBUTE_UNUSED)
    {
        return ...;
    }

    #endif

> +    struct utmpx id = {.ut_type = BOOT_TIME};
> +    struct utmpx *res = NULL;
> +
> +    if (!(res = getutxid(&id))) {

getutxid is not thread safe

> +        int theerrno = errno;
> +        endutxent();
> +        return -theerrno;
> +    }
> +
> +    *when = res->ut_tv.tv_sec;
> +    endutxent();
> +    return 0;
> +#else
> +    /* Unfortunately, mingw lacks utmp */
> +    errno = ENOSYS;
> +    return -errno;
> +#endif
> +}

Jirka




More information about the libvir-list mailing list