2 * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
20 #include <sys/types.h>
31 #endif /* STDC_HEADERS */
33 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
42 #if TIME_WITH_SYS_TIME
46 # if defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
47 # include <sys/sysctl.h>
48 # elif defined(HAVE_GETUTXID)
50 # elif defined(HAVE_GETUTID)
53 #endif /* !__linux__ */
56 #include "sudo_debug.h"
59 * Fill in a struct timeval with the time the system booted.
60 * Returns 1 on success and 0 on failure.
63 #if defined(__linux__)
65 get_boottime(struct timeval *tv)
71 debug_decl(get_boottime, SUDO_DEBUG_UTIL)
73 /* read btime from /proc/stat */
74 fp = fopen("/proc/stat", "r");
76 while ((len = getline(&line, &linesize, fp)) != -1) {
77 if (strncmp(line, "btime ", 6) == 0) {
78 tv->tv_sec = atoi(line + 6);
90 #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
93 get_boottime(struct timeval *tv)
97 debug_decl(get_boottime, SUDO_DEBUG_UTIL)
100 mib[1] = KERN_BOOTTIME;
102 if (sysctl(mib, 2, tv, &size, NULL, 0) != -1)
103 debug_return_bool(1);
105 debug_return_bool(0);
108 #elif defined(HAVE_GETUTXID)
111 get_boottime(struct timeval *tv)
113 struct utmpx *ut, key;
114 debug_decl(get_boottime, SUDO_DEBUG_UTIL)
116 memset(&key, 0, sizeof(key));
117 key.ut_type = BOOT_TIME;
119 if ((ut = getutxid(&key)) != NULL) {
120 tv->tv_sec = ut->ut_tv.tv_sec;
121 tv->tv_usec = ut->ut_tv.tv_usec;
124 debug_return_bool(ut != NULL);
127 #elif defined(HAVE_GETUTID)
130 get_boottime(struct timeval *tv)
132 struct utmp *ut, key;
133 debug_decl(get_boottime, SUDO_DEBUG_UTIL)
135 memset(&key, 0, sizeof(key));
136 key.ut_type = BOOT_TIME;
138 if ((ut = getutid(&key)) != NULL) {
139 tv->tv_sec = ut->ut_time;
143 debug_return_bool(ut != NULL);
149 get_boottime(struct timeval *tv)
151 debug_decl(get_boottime, SUDO_DEBUG_UTIL)
152 debug_return_bool(0);