summaryrefslogtreecommitdiff
path: root/src/util/virhostuptime.c
blob: 2467ea51b0c77aeebf4a72b2bc8f86997197e698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * 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>

#ifdef WITH_GETUTXID
# include <utmpx.h>
#endif

#include "virhostuptime.h"
#include "virfile.h"
#include "virlog.h"
#include "virstring.h"
#include "virtime.h"
#include "virthread.h"

#include <math.h>

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("util.virhostuptime");

static unsigned long long bootTime;
static int bootTimeErrno;
static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;

#if defined(__linux__)
# define UPTIME_FILE  "/proc/uptime"
static int
virHostGetBootTimeProcfs(unsigned long long *btime)
{
    unsigned long long now;
    double up;
    g_autofree char *buf = NULL;
    char *tmp;

    if (virTimeMillisNow(&now) < 0)
        return -errno;

    /* 1KiB limit is more than enough. */
    if (virFileReadAll(UPTIME_FILE, 1024, &buf) < 0)
        return -errno;

    /* buf contains two doubles now:
     *   $uptime $idle_time
     * We're interested only in the first one */
    if (!(tmp = strchr(buf, ' '))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("uptime file has unexpected format '%1$s'"),
                       buf);
        return -EINVAL;
    }

    *tmp = '\0';

    if (virStrToDouble(buf, NULL, &up) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse uptime value '%1$s'"),
                       buf);
        return -EINVAL;
    }

    *btime = llround(now / 1000 - up);

    return 0;
}
#endif /* defined(__linux__) */

#if defined(WITH_GETUTXID) || defined(__linux__)
static void
virHostGetBootTimeOnceInit(void)
{
# ifdef WITH_GETUTXID
    struct utmpx id = {.ut_type = BOOT_TIME};
    struct utmpx *res = NULL;

    if (!(res = getutxid(&id))) {
        bootTimeErrno = errno;
    } else {
        bootTime = res->ut_tv.tv_sec;
    }

    endutxent();
# endif /* WITH_GETUTXID */

# ifdef __linux__
    if (bootTimeErrno != 0 || bootTime == 0)
        bootTimeErrno = -virHostGetBootTimeProcfs(&bootTime);
# endif /* __linux__ */
}

#else /* !defined(WITH_GETUTXID) && !defined(__linux__) */

static void
virHostGetBootTimeOnceInit(void)
{
    bootTimeErrno = ENOSYS;
}
#endif /* !defined(WITH_GETUTXID) && !defined(__linux__) */

/**
 * virHostGetBootTime:
 * @when: UNIX timestamp of boot time
 *
 * Get a UNIX timestamp of host boot time and store it at @when.
 *
 * Return: 0 on success,
 *        -1 otherwise.
 */
int
virHostGetBootTime(unsigned long long *when)
{
    if (virHostBootTimeInit() < 0)
        return -1;

    if (bootTimeErrno) {
        errno = bootTimeErrno;
        return -1;
    }

    *when = bootTime;
    return 0;
}


int
virHostBootTimeInit(void)
{
    if (virOnce(&virHostGetBootTimeOnce, virHostGetBootTimeOnceInit) < 0)
        return -1;

    return 0;
}