Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 698424
Collapse All | Expand All

(-)a/src/util/virhostuptime.c (-4 / +60 lines)
Lines 25-40 Link Here
25
#endif
25
#endif
26
26
27
#include "virhostuptime.h"
27
#include "virhostuptime.h"
28
#include "viralloc.h"
29
#include "virfile.h"
30
#include "virlog.h"
31
#include "virstring.h"
32
#include "virtime.h"
28
#include "virthread.h"
33
#include "virthread.h"
29
34
35
#define VIR_FROM_THIS VIR_FROM_NONE
36
37
VIR_LOG_INIT("util.virhostuptime");
38
30
static unsigned long long bootTime;
39
static unsigned long long bootTime;
31
static int bootTimeErrno;
40
static int bootTimeErrno;
32
static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;
41
static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;
33
42
34
#ifdef HAVE_GETUTXID
43
#if defined(__linux__)
44
# define UPTIME_FILE  "/proc/uptime"
45
static int
46
virHostGetBootTimeProcfs(unsigned long long *btime)
47
{
48
    unsigned long long now;
49
    double up;
50
    VIR_AUTOFREE (char *buf) = NULL;
51
    char *tmp;
52
53
    if (virTimeMillisNow(&now) < 0)
54
        return -errno;
55
56
    /* 1KiB limit is more than enough. */
57
    if (virFileReadAll(UPTIME_FILE, 1024, &buf) < 0)
58
        return -errno;
59
60
    /* buf contains two doubles now:
61
     *   $uptime $idle_time
62
     * We're interested only in the first one */
63
    if (!(tmp = strchr(buf, ' '))) {
64
        virReportError(VIR_ERR_INTERNAL_ERROR,
65
                       _("uptime file has unexpected format '%s'"),
66
                       buf);
67
        return -EINVAL;
68
    }
69
70
    *tmp = '\0';
71
72
    if (virStrToDouble(buf, NULL, &up) < 0) {
73
        virReportError(VIR_ERR_INTERNAL_ERROR,
74
                       _("Unable to parse sched info value '%s'"),
75
                       buf);
76
        return -EINVAL;
77
    }
78
79
    *btime = now / 1000 - up + 0.5;
80
81
    return 0;
82
}
83
#endif /* defined(__linux__) */
84
85
#if defined(HAVE_GETUTXID) || defined(__linux__)
35
static void
86
static void
36
virHostGetBootTimeOnceInit(void)
87
virHostGetBootTimeOnceInit(void)
37
{
88
{
89
# ifdef HAVE_GETUTXID
38
    struct utmpx id = {.ut_type = BOOT_TIME};
90
    struct utmpx id = {.ut_type = BOOT_TIME};
39
    struct utmpx *res = NULL;
91
    struct utmpx *res = NULL;
40
92
Lines 45-60 Link Here
45
    }
97
    }
46
98
47
    endutxent();
99
    endutxent();
100
# endif /* HAVE_GETUTXID */
101
102
    if (bootTimeErrno != 0 || bootTimeErrno == 0) {
103
        bootTimeErrno = -virHostGetBootTimeProcfs(&bootTime);
104
    }
48
}
105
}
49
106
50
#else /* !HAVE_GETUTXID */
107
#else /* !defined(HAVE_GETUTXID) && !defined(__linux__) */
51
108
52
static void
109
static void
53
virHostGetBootTimeOnceInit(void)
110
virHostGetBootTimeOnceInit(void)
54
{
111
{
55
    bootTimeErrno = ENOSYS;
112
    bootTimeErrno = ENOSYS;
56
}
113
}
57
#endif /* HAVE_GETUTXID */
114
#endif /* !defined(HAVE_GETUTXID) && !defined(__linux__) */
58
115
59
/**
116
/**
60
 * virHostGetBootTime:
117
 * virHostGetBootTime:
61
-

Return to bug 698424